|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:sqlite_async/sqlite_async.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +import 'util.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('Close Tests', () { |
| 10 | + late String path; |
| 11 | + |
| 12 | + setUp(() async { |
| 13 | + path = dbPath(); |
| 14 | + await cleanDb(path: path); |
| 15 | + }); |
| 16 | + |
| 17 | + tearDown(() async { |
| 18 | + await cleanDb(path: path); |
| 19 | + }); |
| 20 | + |
| 21 | + createTables(SqliteDatabase db) async { |
| 22 | + await db.writeTransaction((tx) async { |
| 23 | + await tx.execute( |
| 24 | + 'CREATE TABLE test_data(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT)'); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + test('Open and close', () async { |
| 29 | + // Test that that the journal files are properly deleted after closing. |
| 30 | + // If the write connection is closed before the read connections, that is |
| 31 | + // not the case. |
| 32 | + |
| 33 | + final db = await setupDatabase(path: path); |
| 34 | + await createTables(db); |
| 35 | + |
| 36 | + await db.execute( |
| 37 | + 'INSERT INTO test_data(description) VALUES(?)', ['Test Data']); |
| 38 | + await db.getAll('SELECT * FROM test_data'); |
| 39 | + |
| 40 | + expect(await File('$path-wal').exists(), equals(true)); |
| 41 | + expect(await File('$path-shm').exists(), equals(true)); |
| 42 | + |
| 43 | + await db.close(); |
| 44 | + |
| 45 | + expect(await File(path).exists(), equals(true)); |
| 46 | + |
| 47 | + expect(await File('$path-wal').exists(), equals(false)); |
| 48 | + expect(await File('$path-shm').exists(), equals(false)); |
| 49 | + |
| 50 | + expect(await File('$path-journal').exists(), equals(false)); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
0 commit comments