From 3db32be42b92a0bc7f37d7732988563a4f597904 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 2 Apr 2022 23:55:08 +0200 Subject: [PATCH] Restructure test utils in drift --- drift/dart_test.yaml | 2 +- drift/test/batch_test.dart | 2 +- drift/test/custom_queries_test.dart | 2 +- drift/test/data/tables/custom_tables.dart | 5 ++ drift/test/data/tables/todos.dart | 7 +-- drift/test/data/utils/expect_equality.dart | 11 ---- drift/test/data/utils/null_executor.dart | 52 ------------------- drift/test/database_test.dart | 2 +- drift/test/delete_test.dart | 2 +- drift/test/engines/connection_pool_test.dart | 2 +- drift/test/error_handling_test.dart | 2 +- drift/test/expressions/aggregate_test.dart | 2 +- drift/test/expressions/algebra_test.dart | 3 +- drift/test/expressions/bools_test.dart | 3 +- drift/test/expressions/comparable_test.dart | 2 +- drift/test/expressions/constant_test.dart | 2 +- .../expressions/datetime_expression_test.dart | 3 +- drift/test/expressions/exists_test.dart | 2 +- drift/test/expressions/expression_test.dart | 3 +- .../expressions_integration_test.dart | 5 +- .../test/expressions/in_expression_test.dart | 2 +- drift/test/expressions/null_check_test.dart | 3 +- drift/test/expressions/text_test.dart | 2 +- drift/test/expressions/variable_test.dart | 2 +- drift/test/expressions/when_test.dart | 2 +- .../extensions/fts5_integration_test.dart | 5 +- .../extensions/json1_integration_test.dart | 5 +- drift/test/extensions/json1_test.dart | 2 +- .../extensions/moor_ffi_functions_test.dart | 8 ++- drift/test/insert_test.dart | 2 +- ...dart => drift_files_integration_test.dart} | 7 ++- ..._files_test.dart => drift_files_test.dart} | 2 +- .../insert_integration_test.dart | 5 +- .../list_subquery_integration_test.dart | 5 +- .../integration_tests/regress_1232_test.dart | 5 +- .../integration_tests/regress_1235_test.dart | 3 +- .../integration_tests/regress_1442_test.dart | 7 ++- .../integration_tests/regress_1589_test.dart | 1 + .../integration_tests/regress_1599_test.dart | 4 +- .../select_integration_test.dart | 5 +- drift/test/isolate_test.dart | 2 +- drift/test/join_test.dart | 2 +- drift/test/rx_streams_test.dart | 2 +- drift/test/schema_test.dart | 2 +- drift/test/select_test.dart | 2 +- drift/test/streams_test.dart | 2 +- drift/test/tables_test.dart | 2 +- drift/test/test_utils/database_stub.dart | 5 ++ drift/test/test_utils/database_vm.dart | 6 +++ drift/test/test_utils/database_web.dart | 7 +++ .../matchers.dart} | 10 ++++ .../{data/utils => test_utils}/mocks.dart | 2 - drift/test/test_utils/sqlite_server.dart | 21 ++++++++ drift/test/test_utils/test_utils.dart | 5 ++ drift/test/transactions_test.dart | 2 +- drift/test/update_test.dart | 2 +- drift/test/utils/lazy_database_test.dart | 2 +- 57 files changed, 123 insertions(+), 144 deletions(-) delete mode 100644 drift/test/data/utils/expect_equality.dart delete mode 100644 drift/test/data/utils/null_executor.dart rename drift/test/integration_tests/{moor_files_integration_test.dart => drift_files_integration_test.dart} (96%) rename drift/test/integration_tests/{moor_files_test.dart => drift_files_test.dart} (99%) create mode 100644 drift/test/test_utils/database_stub.dart create mode 100644 drift/test/test_utils/database_vm.dart create mode 100644 drift/test/test_utils/database_web.dart rename drift/test/{data/utils/expect_generated.dart => test_utils/matchers.dart} (90%) rename drift/test/{data/utils => test_utils}/mocks.dart (99%) create mode 100644 drift/test/test_utils/sqlite_server.dart create mode 100644 drift/test/test_utils/test_utils.dart diff --git a/drift/dart_test.yaml b/drift/dart_test.yaml index dc3eb595..83f619ae 100644 --- a/drift/dart_test.yaml +++ b/drift/dart_test.yaml @@ -11,7 +11,7 @@ platforms: - vm presets: - # Only run on browsers when requested. In CI, we only test on browsers when the VM tests went through + # Only run on browsers when requested. In CI, we only test on browsers after the VM tests went through browsers: platforms: - chrome diff --git a/drift/test/batch_test.dart b/drift/test/batch_test.dart index f01f914a..e4da5344 100644 --- a/drift/test/batch_test.dart +++ b/drift/test/batch_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/custom_queries_test.dart b/drift/test/custom_queries_test.dart index 818e6a3f..cec58549 100644 --- a/drift/test/custom_queries_test.dart +++ b/drift/test/custom_queries_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; // ignore_for_file: lines_longer_than_80_chars diff --git a/drift/test/data/tables/custom_tables.dart b/drift/test/data/tables/custom_tables.dart index 6529fd42..5f3f7e27 100644 --- a/drift/test/data/tables/custom_tables.dart +++ b/drift/test/data/tables/custom_tables.dart @@ -19,6 +19,11 @@ class CustomTablesDb extends _$CustomTablesDb { driftRuntimeOptions.dontWarnAboutMultipleDatabases = true; } + CustomTablesDb.connect(DatabaseConnection connection) + : super.connect(connection) { + driftRuntimeOptions.dontWarnAboutMultipleDatabases = true; + } + @override int get schemaVersion => 1; diff --git a/drift/test/data/tables/todos.dart b/drift/test/data/tables/todos.dart index 0f18bf52..396e1924 100644 --- a/drift/test/data/tables/todos.dart +++ b/drift/test/data/tables/todos.dart @@ -2,8 +2,6 @@ import 'package:drift/drift.dart'; // ignore: import_of_legacy_library_into_null_safe import 'package:uuid/uuid.dart'; -import '../utils/null_executor.dart'; - part 'todos.g.dart'; mixin AutoIncrement on Table { @@ -167,7 +165,7 @@ abstract class TodoWithCategoryView extends View { }, ) class TodoDb extends _$TodoDb { - TodoDb([QueryExecutor? e]) : super(e ?? const NullExecutor()) { + TodoDb([QueryExecutor? e]) : super(e ?? _nullExecutor) { driftRuntimeOptions.dontWarnAboutMultipleDatabases = true; } @@ -195,3 +193,6 @@ class TodoDb extends _$TodoDb { class SomeDao extends DatabaseAccessor with _$SomeDaoMixin { SomeDao(TodoDb db) : super(db); } + +QueryExecutor get _nullExecutor => + LazyDatabase(() => throw UnsupportedError('stub')); diff --git a/drift/test/data/utils/expect_equality.dart b/drift/test/data/utils/expect_equality.dart deleted file mode 100644 index 71865bd6..00000000 --- a/drift/test/data/utils/expect_equality.dart +++ /dev/null @@ -1,11 +0,0 @@ -import 'package:test/test.dart'; - -void expectEquals(dynamic a, dynamic expected) { - expect(a, equals(expected)); - expect(a.hashCode, equals(expected.hashCode)); -} - -void expectNotEquals(dynamic a, dynamic expected) { - expect(a, isNot(equals(expected))); - expect(a.hashCode, isNot(equals(expected.hashCode))); -} diff --git a/drift/test/data/utils/null_executor.dart b/drift/test/data/utils/null_executor.dart deleted file mode 100644 index 62ebf046..00000000 --- a/drift/test/data/utils/null_executor.dart +++ /dev/null @@ -1,52 +0,0 @@ -import 'package:drift/backends.dart'; - -class NullExecutor implements QueryExecutor { - const NullExecutor(); - - @override - TransactionExecutor beginTransaction() { - throw UnsupportedError('beginTransaction'); - } - - @override - Future ensureOpen(QueryExecutorUser user) { - throw UnsupportedError('ensureOpen'); - } - - @override - Future runBatched(BatchedStatements statements) { - throw UnsupportedError('runBatched'); - } - - @override - Future runCustom(String statement, [List? args]) { - throw UnsupportedError('runCustom'); - } - - @override - Future runDelete(String statement, List args) { - throw UnsupportedError('runDelete'); - } - - @override - Future runInsert(String statement, List args) { - throw UnsupportedError('runInsert'); - } - - @override - Future>> runSelect( - String statement, List args) { - throw UnsupportedError('runSelect'); - } - - @override - Future runUpdate(String statement, List args) { - throw UnsupportedError('runUpdate'); - } - - @override - Future close() => Future.value(); - - @override - SqlDialect get dialect => SqlDialect.sqlite; -} diff --git a/drift/test/database_test.dart b/drift/test/database_test.dart index 2e5eef94..de4364b1 100644 --- a/drift/test/database_test.dart +++ b/drift/test/database_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; class _FakeDb extends GeneratedDatabase { _FakeDb(SqlTypeSystem types, QueryExecutor executor) : super(types, executor); diff --git a/drift/test/delete_test.dart b/drift/test/delete_test.dart index 0a9e46c9..7e77ef0a 100644 --- a/drift/test/delete_test.dart +++ b/drift/test/delete_test.dart @@ -5,7 +5,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/engines/connection_pool_test.dart b/drift/test/engines/connection_pool_test.dart index 5429173e..938e663c 100644 --- a/drift/test/engines/connection_pool_test.dart +++ b/drift/test/engines/connection_pool_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/mocks.dart'; +import '../test_utils/test_utils.dart'; void main() { late MockExecutor read, write; diff --git a/drift/test/error_handling_test.dart b/drift/test/error_handling_test.dart index 928e4202..f2dafaa8 100644 --- a/drift/test/error_handling_test.dart +++ b/drift/test/error_handling_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; // the content is set to non-null and the title must be between 4 and 16 chars // long diff --git a/drift/test/expressions/aggregate_test.dart b/drift/test/expressions/aggregate_test.dart index c670598f..84daa9a0 100644 --- a/drift/test/expressions/aggregate_test.dart +++ b/drift/test/expressions/aggregate_test.dart @@ -1,7 +1,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const foo = CustomExpression('foo', precedence: Precedence.primary); diff --git a/drift/test/expressions/algebra_test.dart b/drift/test/expressions/algebra_test.dart index cff305d8..a3f91063 100644 --- a/drift/test/expressions/algebra_test.dart +++ b/drift/test/expressions/algebra_test.dart @@ -1,8 +1,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; -import '../data/utils/expect_equality.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const i1 = CustomExpression('i1', precedence: Precedence.primary); diff --git a/drift/test/expressions/bools_test.dart b/drift/test/expressions/bools_test.dart index 07ad15af..4a2e849c 100644 --- a/drift/test/expressions/bools_test.dart +++ b/drift/test/expressions/bools_test.dart @@ -1,8 +1,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; -import '../data/utils/expect_equality.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const a = CustomExpression('a', precedence: Precedence.primary); diff --git a/drift/test/expressions/comparable_test.dart b/drift/test/expressions/comparable_test.dart index 9cdc9520..a5f595d6 100644 --- a/drift/test/expressions/comparable_test.dart +++ b/drift/test/expressions/comparable_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_equality.dart'; +import '../test_utils/test_utils.dart'; void main() { const expression = diff --git a/drift/test/expressions/constant_test.dart b/drift/test/expressions/constant_test.dart index fa399e6e..fcb37359 100644 --- a/drift/test/expressions/constant_test.dart +++ b/drift/test/expressions/constant_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_equality.dart'; +import '../test_utils/test_utils.dart'; void main() { group('string literals', () { diff --git a/drift/test/expressions/datetime_expression_test.dart b/drift/test/expressions/datetime_expression_test.dart index 789ef738..2971ccd1 100644 --- a/drift/test/expressions/datetime_expression_test.dart +++ b/drift/test/expressions/datetime_expression_test.dart @@ -1,8 +1,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; -import '../data/utils/expect_equality.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; typedef _Extractor = Expression Function(Expression d); diff --git a/drift/test/expressions/exists_test.dart b/drift/test/expressions/exists_test.dart index 60ee7fc5..4237b4a5 100644 --- a/drift/test/expressions/exists_test.dart +++ b/drift/test/expressions/exists_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { test('exists expressions are generated', () { diff --git a/drift/test/expressions/expression_test.dart b/drift/test/expressions/expression_test.dart index 9f36ba04..03b26bc1 100644 --- a/drift/test/expressions/expression_test.dart +++ b/drift/test/expressions/expression_test.dart @@ -4,8 +4,7 @@ import 'package:test/test.dart'; import '../data/tables/custom_tables.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_generated.dart'; -import '../data/utils/mocks.dart'; +import '../test_utils/test_utils.dart'; class _UnknownExpr extends Expression { @override diff --git a/drift/test/expressions/expressions_integration_test.dart b/drift/test/expressions/expressions_integration_test.dart index d30e9750..bab44627 100644 --- a/drift/test/expressions/expressions_integration_test.dart +++ b/drift/test/expressions/expressions_integration_test.dart @@ -1,15 +1,14 @@ -@TestOn('vm') import 'package:drift/drift.dart' hide isNull; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; +import '../test_utils/test_utils.dart'; void main() { late TodoDb db; setUp(() async { - db = TodoDb(NativeDatabase.memory()); + db = TodoDb.connect(testInMemoryDatabase()); // we selectOnly from users for the lack of a better option. Insert one // row so that getSingle works diff --git a/drift/test/expressions/in_expression_test.dart b/drift/test/expressions/in_expression_test.dart index a89cf33b..326d031e 100644 --- a/drift/test/expressions/in_expression_test.dart +++ b/drift/test/expressions/in_expression_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { final db = TodoDb(); diff --git a/drift/test/expressions/null_check_test.dart b/drift/test/expressions/null_check_test.dart index 740929ea..6d4cdedf 100644 --- a/drift/test/expressions/null_check_test.dart +++ b/drift/test/expressions/null_check_test.dart @@ -2,8 +2,7 @@ import 'package:drift/drift.dart'; import 'package:drift/drift.dart' as drift; import 'package:test/test.dart'; -import '../data/utils/expect_equality.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const innerExpression = diff --git a/drift/test/expressions/text_test.dart b/drift/test/expressions/text_test.dart index 81779667..fbc97408 100644 --- a/drift/test/expressions/text_test.dart +++ b/drift/test/expressions/text_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const expression = diff --git a/drift/test/expressions/variable_test.dart b/drift/test/expressions/variable_test.dart index a5bfb3d3..2e2c2c8e 100644 --- a/drift/test/expressions/variable_test.dart +++ b/drift/test/expressions/variable_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { test('maps the variable to sql', () { diff --git a/drift/test/expressions/when_test.dart b/drift/test/expressions/when_test.dart index a4bfe123..d584498f 100644 --- a/drift/test/expressions/when_test.dart +++ b/drift/test/expressions/when_test.dart @@ -1,7 +1,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const x = CustomExpression('x'); diff --git a/drift/test/extensions/fts5_integration_test.dart b/drift/test/extensions/fts5_integration_test.dart index dbc3ce91..f0ffe579 100644 --- a/drift/test/extensions/fts5_integration_test.dart +++ b/drift/test/extensions/fts5_integration_test.dart @@ -1,13 +1,12 @@ @Tags(['integration']) -@TestOn('vm') -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/custom_tables.dart'; +import '../test_utils/test_utils.dart'; void main() { test('fts5 integration test', () async { - final db = CustomTablesDb(NativeDatabase.memory()); + final db = CustomTablesDb.connect(testInMemoryDatabase()); await db.into(db.email).insert(EmailCompanion.insert( sender: 'foo@example.org', title: 'Hello world', body: 'Test email')); diff --git a/drift/test/extensions/json1_integration_test.dart b/drift/test/extensions/json1_integration_test.dart index 51aa40c6..b8c7a2f8 100644 --- a/drift/test/extensions/json1_integration_test.dart +++ b/drift/test/extensions/json1_integration_test.dart @@ -1,17 +1,16 @@ @Tags(['integration']) -@TestOn('vm') import 'dart:convert'; import 'package:drift/drift.dart'; import 'package:drift/extensions/json1.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; +import '../test_utils/test_utils.dart'; void main() { test('json1 integration test', () async { - final db = TodoDb(NativeDatabase.memory()); + final db = TodoDb.connect(testInMemoryDatabase()); const jsonObject = { 'foo': 'bar', 'array': [ diff --git a/drift/test/extensions/json1_test.dart b/drift/test/extensions/json1_test.dart index 62bdee1d..0ac69230 100644 --- a/drift/test/extensions/json1_test.dart +++ b/drift/test/extensions/json1_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:drift/extensions/json1.dart'; import 'package:test/test.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { test('json1 functions generate valid sql', () { diff --git a/drift/test/extensions/moor_ffi_functions_test.dart b/drift/test/extensions/moor_ffi_functions_test.dart index 45ded38e..a8eb2b13 100644 --- a/drift/test/extensions/moor_ffi_functions_test.dart +++ b/drift/test/extensions/moor_ffi_functions_test.dart @@ -1,11 +1,9 @@ -@TestOn('vm') import 'package:drift/drift.dart'; import 'package:drift/extensions/native.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/expect_generated.dart'; +import '../test_utils/test_utils.dart'; void main() { const a = CustomExpression('a'); @@ -34,7 +32,7 @@ void main() { }); test('containsCase integration test', () async { - final db = TodoDb(NativeDatabase.memory()); + final db = TodoDb.connect(testInMemoryDatabase()); // insert exactly one row so that we can evaluate expressions from Dart await db.into(db.pureDefaults).insert(PureDefaultsCompanion.insert()); @@ -61,7 +59,7 @@ void main() { late TodoDb db; setUp(() async { - db = TodoDb(NativeDatabase.memory()); + db = TodoDb.connect(testInMemoryDatabase()); // insert exactly one row so that we can evaluate expressions from Dart await db.into(db.pureDefaults).insert(PureDefaultsCompanion.insert()); }); diff --git a/drift/test/insert_test.dart b/drift/test/insert_test.dart index 4f973a6a..18e45f77 100644 --- a/drift/test/insert_test.dart +++ b/drift/test/insert_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/integration_tests/moor_files_integration_test.dart b/drift/test/integration_tests/drift_files_integration_test.dart similarity index 96% rename from drift/test/integration_tests/moor_files_integration_test.dart rename to drift/test/integration_tests/drift_files_integration_test.dart index 904fca89..ae41aaf3 100644 --- a/drift/test/integration_tests/moor_files_integration_test.dart +++ b/drift/test/integration_tests/drift_files_integration_test.dart @@ -1,18 +1,17 @@ import 'package:drift/drift.dart' hide isNull; -import 'package:drift/native.dart'; + import 'package:test/test.dart'; import '../data/tables/converter.dart'; import '../data/tables/custom_tables.dart'; import '../skips.dart'; +import '../test_utils/test_utils.dart'; void main() { - late NativeDatabase executor; late CustomTablesDb db; setUp(() { - executor = NativeDatabase.memory(); - db = CustomTablesDb(executor); + db = CustomTablesDb.connect(testInMemoryDatabase()); }); tearDown(() => db.close()); diff --git a/drift/test/integration_tests/moor_files_test.dart b/drift/test/integration_tests/drift_files_test.dart similarity index 99% rename from drift/test/integration_tests/moor_files_test.dart rename to drift/test/integration_tests/drift_files_test.dart index b9c423a3..36df0ebc 100644 --- a/drift/test/integration_tests/moor_files_test.dart +++ b/drift/test/integration_tests/drift_files_test.dart @@ -4,7 +4,7 @@ import 'package:test/test.dart'; import '../data/tables/converter.dart'; import '../data/tables/custom_tables.dart'; -import '../data/utils/mocks.dart'; +import '../test_utils/test_utils.dart'; const _createNoIds = 'CREATE TABLE IF NOT EXISTS no_ids (payload BLOB NOT NULL PRIMARY KEY) ' diff --git a/drift/test/integration_tests/insert_integration_test.dart b/drift/test/integration_tests/insert_integration_test.dart index 0022f5b6..95a71890 100644 --- a/drift/test/integration_tests/insert_integration_test.dart +++ b/drift/test/integration_tests/insert_integration_test.dart @@ -1,16 +1,15 @@ -@TestOn('vm') import 'package:drift/drift.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; import '../skips.dart'; +import '../test_utils/test_utils.dart'; void main() { late TodoDb db; setUp(() { - db = TodoDb(NativeDatabase.memory()); + db = TodoDb.connect(testInMemoryDatabase()); }); tearDown(() => db.close()); diff --git a/drift/test/integration_tests/list_subquery_integration_test.dart b/drift/test/integration_tests/list_subquery_integration_test.dart index eb5dcce3..23185268 100644 --- a/drift/test/integration_tests/list_subquery_integration_test.dart +++ b/drift/test/integration_tests/list_subquery_integration_test.dart @@ -1,15 +1,14 @@ -@TestOn('vm') import 'package:drift/drift.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/custom_tables.dart'; +import '../test_utils/test_utils.dart'; void main() { late CustomTablesDb db; setUp(() { - db = CustomTablesDb(NativeDatabase.memory()); + db = CustomTablesDb.connect(testInMemoryDatabase()); }); tearDown(() => db.close()); diff --git a/drift/test/integration_tests/regress_1232_test.dart b/drift/test/integration_tests/regress_1232_test.dart index 65d03827..f6f09b16 100644 --- a/drift/test/integration_tests/regress_1232_test.dart +++ b/drift/test/integration_tests/regress_1232_test.dart @@ -1,14 +1,13 @@ -@TestOn('vm') import 'package:drift/drift.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; +import '../test_utils/test_utils.dart'; void main() { test('regression test for #1232', () async { // replace with generated table - final db = TodoDb(NativeDatabase.memory()); + final db = TodoDb.connect(testInMemoryDatabase()); final someTables = {db.todosTable}; await db.customStatement('create table tbl (x int)'); diff --git a/drift/test/integration_tests/regress_1235_test.dart b/drift/test/integration_tests/regress_1235_test.dart index 05131dd9..059d4194 100644 --- a/drift/test/integration_tests/regress_1235_test.dart +++ b/drift/test/integration_tests/regress_1235_test.dart @@ -1,6 +1,5 @@ import 'dart:async'; -import 'package:drift/native.dart'; import 'package:test/scaffolding.dart'; import '../data/tables/todos.dart'; @@ -10,7 +9,7 @@ void main() { // See https://github.com/simolus3/drift/issues/1235. We shouldn't run async // work without users being aware of it, and no one expects creating an // instance to schedule new microtasks. - noAsync(() => TodoDb(NativeDatabase.memory())); + noAsync(TodoDb.new); }); } diff --git a/drift/test/integration_tests/regress_1442_test.dart b/drift/test/integration_tests/regress_1442_test.dart index 7917817e..3c3f5a2c 100644 --- a/drift/test/integration_tests/regress_1442_test.dart +++ b/drift/test/integration_tests/regress_1442_test.dart @@ -1,13 +1,12 @@ -@TestOn('vm') import 'dart:async'; import 'package:drift/drift.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; +import '../test_utils/test_utils.dart'; + class _TestDb extends GeneratedDatabase { - _TestDb() - : super(const SqlTypeSystem.withDefaults(), NativeDatabase.memory()); + _TestDb() : super.connect(testInMemoryDatabase()); @override final List allTables = const []; @override diff --git a/drift/test/integration_tests/regress_1589_test.dart b/drift/test/integration_tests/regress_1589_test.dart index e6162189..cd952337 100644 --- a/drift/test/integration_tests/regress_1589_test.dart +++ b/drift/test/integration_tests/regress_1589_test.dart @@ -1,3 +1,4 @@ +@TestOn('vm') import 'package:drift/drift.dart'; import 'package:drift/native.dart'; import 'package:test/test.dart'; diff --git a/drift/test/integration_tests/regress_1599_test.dart b/drift/test/integration_tests/regress_1599_test.dart index ad9b305d..329a9ecc 100644 --- a/drift/test/integration_tests/regress_1599_test.dart +++ b/drift/test/integration_tests/regress_1599_test.dart @@ -1,13 +1,13 @@ import 'package:drift/drift.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/converter.dart'; import '../data/tables/custom_tables.dart'; +import '../test_utils/test_utils.dart'; void main() { test('Dart queries on views update correctly', () async { - final db = CustomTablesDb(NativeDatabase.memory()); + final db = CustomTablesDb.connect(testInMemoryDatabase()); addTearDown(db.close); expect( diff --git a/drift/test/integration_tests/select_integration_test.dart b/drift/test/integration_tests/select_integration_test.dart index 06f3d763..dda4a847 100644 --- a/drift/test/integration_tests/select_integration_test.dart +++ b/drift/test/integration_tests/select_integration_test.dart @@ -1,16 +1,15 @@ -@TestOn('vm') import 'package:collection/collection.dart'; import 'package:drift/drift.dart'; -import 'package:drift/native.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; +import '../test_utils/test_utils.dart'; void main() { late TodoDb db; setUp(() { - db = TodoDb(NativeDatabase.memory()); + db = TodoDb.connect(testInMemoryDatabase()); }); tearDown(() => db.close()); diff --git a/drift/test/isolate_test.dart b/drift/test/isolate_test.dart index a3e13a43..9388558d 100644 --- a/drift/test/isolate_test.dart +++ b/drift/test/isolate_test.dart @@ -9,7 +9,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { // Using the DriftIsolate apis without actually running on a background diff --git a/drift/test/join_test.dart b/drift/test/join_test.dart index c02d7a9d..e09fedd1 100644 --- a/drift/test/join_test.dart +++ b/drift/test/join_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/rx_streams_test.dart b/drift/test/rx_streams_test.dart index e8dcdc34..4923ddb5 100644 --- a/drift/test/rx_streams_test.dart +++ b/drift/test/rx_streams_test.dart @@ -3,7 +3,7 @@ import 'package:rxdart/rxdart.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/schema_test.dart b/drift/test/schema_test.dart index 23cae646..de8a1e6d 100644 --- a/drift/test/schema_test.dart +++ b/drift/test/schema_test.dart @@ -3,7 +3,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/select_test.dart b/drift/test/select_test.dart index ef277903..82a05b10 100644 --- a/drift/test/select_test.dart +++ b/drift/test/select_test.dart @@ -5,7 +5,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; final _dataOfTodoEntry = { 'id': 10, diff --git a/drift/test/streams_test.dart b/drift/test/streams_test.dart index c7a447f5..a5ae2e01 100644 --- a/drift/test/streams_test.dart +++ b/drift/test/streams_test.dart @@ -8,7 +8,7 @@ import 'package:test/test.dart'; import 'data/tables/custom_tables.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/tables_test.dart b/drift/test/tables_test.dart index 50aa099a..8f1f1d44 100644 --- a/drift/test/tables_test.dart +++ b/drift/test/tables_test.dart @@ -2,7 +2,7 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/test_utils/database_stub.dart b/drift/test/test_utils/database_stub.dart new file mode 100644 index 00000000..1c1bd1cf --- /dev/null +++ b/drift/test/test_utils/database_stub.dart @@ -0,0 +1,5 @@ +import 'package:drift/drift.dart'; + +DatabaseConnection testInMemoryDatabase() { + throw UnsupportedError('Stub, should resolve to web or vm'); +} diff --git a/drift/test/test_utils/database_vm.dart b/drift/test/test_utils/database_vm.dart new file mode 100644 index 00000000..e87e0714 --- /dev/null +++ b/drift/test/test_utils/database_vm.dart @@ -0,0 +1,6 @@ +import 'package:drift/drift.dart'; +import 'package:drift/native.dart'; + +DatabaseConnection testInMemoryDatabase() { + return DatabaseConnection.fromExecutor(NativeDatabase.memory()); +} diff --git a/drift/test/test_utils/database_web.dart b/drift/test/test_utils/database_web.dart new file mode 100644 index 00000000..90d7cbd4 --- /dev/null +++ b/drift/test/test_utils/database_web.dart @@ -0,0 +1,7 @@ +import 'package:drift/drift.dart'; +import 'package:drift/remote.dart'; +import 'package:test/scaffolding.dart'; + +DatabaseConnection testInMemoryDatabase() { + return remote(spawnHybridUri('/test/test_utils/sqlite_server.dart')); +} diff --git a/drift/test/data/utils/expect_generated.dart b/drift/test/test_utils/matchers.dart similarity index 90% rename from drift/test/data/utils/expect_generated.dart rename to drift/test/test_utils/matchers.dart index 2ec742f9..f192b2e9 100644 --- a/drift/test/data/utils/expect_generated.dart +++ b/drift/test/test_utils/matchers.dart @@ -1,6 +1,16 @@ import 'package:drift/drift.dart'; import 'package:test/test.dart'; +void expectEquals(dynamic a, dynamic expected) { + expect(a, equals(expected)); + expect(a.hashCode, equals(expected.hashCode)); +} + +void expectNotEquals(dynamic a, dynamic expected) { + expect(a, isNot(equals(expected))); + expect(a.hashCode, isNot(equals(expected.hashCode))); +} + /// Matcher for [Component]-subclasses. Expect that a component generates the /// matching [sql] and, optionally, the matching [variables]. Matcher generates(dynamic sql, [dynamic variables]) { diff --git a/drift/test/data/utils/mocks.dart b/drift/test/test_utils/mocks.dart similarity index 99% rename from drift/test/data/utils/mocks.dart rename to drift/test/test_utils/mocks.dart index 52d4144f..7b8c8d8a 100644 --- a/drift/test/data/utils/mocks.dart +++ b/drift/test/test_utils/mocks.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:drift/drift.dart'; import 'package:drift/src/runtime/executor/stream_queries.dart'; import 'package:mockito/mockito.dart'; diff --git a/drift/test/test_utils/sqlite_server.dart b/drift/test/test_utils/sqlite_server.dart new file mode 100644 index 00000000..b766aaa0 --- /dev/null +++ b/drift/test/test_utils/sqlite_server.dart @@ -0,0 +1,21 @@ +import 'package:drift/drift.dart'; +import 'package:drift/native.dart'; +import 'package:drift/remote.dart'; +import 'package:stream_channel/stream_channel.dart'; + +/// This function is used as a [hybrid test] call to use the system's sqlite +/// in browser test. +/// +/// To avoid excessive mocking, drift tests run against an actual sqlite3, but +/// getting sqlite3 to run on the browser is a bit of a hassle and most tests +/// exist to test core drift components, not the sqlite3 web implementation. +/// +/// While we have separate integration tests to ensure drift works in the +/// browser, unit tests just use a stream channel and a drift remote. +/// +/// [hybrid test]: https://pub.dev/packages/test#browservm-hybrid-tests +Future hybridMain(StreamChannel channel) async { + final connection = DatabaseConnection.fromExecutor(NativeDatabase.memory()); + final server = DriftServer(connection); + server.serve(channel); +} diff --git a/drift/test/test_utils/test_utils.dart b/drift/test/test_utils/test_utils.dart new file mode 100644 index 00000000..b88dd650 --- /dev/null +++ b/drift/test/test_utils/test_utils.dart @@ -0,0 +1,5 @@ +export 'database_stub.dart' + if (dart.library.ffi) 'database_vm.dart' + if (dart.library.js) 'database_web.dart'; +export 'matchers.dart'; +export 'mocks.dart'; diff --git a/drift/test/transactions_test.dart b/drift/test/transactions_test.dart index c5ec42d6..0e13f577 100644 --- a/drift/test/transactions_test.dart +++ b/drift/test/transactions_test.dart @@ -6,7 +6,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/update_test.dart b/drift/test/update_test.dart index 19c2dce0..cc67f57e 100644 --- a/drift/test/update_test.dart +++ b/drift/test/update_test.dart @@ -5,7 +5,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'data/tables/todos.dart'; -import 'data/utils/mocks.dart'; +import 'test_utils/test_utils.dart'; void main() { late TodoDb db; diff --git a/drift/test/utils/lazy_database_test.dart b/drift/test/utils/lazy_database_test.dart index a00fa3ad..fd6fb736 100644 --- a/drift/test/utils/lazy_database_test.dart +++ b/drift/test/utils/lazy_database_test.dart @@ -5,7 +5,7 @@ import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import '../data/tables/todos.dart'; -import '../data/utils/mocks.dart'; +import '../test_utils/test_utils.dart'; class _LazyQueryUserForTest extends QueryExecutorUser { @override