|
| 1 | +import 'package:drift/drift.dart' as drift; |
| 2 | +import 'package:drift/native.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:on_time_front/core/database/database.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + late AppDatabase database; |
| 8 | + |
| 9 | + setUp(() { |
| 10 | + database = AppDatabase.forTesting(NativeDatabase.memory()); |
| 11 | + }); |
| 12 | + |
| 13 | + tearDown(() async { |
| 14 | + await database.close(); |
| 15 | + }); |
| 16 | + |
| 17 | + test( |
| 18 | + 'creates lookup indexes for schedule and preparation DAO predicates', |
| 19 | + () async { |
| 20 | + for (final entry in _expectedLookupIndexesByTable.entries) { |
| 21 | + final indexNames = await _indexNames(database, entry.key); |
| 22 | + |
| 23 | + expect( |
| 24 | + indexNames, |
| 25 | + containsAll(entry.value), |
| 26 | + reason: '${entry.key} should declare indexes for DAO lookup paths', |
| 27 | + ); |
| 28 | + } |
| 29 | + }, |
| 30 | + ); |
| 31 | + |
| 32 | + test('adds lookup indexes when upgrading a schema 3 database', () async { |
| 33 | + expect(database.schemaVersion, 4); |
| 34 | + |
| 35 | + await _dropExpectedLookupIndexes(database); |
| 36 | + |
| 37 | + for (final entry in _expectedLookupIndexesByTable.entries) { |
| 38 | + expect( |
| 39 | + await _indexNames(database, entry.key), |
| 40 | + isNot(containsAll(entry.value)), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + await database.migration.onUpgrade(database.createMigrator(), 3, 4); |
| 45 | + |
| 46 | + for (final entry in _expectedLookupIndexesByTable.entries) { |
| 47 | + final indexNames = await _indexNames(database, entry.key); |
| 48 | + |
| 49 | + expect( |
| 50 | + indexNames, |
| 51 | + containsAll(entry.value), |
| 52 | + reason: '${entry.key} indexes should be added by the 3 -> 4 migration', |
| 53 | + ); |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +const _expectedLookupIndexesByTable = { |
| 59 | + 'preparation_schedules': { |
| 60 | + 'preparation_schedules_schedule_id_idx', |
| 61 | + 'preparation_schedules_next_preparation_id_idx', |
| 62 | + }, |
| 63 | + 'preparation_users': { |
| 64 | + 'preparation_users_user_id_idx', |
| 65 | + 'preparation_users_next_preparation_id_idx', |
| 66 | + }, |
| 67 | + 'schedules': {'schedules_schedule_time_idx', 'schedules_place_id_idx'}, |
| 68 | +}; |
| 69 | + |
| 70 | +Future<Set<String>> _indexNames(AppDatabase database, String tableName) async { |
| 71 | + final rows = await database |
| 72 | + .customSelect( |
| 73 | + ''' |
| 74 | + SELECT name |
| 75 | + FROM sqlite_master |
| 76 | + WHERE type = 'index' AND tbl_name = ? |
| 77 | + ''', |
| 78 | + variables: [drift.Variable.withString(tableName)], |
| 79 | + ) |
| 80 | + .get(); |
| 81 | + |
| 82 | + return {for (final row in rows) row.read<String>('name')}; |
| 83 | +} |
| 84 | + |
| 85 | +Future<void> _dropExpectedLookupIndexes(AppDatabase database) async { |
| 86 | + for (final indexNames in _expectedLookupIndexesByTable.values) { |
| 87 | + for (final indexName in indexNames) { |
| 88 | + await database.customStatement('DROP INDEX IF EXISTS $indexName'); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments