Skip to content

Commit f0fdd23

Browse files
Add tests for watch with comparator
1 parent 577e0d2 commit f0fdd23

File tree

3 files changed

+153
-52
lines changed

3 files changed

+153
-52
lines changed

packages/web/tests/watch.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
AbstractPowerSyncDatabase,
33
ArrayComparator,
44
GetAllQuery,
5+
QueryResult,
56
WatchedQueryDifferential,
67
WatchedQueryState
78
} from '@powersync/common';
@@ -367,6 +368,56 @@ describe('Watch Tests', { sequential: true }, () => {
367368
expect(state.data).toHaveLength(1);
368369
});
369370

371+
it('should compare results with old watch method', async () => {
372+
const controller = new AbortController();
373+
374+
const resultSets: QueryResult[] = [];
375+
376+
// Wait for the first query load
377+
await new Promise<void>((resolve) => {
378+
powersync.watch(
379+
'SELECT * FROM assets WHERE make = ?',
380+
['test'],
381+
{
382+
onResult: (result) => {
383+
// Mark that we received the first result, this helps with counting events.
384+
resolve();
385+
resultSets.push(result);
386+
}
387+
},
388+
{
389+
signal: controller.signal,
390+
comparator: {
391+
checkEquality: (current, previous) => {
392+
return JSON.stringify(current) === JSON.stringify(previous);
393+
}
394+
}
395+
}
396+
);
397+
});
398+
399+
onTestFinished(() => controller.abort());
400+
401+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);
402+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
403+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
404+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
405+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
406+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
407+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
408+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['nottest', uuid()]);
409+
await powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);
410+
411+
await vi.waitFor(
412+
() => {
413+
expect(resultSets[resultSets.length - 1]?.rows?._array?.map((r) => r.make)).deep.eq(['test', 'test']);
414+
// We should only have updated less than or equal 3 times
415+
expect(resultSets.length).lessThanOrEqual(3);
416+
},
417+
{ timeout: 1000, interval: 100 }
418+
);
419+
});
420+
370421
it('should only report updates for relevant changes', async () => {
371422
const watch = powersync
372423
.query<{ make: string }>({

tools/powersynctests/ios/Podfile.lock

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PODS:
3131
- ReactCommon/turbomodule/bridging
3232
- ReactCommon/turbomodule/core
3333
- Yoga
34-
- powersync-op-sqlite (0.7.2):
34+
- powersync-op-sqlite (0.7.3):
3535
- DoubleConversion
3636
- glog
3737
- hermes-engine
@@ -1829,71 +1829,71 @@ SPEC CHECKSUMS:
18291829
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
18301830
glog: eb93e2f488219332457c3c4eafd2738ddc7e80b8
18311831
hermes-engine: b417d2b2aee3b89b58e63e23a51e02be91dc876d
1832-
op-sqlite: af963896bb0d5393f12e07189bb39b1eaa5f4ac3
1833-
powersync-op-sqlite: a80d0cc9d4790470cea1752c83c0c9d1b6a30bb1
1832+
op-sqlite: 27db77bc067129a35dccf00dad447e683318825d
1833+
powersync-op-sqlite: a2913a28ebedd7dccbaf2c7e330e03b25cb82611
18341834
powersync-sqlite-core: a58efd88833861f0a8bb636c171bdf0ed55c9801
1835-
RCT-Folly: e78785aa9ba2ed998ea4151e314036f6c49e6d82
1835+
RCT-Folly: 36fe2295e44b10d831836cc0d1daec5f8abcf809
18361836
RCTDeprecation: b2eecf2d60216df56bc5e6be5f063826d3c1ee35
18371837
RCTRequired: 78522de7dc73b81f3ed7890d145fa341f5bb32ea
18381838
RCTTypeSafety: c135dd2bf50402d87fd12884cbad5d5e64850edd
18391839
React: b229c49ed5898dab46d60f61ed5a0bfa2ee2fadb
18401840
React-callinvoker: 2ac508e92c8bd9cf834cc7d7787d94352e4af58f
1841-
React-Core: 325b4f6d9162ae8b9a6ff42fe78e260eb124180d
1842-
React-CoreModules: 558041e5258f70cd1092f82778d07b8b2ff01897
1843-
React-cxxreact: 8fff17cbe76e6a8f9991b59552e1235429f9c74b
1841+
React-Core: 13cdd1558d0b3f6d9d5a22e14d89150280e79f02
1842+
React-CoreModules: b07a6744f48305405e67c845ebf481b6551b712a
1843+
React-cxxreact: 1055a86c66ac35b4e80bd5fb766aed5f494dfff4
18441844
React-debug: 0a5fcdbacc6becba0521e910c1bcfdb20f32a3f6
1845-
React-defaultsnativemodule: 618dc50a0fad41b489997c3eb7aba3a74479fd14
1846-
React-domnativemodule: 7ba599afb6c2a7ec3eb6450153e2efe0b8747e9a
1847-
React-Fabric: 252112089d2c63308f4cbfade4010b6606db67d1
1848-
React-FabricComponents: 3c0f75321680d14d124438ab279c64ec2a3d13c4
1849-
React-FabricImage: 728b8061cdec2857ca885fd605ee03ad43ffca98
1845+
React-defaultsnativemodule: 4bb28fc97fee5be63a9ebf8f7a435cfe8ba69459
1846+
React-domnativemodule: b36a11c2597243d7563985028c51ece988d8ae33
1847+
React-Fabric: afc561718f25b2cd800b709d934101afe376a12c
1848+
React-FabricComponents: f4e0a4e18a27bf6d39cbf2a0b42f37a92fa4e37f
1849+
React-FabricImage: 37d8e8b672eda68a19d71143eb65148084efb325
18501850
React-featureflags: 19682e02ef5861d96b992af16a19109c3dfc1200
1851-
React-featureflagsnativemodule: 23528c7e7d50782b7ef0804168ba40bbaf1e86ab
1852-
React-graphics: fefe48f71bfe6f48fd037f59e8277b12e91b6be1
1853-
React-hermes: a9a0c8377627b5506ef9a7b6f60a805c306e3f51
1854-
React-idlecallbacksnativemodule: 7e2b6a3b70e042f89cd91dbd73c479bb39a72a7e
1855-
React-ImageManager: e3300996ac2e2914bf821f71e2f2c92ae6e62ae2
1856-
React-jserrorhandler: fa75876c662e5d7e79d6efc763fc9f4c88e26986
1857-
React-jsi: f3f51595cc4c089037b536368f016d4742bf9cf7
1858-
React-jsiexecutor: cca6c232db461e2fd213a11e9364cfa6fdaa20eb
1859-
React-jsinspector: 2bd4c9fddf189d6ec2abf4948461060502582bef
1860-
React-jsinspectortracing: a417d8a0ad481edaa415734b4dac81e3e5ee7dc6
1861-
React-jsitracing: 1ff7172c5b0522cbf6c98d82bdbb160e49b5804e
1862-
React-logger: 018826bfd51b9f18e87f67db1590bc510ad20664
1863-
React-Mapbuffer: 3c11cee7737609275c7b66bd0b1de475f094cedf
1864-
React-microtasksnativemodule: 843f352b32aacbe13a9c750190d34df44c3e6c2c
1865-
react-native-get-random-values: d16467cf726c618e9c7a8c3c39c31faa2244bbba
1866-
React-NativeModulesApple: 88433b6946778bea9c153e27b671de15411bf225
1867-
React-perflogger: 9e8d3c0dc0194eb932162812a168aa5dc662f418
1868-
React-performancetimeline: 5a2d6efef52bdcefac079c7baa30934978acd023
1851+
React-featureflagsnativemodule: d7cddf6d907b4e5ab84f9e744b7e88461656e48c
1852+
React-graphics: b0f78580cdaf5800d25437e3d41cc6c3d83b7aea
1853+
React-hermes: 71186f872c932e4574d5feb3ed754dda63a0b3bd
1854+
React-idlecallbacksnativemodule: dd2af19cdd3bc55149d17a2409ed72b694dfbe9c
1855+
React-ImageManager: a77dde8d5aa6a2b6962c702bf3a47695ef0aa32b
1856+
React-jserrorhandler: 9c14e89f12d5904257a79aaf84a70cd2e5ac07ba
1857+
React-jsi: 0775a66820496769ad83e629f0f5cce621a57fc7
1858+
React-jsiexecutor: 2cf5ba481386803f3c88b85c63fa102cba5d769e
1859+
React-jsinspector: 8052d532bb7a98b6e021755674659802fb140cc5
1860+
React-jsinspectortracing: bdd8fd0adcb4813663562e7874c5842449df6d8a
1861+
React-jsitracing: 2bab3bf55de3d04baf205def375fa6643c47c794
1862+
React-logger: 795cd5055782db394f187f9db0477d4b25b44291
1863+
React-Mapbuffer: 0502faf46cab8fb89cfc7bf3e6c6109b6ef9b5de
1864+
React-microtasksnativemodule: 663bc64e3a96c5fc91081923ae7481adc1359a78
1865+
react-native-get-random-values: 21325b2244dfa6b58878f51f9aa42821e7ba3d06
1866+
React-NativeModulesApple: 16fbd5b040ff6c492dacc361d49e63cba7a6a7a1
1867+
React-perflogger: ab51b7592532a0ea45bf6eed7e6cae14a368b678
1868+
React-performancetimeline: bc2e48198ec814d578ac8401f65d78a574358203
18691869
React-RCTActionSheet: 592674cf61142497e0e820688f5a696e41bf16dd
1870-
React-RCTAnimation: e6d669872f9b3b4ab9527aab283b7c49283236b7
1871-
React-RCTAppDelegate: de2343fe08be4c945d57e0ecce44afcc7dd8fc03
1872-
React-RCTBlob: 3e2dce94c56218becc4b32b627fc2293149f798d
1873-
React-RCTFabric: cac2c033381d79a5956e08550b0220cb2d78ea93
1874-
React-RCTFBReactNativeSpec: d10ca5e0ccbfeac8c047361fedf8e4ac653887b6
1875-
React-RCTImage: dc04b176c022d12a8f55ae7a7279b1e091066ae0
1876-
React-RCTLinking: 88f5e37fe4f26fbc80791aa2a5f01baf9b9a3fd5
1877-
React-RCTNetwork: f213693565efbd698b8e9c18d700a514b49c0c8e
1878-
React-RCTSettings: a2d32a90c45a3575568cad850abc45924999b8a5
1879-
React-RCTText: 54cdcd1cbf6f6a91dc6317f5d2c2b7fc3f6bf7a0
1880-
React-RCTVibration: 11dae0e7f577b5807bb7d31e2e881eb46f854fd4
1870+
React-RCTAnimation: 8fbb8dba757b49c78f4db403133ab6399a4ce952
1871+
React-RCTAppDelegate: 7f88baa8cb4e5d6c38bb4d84339925c70c9ac864
1872+
React-RCTBlob: f89b162d0fe6b570a18e755eb16cbe356d3c6d17
1873+
React-RCTFabric: 8ad6d875abe6e87312cef90e4b15ef7f6bed72e6
1874+
React-RCTFBReactNativeSpec: 8c29630c2f379c729300e4c1e540f3d1b78d1936
1875+
React-RCTImage: ccac9969940f170503857733f9a5f63578e106e1
1876+
React-RCTLinking: d82427bbf18415a3732105383dff119131cadd90
1877+
React-RCTNetwork: 12ad4d0fbde939e00251ca5ca890da2e6825cc3c
1878+
React-RCTSettings: e7865bf9f455abf427da349c855f8644b5c39afa
1879+
React-RCTText: 2cdfd88745059ec3202a0842ea75a956c7d6f27d
1880+
React-RCTVibration: a3a1458e6230dfd64b3768ebc0a4aac430d9d508
18811881
React-rendererconsistency: 64e897e00d2568fd8dfe31e2496f80e85c0aaad1
1882-
React-rendererdebug: 41ce452460c44bba715d9e41d5493a96de277764
1882+
React-rendererdebug: a3f6d3ae7d2fa0035885026756281c07ee32479e
18831883
React-rncore: 58748c2aa445f56b99e5118dad0aedb51c40ce9f
1884-
React-RuntimeApple: 7785ed0d8ae54da65a88736bb63ca97608a6d933
1885-
React-RuntimeCore: 6029ea70bc77f98cfd43ebe69217f14e93ba1f12
1884+
React-RuntimeApple: f0fda7bacabd32daa099cfda8f07466c30acd149
1885+
React-RuntimeCore: 683ee0b6a76d4b4bf6fbf83a541895b4887cc636
18861886
React-runtimeexecutor: a188df372373baf5066e6e229177836488799f80
1887-
React-RuntimeHermes: a264609c28b796edfffc8ae4cb8fad1773ab948b
1888-
React-runtimescheduler: 23ec3a1e0fb1ec752d1a9c1fb15258c30bfc7222
1887+
React-RuntimeHermes: 907c8e9bec13ea6466b94828c088c24590d4d0b6
1888+
React-runtimescheduler: a2e2a39125dd6426b5d8b773f689d660cd7c5f60
18891889
React-timing: bb220a53a795ed57976a4855c521f3de2f298fe5
1890-
React-utils: 3b054aaebe658fc710a8d239d0e4b9fd3e0b78f9
1891-
ReactAppDependencyProvider: a1fb08dfdc7ebc387b2e54cfc9decd283ed821d8
1892-
ReactCodegen: 008c319179d681a6a00966edfc67fda68f9fbb2e
1893-
ReactCommon: 0c097b53f03d6bf166edbcd0915da32f3015dd90
1894-
RNVectorIcons: bd818296a51dc2bb8c3bd97a3ca399df1afe216d
1890+
React-utils: 300d8bbb6555dcffaca71e7a0663201b5c7edbbc
1891+
ReactAppDependencyProvider: f2e81d80afd71a8058589e19d8a134243fa53f17
1892+
ReactCodegen: a63a0ab6ae824aef2e8c744981edd718b16eb9f2
1893+
ReactCommon: 3d39389f8e2a2157d5c999f8fba57bd1c8f226f0
1894+
RNVectorIcons: 14a0c42f16a26bcc3e79a19bc1c5718284b1d469
18951895
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
1896-
Yoga: afd04ff05ebe0121a00c468a8a3c8080221cb14c
1896+
Yoga: 9b7fb56e7b08cde60e2153344fa6afbd88e5d99f
18971897

18981898
PODFILE CHECKSUM: a15b54e8d191759ce7fcccb262b8753851ec9fde
18991899

tools/powersynctests/src/tests/queries.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
column,
55
LockContext,
66
PowerSyncDatabase,
7+
QueryResult,
78
Schema,
89
Table
910
} from '@powersync/react-native';
@@ -653,5 +654,54 @@ export function registerBaseTests() {
653654
expect(results.map((r) => r.status)).deep.equal(Array(tests.length).fill('rejected'));
654655
}
655656
});
657+
658+
it('should compare results with old watch method', async () => {
659+
const controller = new AbortController();
660+
661+
const resultSets: QueryResult[] = [];
662+
663+
// Wait for the first query load
664+
const donePromise = new Promise<void>((resolve) => {
665+
db.watch(
666+
'SELECT * FROM users WHERE name = ?',
667+
['test'],
668+
{
669+
onResult: (result) => {
670+
// Mark that we received the first result, this helps with counting events.
671+
if (result.rows?._array?.length == 2) {
672+
resolve();
673+
}
674+
resultSets.push(result);
675+
}
676+
},
677+
{
678+
signal: controller.signal,
679+
comparator: {
680+
checkEquality: (current, previous) => {
681+
return JSON.stringify(current) === JSON.stringify(previous);
682+
}
683+
}
684+
}
685+
);
686+
});
687+
688+
689+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['test']);
690+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
691+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
692+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
693+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
694+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
695+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
696+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['nottest']);
697+
await db.execute('INSERT INTO users(id, name) VALUES (uuid(), ?)', ['test']);
698+
699+
700+
await donePromise;
701+
702+
expect(resultSets[resultSets.length - 1]?.rows?._array?.map((r) => r.name)).deep.eq(['test', 'test']);
703+
// We should only have updated less than or equal 3 times
704+
expect(resultSets.length).lessThanOrEqual(3);
705+
});
656706
});
657707
}

0 commit comments

Comments
 (0)