@routier/pouchdb-plugin 0.0.1-alpha.1 → 0.0.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1520 +0,0 @@
1
- import { faker } from '@faker-js/faker';
2
- import { describe, it, expect, vi, afterAll } from 'vitest';
3
- import { generateData, wait, seedData } from '@routier/testing-plugin';
4
- import { IDbPlugin, UnknownRecord, uuidv4 } from '@routier/core';
5
- import { PouchDbPlugin } from '../PouchDbPlugin';
6
- import { TestDataStore } from './datastore/PouchDbDatastore';
7
-
8
- const generateDbName = () => `${uuidv4()}-db`;
9
- const pluginFactory: (dbname?: string) => IDbPlugin = (dbname?: string) => new PouchDbPlugin(dbname ?? generateDbName());
10
- const stores: TestDataStore[] = [];
11
- const factory = (dbname?: string) => {
12
-
13
- const store = new TestDataStore(pluginFactory(dbname));
14
-
15
- stores.push(store);
16
-
17
- return store;
18
- };
19
-
20
- describe("Product Tests", () => {
21
-
22
- afterAll(async () => {
23
- await Promise.all(stores.map(x => x.destroyAsync()));
24
- });
25
-
26
- describe('Save Operations', () => {
27
- it("can save changes when there are no changes", async () => {
28
- const dataStore = factory();
29
- const result = await dataStore.saveChangesAsync();
30
- expect(result.aggregate.size).toBe(0);
31
- });
32
- });
33
-
34
- describe('Add Operations', () => {
35
- it("Can add a basic product", async () => {
36
- const dataStore = factory();
37
- // Arrange
38
- const [item] = generateData(dataStore.products.schema, 1);
39
-
40
- // Act
41
- const [added] = await dataStore.products.addAsync(item);
42
- const response = await dataStore.saveChangesAsync();
43
-
44
- // Assert
45
- expect(response.aggregate.size).toBe(1);
46
- expect(added._id).toStrictEqual(expect.any(String));
47
- expect(added.category).toBe(item.category);
48
- expect(added.name).toBe(item.name);
49
- expect(added.inStock).toBe(item.inStock);
50
- expect(added.price).toBe(item.price);
51
- expect(added.tags).toEqual(item.tags);
52
- });
53
-
54
- it("Can add multiple products", async () => {
55
- const dataStore = factory();
56
- // Arrange
57
- const items = generateData(dataStore.products.schema, 2);
58
-
59
- // Act
60
- const added = await dataStore.products.addAsync(...items);
61
- const response = await dataStore.saveChangesAsync();
62
-
63
- // Assert
64
- expect(response.aggregate.size).toBe(2);
65
- expect(added).toHaveLength(2);
66
- added.forEach((product, index) => {
67
- expect(product._id).toStrictEqual(expect.any(String));
68
- expect(product.category).toBe(items[index].category);
69
- expect(product.name).toBe(items[index].name);
70
- expect(product.inStock).toBe(items[index].inStock);
71
- expect(product.price).toBe(items[index].price);
72
- expect(product.tags).toEqual(items[index].tags);
73
- });
74
- });
75
-
76
- describe("Proxy change tracking", () => {
77
- it("Add should be a proxy after calling addAsync", async () => {
78
- const dataStore = factory();
79
- // Arrange
80
- const [item] = generateData(dataStore.products.schema, 1);
81
-
82
- // Act
83
- const [added] = await dataStore.products.addAsync(item);
84
-
85
- // __tracking__ is undefined until saveChanges is called
86
- expect((added as UnknownRecord).__tracking__).toBeUndefined();
87
- expect((added as UnknownRecord).__isProxy__).toBe(true);
88
- });
89
-
90
- it("previewChanges should not set change tracking changes", async () => {
91
- const dataStore = factory();
92
- // Arrange
93
- const [item] = generateData(dataStore.products.schema, 1);
94
-
95
- // Act
96
- const [added] = await dataStore.products.addAsync(item);
97
-
98
- await dataStore.previewChangesAsync();
99
-
100
- // __tracking__ is undefined until saveChanges is called
101
- // previewChanges should not set change tracking deltas
102
- expect((added as UnknownRecord).__tracking__).toBeUndefined();
103
- expect((added as UnknownRecord).__isProxy__).toBe(true);
104
- });
105
-
106
- it("hasChanges should not set change tracking changes", async () => {
107
- const dataStore = factory();
108
- // Arrange
109
- const [item] = generateData(dataStore.products.schema, 1);
110
-
111
- // Act
112
- const [added] = await dataStore.products.addAsync(item);
113
-
114
- const hasChanges = await dataStore.hasChangesAsync();
115
-
116
- // __tracking__ is undefined until saveChanges is called
117
- // hasChangesAsync should not set change tracking deltas
118
- expect(hasChanges).toBe(true);
119
- expect((added as UnknownRecord).__tracking__).toBeUndefined();
120
- expect((added as UnknownRecord).__isProxy__).toBe(true);
121
- });
122
-
123
- it("Add should not set the id", async () => {
124
- const dataStore = factory();
125
- // Arrange
126
- const [item] = generateData(dataStore.products.schema, 1);
127
-
128
- // Act
129
- const [added] = await dataStore.products.addAsync(item);
130
-
131
- expect((added as UnknownRecord)._id).toBeUndefined();
132
- });
133
-
134
- it("should not mark and entity as dirty when the save result is modified", async () => {
135
- const dataStore = factory();
136
- // Arrange
137
- const [item] = generateData(dataStore.products.schema, 1);
138
-
139
- // Act
140
- const [added] = await dataStore.products.addAsync(item);
141
-
142
- const responseOne = await dataStore.saveChangesAsync();
143
- expect(responseOne.aggregate.size).toBe(1);
144
-
145
- added.name === "CHANGED";
146
-
147
- const responseTwo = await dataStore.saveChangesAsync();
148
- expect(responseTwo.aggregate.size).toBe(0);
149
- });
150
- });
151
- });
152
-
153
- describe('Remove Operations', () => {
154
- it("can remove one entity", async () => {
155
- const dataStore = factory();
156
- // Arrange
157
- await seedData(dataStore, () => dataStore.products, 2);
158
-
159
- // Act
160
- const found = await dataStore.products.firstAsync();
161
- await dataStore.products.removeAsync(found);
162
- const response = await dataStore.saveChangesAsync();
163
- expect(response.aggregate.size).toBe(1);
164
- const all = await dataStore.products.toArrayAsync();
165
- // Assert
166
- expect(all.length).toBe(1);
167
- });
168
-
169
- it("can remove many entities", async () => {
170
- const dataStore = factory();
171
- // Arrange
172
- await seedData(dataStore, () => dataStore.products, 20);
173
-
174
- // Act
175
- const found = await dataStore.products.take(10).toArrayAsync();
176
-
177
- expect(found.length).toBe(10);
178
-
179
- await dataStore.products.removeAsync(...found);
180
- const response = await dataStore.saveChangesAsync();
181
- expect(response.aggregate.size).toBe(10);
182
- const all = await dataStore.products.toArrayAsync();
183
- // Assert
184
- expect(all.length).toBe(10);
185
- });
186
-
187
- it("can remove many entities with a query", async () => {
188
- const dataStore = factory();
189
- // Arrange
190
- await seedData(dataStore, () => dataStore.products, 20);
191
-
192
- // Act
193
- await dataStore.products.take(10).removeAsync();
194
- const response = await dataStore.saveChangesAsync();
195
-
196
- expect(response.aggregate.size).toBe(10);
197
- const all = await dataStore.products.toArrayAsync();
198
-
199
- // Assert
200
- expect(all.length).toBe(10);
201
- });
202
-
203
- it("can remove many entities with a query", async () => {
204
- const dataStore = factory();
205
-
206
- // seedData will call saveChanges
207
- await dataStore.products.addAsync({
208
- category: "test_category",
209
- inStock: false,
210
- name: "test_name",
211
- price: 100,
212
- tags: ['accessory']
213
- });
214
-
215
- // Arrange
216
- await seedData(dataStore, () => dataStore.products, 20);
217
-
218
- // Act
219
- await dataStore.products.where(x => x.name === "test_name").removeAsync();
220
- const response = await dataStore.saveChangesAsync();
221
-
222
- expect(response.aggregate.size).toBe(1);
223
- const all = await dataStore.products.toArrayAsync();
224
-
225
- // Assert
226
- expect(all.length).toBe(20);
227
- });
228
- });
229
-
230
- describe('Update Operations', () => {
231
- it("should update one item", async () => {
232
- const dataStore = factory();
233
- // Arrange
234
- await seedData(dataStore, () => dataStore.products, 2);
235
- // Act
236
- const found = await dataStore.products.firstAsync();
237
- const word = faker.lorem.word();
238
- found.name = word;
239
- const response = await dataStore.saveChangesAsync();
240
- expect(response.aggregate.size).toBe(1);
241
- const foundAfterSave = await dataStore.products.firstAsync(w => w._id === found._id);
242
- // Assert
243
- expect(foundAfterSave.name).toBe(word);
244
- });
245
-
246
- it("should update one item twice", async () => {
247
- const dataStore = factory();
248
- await seedData(dataStore, () => dataStore.products, 2);
249
-
250
- const found = await dataStore.products.firstAsync();
251
- const firstWord = faker.lorem.word();
252
- found.name = firstWord;
253
- const firstResponse = await dataStore.saveChangesAsync();
254
-
255
- expect(firstResponse.aggregate.size).toBe(1);
256
- const foundAfterFirstSave = await dataStore.products.firstAsync(w => w._id === found._id);
257
- expect(foundAfterFirstSave.name).toBe(firstWord);
258
-
259
- const secondWord = faker.lorem.word();
260
- found.name = secondWord;
261
- const secondResponse = await dataStore.saveChangesAsync();
262
-
263
- const foundAfterSecondSave = await dataStore.products.firstAsync(w => w._id === found._id);
264
- expect(foundAfterSecondSave.name).toBe(secondWord);
265
- expect(secondResponse.aggregate.size).toBe(1);
266
- });
267
-
268
- it("should update many items", async () => {
269
- const dataStore = factory();
270
- // Arrange
271
- await seedData(dataStore, () => dataStore.products, 20);
272
- // Act
273
- const found = await dataStore.products.take(10).toArrayAsync();
274
-
275
- for (const product of found) {
276
- product.name = faker.lorem.word();
277
- product.price = faker.number.float();
278
- }
279
-
280
- const response = await dataStore.saveChangesAsync();
281
- expect(response.aggregate.size).toBe(10);
282
- expect(response.aggregate.updates).toBe(10);
283
- });
284
-
285
- it("should not save anything when property is reverted to original value", async () => {
286
- const dataStore = factory();
287
- // Arrange
288
- await seedData(dataStore, () => dataStore.products, 2);
289
- // Act
290
- const found = await dataStore.products.firstAsync();
291
- const word = faker.lorem.word();
292
- const old = found.name;
293
- found.name = word;
294
- found.name = old;
295
- const response = await dataStore.saveChangesAsync();
296
- expect(response.aggregate.size).toBe(0);
297
- const foundAfterSave = await dataStore.products.firstAsync(w => w._id === found._id);
298
- // Assert
299
- expect(foundAfterSave.name).toBe(old);
300
- });
301
-
302
- it("should save the value that was set last", async () => {
303
- const dataStore = factory();
304
- // Arrange
305
- await seedData(dataStore, () => dataStore.products, 2);
306
- // Act
307
- const found = await dataStore.products.firstAsync();
308
-
309
- let lastValue = 0;
310
- for (let i = 0; i < 100; i++) {
311
- lastValue = 100 * i;
312
- found.price = lastValue;
313
- }
314
-
315
- const response = await dataStore.saveChangesAsync();
316
- expect(response.aggregate.size).toBe(1);
317
- const foundAfterSave = await dataStore.products.firstAsync(w => w._id === found._id);
318
-
319
- // Assert
320
- expect(foundAfterSave.price).toBe(lastValue);
321
- });
322
-
323
- it("Can update an array and remove all items", async () => {
324
- const dataStore = factory();
325
- // Arrange
326
- await dataStore.products.addAsync({
327
- category: "test_category",
328
- inStock: false,
329
- name: "test_name",
330
- price: 100,
331
- tags: ['accessory']
332
- });
333
- await dataStore.saveChangesAsync();
334
-
335
- // Act
336
- const found = await dataStore.products.firstAsync();
337
- found.tags = [];
338
- const response = await dataStore.saveChangesAsync();
339
- expect(response.aggregate.size).toBe(1);
340
- const foundAfterSave = await dataStore.products.firstAsync(w => w._id === found._id);
341
- // Assert
342
- expect(foundAfterSave.tags.length).toBe(0);
343
- });
344
-
345
- it("QUIRK: should not update a nested array if it is not set through assignment", async () => {
346
- // SOLUTION: Set the property to the expected result
347
- const dataStore = factory();
348
- // Arrange
349
- await dataStore.products.addAsync({
350
- category: "test_category",
351
- inStock: false,
352
- name: "test_name",
353
- price: 100,
354
- tags: ['accessory']
355
- });
356
- await dataStore.saveChangesAsync();
357
-
358
- // Act
359
- const found = await dataStore.products.firstAsync();
360
- found.tags.length = 0;
361
- const response = await dataStore.saveChangesAsync();
362
- expect(response.aggregate.size).toBe(0);
363
- const foundAfterSave = await dataStore.products.firstAsync(w => w._id === found._id);
364
- // Assert - We modified it by ref so it was changed but not saved
365
- expect(foundAfterSave.tags.length).toBe(0);
366
- });
367
-
368
- it("should not mark the item as dirty when the property is set to the same value", async () => {
369
- const dataStore = factory();
370
- // Arrange
371
- await dataStore.products.addAsync({
372
- category: "test_category",
373
- inStock: false,
374
- name: "test_name",
375
- price: 100,
376
- tags: ['accessory']
377
- });
378
- await dataStore.saveChangesAsync();
379
-
380
- // Act
381
- const found = await dataStore.products.firstAsync();
382
- found.price = 100;
383
-
384
- const response = await dataStore.saveChangesAsync();
385
- expect(response.aggregate.size).toBe(0);
386
- });
387
-
388
- it("should force mark an item as dirty and should be saved", async () => {
389
- const dataStore = factory();
390
- // Arrange
391
- await dataStore.products.addAsync({
392
- category: "test_category",
393
- inStock: false,
394
- name: "test_name",
395
- price: 100,
396
- tags: ['accessory']
397
- });
398
- await dataStore.saveChangesAsync();
399
-
400
- // Act
401
- const found = await dataStore.products.firstAsync();
402
- found.price = 100;
403
-
404
- dataStore.products.attachments.markDirty(found);
405
-
406
- dataStore.products.attachments.getChangeType(found);
407
-
408
- const response = await dataStore.saveChangesAsync();
409
- expect(response.aggregate.size).toBe(1);
410
- });
411
- });
412
-
413
- describe('Attachment Operations', () => {
414
-
415
- it("Should attach entity", async () => {
416
- const dataStore = factory();
417
- const generatedData = generateData(dataStore.products.schema, 1);
418
- const secondDataStore = factory();
419
- const [added] = await dataStore.products.addAsync(...generatedData);
420
- await dataStore.saveChangesAsync();
421
-
422
- expect(secondDataStore.products.attachments.has(added)).toBe(false);
423
-
424
- secondDataStore.products.attachments.set(added);
425
-
426
- expect(secondDataStore.products.attachments.has(added)).toBe(true);
427
- });
428
-
429
- it("Should attach entity and save update across contexts pointed to the same database", async () => {
430
- const dbname = generateDbName();
431
- const dataStore = factory(dbname);
432
- const generatedData = generateData(dataStore.products.schema, 1);
433
- const secondDataStore = factory(dbname);
434
- await dataStore.products.addAsync(...generatedData);
435
- await dataStore.saveChangesAsync();
436
-
437
- const found = await dataStore.products.firstAsync();
438
-
439
- expect(secondDataStore.products.attachments.has(found)).toBe(false);
440
-
441
- secondDataStore.products.attachments.set(found);
442
-
443
- found.category = "changed_value";
444
-
445
- const result = await secondDataStore.saveChangesAsync();
446
-
447
- expect(result.aggregate.updates).toBe(1);
448
- });
449
-
450
- it("Should detach entity", async () => {
451
- const dataStore = factory();
452
- const generatedData = generateData(dataStore.products.schema, 1);
453
- const [added] = await dataStore.products.addAsync(...generatedData);
454
- await dataStore.saveChangesAsync();
455
-
456
- dataStore.products.attachments.set(added);
457
- expect(dataStore.products.attachments.has(added)).toBe(true);
458
-
459
- dataStore.products.attachments.remove(added);
460
- expect(dataStore.products.attachments.has(added)).toBe(false);
461
- });
462
-
463
- it("Should get attached entity", async () => {
464
- const dataStore = factory();
465
- const generatedData = generateData(dataStore.products.schema, 1);
466
- const [added] = await dataStore.products.addAsync(...generatedData);
467
- await dataStore.saveChangesAsync();
468
-
469
- dataStore.products.attachments.set(added);
470
- const found = dataStore.products.attachments.get(added);
471
-
472
- expect(found).toBeDefined();
473
- expect(found?._id).toBe(added._id);
474
- });
475
-
476
- it("Should filter attached entities", async () => {
477
- const dataStore = factory();
478
- const secondDataStore = factory();
479
- await seedData(dataStore, () => dataStore.products, 100);
480
-
481
- const items = await dataStore.products.where(x => x.price > 100).take(10).toArrayAsync();
482
- secondDataStore.products.attachments.set(...items);
483
-
484
- const filtered = secondDataStore.products.attachments.filter(w => w.price > 100);
485
- expect(filtered.length).toBe(items.length);
486
- });
487
-
488
- it("Should find attached entity", async () => {
489
- const dataStore = factory();
490
- const secondDataStore = factory();
491
- await seedData(dataStore, () => dataStore.products, 100);
492
-
493
- const items = await dataStore.products.where(x => x.price > 100).take(10).toArrayAsync();
494
- secondDataStore.products.attachments.set(...items);
495
-
496
- const found = secondDataStore.products.attachments.find(w => w.price > 100);
497
- expect(Array.isArray(found)).toBe(false);
498
- expect(found).toBeDefined();
499
- });
500
-
501
- it("Should mark entity as dirty", async () => {
502
- const dataStore = factory();
503
- await seedData(dataStore, () => dataStore.products, 100);
504
-
505
- const first = await dataStore.products.firstAsync();
506
-
507
- dataStore.products.attachments.markDirty(first);
508
-
509
- const changeType = dataStore.products.attachments.getChangeType(first);
510
- expect(changeType).toBeDefined();
511
- });
512
-
513
-
514
- it("Should detach and attach and mark entity as dirty", async () => {
515
- const dataStore = factory();
516
- const secondDataStore = factory();
517
- await seedData(dataStore, () => dataStore.products, 100);
518
-
519
- const first = await dataStore.products.firstAsync();
520
-
521
- dataStore.products.attachments.remove(first);
522
-
523
- expect(dataStore.products.attachments.getChangeType(first)).toBeUndefined();
524
- expect(secondDataStore.products.attachments.getChangeType(first)).toBeUndefined();
525
-
526
- secondDataStore.products.attachments.set(first);
527
-
528
- expect(secondDataStore.products.attachments.getChangeType(first)).toBe("notModified");
529
- secondDataStore.products.attachments.markDirty(first);
530
- expect(secondDataStore.products.attachments.getChangeType(first)).toBe("markedDirty");
531
- });
532
-
533
- it("Should get change type for attached entity", async () => {
534
- const dataStore = factory();
535
- await seedData(dataStore, () => dataStore.products, 100);
536
-
537
- const first = await dataStore.products.firstAsync();
538
-
539
- const changeType = dataStore.products.attachments.getChangeType(first);
540
- expect(changeType).toBeDefined();
541
- });
542
-
543
- it("Should return undefined for change type of unattached entity", async () => {
544
- const dataStore = factory();
545
- const secondDataStore = factory();
546
- await seedData(dataStore, () => dataStore.products, 100);
547
-
548
- const first = await dataStore.products.firstAsync();
549
-
550
- const changeType = secondDataStore.products.attachments.getChangeType(first);
551
- expect(changeType).toBeUndefined();
552
- });
553
-
554
- it('added items are not attached until saveChanges is called', async () => {
555
- const dataStore = factory();
556
- // Arrange
557
- const [added] = await dataStore.products.addAsync({
558
- category: "test_category",
559
- inStock: false,
560
- name: "test_name",
561
- price: 100,
562
- tags: ['accessory']
563
- });
564
-
565
- const changeType = dataStore.products.attachments.getChangeType(added);
566
-
567
- expect(changeType).toBeUndefined();
568
- });
569
-
570
- it('added items are not attached until saveChanges is called', async () => {
571
- const dataStore = factory();
572
- // Arrange
573
- const [added] = await dataStore.products.addAsync({
574
- category: "test_category",
575
- inStock: false,
576
- name: "test_name",
577
- price: 100,
578
- tags: ['accessory']
579
- });
580
-
581
- await dataStore.saveChangesAsync();
582
-
583
- const changeType = dataStore.products.attachments.getChangeType(added);
584
-
585
- expect(changeType).toBe("notModified");
586
- });
587
-
588
- it('should remove an attached item and not be saved', async () => {
589
- const dataStore = factory();
590
- // Arrange
591
- await dataStore.products.addAsync({
592
- category: "test_category",
593
- inStock: false,
594
- name: "test_name",
595
- price: 100,
596
- tags: ['accessory']
597
- });
598
-
599
- await dataStore.saveChangesAsync();
600
-
601
- const found = await dataStore.products.firstAsync();
602
-
603
- found.name = "CHANGED";
604
-
605
- const changeType = dataStore.products.attachments.getChangeType(found);
606
-
607
- expect(changeType).toBe("propertiesChanged");
608
-
609
- dataStore.products.attachments.remove(found);
610
-
611
- const response = await dataStore.saveChangesAsync();
612
-
613
- expect(response.aggregate.size).toBe(0);
614
- });
615
-
616
- it('should filter attached items', async () => {
617
- const dataStore = factory();
618
- // Arrange
619
- await dataStore.products.addAsync({
620
- category: "test_category",
621
- inStock: false,
622
- name: "test_name",
623
- price: 100,
624
- tags: ['accessory']
625
- }, {
626
- category: "test_category",
627
- inStock: true,
628
- name: "test_name_2",
629
- price: 1000,
630
- tags: ['accessory']
631
- });
632
-
633
- await dataStore.saveChangesAsync();
634
-
635
- const attachments = dataStore.products.attachments.filter(x => x.category === "test_category");
636
-
637
- expect(attachments.length).toBe(2);
638
- });
639
-
640
- it('should have attached item', async () => {
641
- const dataStore = factory();
642
- // Arrange
643
- await dataStore.products.addAsync({
644
- category: "test_category",
645
- inStock: false,
646
- name: "test_name",
647
- price: 100,
648
- tags: ['accessory']
649
- });
650
-
651
- await dataStore.saveChangesAsync();
652
-
653
- const found = await dataStore.products.firstAsync();
654
-
655
- expect(dataStore.products.attachments.has(found)).toBe(true);
656
- });
657
-
658
- it('should get attached item', async () => {
659
- const dataStore = factory();
660
- // Arrange
661
- await dataStore.products.addAsync({
662
- category: "test_category",
663
- inStock: false,
664
- name: "test_name",
665
- price: 100,
666
- tags: ['accessory']
667
- });
668
-
669
- await dataStore.saveChangesAsync();
670
-
671
- const found = await dataStore.products.firstAsync();
672
-
673
- expect(dataStore.products.attachments.get(found)).toEqual(found);
674
- });
675
-
676
- it('should find attached item', async () => {
677
- const dataStore = factory();
678
- // Arrange
679
- await dataStore.products.addAsync({
680
- category: "test_category",
681
- inStock: false,
682
- name: "test_name",
683
- price: 100,
684
- tags: ['accessory']
685
- });
686
-
687
- await dataStore.saveChangesAsync();
688
-
689
- const found = await dataStore.products.firstAsync();
690
-
691
- expect(dataStore.products.attachments.find(x => x._id === found._id)).toEqual(found);
692
- });
693
- })
694
-
695
- describe('ToArray Operations', () => {
696
- it("all records", async () => {
697
- const dataStore = factory();
698
- // Arrange
699
- await seedData(dataStore, () => dataStore.products);
700
-
701
- // Act
702
- const found = await dataStore.products.toArrayAsync();
703
-
704
- // Assert
705
- expect(found.length).toBe(2);
706
- });
707
-
708
- it("toArrayAsync with no seed data", async () => {
709
- const dataStore = factory();
710
- // Act
711
- const found = await dataStore.products.toArrayAsync();
712
-
713
- // Assert
714
- expect(found.length).toBe(0);
715
- });
716
-
717
- it("where + skip + toArrayAsync", async () => {
718
- const dataStore = factory();
719
- // Arrange
720
- await seedData(dataStore, () => dataStore.products, 100);
721
-
722
- // Act
723
- const total = await dataStore.products.toArrayAsync();
724
- const found = await dataStore.products.where(w => w._id != "_________").skip(1).toArrayAsync();
725
-
726
- // Assert
727
- expect(total.length).toBe(100);
728
- expect(found.length).toBe(99);
729
- });
730
-
731
- it("where + skip + toArrayAsync with sort and take", async () => {
732
- const dataStore = factory();
733
- // Arrange
734
- await seedData(dataStore, () => dataStore.products, 2);
735
-
736
- // Act
737
- const found = await dataStore.products.where(w => w._id != "").sort(w => w.name).skip(1).take(1).toArrayAsync();
738
-
739
- // Assert
740
- expect(found.length).toBe(1);
741
- });
742
-
743
- it("where + toArrayAsync", async () => {
744
- const dataStore = factory();
745
- // Arrange
746
- await seedData(dataStore, () => dataStore.products);
747
-
748
- // Act
749
- const found = await dataStore.products.where(w => w._id != "").toArrayAsync();
750
-
751
- // Assert
752
- expect(found.length).toBe(2);
753
- });
754
-
755
- it("map + toArrayAsync + one property", async () => {
756
- const dataStore = factory();
757
- // Arrange
758
- await seedData(dataStore, () => dataStore.products, 100);
759
-
760
- // Act
761
- const found = await dataStore.products.map(w => w._id).toArrayAsync()
762
-
763
- // Assert
764
- expect(found).toBeDefined();
765
- expect(found.length).toBe(100);
766
- expect(found[0]).toBeTypeOf("string");
767
- });
768
-
769
- it("sort", async () => {
770
- const dataStore = factory();
771
- // Arrange
772
- await seedData(dataStore, () => dataStore.products, 200);
773
-
774
- const sorted = await dataStore.products.sort(w => w.price).toArrayAsync();
775
-
776
- let last: null | number = null;
777
-
778
- for (const item of sorted) {
779
- if (last == null) {
780
- last = item.price;
781
- continue;
782
- }
783
-
784
- expect(last).toBeLessThanOrEqual(item.price);
785
- last = item.price;
786
- }
787
- });
788
-
789
- it("sort descending", async () => {
790
- const dataStore = factory();
791
- // Arrange
792
- await seedData(dataStore, () => dataStore.products, 200);
793
-
794
- const sorted = await dataStore.products.sortDescending(w => w.price).toArrayAsync();
795
-
796
- let last: null | number = null;
797
-
798
- for (const item of sorted) {
799
- if (last == null) {
800
- last = item.price;
801
- continue;
802
- }
803
-
804
- expect(last).toBeGreaterThanOrEqual(item.price);
805
- last = item.price;
806
- }
807
- });
808
-
809
- it("where + where", async () => {
810
- const dataStore = factory();
811
- // Arrange
812
- await seedData(dataStore, () => dataStore.products, 200);
813
-
814
- const all = await dataStore.products.toArrayAsync();
815
- const count = await dataStore.products.where(w => w.price > 100).where(w => w.name.startsWith("s")).toArrayAsync();
816
-
817
- const expectedSum = all.filter(w => w.price > 100 && w.name.startsWith("s"));
818
-
819
- expectedSum.sort();
820
- count.sort();
821
-
822
- expect(expectedSum).toStrictEqual(count);
823
- });
824
-
825
- it("where + sort + toArrayAsync", async () => {
826
- const dataStore = factory();
827
- // Arrange
828
- await seedData(dataStore, () => dataStore.products, 200);
829
-
830
- const all = await dataStore.products.toArrayAsync();
831
- const result = await dataStore.products
832
- .where(w => w.price > 100)
833
- .sortDescending(w => w.price)
834
- .map(w => w.price)
835
- .toArrayAsync();
836
-
837
- const expected = all.filter(w => w.price > 100).map(w => w.price);
838
-
839
- expected.sort((a, b) => b - a)
840
-
841
- expect(expected).toStrictEqual(result);
842
- });
843
- });
844
-
845
- describe('First Operations', () => {
846
- it("firstAsync with result", async () => {
847
- const dataStore = factory();
848
- // Arrange
849
- await seedData(dataStore, () => dataStore.products);
850
-
851
- // Act
852
- const found = await dataStore.products.firstAsync();
853
-
854
- // Assert
855
- expect(found).toBeDefined();
856
- });
857
-
858
- it("firstAsync with no result", async () => {
859
- const dataStore = factory();
860
- // Act
861
- await expect(dataStore.products.firstAsync(w => w._id === "SomeMissingId")).rejects.toThrow();
862
- });
863
-
864
- it("where + firstAsync with no result found", async () => {
865
- const dataStore = factory();
866
- // Arrange
867
- await seedData(dataStore, () => dataStore.products);
868
-
869
- // Act
870
- await expect(dataStore.products.where(w => w._id === "SomeMissingId").firstAsync()).rejects.toThrow();
871
- });
872
-
873
- it("where + firstAsync", async () => {
874
- const dataStore = factory();
875
- // Arrange
876
- await seedData(dataStore, () => dataStore.products);
877
-
878
- // Act
879
- const found = await dataStore.products.where(w => w._id != "").firstAsync();
880
-
881
- // Assert
882
- expect(found).toBeDefined();
883
- });
884
-
885
- it("throws: where + firstAsync", async () => {
886
- const dataStore = factory();
887
- // Arrange
888
- await seedData(dataStore, () => dataStore.products);
889
-
890
- // Act/Assert
891
- await expect(dataStore.products.where(w => w._id === "").firstAsync()).rejects.toThrow();
892
- });
893
- });
894
-
895
- describe('FirstOrUndefined Operations', () => {
896
- it("firstOrUndefinedAsync with result", async () => {
897
- const dataStore = factory();
898
- // Arrange
899
- await seedData(dataStore, () => dataStore.products);
900
-
901
- // Act
902
- const found = await dataStore.products.firstOrUndefinedAsync();
903
-
904
- // Assert
905
- expect(found).toBeDefined();
906
- });
907
-
908
- it("firstOrUndefinedAsync with no result", async () => {
909
- const dataStore = factory();
910
- // Act
911
- const found = await dataStore.products.firstOrUndefinedAsync();
912
-
913
- // Assert
914
- expect(found).toBeUndefined();
915
- });
916
-
917
- it("where + firstOrUndefinedAsync", async () => {
918
- const dataStore = factory();
919
- // Arrange
920
- await seedData(dataStore, () => dataStore.products);
921
-
922
- // Act
923
- const found = await dataStore.products.where(w => w._id != "").firstOrUndefinedAsync();
924
-
925
- // Assert
926
- expect(found).toBeDefined();
927
- });
928
-
929
- it("map + firstOrUndefinedAsync + one property", async () => {
930
- const dataStore = factory();
931
- // Arrange
932
- await seedData(dataStore, () => dataStore.products, 100);
933
-
934
- // Act
935
- const found = await dataStore.products.map(w => w._id).firstOrUndefinedAsync();
936
-
937
- // Assert
938
- expect(found).toBeDefined();
939
- expect(found).toBeTypeOf("string");
940
- });
941
-
942
- it("map + firstOrUndefinedAsync + two properties", async () => {
943
- const dataStore = factory();
944
- // Arrange
945
- await seedData(dataStore, () => dataStore.products, 100);
946
-
947
- // Act
948
- const found = await dataStore.products.map(w => ({ first: w._id, second: w.inStock })).firstOrUndefinedAsync();
949
-
950
- // Assert
951
- expect(found).toBeDefined();
952
- expect(found?.first).toBeDefined();
953
- expect(found?.second).toBeDefined();
954
- });
955
- });
956
-
957
- describe('Every Operations', () => {
958
- it("everyAsync = true", async () => {
959
- const dataStore = factory();
960
- // Arrange
961
- await seedData(dataStore, () => dataStore.products);
962
-
963
- // Act
964
- const found = await dataStore.products.everyAsync(w => w._id != "");
965
-
966
- // Assert
967
- expect(found).toBe(true);
968
- });
969
-
970
- it("everyAsync = false", async () => {
971
- const dataStore = factory();
972
- // Arrange
973
- await seedData(dataStore, () => dataStore.products);
974
-
975
- // Act
976
- const found = await dataStore.products.everyAsync(w => w._id === "");
977
-
978
- // Assert
979
- expect(found).toBe(false);
980
- });
981
- });
982
-
983
- describe('Some Operations', () => {
984
- it("someAsync = true", async () => {
985
- const dataStore = factory();
986
- // Arrange
987
- await seedData(dataStore, () => dataStore.products);
988
-
989
- // Act
990
- const found = await dataStore.products.someAsync(w => w._id != "");
991
-
992
- // Assert
993
- expect(found).toBe(true);
994
- });
995
-
996
- it("someAsync = false", async () => {
997
- const dataStore = factory();
998
- // Arrange
999
- await seedData(dataStore, () => dataStore.products);
1000
-
1001
- // Act
1002
- const found = await dataStore.products.someAsync(w => w._id === "");
1003
-
1004
- // Assert
1005
- expect(found).toBe(false);
1006
- });
1007
-
1008
- it("where + someAsync", async () => {
1009
- const dataStore = factory();
1010
- // Arrange
1011
- await seedData(dataStore, () => dataStore.products);
1012
-
1013
- // Act
1014
- const found = await dataStore.products.where(w => w._id != "").someAsync();
1015
-
1016
- // Assert
1017
- expect(found).toBeDefined();
1018
- });
1019
- });
1020
-
1021
- describe('Count Operations', () => {
1022
- it("countAsync", async () => {
1023
- const dataStore = factory();
1024
- // Arrange
1025
- await seedData(dataStore, () => dataStore.products, 200);
1026
-
1027
- const all = await dataStore.products.toArrayAsync();
1028
- const count = await dataStore.products.map(w => w.price).countAsync();
1029
-
1030
- expect(all.length).toBe(count);
1031
- });
1032
-
1033
- it("where + countAsync", async () => {
1034
- const dataStore = factory();
1035
- // Arrange
1036
- await seedData(dataStore, () => dataStore.products);
1037
-
1038
- // Act
1039
- const found = await dataStore.products.where(w => w._id != "").countAsync();
1040
-
1041
- // Assert
1042
- expect(found).toBe(2);
1043
- });
1044
-
1045
- it("where + countAsync with no data", async () => {
1046
- const dataStore = factory();
1047
- // Act
1048
- const found = await dataStore.products.where(w => w._id != "").countAsync();
1049
-
1050
- // Assert
1051
- expect(found).toBe(0);
1052
- });
1053
- });
1054
-
1055
- describe('Max Operations', () => {
1056
- it("max", async () => {
1057
- const dataStore = factory();
1058
- // Arrange
1059
- await seedData(dataStore, () => dataStore.products, 200);
1060
-
1061
- const all = await dataStore.products.toArrayAsync();
1062
- const max = await dataStore.products.maxAsync(w => w.price);
1063
-
1064
- all.sort((a, b) => b.price - a.price);
1065
-
1066
- expect(max).toBe(all[0].price);
1067
- });
1068
- });
1069
-
1070
- describe('Min Operations', () => {
1071
- it("min", async () => {
1072
- const dataStore = factory();
1073
- // Arrange
1074
- await seedData(dataStore, () => dataStore.products, 200);
1075
-
1076
- const all = await dataStore.products.toArrayAsync();
1077
- const min = await dataStore.products.minAsync(w => w.price);
1078
-
1079
- all.sort((a, b) => a.price - b.price);
1080
-
1081
- expect(min).toBe(all[0].price);
1082
- });
1083
-
1084
- it("where + min", async () => {
1085
- const dataStore = factory();
1086
- // Arrange
1087
- await seedData(dataStore, () => dataStore.products, 200);
1088
-
1089
- const all = await dataStore.products.toArrayAsync();
1090
- const min = await dataStore.products.where(w => w.price > 100).minAsync(w => w.price);
1091
-
1092
- const filtered = all.filter(w => w.price > 100);
1093
- filtered.sort((a, b) => a.price - b.price);
1094
-
1095
- expect(min).toBe(filtered[0].price);
1096
- });
1097
- });
1098
-
1099
- describe('Sum Operations', () => {
1100
- it("sumAsync", async () => {
1101
- const dataStore = factory();
1102
- // Arrange
1103
- await seedData(dataStore, () => dataStore.products, 200);
1104
-
1105
- const all = await dataStore.products.toArrayAsync();
1106
- const sum = await dataStore.products.sumAsync(w => w.price);
1107
-
1108
- expect(all.reduce((a, v) => a + v.price, 0)).toBe(sum);
1109
- });
1110
-
1111
- it("where + sumAsync", async () => {
1112
- const dataStore = factory();
1113
- // Arrange
1114
- await seedData(dataStore, () => dataStore.products, 200);
1115
-
1116
- const all = await dataStore.products.toArrayAsync();
1117
- const count = await dataStore.products.where(w => w.price > 100).sumAsync(w => w.price);
1118
-
1119
- const expectedSum = all.filter(w => w.price > 100).reduce((a, v) => a + v.price, 0);
1120
- expect(expectedSum).toBe(count);
1121
- });
1122
- });
1123
-
1124
- describe('Distinct Operations', () => {
1125
- it("distinctAsync numbers", async () => {
1126
- const dataStore = factory();
1127
- // Arrange
1128
- await seedData(dataStore, () => dataStore.products, 200);
1129
-
1130
- const all = await dataStore.products.toArrayAsync();
1131
- const result = await dataStore.products.map(w => w.price).distinctAsync();
1132
-
1133
- const noDups = [...new Set(all.map(w => w.price))];
1134
- expect(result.length).toBe(noDups.length)
1135
- expect(noDups).toStrictEqual(result);
1136
- });
1137
-
1138
- it("where + distinctAsync numbers", async () => {
1139
- const dataStore = factory();
1140
- // Arrange
1141
- await seedData(dataStore, () => dataStore.products, 200);
1142
-
1143
- const all = await dataStore.products.toArrayAsync();
1144
- const result = await dataStore.products.where(w => w.price > 10).map(w => w.price).distinctAsync();
1145
-
1146
- const noDups = [...new Set(all.filter(w => w.price > 10).map(w => w.price))];
1147
- expect(result.length).toBe(noDups.length)
1148
- expect(noDups).toStrictEqual(result);
1149
- });
1150
-
1151
- it("distinctAsync strings", async () => {
1152
- const dataStore = factory();
1153
- // Arrange
1154
- await seedData(dataStore, () => dataStore.products, 200);
1155
-
1156
- const all = await dataStore.products.toArrayAsync();
1157
- const result = await dataStore.products.map(w => w.name).distinctAsync();
1158
-
1159
- const noDups = [...new Set(all.map(w => w.name))];
1160
- expect(result.length).toBe(noDups.length)
1161
- expect(noDups).toStrictEqual(result);
1162
- });
1163
-
1164
- it("DistinctAsync dates", async () => {
1165
- const dataStore = factory();
1166
- // Arrange
1167
- await seedData(dataStore, () => dataStore.products, 200);
1168
-
1169
- const all = await dataStore.products.toArrayAsync();
1170
- const result = await dataStore.products.map(w => w.createdDate).distinctAsync();
1171
-
1172
- // deserializer still needs to be run on mappings
1173
- const noDups = [...new Set(all.map(w => w.createdDate.toISOString()))].map(w => new Date(w));
1174
- expect(result.length).toBe(noDups.length)
1175
- expect(noDups).toStrictEqual(result);
1176
- });
1177
- });
1178
-
1179
- describe('DataStore Methods', () => {
1180
- it("Should preview changes when no changes exist", async () => {
1181
- const dataStore = factory();
1182
- const changes = await dataStore.previewChangesAsync();
1183
- expect(changes.aggregate.size).toBe(0);
1184
- expect(changes.aggregate.size).toBe(0);
1185
- expect(changes.aggregate.size).toBe(0);
1186
- });
1187
-
1188
- it("Should preview changes when entities are added", async () => {
1189
- const dataStore = factory();
1190
- const generatedData = generateData(dataStore.products.schema, 2);
1191
- await dataStore.products.addAsync(...generatedData);
1192
-
1193
- const changes = await dataStore.previewChangesAsync();
1194
- expect(changes.aggregate.adds).toBe(2);
1195
- expect(changes.aggregate.removes).toBe(0);
1196
- expect(changes.aggregate.updates).toBe(0);
1197
- });
1198
-
1199
- it("Should preview changes when entities are updated", async () => {
1200
- const dataStore = factory();
1201
- const [added] = await dataStore.products.addAsync(...generateData(dataStore.products.schema, 1));
1202
- await dataStore.saveChangesAsync();
1203
-
1204
- added.name = "Updated Name";
1205
- const changes = await dataStore.previewChangesAsync();
1206
- expect(changes.aggregate.adds).toBe(0);
1207
- expect(changes.aggregate.removes).toBe(0);
1208
- expect(changes.aggregate.updates).toBe(1);
1209
- });
1210
-
1211
- it("Should preview changes when entities are removed", async () => {
1212
- const dataStore = factory();
1213
- const [added] = await dataStore.products.addAsync(...generateData(dataStore.products.schema, 1));
1214
- await dataStore.saveChangesAsync();
1215
-
1216
- await dataStore.products.removeAsync(added);
1217
- const changes = await dataStore.previewChangesAsync();
1218
- expect(changes.aggregate.adds).toBe(0);
1219
- expect(changes.aggregate.removes).toBe(1);
1220
- expect(changes.aggregate.updates).toBe(0);
1221
- });
1222
-
1223
- it("Should check hasChanges when no changes exist", async () => {
1224
- const dataStore = factory();
1225
- const hasChanges = await dataStore.hasChangesAsync();
1226
- expect(hasChanges).toBe(false);
1227
- });
1228
-
1229
- it("Should check hasChanges when entities are added", async () => {
1230
- const dataStore = factory();
1231
- await dataStore.products.addAsync(...generateData(dataStore.products.schema, 1));
1232
- const hasChanges = await dataStore.hasChangesAsync();
1233
- expect(hasChanges).toBe(true);
1234
- });
1235
-
1236
- it("Should check hasChanges when entities are updated", async () => {
1237
- const dataStore = factory();
1238
- const [added] = await dataStore.products.addAsync(...generateData(dataStore.products.schema, 1));
1239
- await dataStore.saveChangesAsync();
1240
-
1241
- added.name = "Updated Name";
1242
- const hasChanges = await dataStore.hasChangesAsync();
1243
- expect(hasChanges).toBe(true);
1244
- });
1245
-
1246
- it("Should check hasChanges when entities are removed", async () => {
1247
- const dataStore = factory();
1248
- const [added] = await dataStore.products.addAsync(...generateData(dataStore.products.schema, 1));
1249
- await dataStore.saveChangesAsync();
1250
-
1251
- await dataStore.products.removeAsync(added);
1252
- const hasChanges = await dataStore.hasChangesAsync();
1253
- expect(hasChanges).toBe(true);
1254
- });
1255
- });
1256
-
1257
- describe('RemoveAll Operations', () => {
1258
- it("Should remove all entities", async () => {
1259
- const dataStore = factory();
1260
- await seedData(dataStore, () => dataStore.products, 10);
1261
- const initialCount = await dataStore.products.countAsync();
1262
- expect(initialCount).toBe(10);
1263
-
1264
- await dataStore.products.removeAllAsync();
1265
- const response = await dataStore.saveChangesAsync();
1266
- expect(response.aggregate.removes).toBe(10);
1267
-
1268
- const finalCount = await dataStore.products.countAsync();
1269
- expect(finalCount).toBe(0);
1270
- });
1271
-
1272
- it("Should remove all entities when collection is empty", async () => {
1273
- const dataStore = factory();
1274
- const initialCount = await dataStore.products.countAsync();
1275
- expect(initialCount).toBe(0);
1276
-
1277
- await dataStore.products.removeAllAsync();
1278
- const response = await dataStore.saveChangesAsync();
1279
- expect(response.aggregate.removes).toBe(0);
1280
-
1281
- const finalCount = await dataStore.products.countAsync();
1282
- expect(finalCount).toBe(0);
1283
- });
1284
- });
1285
-
1286
- describe('Tag Operations', () => {
1287
- it("Should tag entities for addition", async () => {
1288
- const dataStore = factory();
1289
- const [item] = generateData(dataStore.products.schema, 1);
1290
- dataStore.products.tag("test-tag").add([item], () => { });
1291
- expect(dataStore.products).toBeDefined();
1292
- });
1293
- });
1294
-
1295
- describe('Instance Operations', () => {
1296
- it("Should create instances and omit id when it is identity", async () => {
1297
- const dataStore = factory();
1298
- const [item] = generateData(dataStore.products.schema, 1);
1299
- const instances = dataStore.products.instance(item);
1300
- expect(instances.length).toBe(1);
1301
- expect((instances[0] as any)._id).toBeUndefined();
1302
- });
1303
-
1304
- it("Should create multiple instances from raw data", async () => {
1305
- const dataStore = factory();
1306
- const items = generateData(dataStore.products.schema, 3);
1307
- const instances = dataStore.products.instance(...items);
1308
- expect(instances.length).toBe(3);
1309
- instances.forEach(instance => {
1310
- expect(instance.category).toStrictEqual(expect.any(String));
1311
- });
1312
- });
1313
- });
1314
-
1315
- describe('Parameterized Queries', () => {
1316
- it("Should filter with parameterized query", async () => {
1317
- const dataStore = factory();
1318
- await seedData(dataStore, () => dataStore.products, 100);
1319
- const minPrice = 100;
1320
- const found = await dataStore.products.where(([w, p]) => w.price > p.minPrice, { minPrice }).toArrayAsync();
1321
- const all = await dataStore.products.toArrayAsync();
1322
- const expected = all.filter(w => w.price > minPrice);
1323
- expect(found.length).toBe(expected.length);
1324
- });
1325
-
1326
- it("Should filter with multiple parameters", async () => {
1327
- const dataStore = factory();
1328
- await seedData(dataStore, () => dataStore.products, 100);
1329
- const params = { minPrice: 50, maxPrice: 200, category: "electronics" };
1330
- const found = await dataStore.products.where(([w, p]) => w.price >= p.minPrice && w.price <= p.maxPrice && w.category === p.category, params).toArrayAsync();
1331
- const all = await dataStore.products.toArrayAsync();
1332
- const expected = all.filter(w => w.price >= params.minPrice && w.price <= params.maxPrice && w.category === params.category);
1333
- expect(found.length).toBe(expected.length);
1334
- });
1335
-
1336
- it("Should use parameterized query with first", async () => {
1337
- const dataStore = factory();
1338
- await seedData(dataStore, () => dataStore.products, 50);
1339
- const minPrice = 100;
1340
- const found = await dataStore.products.where(([w, p]) => w.price > p.minPrice, { minPrice }).firstAsync();
1341
- expect(found.price).toBeGreaterThan(minPrice);
1342
- });
1343
-
1344
- it("Should use parameterized query with firstOrUndefined", async () => {
1345
- const dataStore = factory();
1346
- await seedData(dataStore, () => dataStore.products, 50);
1347
- const minPrice = 1000;
1348
- const found = await dataStore.products.where(([w, p]) => w.price > p.minPrice, { minPrice }).firstOrUndefinedAsync();
1349
- expect(found).toBeUndefined();
1350
- });
1351
-
1352
- it("Should use parameterized query with count", async () => {
1353
- const dataStore = factory();
1354
- await seedData(dataStore, () => dataStore.products, 100);
1355
- const minPrice = 100;
1356
- const count = await dataStore.products.where(([w, p]) => w.price > p.minPrice, { minPrice }).countAsync();
1357
- const all = await dataStore.products.toArrayAsync();
1358
- const expected = all.filter(w => w.price > minPrice).length;
1359
- expect(count).toBe(expected);
1360
- });
1361
-
1362
- it("Should use parameterized query with sum", async () => {
1363
- const dataStore = factory();
1364
- await seedData(dataStore, () => dataStore.products, 100);
1365
- const minPrice = 100;
1366
- const sum = await dataStore.products.where(([w, p]) => w.price > p.minPrice, { minPrice }).sumAsync(w => w.price);
1367
- const all = await dataStore.products.toArrayAsync();
1368
- const expected = all.filter(w => w.price > minPrice).reduce((acc, w) => acc + w.price, 0);
1369
- expect(sum).toBe(expected);
1370
- });
1371
- });
1372
-
1373
- describe('Take and Skip Operations', () => {
1374
- it("Should take specified number of entities", async () => {
1375
- const dataStore = factory();
1376
- await seedData(dataStore, () => dataStore.products, 100);
1377
- const found = await dataStore.products.take(10).toArrayAsync();
1378
- expect(found.length).toBe(10);
1379
- });
1380
-
1381
- it("Should skip specified number of entities", async () => {
1382
- const dataStore = factory();
1383
- await seedData(dataStore, () => dataStore.products, 100);
1384
- const found = await dataStore.products.skip(10).toArrayAsync();
1385
- expect(found.length).toBe(90);
1386
- });
1387
-
1388
- it("Should combine where, skip, and take", async () => {
1389
- const dataStore = factory();
1390
- await seedData(dataStore, () => dataStore.products, 100);
1391
- const found = await dataStore.products.where(w => w.price > 100).skip(5).take(10).toArrayAsync();
1392
- expect(found.length).toBeLessThanOrEqual(10);
1393
- });
1394
-
1395
- it("Should handle take with insufficient data", async () => {
1396
- const dataStore = factory();
1397
- await seedData(dataStore, () => dataStore.products, 5);
1398
- const found = await dataStore.products.take(10).toArrayAsync();
1399
- expect(found.length).toBe(5);
1400
- });
1401
-
1402
- it("Should handle skip with insufficient data", async () => {
1403
- const dataStore = factory();
1404
- await seedData(dataStore, () => dataStore.products, 5);
1405
- const found = await dataStore.products.skip(10).toArrayAsync();
1406
- expect(found.length).toBe(0);
1407
- });
1408
- });
1409
-
1410
- describe('Complex Queries', () => {
1411
- it("Should handle complex query with multiple operations", async () => {
1412
- const dataStore = factory();
1413
- await seedData(dataStore, () => dataStore.products, 1000);
1414
- const found = await dataStore.products
1415
- .where(w => w.price > 50)
1416
- .sort(w => w.price)
1417
- .skip(10)
1418
- .take(20)
1419
- .map(w => ({ id: w._id, price: w.price }))
1420
- .toArrayAsync();
1421
- expect(found.length).toBeLessThanOrEqual(20);
1422
- expect(found[0]).toHaveProperty('id');
1423
- expect(found[0]).toHaveProperty('price');
1424
- });
1425
-
1426
- it("Should handle aggregation query", async () => {
1427
- const dataStore = factory();
1428
- await seedData(dataStore, () => dataStore.products, 1000);
1429
- const totalValue = await dataStore.products
1430
- .where(w => w.inStock)
1431
- .sumAsync(w => w.price);
1432
- expect(totalValue).toBeGreaterThan(0);
1433
- });
1434
-
1435
- it("Should handle nested filtering", async () => {
1436
- const dataStore = factory();
1437
- await seedData(dataStore, () => dataStore.products, 200);
1438
- const found = await dataStore.products
1439
- .where(w => w.price > 100)
1440
- .where(w => w.name.startsWith("s"))
1441
- .where(w => w.category === "electronics")
1442
- .toArrayAsync();
1443
- const all = await dataStore.products.toArrayAsync();
1444
- const expected = all.filter(w => w.price > 100 && w.name.startsWith("s") && w.category === "electronics");
1445
- expect(found.length).toBe(expected.length);
1446
- });
1447
-
1448
- it("Should handle complex sorting and mapping", async () => {
1449
- const dataStore = factory();
1450
- await seedData(dataStore, () => dataStore.products, 200);
1451
- const result = await dataStore.products
1452
- .where(w => w.price > 100)
1453
- .sortDescending(w => w.price)
1454
- .map(w => ({ id: w._id, price: w.price, name: w.name }))
1455
- .take(10)
1456
- .toArrayAsync();
1457
- expect(result.length).toBeLessThanOrEqual(10);
1458
- expect(result[0]).toHaveProperty('id');
1459
- expect(result[0]).toHaveProperty('price');
1460
- expect(result[0]).toHaveProperty('name');
1461
- });
1462
- });
1463
-
1464
- describe('Error Handling', () => {
1465
- it("Should handle first with no matching entities", async () => {
1466
- const dataStore = factory();
1467
- await seedData(dataStore, () => dataStore.products, 10);
1468
- await expect(dataStore.products.where(w => w._id === "non-existent").firstAsync()).rejects.toThrow();
1469
- });
1470
-
1471
- it("Should handle firstOrUndefined with no matching entities", async () => {
1472
- const dataStore = factory();
1473
- await seedData(dataStore, () => dataStore.products, 10);
1474
- const result = await dataStore.products.where(w => w._id === "non-existent").firstOrUndefinedAsync();
1475
- expect(result).toBeUndefined();
1476
- });
1477
-
1478
- it("Should handle min with empty collection", async () => {
1479
- const dataStore = factory();
1480
- await expect(dataStore.products.minAsync(w => w.price)).rejects.toThrow();
1481
- });
1482
-
1483
- it("Should handle max with empty collection", async () => {
1484
- const dataStore = factory();
1485
- await expect(dataStore.products.maxAsync(w => w.price)).rejects.toThrow();
1486
- });
1487
-
1488
- it("Should handle sum with empty collection", async () => {
1489
- const dataStore = factory();
1490
- const result = await dataStore.products.sumAsync(w => w.price);
1491
- expect(result).toBe(0);
1492
- });
1493
-
1494
- it("Should handle count with empty collection", async () => {
1495
- const dataStore = factory();
1496
- const result = await dataStore.products.countAsync();
1497
- expect(result).toBe(0);
1498
- });
1499
- });
1500
-
1501
- describe('Subscription Management', () => {
1502
- it("Should not fire callbacks after unsubscribe", async () => {
1503
- const dataStore = factory();
1504
- const callback = vi.fn();
1505
- await seedData(dataStore, () => dataStore.products);
1506
-
1507
- const unsubscribe = dataStore.products.subscribe().where(w => w._id != "").firstOrUndefined(callback);
1508
-
1509
- await wait(500);
1510
-
1511
- unsubscribe();
1512
-
1513
- await dataStore.products.addAsync(...generateData(dataStore.products.schema, 1));
1514
- await dataStore.saveChangesAsync();
1515
- await wait(500);
1516
-
1517
- expect(callback).toHaveBeenCalledTimes(1);
1518
- });
1519
- });
1520
- });