|
1 | | -import { DiffTriggerOperation, ExtractedTriggerDiffRecord, TriggerDiffRecord } from '@powersync/common'; |
2 | | -// import 'source-map-support/register'; |
| 1 | +import { |
| 2 | + DiffTriggerOperation, |
| 3 | + ExtractedTriggerDiffRecord, |
| 4 | + sanitizeUUID, |
| 5 | + TriggerDiffRecord, |
| 6 | + whenClause |
| 7 | +} from '@powersync/common'; |
3 | 8 | import { describe, expect, vi } from 'vitest'; |
4 | 9 | import { Database, databaseTest } from './utils'; |
5 | 10 |
|
@@ -111,7 +116,9 @@ describe('Triggers', () => { |
111 | 116 | await database.triggers.trackTableDiff({ |
112 | 117 | source: 'todos', |
113 | 118 | columns: ['list_id'], |
114 | | - when: { [DiffTriggerOperation.INSERT]: `json_extract(NEW.data, '$.list_id') = '${firstList.id}'` }, |
| 119 | + when: { |
| 120 | + [DiffTriggerOperation.INSERT]: whenClause`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(firstList.id)}` |
| 121 | + }, |
115 | 122 | operations: [DiffTriggerOperation.INSERT], |
116 | 123 | onChange: async (context) => { |
117 | 124 | // Fetches the current state of todo records that were inserted during this diff window. |
@@ -189,7 +196,7 @@ describe('Triggers', () => { |
189 | 196 | */ |
190 | 197 | await database.triggers.trackTableDiff({ |
191 | 198 | source: 'lists', |
192 | | - when: { [DiffTriggerOperation.UPDATE]: `NEW.id = '${list.id}'` }, |
| 199 | + when: { [DiffTriggerOperation.UPDATE]: whenClause`NEW.id = ${sanitizeUUID(list.id)}` }, |
193 | 200 | operations: [DiffTriggerOperation.UPDATE, DiffTriggerOperation.DELETE], |
194 | 201 | onChange: async (context) => { |
195 | 202 | // Fetches the todo records that were inserted during this diff |
@@ -298,7 +305,9 @@ describe('Triggers', () => { |
298 | 305 | await database.triggers.trackTableDiff({ |
299 | 306 | source: 'todos', |
300 | 307 | columns: ['list_id'], |
301 | | - when: { [DiffTriggerOperation.INSERT]: `json_extract(NEW.data, '$.list_id') = '${firstList.id}'` }, |
| 308 | + when: { |
| 309 | + [DiffTriggerOperation.INSERT]: whenClause`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(firstList.id)}` |
| 310 | + }, |
302 | 311 | operations: [DiffTriggerOperation.INSERT], |
303 | 312 | onChange: async (context) => { |
304 | 313 | // Fetches the todo records that were inserted during this diff |
@@ -342,11 +351,11 @@ describe('Triggers', () => { |
342 | 351 | async () => { |
343 | 352 | expect(todos.length).toEqual(todoCreationCount); |
344 | 353 | }, |
345 | | - { timeout: 10000, interval: 1000 } |
| 354 | + { timeout: 1000, interval: 100 } |
346 | 355 | ); |
347 | 356 | }); |
348 | 357 |
|
349 | | - databaseTest('Should extract diff values', { timeout: 10000 }, async ({ database }) => { |
| 358 | + databaseTest('Should extract diff values', async ({ database }) => { |
350 | 359 | await database.execute( |
351 | 360 | /* sql */ ` |
352 | 361 | INSERT INTO |
@@ -386,7 +395,9 @@ describe('Triggers', () => { |
386 | 395 | // The onChange handler is guaranteed to see any change after the state above. |
387 | 396 | await database.triggers.trackTableDiff({ |
388 | 397 | source: 'todos', |
389 | | - when: { [DiffTriggerOperation.INSERT]: `json_extract(NEW.data, '$.list_id') = '${firstList.id}'` }, |
| 398 | + when: { |
| 399 | + [DiffTriggerOperation.INSERT]: whenClause`json_extract(NEW.data, '$.list_id') = ${sanitizeUUID(firstList.id)}` |
| 400 | + }, |
390 | 401 | operations: [DiffTriggerOperation.INSERT], |
391 | 402 | onChange: async (context) => { |
392 | 403 | // Fetches the content of the records at the time of the operation |
@@ -416,11 +427,11 @@ describe('Triggers', () => { |
416 | 427 | expect(changes.map((c) => c.content)).toEqual(['todo 1', 'todo 2', 'todo 3']); |
417 | 428 | expect(changes.every((c) => c.operation === DiffTriggerOperation.INSERT)).toBeTruthy(); |
418 | 429 | }, |
419 | | - { timeout: 10000, interval: 1000 } |
| 430 | + { timeout: 1000, interval: 100 } |
420 | 431 | ); |
421 | 432 | }); |
422 | 433 |
|
423 | | - databaseTest('Should allow tracking 0 columns', { timeout: 10000 }, async ({ database }) => { |
| 434 | + databaseTest('Should allow tracking 0 columns', { timeout: 1000 }, async ({ database }) => { |
424 | 435 | /** |
425 | 436 | * Tracks the ids of todos reported via the trigger |
426 | 437 | */ |
@@ -491,7 +502,7 @@ describe('Triggers', () => { |
491 | 502 | async () => { |
492 | 503 | expect(changes).toEqual(ids); |
493 | 504 | }, |
494 | | - { timeout: 10000, interval: 1000 } |
| 505 | + { timeout: 1000, interval: 100 } |
495 | 506 | ); |
496 | 507 | }); |
497 | 508 | }); |
0 commit comments