Skip to content

Commit b477f61

Browse files
committed
Add explicit date format tests
1 parent 5523540 commit b477f61

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

modules/module-mongodb/test/src/mongo_test.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { mongo } from '@powersync/lib-service-mongodb';
2-
import { applyRowContext, CompatibilityContext, SqliteInputRow, SqlSyncRules } from '@powersync/service-sync-rules';
2+
import {
3+
applyRowContext,
4+
CompatibilityContext,
5+
CompatibilityLevel,
6+
SqliteInputRow,
7+
SqlSyncRules
8+
} from '@powersync/service-sync-rules';
39
import { describe, expect, test } from 'vitest';
410

511
import { MongoRouteAPIAdapter } from '@module/api/MongoRouteAPIAdapter.js';
@@ -526,6 +532,38 @@ bucket_definitions:
526532
errors: []
527533
});
528534
});
535+
536+
test('date format', async () => {
537+
const { db, client } = await connectMongoData();
538+
const collection = db.collection('test_data');
539+
try {
540+
await setupTable(db);
541+
await collection.insertOne({
542+
fraction: new Date('2023-03-06 15:47:01.123+02'),
543+
noFraction: new Date('2023-03-06 15:47:01+02')
544+
});
545+
546+
const rawResults = await db
547+
.collection('test_data')
548+
.find({}, { sort: { _id: 1 } })
549+
.toArray();
550+
const [row] = [...ChangeStream.getQueryData(rawResults)];
551+
552+
const oldFormat = applyRowContext(row, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY);
553+
expect(oldFormat).toMatchObject({
554+
fraction: '2023-03-06 13:47:01.123Z',
555+
noFraction: '2023-03-06 13:47:01.000Z'
556+
});
557+
558+
const newFormat = applyRowContext(row, new CompatibilityContext(CompatibilityLevel.SYNC_STREAMS));
559+
expect(newFormat).toMatchObject({
560+
fraction: '2023-03-06T13:47:01.123Z',
561+
noFraction: '2023-03-06T13:47:01.000Z'
562+
});
563+
} finally {
564+
await client.close();
565+
}
566+
});
529567
});
530568

531569
/**

modules/module-postgres/test/src/pg_test.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
CompatibilityContext,
66
SqliteInputRow,
77
DateTimeValue,
8-
TimeValue
8+
TimeValue,
9+
CompatibilityLevel
910
} from '@powersync/service-sync-rules';
1011
import { describe, expect, test } from 'vitest';
1112
import { clearTestDb, connectPgPool, connectPgWire, TEST_URI } from './util.js';
@@ -436,6 +437,39 @@ VALUES(10, ARRAY['null']::TEXT[]);
436437
// const schema = await api.getConnectionsSchema(db);
437438
// expect(schema).toMatchSnapshot();
438439
});
440+
441+
test('date formats', async () => {
442+
const db = await connectPgWire();
443+
try {
444+
await setupTable(db);
445+
446+
await db.query(`
447+
INSERT INTO test_data(id, time, timestamp, timestamptz) VALUES (1, '17:42:01.12', '2023-03-06 15:47:12.4', '2023-03-06 15:47+02');
448+
`);
449+
450+
const [row] = [
451+
...WalStream.getQueryData(
452+
pgwire.pgwireRows(await db.query(`SELECT time, timestamp, timestamptz FROM test_data`))
453+
)
454+
];
455+
456+
const oldFormat = applyRowContext(row, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY);
457+
expect(oldFormat).toMatchObject({
458+
time: '17:42:01.12',
459+
timestamp: '2023-03-06 15:47:12.4',
460+
timestamptz: '2023-03-06 13:47:00Z'
461+
});
462+
463+
const newFormat = applyRowContext(row, new CompatibilityContext(CompatibilityLevel.SYNC_STREAMS));
464+
expect(newFormat).toMatchObject({
465+
time: '17:42:01.120000',
466+
timestamp: '2023-03-06T15:47:12.400000',
467+
timestamptz: '2023-03-06T13:47:00.000000Z'
468+
});
469+
} finally {
470+
await db.end();
471+
}
472+
});
439473
});
440474

441475
/**

0 commit comments

Comments
 (0)