Start writing some unit tests for schema verification

This commit is contained in:
Simon Binder 2020-11-13 18:06:42 +01:00
parent 708033c88c
commit d44699f0ee
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,72 @@
import 'package:moor/moor.dart';
import 'package:moor_generator/api/migrations.dart';
import 'package:test/test.dart';
void main() {
final verifier = SchemaVerifier(_TestHelper());
group('startAt', () {
test('starts at the requested version', () async {
final db = (await verifier.startAt(17)).executor;
await db.ensureOpen(_DelegatedUser(17, (_, details) async {
expect(details.wasCreated, isFalse, reason: 'was opened before');
expect(details.hadUpgrade, isFalse, reason: 'no upgrade expected');
}));
});
});
group('migrateAndValidate', () {
test('invokes a migration', () async {
moorRuntimeOptions.dontWarnAboutMultipleDatabases = true;
OpeningDetails capturedDetails;
final connection = await verifier.startAt(3);
final db = _TestDatabase(connection.executor, 7)
..migration = MigrationStrategy(onUpgrade: (m, from, to) async {
capturedDetails = OpeningDetails(from, to);
});
await verifier.migrateAndValidate(db, 4);
expect(capturedDetails.versionBefore, 3);
expect(capturedDetails.versionNow, 4);
moorRuntimeOptions.dontWarnAboutMultipleDatabases = false;
});
});
}
class _TestHelper implements SchemaInstantiationHelper {
@override
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
return _TestDatabase(db, version);
}
}
class _TestDatabase extends GeneratedDatabase {
@override
final int schemaVersion;
@override
MigrationStrategy migration = MigrationStrategy();
_TestDatabase(QueryExecutor executor, this.schemaVersion)
: super(const SqlTypeSystem.withDefaults(), executor);
@override
Iterable<TableInfo<Table, DataClass>> get allTables {
return const Iterable.empty();
}
}
class _DelegatedUser extends QueryExecutorUser {
@override
final int schemaVersion;
final Future<void> Function(QueryExecutor, OpeningDetails) _beforeOpen;
_DelegatedUser(this.schemaVersion, this._beforeOpen);
@override
Future<void> beforeOpen(QueryExecutor executor, OpeningDetails details) {
return _beforeOpen(executor, details);
}
}