-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbasic_test.dart
223 lines (179 loc) · 6.62 KB
/
basic_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// TODO
@TestOn('!browser')
library;
import 'dart:async';
import 'package:drift/drift.dart';
import 'package:drift_sqlite_async/drift_sqlite_async.dart';
import 'package:sqlite_async/sqlite_async.dart';
import 'package:test/test.dart';
import './utils/test_utils.dart';
class EmptyDatabase extends GeneratedDatabase {
EmptyDatabase(super.executor);
@override
Iterable<TableInfo<Table, dynamic>> get allTables => [];
@override
int get schemaVersion => 1;
}
void main() {
group('Basic Tests', () {
late String path;
late SqliteDatabase db;
late SqliteAsyncDriftConnection connection;
late EmptyDatabase dbu;
createTables(SqliteDatabase db) async {
await db.writeTransaction((tx) async {
await tx.execute(
'CREATE TABLE test_data(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT)');
});
}
setUp(() async {
path = dbPath();
await cleanDb(path: path);
db = await setupDatabase(path: path);
connection = SqliteAsyncDriftConnection(db);
dbu = EmptyDatabase(connection);
await createTables(db);
});
tearDown(() async {
await dbu.close();
await db.close();
await cleanDb(path: path);
});
test('INSERT/SELECT', () async {
final insertRowId = await dbu.customInsert(
'INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test Data')]);
expect(insertRowId, greaterThanOrEqualTo(1));
final result = await dbu
.customSelect('SELECT description FROM test_data')
.getSingle();
expect(result.data, equals({'description': 'Test Data'}));
});
test('INSERT RETURNING', () async {
final row = await dbu.customSelect(
'INSERT INTO test_data(description) VALUES(?) RETURNING *',
variables: [Variable('Test Data')]).getSingle();
expect(row.data['description'], equals('Test Data'));
});
test('Flat transaction', () async {
await dbu.transaction(() async {
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test Data')]);
// This runs outside the transaction - should not see the insert
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 0}));
// This runs in the transaction - should see the insert
expect(
(await dbu
.customSelect('select count(*) as count from test_data')
.getSingle())
.data,
equals({'count': 1}));
});
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 1}));
});
test('Flat transaction rollback', () async {
final testException = Exception('abort');
try {
await dbu.transaction(() async {
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test Data')]);
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 0}));
throw testException;
});
// ignore: dead_code
throw Exception('Exception expected');
} catch (e) {
expect(e, equals(testException));
}
// Rolled back - no data persisted
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 0}));
});
test('Nested transaction', () async {
await dbu.transaction(() async {
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test 1')]);
await dbu.transaction(() async {
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test 2')]);
});
// This runs outside the transaction
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 0}));
// This runs in the transaction
final countInTransaction = (await dbu
.customSelect('select count(*) as count from test_data')
.getSingle())
.data;
expect(countInTransaction, equals({'count': 2}));
});
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 2}));
});
test('Nested transaction rollback', () async {
final testException = Exception('abort');
await dbu.transaction(() async {
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test 1')]);
try {
await dbu.transaction(() async {
await dbu.customInsert(
'INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test 2')]);
throw testException;
});
// ignore: dead_code
throw Exception('Exception expected');
} catch (e) {
expect(e, equals(testException));
}
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test 3')]);
// This runs outside the transaction
expect(await db.get('select count(*) as count from test_data'),
equals({'count': 0}));
});
expect(
await db
.getAll('select description from test_data order by description'),
equals([
{'description': 'Test 1'},
{'description': 'Test 3'}
]));
}, skip: 'sqlite_async does not support nested transactions');
test('Concurrent select', () async {
var completer1 = Completer<void>();
var completer2 = Completer<void>();
final tx1 = dbu.transaction(() async {
await dbu.customInsert('INSERT INTO test_data(description) VALUES(?)',
variables: [Variable('Test Data')]);
completer2.complete();
// Stay in the transaction until the check below completed.
await completer1.future;
});
await completer2.future;
try {
// This times out if concurrent select is not supported
expect(
(await dbu
.customSelect('select count(*) as count from test_data')
.getSingle()
.timeout(const Duration(milliseconds: 500)))
.data,
equals({'count': 0}));
} finally {
completer1.complete();
}
await tx1;
expect(
(await dbu
.customSelect('select count(*) as count from test_data')
.getSingle())
.data,
equals({'count': 1}));
});
});
}