Add integration tests for web migrations

This commit is contained in:
Simon Binder 2024-02-24 12:45:19 +01:00
parent d1d2b7ffe5
commit 54e205af51
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 46 additions and 3 deletions

View File

@ -162,6 +162,17 @@ class DriftWebDriver {
}
}
Future<void> setSchemaVersion(int version) async {
final result = await driver.executeAsync(
'set_schema_version(arguments[0], arguments[1])',
[version.toString()],
);
if (result != true) {
throw 'Could not set schema version';
}
}
Future<void> deleteDatabase(WebStorageApi storageApi, String name) async {
await driver.executeAsync('delete_database(arguments[0], arguments[1])', [
json.encode([storageApi.name, name]),

View File

@ -12,5 +12,13 @@ class TestDatabase extends _$TestDatabase {
TestDatabase(super.e);
@override
int get schemaVersion => 1;
MigrationStrategy get migration => MigrationStrategy(
onUpgrade: (m, from, to) async {
await into(testTable).insert(
TestTableCompanion.insert(content: 'from onUpgrade migration'));
},
);
@override
int schemaVersion = 1;
}

View File

@ -40,7 +40,11 @@ enum Browser {
Future<Process> spawnDriver() async {
return switch (this) {
firefox => Process.start('geckodriver', []),
firefox => Process.start('geckodriver', []).then((result) async {
// geckodriver seems to take a while to initialize
await Future.delayed(const Duration(seconds: 1));
return result;
}),
chrome =>
Process.start('chromedriver', ['--port=4444', '--url-base=/wd/hub']),
};
@ -156,6 +160,20 @@ void main() {
final finalImpls = await driver.probeImplementations();
expect(finalImpls.existing, isEmpty);
});
test('migrations', () async {
await driver.openDatabase(entry);
await driver.insertIntoDatabase();
await driver.waitForTableUpdate();
await driver.closeDatabase();
await driver.driver.refresh();
await driver.setSchemaVersion(2);
await driver.openDatabase(entry);
// The migration adds a row
expect(await driver.amountOfRows, 2);
});
}
group(

View File

@ -18,6 +18,7 @@ TestDatabase? openedDatabase;
StreamQueue<void>? tableUpdates;
InitializationMode initializationMode = InitializationMode.none;
int schemaVersion = 1;
void main() {
_addCallbackForWebDriver('detectImplementations', _detectImplementations);
@ -32,6 +33,10 @@ void main() {
initializationMode = InitializationMode.values.byName(arg!);
return true;
});
_addCallbackForWebDriver('set_schema_version', (arg) async {
schemaVersion = int.parse(arg!);
return true;
});
_addCallbackForWebDriver('delete_database', (arg) async {
final result = await WasmDatabase.probe(
sqlite3Uri: sqlite3WasmUri,
@ -158,7 +163,8 @@ Future<void> _open(String? implementationName) async {
connection = result.resolvedExecutor;
}
final db = openedDatabase = TestDatabase(connection);
final db =
openedDatabase = TestDatabase(connection)..schemaVersion = schemaVersion;
// Make sure it works!
await db.customSelect('SELECT database_host()').get();