drift/moor/test/schema_test.dart

135 lines
4.2 KiB
Dart
Raw Normal View History

2019-03-09 07:37:22 -08:00
import 'package:moor/moor.dart';
2019-09-26 13:52:20 -07:00
import 'package:test/test.dart';
2019-02-14 09:40:58 -08:00
2019-02-17 04:07:27 -08:00
import 'data/tables/todos.dart';
import 'data/utils/mocks.dart';
2019-02-14 09:40:58 -08:00
void main() {
TodoDb db;
QueryExecutor mockExecutor;
2019-02-14 09:40:58 -08:00
setUp(() {
mockExecutor = MockExecutor();
db = TodoDb(mockExecutor);
2019-02-14 09:40:58 -08:00
});
group('Migrations', () {
test('creates all tables', () async {
await db.beforeOpen(mockExecutor, const OpeningDetails(null, 1));
2019-02-14 09:40:58 -08:00
2019-03-06 12:43:16 -08:00
// should create todos, categories, users and shared_todos table
verify(mockExecutor.runCustom(
'CREATE TABLE IF NOT EXISTS todos '
'(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR NULL, '
2019-02-14 11:46:25 -08:00
'content VARCHAR NOT NULL, target_date INTEGER NULL, '
'category INTEGER NULL);',
[]));
2019-02-14 09:40:58 -08:00
verify(mockExecutor.runCustom(
'CREATE TABLE IF NOT EXISTS categories '
'(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '
'`desc` VARCHAR NOT NULL UNIQUE);',
[]));
2019-02-14 09:40:58 -08:00
verify(mockExecutor.runCustom(
'CREATE TABLE IF NOT EXISTS users '
'(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '
'name VARCHAR NOT NULL, '
'is_awesome INTEGER NOT NULL DEFAULT 1 CHECK (is_awesome in (0, 1)), '
'profile_picture BLOB NOT NULL, '
'creation_time INTEGER NOT NULL '
"DEFAULT (strftime('%s', CURRENT_TIMESTAMP)));",
[]));
2019-03-06 12:43:16 -08:00
verify(mockExecutor.runCustom(
'CREATE TABLE IF NOT EXISTS shared_todos ('
2019-04-19 11:38:58 -07:00
'todo INTEGER NOT NULL, '
'user INTEGER NOT NULL, '
'PRIMARY KEY (todo, user), '
'FOREIGN KEY (todo) REFERENCES todos(id), '
'FOREIGN KEY (user) REFERENCES users(id)'
');',
[]));
2019-04-29 09:04:40 -07:00
verify(mockExecutor.runCustom(
'CREATE TABLE IF NOT EXISTS '
2019-05-12 01:52:02 -07:00
'table_without_p_k ('
'not_really_an_id INTEGER NOT NULL, '
'some_float REAL NOT NULL, '
'custom VARCHAR NOT NULL'
');',
[]));
2019-02-14 09:40:58 -08:00
});
test('creates individual tables', () async {
await db.createMigrator().createTable(db.users);
2019-02-14 09:40:58 -08:00
verify(mockExecutor.runCustom(
'CREATE TABLE IF NOT EXISTS users '
'(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, '
'name VARCHAR NOT NULL, '
'is_awesome INTEGER NOT NULL DEFAULT 1 CHECK (is_awesome in (0, 1)), '
'profile_picture BLOB NOT NULL, '
'creation_time INTEGER NOT NULL '
"DEFAULT (strftime('%s', CURRENT_TIMESTAMP)));",
[]));
2019-02-14 09:40:58 -08:00
});
test('drops tables', () async {
await db.createMigrator().deleteTable('users');
2019-02-14 09:40:58 -08:00
verify(mockExecutor.runCustom('DROP TABLE IF EXISTS users;'));
2019-02-14 09:40:58 -08:00
});
test('adds columns', () async {
await db.createMigrator().addColumn(db.users, db.users.isAwesome);
2019-02-14 09:40:58 -08:00
verify(mockExecutor.runCustom('ALTER TABLE users ADD COLUMN '
'is_awesome INTEGER NOT NULL DEFAULT 1 '
'CHECK (is_awesome in (0, 1));'));
2019-02-14 09:40:58 -08:00
});
});
test('custom statements', () async {
await db.customStatement('some custom statement');
verify(mockExecutor.runCustom('some custom statement'));
});
test('upgrading a database without schema migration throws', () async {
final db = _DefaultDb(MockExecutor());
expect(
() => db.beforeOpen(db.executor, const OpeningDetails(2, 3)),
throwsA(const TypeMatcher<Exception>()),
);
});
test('can use migrations inside schema callbacks', () async {
final executor = MockExecutor();
TodoDb db;
db = TodoDb(executor)
..migration = MigrationStrategy(onUpgrade: (m, from, to) async {
await db.transaction(() async {
await m.createTable(db.users);
});
});
await db.beforeOpen(executor, const OpeningDetails(2, 3));
verify(executor.beginTransaction());
verify(executor.transactions.runCustom(any, any));
verifyNever(executor.runCustom(any, any));
});
}
class _DefaultDb extends GeneratedDatabase {
_DefaultDb(QueryExecutor executor)
// ignore: prefer_const_constructors
: super(SqlTypeSystem.withDefaults(), executor);
@override
List<TableInfo<Table, DataClass>> get allTables => [];
@override
int get schemaVersion => 2;
}