@@ -189,6 +189,73 @@ void main() {
189
189
);
190
190
});
191
191
192
+ group ('nested transaction' , () {
193
+ const insert = 'INSERT INTO test_data (description) VALUES(?);' ;
194
+ late SqliteDatabase db;
195
+
196
+ setUp (() async {
197
+ db = await testUtils.setupDatabase (path: path);
198
+ await createTables (db);
199
+ });
200
+
201
+ tearDown (() => db.close ());
202
+
203
+ test ('run in outer transaction' , () async {
204
+ await db.writeTransaction ((tx) async {
205
+ await tx.execute (insert, ['first' ]);
206
+
207
+ await tx.writeTransaction ((tx) async {
208
+ await tx.execute (insert, ['second' ]);
209
+ });
210
+
211
+ expect (await tx.getAll ('SELECT * FROM test_data' ), hasLength (2 ));
212
+ });
213
+
214
+ expect (await db.getAll ('SELECT * FROM test_data' ), hasLength (2 ));
215
+ });
216
+
217
+ test ('can rollback inner transaction' , () async {
218
+ await db.writeTransaction ((tx) async {
219
+ await tx.execute (insert, ['first' ]);
220
+
221
+ await tx.writeTransaction ((tx) async {
222
+ await tx.execute (insert, ['second' ]);
223
+ });
224
+
225
+ await expectLater (() async {
226
+ await tx.writeTransaction ((tx) async {
227
+ await tx.execute (insert, ['third' ]);
228
+ expect (await tx.getAll ('SELECT * FROM test_data' ), hasLength (3 ));
229
+ throw 'rollback please' ;
230
+ });
231
+ }, throwsA (anything));
232
+
233
+ expect (await tx.getAll ('SELECT * FROM test_data' ), hasLength (2 ));
234
+ });
235
+
236
+ expect (await db.getAll ('SELECT * FROM test_data' ), hasLength (2 ));
237
+ });
238
+
239
+ test ('cannot use outer transaction while inner is active' , () async {
240
+ await db.writeTransaction ((outer) async {
241
+ await outer.writeTransaction ((inner) async {
242
+ await expectLater (outer.execute ('SELECT 1' ), throwsStateError);
243
+ });
244
+ });
245
+ });
246
+
247
+ test ('cannot use inner after leaving scope' , () async {
248
+ await db.writeTransaction ((tx) async {
249
+ late SqliteWriteContext inner;
250
+ await tx.writeTransaction ((tx) async {
251
+ inner = tx;
252
+ });
253
+
254
+ await expectLater (inner.execute ('SELECT 1' ), throwsStateError);
255
+ });
256
+ });
257
+ });
258
+
192
259
test ('can use raw database instance' , () async {
193
260
final factory = await testUtils.testFactory ();
194
261
final raw = await factory .openDatabaseForSingleConnection ();
0 commit comments