Skip to content

Commit d155459

Browse files
committed
Add tests
1 parent 8212a23 commit d155459

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

packages/sqlite_async/lib/src/impl/context.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ final class ScopedWriteContext extends ScopedReadContext
162162
}
163163
rethrow;
164164
} finally {
165+
_isLocked = false;
165166
inner?.invalidate();
166167
}
167168
}

packages/sqlite_async/test/basic_test.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,73 @@ void main() {
189189
);
190190
});
191191

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+
192259
test('can use raw database instance', () async {
193260
final factory = await testUtils.testFactory();
194261
final raw = await factory.openDatabaseForSingleConnection();

0 commit comments

Comments
 (0)