-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathmongo_test.test.ts
More file actions
599 lines (536 loc) · 18.5 KB
/
Copy pathmongo_test.test.ts
File metadata and controls
599 lines (536 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import { mongo } from '@powersync/lib-service-mongodb';
import {
applyRowContext,
CompatibilityContext,
CompatibilityEdition,
SqliteInputRow,
SqlSyncRules,
TimeValuePrecision
} from '@powersync/service-sync-rules';
import { describe, expect, test } from 'vitest';
import { MongoRouteAPIAdapter } from '@module/api/MongoRouteAPIAdapter.js';
import { ChangeStream } from '@module/replication/ChangeStream.js';
import { constructAfterRecord } from '@module/replication/MongoRelation.js';
import { PostImagesOption } from '@module/types/types.js';
import { clearTestDb, connectMongoData, TEST_CONNECTION_OPTIONS } from './util.js';
describe('mongo data types', () => {
async function setupTable(db: mongo.Db) {
await clearTestDb(db);
}
async function insert(collection: mongo.Collection) {
await collection.insertMany([
{
_id: 1 as any,
null: null,
text: 'text',
uuid: new mongo.UUID('baeb2514-4c57-436d-b3cc-c1256211656d'),
bool: true,
bytea: Buffer.from('test'),
int2: 1000,
int4: 1000000,
int8: 9007199254740993n,
float: 3.14,
decimal: new mongo.Decimal128('3.14')
},
{ _id: 2 as any, nested: { test: 'thing' } },
{ _id: 3 as any, date: new Date('2023-03-06 15:47+02') },
{
_id: 4 as any,
timestamp: mongo.Timestamp.fromBits(123, 456),
objectId: mongo.ObjectId.createFromHexString('66e834cc91d805df11fa0ecb'),
regexp: new mongo.BSONRegExp('test', 'i'),
minKey: new mongo.MinKey(),
maxKey: new mongo.MaxKey(),
symbol: new mongo.BSONSymbol('test'),
js: new mongo.Code('testcode'),
js2: new mongo.Code('testcode', { foo: 'bar' }),
pointer: new mongo.DBRef('mycollection', mongo.ObjectId.createFromHexString('66e834cc91d805df11fa0ecb')),
pointer2: new mongo.DBRef(
'mycollection',
mongo.ObjectId.createFromHexString('66e834cc91d805df11fa0ecb'),
'mydb',
{ foo: 'bar' }
)
},
{
_id: 6 as any,
int4: -1,
int8: -9007199254740993n,
float: -3.14,
decimal: new mongo.Decimal128('-3.14')
}
]);
}
async function insertUndefined(db: mongo.Db, collection: string, array?: boolean) {
// MongoDB has deprecated the `undefined` value, making it really
// difficult to insert one into the database.
// mapReduce is also deprecated, but it's one way to still generate
// the value.
const mapInput = db.collection('map_input');
await mapInput.insertOne({ test: 'test' });
const fin = array ? `return { result: [undefined] }` : `return { result: undefined }`;
await db.command({
mapReduce: 'map_input',
map: new mongo.Code(`function () {
// We only need to emit once for a single result:
emit(5, {});
}`),
reduce: new mongo.Code(`function (key, values) {
// Return an object whose property is explicitly set to undefined
return undefined;
}`),
finalize: new mongo.Code(`function (key, reducedVal) {
${fin};
}`),
out: { merge: 'map_output' }
});
await db
.collection('map_output')
.aggregate([
{ $set: { undefined: '$value.result' } },
{ $project: { undefined: 1 } },
{
$merge: {
into: collection
}
}
])
.toArray();
await mapInput.drop();
await db.collection('map_output').drop();
}
async function insertNested(collection: mongo.Collection) {
await collection.insertMany([
{
_id: 1 as any,
null: [null],
text: ['text'],
uuid: [new mongo.UUID('baeb2514-4c57-436d-b3cc-c1256211656d')],
bool: [true],
bytea: [Buffer.from('test')],
int2: [1000],
int4: [1000000],
int8: [9007199254740993n],
float: [3.14],
decimal: [new mongo.Decimal128('3.14')]
},
{ _id: 2 as any, nested: [{ test: 'thing' }] },
{ _id: 3 as any, date: [new Date('2023-03-06 15:47+02')] },
{
_id: 6 as any,
int4: [-1],
int8: [-9007199254740993n],
float: [-3.14],
decimal: [new mongo.Decimal128('-3.14')]
},
{
_id: 10 as any,
timestamp: [mongo.Timestamp.fromBits(123, 456)],
objectId: [mongo.ObjectId.createFromHexString('66e834cc91d805df11fa0ecb')],
regexp: [new mongo.BSONRegExp('test', 'i')],
minKey: [new mongo.MinKey()],
maxKey: [new mongo.MaxKey()],
symbol: [new mongo.BSONSymbol('test')],
js: [new mongo.Code('testcode')],
pointer: [new mongo.DBRef('mycollection', mongo.ObjectId.createFromHexString('66e834cc91d805df11fa0ecb'))],
undefined: [undefined]
}
]);
}
function checkResults(transformed: SqliteInputRow[]) {
const sqliteValue = transformed.map((e) => applyRowContext(e, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY));
expect(sqliteValue[0]).toMatchObject({
_id: 1n,
text: 'text',
uuid: 'baeb2514-4c57-436d-b3cc-c1256211656d',
bool: 1n,
bytea: new Uint8Array([116, 101, 115, 116]),
int2: 1000n,
int4: 1000000n,
int8: 9007199254740993n,
float: 3.14,
null: null,
decimal: '3.14'
});
expect(sqliteValue[1]).toMatchObject({
_id: 2n,
nested: '{"test":"thing"}'
});
expect(sqliteValue[2]).toMatchObject({
_id: 3n,
date: '2023-03-06 13:47:00.000Z'
});
expect(sqliteValue[3]).toMatchObject({
_id: 4n,
objectId: '66e834cc91d805df11fa0ecb',
timestamp: 1958505087099n,
regexp: '{"pattern":"test","options":"i"}',
minKey: null,
maxKey: null,
symbol: 'test',
js: '{"code":"testcode","scope":null}',
js2: '{"code":"testcode","scope":{"foo":"bar"}}',
pointer: '{"collection":"mycollection","oid":"66e834cc91d805df11fa0ecb","fields":{}}',
pointer2: '{"collection":"mycollection","oid":"66e834cc91d805df11fa0ecb","db":"mydb","fields":{"foo":"bar"}}'
});
// This must specifically be null, and not undefined.
expect(sqliteValue[4].undefined).toBeNull();
expect(sqliteValue[5]).toMatchObject({
_id: 6n,
int4: -1n,
int8: -9007199254740993n,
float: -3.14,
decimal: '-3.14'
});
}
function checkResultsNested(transformed: SqliteInputRow[]) {
const sqliteValue = transformed.map((e) => applyRowContext(e, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY));
expect(sqliteValue[0]).toMatchObject({
_id: 1n,
text: `["text"]`,
uuid: '["baeb2514-4c57-436d-b3cc-c1256211656d"]',
bool: '[1]',
bytea: '[null]',
int2: '[1000]',
int4: '[1000000]',
int8: `[9007199254740993]`,
float: '[3.14]',
null: '[null]'
});
// Note: Depending on to what extent we use the original postgres value, the whitespace may change, and order may change.
// We do expect that decimals and big numbers are preserved.
expect(sqliteValue[1]).toMatchObject({
_id: 2n,
nested: '[{"test":"thing"}]'
});
expect(sqliteValue[2]).toMatchObject({
_id: 3n,
date: '["2023-03-06 13:47:00.000Z"]'
});
expect(sqliteValue[3]).toMatchObject({
_id: 5n,
undefined: '[null]'
});
expect(sqliteValue[4]).toMatchObject({
_id: 6n,
int4: '[-1]',
int8: '[-9007199254740993]',
float: '[-3.14]',
decimal: '["-3.14"]'
});
expect(sqliteValue[5]).toMatchObject({
_id: 10n,
objectId: '["66e834cc91d805df11fa0ecb"]',
timestamp: '[1958505087099]',
regexp: '[{"pattern":"test","options":"i"}]',
symbol: '["test"]',
js: '[{"code":"testcode","scope":null}]',
pointer: '[{"collection":"mycollection","oid":"66e834cc91d805df11fa0ecb","fields":{}}]',
minKey: '[null]',
maxKey: '[null]'
});
}
test('test direct queries', async () => {
const { db, client } = await connectMongoData();
const collection = db.collection('test_data');
try {
await setupTable(db);
await insert(collection);
await insertUndefined(db, 'test_data');
const rawResults = await db
.collection('test_data')
.find({}, { sort: { _id: 1 } })
.toArray();
// It is tricky to save "undefined" with mongo, so we check that it succeeded.
expect(rawResults[4].undefined).toBeUndefined();
const transformed = [...ChangeStream.getQueryData(rawResults)];
checkResults(transformed);
} finally {
await client.close();
}
});
test('test direct queries - arrays', async () => {
const { db, client } = await connectMongoData();
const collection = db.collection('test_data_arrays');
try {
await setupTable(db);
await insertNested(collection);
await insertUndefined(db, 'test_data_arrays', true);
const rawResults = await db
.collection('test_data_arrays')
.find({}, { sort: { _id: 1 } })
.toArray();
expect(rawResults[3].undefined).toEqual([undefined]);
const transformed = [...ChangeStream.getQueryData(rawResults)];
checkResultsNested(transformed);
} finally {
await client.close();
}
});
test('test replication', async () => {
// With MongoDB, replication uses the exact same document format
// as normal queries. We test it anyway.
const { db, client } = await connectMongoData();
const collection = db.collection('test_data');
try {
await setupTable(db);
const stream = db.watch([], {
maxAwaitTimeMS: 50,
fullDocument: 'updateLookup'
});
await stream.tryNext();
await insert(collection);
await insertUndefined(db, 'test_data');
const transformed = await getReplicationTx(stream, 6);
checkResults(transformed);
} finally {
await client.close();
}
});
test('test replication - arrays', async () => {
const { db, client } = await connectMongoData();
const collection = db.collection('test_data');
try {
await setupTable(db);
const stream = db.watch([], {
maxAwaitTimeMS: 50,
fullDocument: 'updateLookup'
});
await stream.tryNext();
await insertNested(collection);
await insertUndefined(db, 'test_data_arrays', true);
const transformed = await getReplicationTx(stream, 6);
checkResultsNested(transformed);
} finally {
await client.close();
}
});
test('connection schema', async () => {
await using adapter = new MongoRouteAPIAdapter({
type: 'mongodb',
...TEST_CONNECTION_OPTIONS
});
const db = adapter.db;
await clearTestDb(db);
const collection = db.collection('test_data');
await setupTable(db);
await insert(collection);
await insertUndefined(db, 'test_data');
const schema = await adapter.getConnectionSchema();
const dbSchema = schema.filter((s) => s.name == TEST_CONNECTION_OPTIONS.database)[0];
expect(dbSchema).not.toBeNull();
expect(dbSchema.tables).toMatchObject([
{
name: 'test_data',
columns: [
{ name: '_id', sqlite_type: 4, internal_type: 'Integer' },
{ name: 'bool', sqlite_type: 4, internal_type: 'Boolean' },
{ name: 'bytea', sqlite_type: 1, internal_type: 'Binary' },
{ name: 'date', sqlite_type: 2, internal_type: 'Date' },
{ name: 'decimal', sqlite_type: 2, internal_type: 'Decimal' },
{ name: 'float', sqlite_type: 8, internal_type: 'Double' },
{ name: 'int2', sqlite_type: 4, internal_type: 'Integer' },
{ name: 'int4', sqlite_type: 4, internal_type: 'Integer' },
{ name: 'int8', sqlite_type: 4, internal_type: 'Long' },
// We can fix these later
{ name: 'js', sqlite_type: 2, internal_type: 'Object' },
{ name: 'js2', sqlite_type: 2, internal_type: 'Object' },
{ name: 'maxKey', sqlite_type: 0, internal_type: 'MaxKey' },
{ name: 'minKey', sqlite_type: 0, internal_type: 'MinKey' },
{ name: 'nested', sqlite_type: 2, internal_type: 'Object' },
{ name: 'null', sqlite_type: 0, internal_type: 'Null' },
{ name: 'objectId', sqlite_type: 2, internal_type: 'ObjectId' },
// We can fix these later
{ name: 'pointer', sqlite_type: 2, internal_type: 'Object' },
{ name: 'pointer2', sqlite_type: 2, internal_type: 'Object' },
{ name: 'regexp', sqlite_type: 2, internal_type: 'RegExp' },
// Can fix this later
{ name: 'symbol', sqlite_type: 2, internal_type: 'String' },
{ name: 'text', sqlite_type: 2, internal_type: 'String' },
{ name: 'timestamp', sqlite_type: 4, internal_type: 'Timestamp' },
{ name: 'undefined', sqlite_type: 0, internal_type: 'Null' },
{ name: 'uuid', sqlite_type: 2, internal_type: 'UUID' }
]
}
]);
});
test('validate postImages', async () => {
await using adapter = new MongoRouteAPIAdapter({
type: 'mongodb',
...TEST_CONNECTION_OPTIONS,
postImages: PostImagesOption.READ_ONLY
});
const db = adapter.db;
await clearTestDb(db);
const collection = db.collection('test_data');
await setupTable(db);
await insert(collection);
const { config: rules } = SqlSyncRules.fromYaml(
`
bucket_definitions:
global:
data:
- select _id as id, * from test_data
`,
{
...adapter.getParseSyncRulesOptions(),
// No schema-based validation at this point
schema: undefined
}
);
const source_table_patterns = rules.getSourceTables();
const results = await adapter.getDebugTablesInfo(source_table_patterns, rules);
const result = results[0];
expect(result).not.toBeNull();
expect(result.table).toMatchObject({
schema: 'powersync_test_data',
name: 'test_data',
replication_id: ['_id'],
data_queries: true,
parameter_queries: false,
errors: [
{
level: 'fatal',
message: 'changeStreamPreAndPostImages not enabled on powersync_test_data.test_data'
}
]
});
});
test('validate postImages - auto-configure', async () => {
await using adapter = new MongoRouteAPIAdapter({
type: 'mongodb',
...TEST_CONNECTION_OPTIONS,
postImages: PostImagesOption.AUTO_CONFIGURE
});
const db = adapter.db;
await clearTestDb(db);
const collection = db.collection('test_data');
await setupTable(db);
await insert(collection);
const { config: rules } = SqlSyncRules.fromYaml(
`
bucket_definitions:
global:
data:
- select _id as id, * from test_data
`,
{
...adapter.getParseSyncRulesOptions(),
// No schema-based validation at this point
schema: undefined
}
);
const source_table_patterns = rules.getSourceTables();
const results = await adapter.getDebugTablesInfo(source_table_patterns, rules);
const result = results[0];
expect(result).not.toBeNull();
expect(result.table).toMatchObject({
schema: 'powersync_test_data',
name: 'test_data',
replication_id: ['_id'],
data_queries: true,
parameter_queries: false,
errors: [
{
level: 'warning',
message:
'changeStreamPreAndPostImages not enabled on powersync_test_data.test_data, will be enabled automatically'
}
]
});
});
test('validate postImages - off', async () => {
await using adapter = new MongoRouteAPIAdapter({
type: 'mongodb',
...TEST_CONNECTION_OPTIONS,
postImages: PostImagesOption.OFF
});
const db = adapter.db;
await clearTestDb(db);
const collection = db.collection('test_data');
await setupTable(db);
await insert(collection);
const { config: rules } = SqlSyncRules.fromYaml(
`
bucket_definitions:
global:
data:
- select _id as id, * from test_data
`,
{
...adapter.getParseSyncRulesOptions(),
// No schema-based validation at this point
schema: undefined
}
);
const source_table_patterns = rules.getSourceTables();
const results = await adapter.getDebugTablesInfo(source_table_patterns, rules);
const result = results[0];
expect(result).not.toBeNull();
expect(result.table).toMatchObject({
schema: 'powersync_test_data',
name: 'test_data',
replication_id: ['_id'],
data_queries: true,
parameter_queries: false,
errors: []
});
});
test('date format', async () => {
const { db, client } = await connectMongoData();
const collection = db.collection('test_data');
try {
await setupTable(db);
await collection.insertOne({
fraction: new Date('2023-03-06 15:47:01.123+02'),
noFraction: new Date('2023-03-06 15:47:01+02')
});
const rawResults = await db
.collection('test_data')
.find({}, { sort: { _id: 1 } })
.toArray();
const [row] = [...ChangeStream.getQueryData(rawResults)];
const oldFormat = applyRowContext(row, CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY);
expect(oldFormat).toMatchObject({
fraction: '2023-03-06 13:47:01.123Z',
noFraction: '2023-03-06 13:47:01.000Z'
});
const newFormat = applyRowContext(row, new CompatibilityContext({ edition: CompatibilityEdition.SYNC_STREAMS }));
expect(newFormat).toMatchObject({
fraction: '2023-03-06T13:47:01.123Z',
noFraction: '2023-03-06T13:47:01.000Z'
});
const reducedPrecisionFormat = applyRowContext(
row,
new CompatibilityContext({
edition: CompatibilityEdition.SYNC_STREAMS,
maxTimeValuePrecision: TimeValuePrecision.seconds
})
);
expect(reducedPrecisionFormat).toMatchObject({
fraction: '2023-03-06T13:47:01Z',
noFraction: '2023-03-06T13:47:01Z'
});
} finally {
await client.close();
}
});
});
/**
* Return all the inserts from the first transaction in the replication stream.
*/
async function getReplicationTx(replicationStream: mongo.ChangeStream, count: number) {
let transformed: SqliteInputRow[] = [];
for await (const doc of replicationStream) {
// Specifically filter out map_input / map_output collections
if (!(doc as any)?.ns?.coll?.startsWith('test_data')) {
continue;
}
transformed.push(constructAfterRecord((doc as any).fullDocument));
if (transformed.length == count) {
break;
}
}
transformed.sort((a, b) => Number(a._id) - Number(b._id));
return transformed;
}