drift/moor/test/schema_test.dart

85 lines
3.0 KiB
Dart
Raw Normal View History

2019-03-09 07:37:22 -08:00
import 'package:moor/moor.dart';
2019-02-14 09:40:58 -08:00
import 'package:test_api/test_api.dart';
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;
MockQueryExecutor mockQueryExecutor;
QueryExecutor mockExecutor;
2019-02-14 09:40:58 -08:00
setUp(() {
mockQueryExecutor = MockQueryExecutor();
mockExecutor = MockExecutor();
db = TodoDb(mockExecutor);
2019-02-14 09:40:58 -08:00
});
group('Migrations', () {
test('creates all tables', () async {
await Migrator(db, mockQueryExecutor).createAllTables();
2019-03-06 12:43:16 -08:00
// should create todos, categories, users and shared_todos table
2019-02-14 09:40:58 -08:00
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS todos '
2019-02-17 04:14:07 -08:00
'(id INTEGER 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(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS categories '
'(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL UNIQUE);'));
2019-02-14 09:40:58 -08:00
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS users '
2019-02-17 04:14:07 -08:00
'(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, '
2019-05-09 06:30:17 -07:00
'is_awesome BOOLEAN 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
2019-04-19 11:38:58 -07:00
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS shared_todos ('
'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(mockQueryExecutor.call('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-05-12 01:52:02 -07:00
');'));
2019-02-14 09:40:58 -08:00
});
test('creates individual tables', () async {
await Migrator(db, mockQueryExecutor).createTable(db.users);
verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS users '
2019-02-17 04:14:07 -08:00
'(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, '
2019-05-09 06:30:17 -07:00
'is_awesome BOOLEAN 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 Migrator(db, mockQueryExecutor).deleteTable('users');
verify(mockQueryExecutor.call('DROP TABLE IF EXISTS users;'));
});
test('adds columns', () async {
await Migrator(db, mockQueryExecutor)
.addColumn(db.users, db.users.isAwesome);
2019-02-14 09:40:58 -08:00
verify(mockQueryExecutor.call('ALTER TABLE users ADD COLUMN '
2019-05-09 06:30:17 -07:00
'is_awesome BOOLEAN 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'));
});
}