-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdb_test.dart
110 lines (92 loc) · 3.21 KB
/
db_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// TODO
@TestOn('!browser')
library;
import 'package:drift/drift.dart';
import 'package:sqlite_async/sqlite_async.dart';
import 'package:test/test.dart';
import './utils/test_utils.dart';
import 'generated/database.dart';
void main() {
group('Generated DB tests', () {
late String path;
late SqliteDatabase db;
late TodoDatabase dbu;
createTables(SqliteDatabase db) async {
await db.writeTransaction((tx) async {
await tx.execute(
'CREATE TABLE todos(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT)');
});
}
setUp(() async {
path = dbPath();
await cleanDb(path: path);
db = await setupDatabase(path: path);
dbu = TodoDatabase(db);
await createTables(db);
});
tearDown(() async {
await dbu.close();
await db.close();
await cleanDb(path: path);
});
test('INSERT/SELECT', () async {
var insertRowId = await dbu
.into(dbu.todoItems)
.insert(TodoItemsCompanion.insert(description: 'Test 1'));
expect(insertRowId, greaterThanOrEqualTo(1));
final result = await dbu.select(dbu.todoItems).getSingle();
expect(result.description, equals('Test 1'));
});
test('watch', () async {
var stream = dbu.select(dbu.todoItems).watch();
var resultsPromise = stream
// toString() so that we can use distinct()
.map((rows) => rows.toString())
// Drift may or may not emit duplicate update notifications.
// We use distinct() to ignore those.
.distinct()
.skipWhile((e) => e == '[]')
.take(3)
.toList();
await dbu.into(dbu.todoItems).insert(
TodoItemsCompanion.insert(id: Value(1), description: 'Test 1'));
await Future.delayed(Duration(milliseconds: 100));
await (dbu.update(dbu.todoItems))
.write(TodoItemsCompanion(description: Value('Test 1B')));
await Future.delayed(Duration(milliseconds: 100));
await (dbu.delete(dbu.todoItems).go());
var results = await resultsPromise.timeout(Duration(milliseconds: 500));
expect(
results,
equals([
'[TodoItem(id: 1, description: Test 1)]',
'[TodoItem(id: 1, description: Test 1B)]',
'[]'
]));
});
test('watch with external updates', () async {
var stream = dbu.select(dbu.todoItems).watch();
var resultsPromise = stream
.map((rows) => rows.toString())
.distinct()
.skipWhile((e) => e == '[]')
.take(3)
.toList();
await db.execute(
'INSERT INTO todos(id, description) VALUES(?, ?)', [1, 'Test 1']);
await Future.delayed(Duration(milliseconds: 100));
await db.execute(
'UPDATE todos SET description = ? WHERE id = ?', ['Test 1B', 1]);
await Future.delayed(Duration(milliseconds: 100));
await db.execute('DELETE FROM todos WHERE id = 1');
var results = await resultsPromise.timeout(Duration(milliseconds: 500));
expect(
results,
equals([
'[TodoItem(id: 1, description: Test 1)]',
'[TodoItem(id: 1, description: Test 1B)]',
'[]'
]));
});
});
}