From 47ef4891e63ec6a9c99e7a93691e8db19c6cf8af Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 20 Jun 2019 12:18:14 +0200 Subject: [PATCH] Test that callbacks are actually called --- moor/test/database_test.dart | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 moor/test/database_test.dart diff --git a/moor/test/database_test.dart b/moor/test/database_test.dart new file mode 100644 index 00000000..0ebbdfb2 --- /dev/null +++ b/moor/test/database_test.dart @@ -0,0 +1,67 @@ +import 'package:test_api/test_api.dart'; +import 'package:moor/moor.dart'; + +import 'data/utils/mocks.dart'; + +class _FakeDb extends GeneratedDatabase { + _FakeDb(SqlTypeSystem types, QueryExecutor executor) : super(types, executor); + + @override + MigrationStrategy get migration { + return MigrationStrategy( + onCreate: (m) async { + await m.issueCustomQuery('created'); + }, + onUpgrade: (m, from, to) async { + await m.issueCustomQuery('updated from $from to $to'); + }, + beforeOpen: (db, details) async { + await db.customSelect( + 'opened: ${details.versionBefore} to ${details.versionNow}'); + }, + ); + } + + @override + List get allTables => []; + @override + int get schemaVersion => 1; +} + +void main() { + test('status of OpeningDetails', () { + expect(const OpeningDetails(null, 1).wasCreated, true); + expect(const OpeningDetails(2, 4).wasCreated, false); + expect(const OpeningDetails(2, 4).hadUpgrade, true); + expect(const OpeningDetails(4, 4).wasCreated, false); + expect(const OpeningDetails(4, 4).hadUpgrade, false); + }); + + group('callbacks', () { + _FakeDb db; + MockExecutor executor; + MockQueryExecutor queryExecutor; + + setUp(() { + executor = MockExecutor(); + queryExecutor = MockQueryExecutor(); + db = _FakeDb(const SqlTypeSystem.withDefaults(), executor); + }); + + test('onCreate', () async { + await db.handleDatabaseCreation(executor: queryExecutor); + verify(queryExecutor.call('created')); + }); + + test('onUpgrade', () async { + await db.handleDatabaseVersionChange( + executor: queryExecutor, from: 2, to: 3); + verify(queryExecutor.call('updated from 2 to 3')); + }); + + test('beforeOpen', () async { + await db.beforeOpenCallback(executor, const OpeningDetails(3, 4)); + verify(executor.runSelect('opened: 3 to 4', [])); + }); + }); +}