|
| 1 | +import 'package:declarative_sqlite/declarative_sqlite.dart'; |
| 2 | +import 'package:declarative_sqlite/src/files/filesystem_file_repository.dart'; |
| 3 | +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + sqfliteFfiInit(); |
| 8 | + final databaseFactory = databaseFactoryFfi; |
| 9 | + |
| 10 | + group('Unified save() method', () { |
| 11 | + late DeclarativeDatabase database; |
| 12 | + |
| 13 | + setUp(() async { |
| 14 | + final schema = SchemaBuilder() |
| 15 | + .table('users', (table) { |
| 16 | + table.text('name').notNull(''); |
| 17 | + table.integer('age').notNull(0); |
| 18 | + table.text('email').lww(); |
| 19 | + table.key(['system_id']).primary(); |
| 20 | + }) |
| 21 | + .build(); |
| 22 | + |
| 23 | + database = await DeclarativeDatabase.open( |
| 24 | + ':memory:', |
| 25 | + databaseFactory: databaseFactory, |
| 26 | + schema: schema, |
| 27 | + fileRepository: FilesystemFileRepository('temp_test'), |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + test('save() inserts a new record when created without system_id', () async { |
| 32 | + // Create a new record without system_id |
| 33 | + final record = GenericDbRecord({ |
| 34 | + 'name': 'Alice', |
| 35 | + 'age': 30, |
| 36 | + 'email': 'alice@example.com', |
| 37 | + }, 'users', database); |
| 38 | + |
| 39 | + // Verify it's marked as new |
| 40 | + expect(record.isNewRecord, isTrue); |
| 41 | + expect(record.systemId, isNull); |
| 42 | + |
| 43 | + // Save should perform an INSERT |
| 44 | + await record.save(); |
| 45 | + |
| 46 | + // After save, should no longer be new and should have system_id |
| 47 | + expect(record.isNewRecord, isFalse); |
| 48 | + expect(record.systemId, isNotNull); |
| 49 | + |
| 50 | + // Verify the record exists in database |
| 51 | + final results = await database.queryTable('users'); |
| 52 | + expect(results.length, 1); |
| 53 | + expect(results[0]['name'], 'Alice'); |
| 54 | + expect(results[0]['age'], 30); |
| 55 | + expect(results[0]['email'], 'alice@example.com'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('save() updates an existing record when loaded from database', () async { |
| 59 | + // First insert a record directly |
| 60 | + final systemId = await database.insert('users', { |
| 61 | + 'name': 'Bob', |
| 62 | + 'age': 25, |
| 63 | + 'email': 'bob@example.com', |
| 64 | + }); |
| 65 | + |
| 66 | + // Load the record from database |
| 67 | + final results = await database.queryTable('users', |
| 68 | + where: 'system_id = ?', |
| 69 | + whereArgs: [systemId] |
| 70 | + ); |
| 71 | + final record = GenericDbRecord(results[0], 'users', database); |
| 72 | + |
| 73 | + // Verify it's marked as existing |
| 74 | + expect(record.isNewRecord, isFalse); |
| 75 | + expect(record.systemId, systemId); |
| 76 | + |
| 77 | + // Modify the record |
| 78 | + record.setValue('email', 'bob.updated@example.com'); |
| 79 | + |
| 80 | + // Save should perform an UPDATE |
| 81 | + await record.save(); |
| 82 | + |
| 83 | + // Verify the update |
| 84 | + final updatedResults = await database.queryTable('users', |
| 85 | + where: 'system_id = ?', |
| 86 | + whereArgs: [systemId] |
| 87 | + ); |
| 88 | + expect(updatedResults.length, 1); |
| 89 | + expect(updatedResults[0]['name'], 'Bob'); // Unchanged |
| 90 | + expect(updatedResults[0]['age'], 25); // Unchanged |
| 91 | + expect(updatedResults[0]['email'], 'bob.updated@example.com'); // Updated |
| 92 | + }); |
| 93 | + |
| 94 | + test('save() can be called multiple times on the same record', () async { |
| 95 | + // Create new record |
| 96 | + final record = GenericDbRecord({ |
| 97 | + 'name': 'Charlie', |
| 98 | + 'age': 35, |
| 99 | + 'email': 'charlie@example.com', |
| 100 | + }, 'users', database); |
| 101 | + |
| 102 | + // First save - inserts |
| 103 | + await record.save(); |
| 104 | + expect(record.isNewRecord, isFalse); |
| 105 | + final systemId = record.systemId; |
| 106 | + expect(systemId, isNotNull); |
| 107 | + |
| 108 | + // Modify and save again - updates (using LWW column) |
| 109 | + record.setValue('email', 'charlie.updated@example.com'); |
| 110 | + await record.save(); |
| 111 | + |
| 112 | + // Verify update worked |
| 113 | + final results = await database.queryTable('users', |
| 114 | + where: 'system_id = ?', |
| 115 | + whereArgs: [systemId] |
| 116 | + ); |
| 117 | + expect(results.length, 1); |
| 118 | + expect(results[0]['email'], 'charlie.updated@example.com'); |
| 119 | + |
| 120 | + // Modify and save one more time - updates again |
| 121 | + record.setValue('email', 'charlie.new@example.com'); |
| 122 | + await record.save(); |
| 123 | + |
| 124 | + // Verify second update worked |
| 125 | + final results2 = await database.queryTable('users', |
| 126 | + where: 'system_id = ?', |
| 127 | + whereArgs: [systemId] |
| 128 | + ); |
| 129 | + expect(results2.length, 1); |
| 130 | + expect(results2[0]['email'], 'charlie.new@example.com'); |
| 131 | + }); |
| 132 | + |
| 133 | + test('save() handles empty modifications on existing record', () async { |
| 134 | + // Insert and load a record |
| 135 | + final systemId = await database.insert('users', { |
| 136 | + 'name': 'Diana', |
| 137 | + 'age': 28, |
| 138 | + 'email': 'diana@example.com', |
| 139 | + }); |
| 140 | + |
| 141 | + final results = await database.queryTable('users', |
| 142 | + where: 'system_id = ?', |
| 143 | + whereArgs: [systemId] |
| 144 | + ); |
| 145 | + final record = GenericDbRecord(results[0], 'users', database); |
| 146 | + |
| 147 | + // Call save without modifications |
| 148 | + await record.save(); |
| 149 | + |
| 150 | + // Should complete without error |
| 151 | + expect(record.isNewRecord, isFalse); |
| 152 | + expect(record.systemId, systemId); |
| 153 | + }); |
| 154 | + |
| 155 | + test('reload() maintains correct isNewRecord state', () async { |
| 156 | + // Insert a record |
| 157 | + final systemId = await database.insert('users', { |
| 158 | + 'name': 'Eve', |
| 159 | + 'age': 32, |
| 160 | + 'email': 'eve@example.com', |
| 161 | + }); |
| 162 | + |
| 163 | + // Load the record |
| 164 | + final results = await database.queryTable('users', |
| 165 | + where: 'system_id = ?', |
| 166 | + whereArgs: [systemId] |
| 167 | + ); |
| 168 | + final record = GenericDbRecord(results[0], 'users', database); |
| 169 | + |
| 170 | + expect(record.isNewRecord, isFalse); |
| 171 | + |
| 172 | + // Reload the record |
| 173 | + await record.reload(); |
| 174 | + |
| 175 | + // Should still not be a new record |
| 176 | + expect(record.isNewRecord, isFalse); |
| 177 | + expect(record.systemId, systemId); |
| 178 | + }); |
| 179 | + |
| 180 | + test('isNewRecord getter provides correct state', () async { |
| 181 | + // New record |
| 182 | + final newRecord = GenericDbRecord({ |
| 183 | + 'name': 'Frank', |
| 184 | + 'age': 40, |
| 185 | + }, 'users', database); |
| 186 | + expect(newRecord.isNewRecord, isTrue); |
| 187 | + |
| 188 | + // Existing record |
| 189 | + final systemId = await database.insert('users', { |
| 190 | + 'name': 'Grace', |
| 191 | + 'age': 29, |
| 192 | + }); |
| 193 | + final results = await database.queryTable('users', |
| 194 | + where: 'system_id = ?', |
| 195 | + whereArgs: [systemId] |
| 196 | + ); |
| 197 | + final existingRecord = GenericDbRecord(results[0], 'users', database); |
| 198 | + expect(existingRecord.isNewRecord, isFalse); |
| 199 | + }); |
| 200 | + }); |
| 201 | +} |
| 202 | + |
| 203 | +/// A generic DbRecord implementation for testing |
| 204 | +class GenericDbRecord extends DbRecord { |
| 205 | + GenericDbRecord( |
| 206 | + Map<String, Object?> data, |
| 207 | + String tableName, |
| 208 | + DeclarativeDatabase database, |
| 209 | + ) : super(data, tableName, database); |
| 210 | +} |
0 commit comments