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 tests for hive_impl
  • Loading branch information
denrase committed Dec 4, 2023
commit b41bcb0e18b9fced1a6ed2c4ff125266371017a0
277 changes: 277 additions & 0 deletions hive/test/sentry_hive_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ void main() {
expect(span?.throwable, error);
}

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', () {
late Fixture fixture;

Expand All @@ -49,6 +66,7 @@ void main() {

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

await fixture.setUp();
});
Expand Down Expand Up @@ -119,6 +137,7 @@ void main() {
when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
when(fixture.mockHive.close()).thenAnswer((_) async => {});
when(fixture.hub.scope).thenReturn(fixture.scope);

await fixture.setUp(injectMockHive: true);
});
Expand Down Expand Up @@ -274,6 +293,259 @@ void main() {
});
});

group('adds breadcrumbs', () {
late Fixture fixture;

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

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

await fixture.setUp();
});

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

test('boxExists adds breadcrumb', () async {
final sut = fixture.getSut();

await sut.openBox<Person>(Fixture.dbName);
await sut.boxExists(Fixture.dbName);

verifyBreadcrumb('boxExists', fixture.getCreatedBreadcrumb());
});

test('close adds breadcrumb', () async {
final sut = fixture.getSut();

await sut.close();

verifyBreadcrumb('close', fixture.getCreatedBreadcrumb());
});

test('deleteBoxFromDisk adds breadcrumb', () async {
final sut = fixture.getSut();

await sut.openBox<Person>(Fixture.dbName);
await sut.deleteBoxFromDisk(Fixture.dbName);

verifyBreadcrumb('deleteBoxFromDisk', fixture.getCreatedBreadcrumb());
});

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

await sut.deleteFromDisk();

verifyBreadcrumb('deleteFromDisk', fixture.getCreatedBreadcrumb());
});

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

await sut.openBox<Person>(Fixture.dbName);

verifyBreadcrumb(
'openBox',
fixture.getCreatedBreadcrumb(),
checkName: true,
);
});

test('openLazyBox adds breadcrumb', () async {
final sut = fixture.getSut();

await sut.openLazyBox<Person>(Fixture.dbName);

verifyBreadcrumb(
'openLazyBox',
fixture.getCreatedBreadcrumb(),
checkName: true,
);
});
});

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

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

when(fixture.hub.options).thenReturn(fixture.options);
when(fixture.hub.getSpan()).thenReturn(fixture.tracer);
when(fixture.mockHive.close()).thenAnswer((_) async => {});
when(fixture.hub.scope).thenReturn(fixture.scope);

await fixture.setUp(injectMockHive: true);
});

test('throwing boxExists adds error span', () async {
final Box<Person> box = MockBox<Person>();
when(
fixture.mockHive.openBox<Person>(
any,
encryptionCipher: anyNamed('encryptionCipher'),
keyComparator: anyNamed('keyComparator'),
compactionStrategy: anyNamed('compactionStrategy'),
crashRecovery: anyNamed('crashRecovery'),
path: anyNamed('path'),
bytes: anyNamed('bytes'),
collection: anyNamed('collection'),
encryptionKey: anyNamed('encryptionKey'),
),
).thenAnswer((_) => Future(() => box));
when(fixture.mockHive.boxExists(any)).thenThrow(fixture.exception);

final sut = fixture.getSut();

await sut.openBox<Person>(Fixture.dbName);
try {
await sut.boxExists(Fixture.dbName);
} catch (error) {
expect(error, fixture.exception);
}

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

test('throwing close adds error span', () async {
when(fixture.mockHive.close()).thenThrow(fixture.exception);

final sut = fixture.getSut();

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

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

test('throwing deleteBoxFromDisk adds error span', () async {
final Box<Person> box = MockBox<Person>();
when(
fixture.mockHive.openBox<Person>(
any,
encryptionCipher: anyNamed('encryptionCipher'),
keyComparator: anyNamed('keyComparator'),
compactionStrategy: anyNamed('compactionStrategy'),
crashRecovery: anyNamed('crashRecovery'),
path: anyNamed('path'),
bytes: anyNamed('bytes'),
collection: anyNamed('collection'),
encryptionKey: anyNamed('encryptionKey'),
),
).thenAnswer((_) => Future(() => box));
when(fixture.mockHive.deleteBoxFromDisk(any))
.thenThrow(fixture.exception);

final sut = fixture.getSut();

await sut.openBox<Person>(Fixture.dbName);
try {
await sut.deleteBoxFromDisk(Fixture.dbName);
} catch (error) {
expect(error, fixture.exception);
}

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

test('throwing deleteFromDisk adds error span', () async {
when(fixture.mockHive.deleteFromDisk()).thenThrow(fixture.exception);

final sut = fixture.getSut();

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

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

test('throwing openBox adds error span', () async {
when(
fixture.mockHive.openBox<Person>(
any,
encryptionCipher: anyNamed('encryptionCipher'),
keyComparator: anyNamed('keyComparator'),
compactionStrategy: anyNamed('compactionStrategy'),
crashRecovery: anyNamed('crashRecovery'),
path: anyNamed('path'),
bytes: anyNamed('bytes'),
collection: anyNamed('collection'),
encryptionKey: anyNamed('encryptionKey'),
),
).thenThrow(fixture.exception);

final sut = fixture.getSut();

try {
await sut.openBox<Person>(Fixture.dbName);
} catch (error) {
expect(error, fixture.exception);
}

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

test('throwing openLazyBox adds error span', () async {
when(
fixture.mockHive.openLazyBox<Person>(
any,
encryptionCipher: anyNamed('encryptionCipher'),
keyComparator: anyNamed('keyComparator'),
compactionStrategy: anyNamed('compactionStrategy'),
crashRecovery: anyNamed('crashRecovery'),
path: anyNamed('path'),
collection: anyNamed('collection'),
encryptionKey: anyNamed('encryptionKey'),
),
).thenThrow(fixture.exception);

final sut = fixture.getSut();

try {
await sut.openLazyBox<Person>(Fixture.dbName);
} catch (error) {
expect(error, fixture.exception);
}

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

group('integrations', () {
late Fixture fixture;

Expand Down Expand Up @@ -319,6 +591,7 @@ class Fixture {
final _context = SentryTransactionContext('name', 'operation');
late final tracer = SentryTracer(_context, hub);
late SentryHiveImpl sut;
late final scope = Scope(options);

Future<void> setUp({bool injectMockHive = false}) async {
if (injectMockHive) {
Expand All @@ -344,4 +617,8 @@ class Fixture {
SentrySpan? getCreatedSpan() {
return tracer.children.last;
}

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