Restructure test utils in drift

This commit is contained in:
Simon Binder 2022-04-02 23:55:08 +02:00
parent 9c31a17926
commit 3db32be42b
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
57 changed files with 123 additions and 144 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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<TodoDb> with _$SomeDaoMixin {
SomeDao(TodoDb db) : super(db);
}
QueryExecutor get _nullExecutor =>
LazyDatabase(() => throw UnsupportedError('stub'));

View File

@ -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)));
}

View File

@ -1,52 +0,0 @@
import 'package:drift/backends.dart';
class NullExecutor implements QueryExecutor {
const NullExecutor();
@override
TransactionExecutor beginTransaction() {
throw UnsupportedError('beginTransaction');
}
@override
Future<bool> ensureOpen(QueryExecutorUser user) {
throw UnsupportedError('ensureOpen');
}
@override
Future<void> runBatched(BatchedStatements statements) {
throw UnsupportedError('runBatched');
}
@override
Future<void> runCustom(String statement, [List<Object?>? args]) {
throw UnsupportedError('runCustom');
}
@override
Future<int> runDelete(String statement, List<Object?> args) {
throw UnsupportedError('runDelete');
}
@override
Future<int> runInsert(String statement, List<Object?> args) {
throw UnsupportedError('runInsert');
}
@override
Future<List<Map<String, Object?>>> runSelect(
String statement, List<Object?> args) {
throw UnsupportedError('runSelect');
}
@override
Future<int> runUpdate(String statement, List<Object?> args) {
throw UnsupportedError('runUpdate');
}
@override
Future<void> close() => Future.value();
@override
SqlDialect get dialect => SqlDialect.sqlite;
}

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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<int>('foo', precedence: Precedence.primary);

View File

@ -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<int>('i1', precedence: Precedence.primary);

View File

@ -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<bool>('a', precedence: Precedence.primary);

View File

@ -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 =

View File

@ -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', () {

View File

@ -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<int?> Function(Expression<DateTime?> d);

View File

@ -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', () {

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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 =

View File

@ -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 =

View File

@ -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', () {

View File

@ -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<String>('x');

View File

@ -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'));

View File

@ -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': [

View File

@ -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', () {

View File

@ -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<double>('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());
});

View File

@ -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;

View File

@ -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());

View File

@ -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) '

View File

@ -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());

View File

@ -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());

View File

@ -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)');

View File

@ -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);
});
}

View File

@ -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<TableInfo> allTables = const [];
@override

View File

@ -1,3 +1,4 @@
@TestOn('vm')
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:test/test.dart';

View File

@ -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(

View File

@ -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());

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -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;

View File

@ -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;

View File

@ -0,0 +1,5 @@
import 'package:drift/drift.dart';
DatabaseConnection testInMemoryDatabase() {
throw UnsupportedError('Stub, should resolve to web or vm');
}

View File

@ -0,0 +1,6 @@
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
DatabaseConnection testInMemoryDatabase() {
return DatabaseConnection.fromExecutor(NativeDatabase.memory());
}

View File

@ -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'));
}

View File

@ -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]) {

View File

@ -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';

View File

@ -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<void> hybridMain(StreamChannel channel) async {
final connection = DatabaseConnection.fromExecutor(NativeDatabase.memory());
final server = DriftServer(connection);
server.serve(channel);
}

View File

@ -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';

View File

@ -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;

View File

@ -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;

View File

@ -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