Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add breqdcrumb tests for box_collection
  • Loading branch information
denrase committed Dec 4, 2023
commit 125b5d9133c9cd43f60a29de18cff342b29ad9aa
173 changes: 173 additions & 0 deletions hive/test/sentry_box_collection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ void main() {
expect(span?.throwable, exception);
}

void verifyBreadcrumb(
String message,
Breadcrumb? crumb, {
bool checkName = false,
String status = 'ok',
}) {
expect(
crumb?.message,
message,
);
expect(crumb?.type, 'query');
if (checkName) {
expect(crumb?.data?[SentryHiveImpl.dbNameKey], Fixture.dbName);
}
expect(crumb?.data?['status'], status);
}

group('adds span when calling', () {
late Fixture fixture;

Expand All @@ -51,6 +68,7 @@ void main() {

when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
when(fixture.hub.scope).thenReturn(fixture.scope);
});

tearDown(() async {
Expand Down Expand Up @@ -111,6 +129,7 @@ void main() {
when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
when(fixture.mockBoxCollection.name).thenReturn(Fixture.dbName);
when(fixture.hub.scope).thenReturn(fixture.scope);
});

tearDown(() async {
Expand Down Expand Up @@ -184,6 +203,155 @@ void main() {
);
});
});

group('adds breadcrumb when calling', () {
late Fixture fixture;

setUp(() async {
fixture = Fixture();
await fixture.setUp();

when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
when(fixture.hub.scope).thenReturn(fixture.scope);
});

tearDown(() async {
await fixture.tearDown();
});

test('open', () async {
await SentryBoxCollection.open(
Fixture.dbName,
{'people'},
hub: fixture.hub,
);

final span = fixture.getCreatedBreadcrumb();
verifyBreadcrumb('open', span);
});

test('openBox', () async {
final sut = await fixture.getSut();

await sut.openBox<Person>('people');

final span = fixture.getCreatedBreadcrumb();
verifyBreadcrumb('openBox', span);
});

test('transaction', () async {
final sut = await fixture.getSut();

final people = await sut.openBox<Person>('people');
await sut.transaction(
() async {
print(people.name);
},
boxNames: ['people'],
);
final span = fixture.getCreatedBreadcrumb();
verifyBreadcrumb('transaction', span);
});

test('deleteFromDisk', () async {
final sut = await fixture.getSut();

await sut.deleteFromDisk();

final span = fixture.getCreatedBreadcrumb();
verifyBreadcrumb('deleteFromDisk', span);
});
});

group('adds error breadcrumb when calling', () {
late Fixture fixture;

setUp(() async {
fixture = Fixture();
await fixture.setUp();

when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
when(fixture.mockBoxCollection.name).thenReturn(Fixture.dbName);
when(fixture.hub.scope).thenReturn(fixture.scope);
});

tearDown(() async {
await fixture.tearDown();
});

// open is static and cannot be mocked

test('throwing openBox', () async {
when(
// ignore: inference_failure_on_function_invocation
fixture.mockBoxCollection.openBox(
any,
preload: anyNamed('preload'),
boxCreator: anyNamed('boxCreator'),
),
).thenThrow(fixture.exception);

final sut = await fixture.getSut(injectMock: true);

try {
// ignore: inference_failure_on_function_invocation
await sut.openBox('people');
} catch (error) {
expect(error, fixture.exception);
}

verifyBreadcrumb(
'openBox',
fixture.getCreatedBreadcrumb(),
status: 'internal_error',
);
});

test('throwing transaction', () async {
when(
fixture.mockBoxCollection.transaction(
any,
boxNames: anyNamed('boxNames'),
readOnly: anyNamed('readOnly'),
),
).thenThrow(fixture.exception);

final sut = await fixture.getSut(injectMock: true);

try {
await sut.transaction(() async {});
} catch (error) {
expect(error, fixture.exception);
}

verifyBreadcrumb(
'transaction',
fixture.getCreatedBreadcrumb(),
status: 'internal_error',
);
});

test('throwing deleteFromDisk', () async {
when(fixture.mockBoxCollection.deleteFromDisk())
.thenThrow(fixture.exception);

final sut = await fixture.getSut(injectMock: true);

try {
await sut.deleteFromDisk();
} catch (error) {
expect(error, fixture.exception);
}

verifyBreadcrumb(
'deleteFromDisk',
fixture.getCreatedBreadcrumb(),
status: 'internal_error',
);
});
});
}

class Fixture {
Expand All @@ -197,6 +365,7 @@ class Fixture {

final _context = SentryTransactionContext('name', 'operation');
late final tracer = SentryTracer(_context, hub);
late final scope = Scope(options);

Future<void> setUp() async {
SentryHive.init(Directory.systemTemp.path);
Expand All @@ -222,4 +391,8 @@ class Fixture {
SentrySpan? getCreatedSpan() {
return tracer.children.last;
}

Breadcrumb? getCreatedBreadcrumb() {
return hub.scope.breadcrumbs.last;
}
}