From 54e205af5168ea1ede3435e0b5d8cbfb7f8893c6 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 24 Feb 2024 12:45:19 +0100 Subject: [PATCH] Add integration tests for web migrations --- .../web_wasm/lib/driver.dart | 11 ++++++++++ .../web_wasm/lib/src/database.dart | 10 +++++++++- .../web_wasm/test/drift_wasm_test.dart | 20 ++++++++++++++++++- .../integration_tests/web_wasm/web/main.dart | 8 +++++++- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/extras/integration_tests/web_wasm/lib/driver.dart b/extras/integration_tests/web_wasm/lib/driver.dart index 6a58ffff..a11dabac 100644 --- a/extras/integration_tests/web_wasm/lib/driver.dart +++ b/extras/integration_tests/web_wasm/lib/driver.dart @@ -162,6 +162,17 @@ class DriftWebDriver { } } + Future 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 deleteDatabase(WebStorageApi storageApi, String name) async { await driver.executeAsync('delete_database(arguments[0], arguments[1])', [ json.encode([storageApi.name, name]), diff --git a/extras/integration_tests/web_wasm/lib/src/database.dart b/extras/integration_tests/web_wasm/lib/src/database.dart index c9c64c32..0221ac2d 100644 --- a/extras/integration_tests/web_wasm/lib/src/database.dart +++ b/extras/integration_tests/web_wasm/lib/src/database.dart @@ -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; } diff --git a/extras/integration_tests/web_wasm/test/drift_wasm_test.dart b/extras/integration_tests/web_wasm/test/drift_wasm_test.dart index 77c1145f..1daf0807 100644 --- a/extras/integration_tests/web_wasm/test/drift_wasm_test.dart +++ b/extras/integration_tests/web_wasm/test/drift_wasm_test.dart @@ -40,7 +40,11 @@ enum Browser { Future 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( diff --git a/extras/integration_tests/web_wasm/web/main.dart b/extras/integration_tests/web_wasm/web/main.dart index c9094787..7b91b287 100644 --- a/extras/integration_tests/web_wasm/web/main.dart +++ b/extras/integration_tests/web_wasm/web/main.dart @@ -18,6 +18,7 @@ TestDatabase? openedDatabase; StreamQueue? 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 _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();