|
| 1 | +@TestOn('!browser') |
| 2 | +import 'dart:async'; |
| 3 | + |
| 4 | +import 'package:sqlite_async/sqlite_async.dart'; |
| 5 | +import 'package:sqlite_async/src/utils/shared_utils.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +import '../utils/test_utils_impl.dart'; |
| 9 | + |
| 10 | +final testUtils = TestUtils(); |
| 11 | + |
| 12 | +void main() { |
| 13 | + group('Schema Tests', () { |
| 14 | + late String path; |
| 15 | + |
| 16 | + setUp(() async { |
| 17 | + path = testUtils.dbPath(); |
| 18 | + await testUtils.cleanDb(path: path); |
| 19 | + }); |
| 20 | + |
| 21 | + tearDown(() async { |
| 22 | + await testUtils.cleanDb(path: path); |
| 23 | + }); |
| 24 | + |
| 25 | + createTables(SqliteDatabase db) async { |
| 26 | + await db.writeTransaction((tx) async { |
| 27 | + await tx.execute( |
| 28 | + 'CREATE TABLE _customers(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)'); |
| 29 | + await tx.execute( |
| 30 | + 'CREATE TABLE _local_customers(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)'); |
| 31 | + await tx |
| 32 | + .execute('CREATE VIEW customers AS SELECT * FROM _local_customers'); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + updateTables(SqliteDatabase db) async { |
| 37 | + await db.writeTransaction((tx) async { |
| 38 | + await tx.execute('DROP VIEW IF EXISTS customers'); |
| 39 | + await tx.execute('CREATE VIEW customers AS SELECT * FROM _customers'); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + test('should refresh schema views', () async { |
| 44 | + final db = await testUtils.setupDatabase(path: path); |
| 45 | + await createTables(db); |
| 46 | + |
| 47 | + final customerTables = |
| 48 | + await getSourceTables(db, "select * from customers"); |
| 49 | + expect(customerTables.contains('_local_customers'), true); |
| 50 | + await updateTables(db); |
| 51 | + |
| 52 | + // without this, source tables are outdated |
| 53 | + await db.refreshSchema(); |
| 54 | + |
| 55 | + final updatedCustomerTables = |
| 56 | + await getSourceTables(db, "select * from customers"); |
| 57 | + expect(updatedCustomerTables.contains('_customers'), true); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should complete refresh schema after transaction', () async { |
| 61 | + var completer1 = Completer<void>(); |
| 62 | + var transactionCompleted = false; |
| 63 | + |
| 64 | + final db = await testUtils.setupDatabase(path: path); |
| 65 | + await createTables(db); |
| 66 | + |
| 67 | + // Start a read transaction |
| 68 | + db.readTransaction((tx) async { |
| 69 | + completer1.complete(); |
| 70 | + await tx.get('select test_sleep(2000)'); |
| 71 | + |
| 72 | + transactionCompleted = true; |
| 73 | + }); |
| 74 | + |
| 75 | + // Wait for the transaction to start |
| 76 | + await completer1.future; |
| 77 | + |
| 78 | + var refreshSchemaFuture = db.refreshSchema(); |
| 79 | + |
| 80 | + // Setup check that refreshSchema completes after the transaction has completed |
| 81 | + var refreshAfterTransaction = false; |
| 82 | + refreshSchemaFuture.then((_) { |
| 83 | + if (transactionCompleted) { |
| 84 | + refreshAfterTransaction = true; |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + await refreshSchemaFuture; |
| 89 | + |
| 90 | + expect(refreshAfterTransaction, isTrue, |
| 91 | + reason: 'refreshSchema completed before transaction finished'); |
| 92 | + |
| 93 | + // Sanity check |
| 94 | + expect(transactionCompleted, isTrue, |
| 95 | + reason: 'Transaction did not complete as expected'); |
| 96 | + }); |
| 97 | + }); |
| 98 | +} |
0 commit comments