|
| 1 | +import 'package:logging/logging.dart'; |
| 2 | +import 'package:powersync/powersync.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +import 'test_server.dart'; |
| 6 | +import 'utils/abstract_test_utils.dart'; |
| 7 | +import 'utils/test_utils_impl.dart'; |
| 8 | + |
| 9 | +final testUtils = TestUtils(); |
| 10 | +const testId = "2290de4f-0488-4e50-abed-f8e8eb1d0b42"; |
| 11 | +const testId2 = "2290de4f-0488-4e50-abed-f8e8eb1d0b43"; |
| 12 | +const partialWarning = |
| 13 | + 'Potentially previously uploaded CRUD entries are still present'; |
| 14 | + |
| 15 | +class TestConnector extends PowerSyncBackendConnector { |
| 16 | + final Function _fetchCredentials; |
| 17 | + final Future<void> Function(PowerSyncDatabase database) _uploadData; |
| 18 | + |
| 19 | + TestConnector(this._fetchCredentials, this._uploadData); |
| 20 | + |
| 21 | + @override |
| 22 | + Future<PowerSyncCredentials?> fetchCredentials() { |
| 23 | + return _fetchCredentials(); |
| 24 | + } |
| 25 | + |
| 26 | + @override |
| 27 | + Future<void> uploadData(PowerSyncDatabase database) async { |
| 28 | + return _uploadData(database); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void main() { |
| 33 | + group('CRUD Tests', () { |
| 34 | + late PowerSyncDatabase powersync; |
| 35 | + late String path; |
| 36 | + |
| 37 | + setUp(() async { |
| 38 | + path = testUtils.dbPath(); |
| 39 | + await testUtils.cleanDb(path: path); |
| 40 | + }); |
| 41 | + |
| 42 | + tearDown(() async { |
| 43 | + // await powersync.disconnectAndClear(); |
| 44 | + await powersync.close(); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should warn for missing upload operations in uploadData', () async { |
| 48 | + var server = await createServer(); |
| 49 | + |
| 50 | + credentialsCallback() async { |
| 51 | + return PowerSyncCredentials( |
| 52 | + endpoint: server.endpoint, |
| 53 | + token: 'token', |
| 54 | + userId: 'userId', |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + uploadData(PowerSyncDatabase db) async { |
| 59 | + // Do nothing |
| 60 | + } |
| 61 | + |
| 62 | + final records = <LogRecord>[]; |
| 63 | + final sub = testWarningLogger.onRecord.listen(records.add); |
| 64 | + |
| 65 | + powersync = |
| 66 | + await testUtils.setupPowerSync(path: path, logger: testWarningLogger); |
| 67 | + powersync.retryDelay = Duration(milliseconds: 0); |
| 68 | + var connector = TestConnector(credentialsCallback, uploadData); |
| 69 | + powersync.connect(connector: connector); |
| 70 | + |
| 71 | + // Create something with CRUD in it. |
| 72 | + await powersync.execute( |
| 73 | + 'INSERT INTO assets(id, description) VALUES(?, ?)', [testId, 'test']); |
| 74 | + |
| 75 | + // Wait for the uploadData to be called. |
| 76 | + await Future.delayed(Duration(milliseconds: 100)); |
| 77 | + |
| 78 | + // Create something else with CRUD in it. |
| 79 | + await powersync.execute( |
| 80 | + 'INSERT INTO assets(id, description) VALUES(?, ?)', |
| 81 | + [testId2, 'test2']); |
| 82 | + |
| 83 | + sub.cancel(); |
| 84 | + |
| 85 | + var warningLogs = records.where((r) => r.level == Level.WARNING).toList(); |
| 86 | + expect(warningLogs, hasLength(2)); |
| 87 | + expect(warningLogs[0].message, contains(partialWarning)); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
0 commit comments