forked from dexie/Dexie.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests-table.js
1001 lines (900 loc) · 45.1 KB
/
tests-table.js
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Dexie from 'dexie';
import {module, stop, start, asyncTest, equal, ok} from 'QUnit';
import {resetDatabase, supports, spawnedTest, promisedTest} from './dexie-unittest-utils';
var db = new Dexie("TestDBTable");
db.version(1).stores({
users: "++id,first,last,&username,*&email,*pets",
folks: "++,first,last",
items: "id",
schema: "" // Test issue #1039
});
var User = db.users.defineClass({
id: Number,
first: String,
last: String,
username: String,
email: [String],
pets: [String],
});
var idOfFirstUser = 0,
idOfLastUser = 0;
db.on("populate", function (trans) {
db.users.add({first: "David", last: "Fahlander", username: "dfahlander", email: ["[email protected]", "[email protected]"], pets: ["dog"]}).then(function(id) {
idOfFirstUser = id;
});
db.users.add({first: "Karl", last: "Faadersköld", username: "kceder", email: ["[email protected]", "[email protected]"], pets: []}).then(function(id) {
idOfLastUser = id;
});
});
module("table", {
setup: function () {
stop();
resetDatabase(db).catch(function (e) {
ok(false, "Error resetting database: " + e.stack);
}).finally(start);
},
teardown: function () {
}
});
promisedTest("Issue #841 - put() ignores date changes", async ()=> {
const updateAssertions = (mods) => {
equal(mods.first, first2, `first value should be ${first2} but is ${mods.first}`)
equal(!!mods.date, true, "date should be included in modifications");
if (mods.date) {
equal(mods.date.getTime(), date2.getTime(), `date should be ${date2} but is ${mods.date}`)
}
};
db.folks.hook("updating", updateAssertions);
const date1 = new Date("2019-05-03");
const date2 = new Date("2020-01-01");
const first1 = "Foo1";
const first2 = "Foo2";
ok(date1.getTime() !== date2.getTime(), "Just verifying input data so that date1 !== date2");
ok(first1 != first2, "first1 different first2");
const id = await db.folks.add({first: first1, last: "Bar", date: date1});
let obj = await db.folks.get(id);
equal(obj.date.getTime(), date1.getTime(), "Date should first equal date1");
equal(obj.first, first1, `first should be '${first1}'`);
obj.first = first2;
obj.date = date2;
await db.folks.put(obj, id);
obj = await db.folks.get(id);
equal(obj.first, first2, `first should have been successfully updated to '${first2}'`);
equal(obj.date.getTime(), date2.getTime(), "Date should have been successfully updated to be date2");
db.folks.hook("updating").unsubscribe(updateAssertions);
});
promisedTest("Issue #966 - put() with dotted field in update hook", async () => {
const updateAssertions = (mods) => {
equal(mods["nested.field"], "value", "mods.nested.field should contain 'value'");
equal(mods.nested, undefined, "mods.nested field should be empty");
return {...mods};
};
db.folks.hook("updating", updateAssertions);
const id = await db.folks.add({first: "first", last: "last"});
await db.folks.put({first: "first", last: "last", "nested.field": "value"}, id);
let obj = await db.folks.get(id);
equal(obj["nested.field"], "value", "obj.nested.field should have been successfully updated to 'value'");
equal(obj.nested, undefined, "obj.nested field should have remained undefined");
db.folks.hook("updating").unsubscribe(updateAssertions);
});
promisedTest("update array property", async () => {
const id = await db.items.put({id: 1, foo: [{bar: 123}]});
await db.items.update(1, {foo: [{bar: 222}]});
const obj = await db.items.get(1);
equal(JSON.stringify(obj.foo), JSON.stringify([{bar: 222}]), "foo har been updated to the new array");
});
promisedTest("Verify #1130 doesn't break contract of hook('updating')", async ()=>{
const updateHook = (mods) => {
return {"address.postalCode": 111};
};
try {
const id = await db.folks.add({
first: "Foo",
last: "Bar",
address: {
city: "Stockholm",
street: "Folkungagatan"
}
});
db.folks.hook("updating", updateHook);
await db.folks.update(id, {
"address.streetNo": 23
});
let foo = await db.folks.get(id);
equal(foo.address.city, "Stockholm", "Correct city Stockholm");
equal(foo.address.street, "Folkungagatan", "Correct street Folkungagatan");
equal(foo.address.streetNo, 23, "Correct streetNo: 23");
equal(foo.address.postalCode, 111, "Hooks should have added postal code");
} finally {
db.folks.hook("updating").unsubscribe(updateHook);
}
});
asyncTest("get", 4, function () {
db.table("users").get(idOfFirstUser).then(function(obj) {
equal(obj.first, "David", "Got the first object");
return db.users.get(idOfLastUser);
}).then(function(obj) {
equal(obj.first, "Karl", "Got the second object");
return db.users.get("nonexisting key");
}).then(function(obj) {
ok(true, "Got then() even when getting non-existing object");
equal(obj, undefined, "Result is 'undefined' when not existing");
}).catch(function(err) {
ok(false, "Error: " + err);
}).finally(start);
});
asyncTest("where", function () {
db.transaction("r", db.users, function () {
db.users.where("username").equals("kceder").first(function (user) {
equal(user.first, "Karl", "where().equals()");
}),
db.users.where("id").above(idOfFirstUser).toArray(function (a) {
ok(a.length == 1, "where().above()");
}),
db.users.where("id").aboveOrEqual(idOfFirstUser).toArray(function (a) {
ok(a.length == 2, "where().aboveOrEqual()");
}),
db.users.where("id").below(idOfLastUser).count(function (count) {
ok(count == 1, "where().below().count()");
}),
db.users.where("id").below(idOfFirstUser).count(function (count) {
ok(count == 0, "where().below().count() should be zero");
}),
db.users.where("id").belowOrEqual(idOfFirstUser).count(function (count) {
ok(count == 1, "where().belowOrEqual()");
}),
db.users.where("id").between(idOfFirstUser, idOfFirstUser).count(function (count) {
ok(count == 0, "where().between(1, 1)");
}),
db.users.where("id").between(0, Infinity).count(function (count) {
ok(count == 2, "where().between(0, Infinity)");
}),
db.users.where("id").between(idOfFirstUser, idOfFirstUser, true, true).count(function (count) {
ok(count == 1, "where().between(1, 1, true, true)");
}),
db.users.where("id").between(1, -1, true, true).count(function (count) {
ok(count == 0, "where().between(1, -1, true, true)");
}),
db.users.where("id").between(idOfFirstUser, idOfLastUser).count(function (count) {
ok(count == 1, "where().between(1, 2)");
}),
db.users.where("id").between(idOfFirstUser, idOfLastUser, true, true).count(function (count) {
ok(count == 2, "where().between(1, 2, true, true)");
}),
db.users.where("id").between(idOfFirstUser, idOfLastUser, false, false).count(function (count) {
ok(count == 0, "where().between(1, 2, false, false)");
});
db.users.where("last").startsWith("Fah").toArray(function (a) {
equal(a.length, 1, "where().startsWith(existing) only matches Fahlander, not Faadersköld");
equal(a[0].first, "David");
});
db.users.where("last").startsWith("Faa").toArray(function (a) {
equal(a.length, 1, "where().startsWith(existing) only matches Faadersköld, not Fahlander");
equal(a[0].first, "Karl");
});
db.users.where("last").startsWith("Fa").toArray(function (a) {
equal(a.length, 2, "length = 2 on: where().startsWith(2 existing)");
equal(a[0].first, "Karl", "Karl found first on last 'Faadersköld'");
equal(a[1].first, "David", "David found second on last 'Fahlander'");
});
db.users.where("last").anyOf("Fahlander", "Faadersköld").toArray(function (a) {
equal(a.length, 2, "in() returned expected number of items");
equal(a[0].last, "Faadersköld", "Faadersköld is first");
});
db.users.where("last").anyOf("Fahlander", "Faadersköld").reverse().toArray(function (a) {
equal(a.length, 2, "in().reverse() returned expected number of items");
equal(a[0].last, "Fahlander", "Fahlander is first");
});
db.users.where("last").anyOf("Faadersköld").toArray(function (a) {
equal(a.length, 1, "in() returned expected number of items");
});
if (supports("multiEntry")) {
db.users.where("email").equals("[email protected]").toArray(function (a) { // Fails in IE with 0 due to that IE is not implementing to index string arrays.
equal(a.length, 1, "Finding items from array members. Expect to fail on IE10/IE11.");
});
db.users.where("email").startsWith("da").distinct().toArray(function (a) { // Fails on IE with 0
equal(a.length, 2, "Found both because both have emails starting with 'da'. Expect to fail on IE10/IE11.");
});
} else {
ok(true, "SKIPPED - MULTIENTRY UNSUPPORTED");
ok(true, "SKIPPED - MULTIENTRY UNSUPPORTED");
}
}).catch(function (e) {
ok(false, "Transaction failed: " + e);
}).finally(function () {
start();
});
});
asyncTest("count", function () {
db.users.count(function (count) {
equal(count, 2, "Table.count()");
}).catch(function (e) {
ok(false, e.message);
}).finally(function(){
start();
});;
});
asyncTest("count with limit", function () {
db.users.limit(1).count(function (count) {
equal(count, 1, "Table.limit().count()");
}).catch(function (e) {
ok(false, e);
}).finally(start);
});
asyncTest("limit(),orderBy(),modify(), abort(), reverse()", function () {
db.transaction("rw", db.users, function () {
// Modify first found user with a helloMessage
db.users.orderBy("first").reverse().limit(1).modify(function (user) {
user.helloMessage = "Hello " + user.first;
});
// Check that the modification went fine:
db.users.orderBy("first").reverse().toArray(function (a) {
equal(a[0].first, "Karl", "First item is Karl");
equal(a[0].helloMessage, "Hello Karl", "Karl got helloMessage 'Hello Karl'");
equal(a[1].first, "David", "Second item is David");
ok(!a[1].helloMessage, "David was not modified due to limit()");
});
}).catch(function (e) {
ok(false, "Error: " + e);
}).finally(function () {
start();
});
});
asyncTest("filter", function () {
db.users.filter(function (user) { return user.email.indexOf("[email protected]") != -1 }).toArray(function (davids) {
equal(1, davids.length, "Got one David");
equal("David", davids[0].first, "The name of the David is David");
}).catch(function (e) {
ok(false, e.stack || e);
}).finally(start);
});
asyncTest("each", function () {
var users = [];
db.users.each(function (user) {
users.push(user);
}).then(function () {
equal(users.length, 2, "Got 2 users");
equal(users[0].first, "David", "Got David");
equal(users[1].first, "Karl", "Got Karl");
}).catch(function (e) {
ok(false, e);
}).finally(start);
});
asyncTest("put", function () {
db.transaction("rw", db.users, function () {
var newUser = { first: "Åke", last: "Persbrant", username: "aper", email: ["[email protected]"] };
db.users.put(newUser).then(function (id) {
ok(id > idOfLastUser, "Got id " + id + " because we didnt supply an id");
equal(newUser.id, id, "The id property of the new user was set");
});
db.users.where("username").equals("aper").first(function (user) {
equal(user.last, "Persbrant", "The correct item was actually added");
user.last = "ChangedLastName";
var currentId = user.id;
db.users.put(user).then(function (id) {
equal(id, currentId, "Still got same id because we update same object");
});
db.users.where("last").equals("ChangedLastName").first(function (user) {
equal(user.last, "ChangedLastName", "LastName was successfully changed");
});
});
}).catch(function (e) {
ok(false, e);
}).finally(start);
});
asyncTest("put-no-transaction", function () {
var newUser = { first: "Åke", last: "Persbrant", username: "aper", email: ["[email protected]"] };
db.users.put(newUser).then(function(id) {
ok(id > idOfLastUser, "Got id " + id + " because we didnt supply an id");
equal(newUser.id, id, "The id property of the new user was set");
return db.users.where("username").equals("aper").first(function(user) {
equal(user.last, "Persbrant", "The correct item was actually added");
user.last = "ChangedLastName";
var userId = user.id;
return db.users.put(user).then(function(id) {
equal(id, userId, "Still got same id because we update same object");
return db.users.where("last").equals("ChangedLastName").first(function(user) {
equal(user.last, "ChangedLastName", "LastName was successfully changed");
});
});
});
}).catch(function(e) {
ok(false, e);
}).finally(start);
});
asyncTest("add", function () {
db.transaction("rw", db.users, function () {
var newUser = { first: "Åke", last: "Persbrant", username: "aper", email: ["[email protected]"] };
db.users.add(newUser).then(function (id) {
ok(id > idOfLastUser, "Got id " + id + " because we didnt supply an id");
equal(newUser.id, id, "The id property of the new user was set");
});
db.users.where("username").equals("aper").first(function (user) {
equal(user.last, "Persbrant", "The correct item was actually added");
});
}).catch(function (e) {
ok(false, "Error: " + e);
}).finally(start);
});
spawnedTest("bulkAdd", function*(){
var highestKey = yield db.users.add({username: "fsdkljfd", email: ["fjkljslk"]});
ok(true, "Highest key was: " + highestKey);
// Delete test item.
yield db.users.delete(highestKey);
ok(true, "Deleted test item");
var result = yield db.users.bulkAdd([
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }
]);
equal (result, highestKey + 2, "Result of bulkAdd() operation was equal to highestKey + 2");
});
spawnedTest("bulkAdd-all-results", function* () {
var dbBulkAddAll = new Dexie("TestDBTableBulkAddAllResults");
dbBulkAddAll.version(1).stores({
dudes: "++,first,last"
});
var highestKey = yield dbBulkAddAll.dudes.add({ username: "fsdkljfd", email: "fjkljslk", find: "bulkAddAll" });
// should be able to get all keys with options object as second argument
var allKeys = yield dbBulkAddAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1", find: "bulkAddAll" },
{ first: "Åke2", last: "Persbrant2", find: "bulkAddAll" }
], { allKeys: true });
var expectedKeys = [highestKey + 1, highestKey + 2];
deepEqual(allKeys, expectedKeys,
"Result of bulkAdd(objects, { allKeys: true }) operation was equal to [highestKey + 1, highestKey + 2]");
// should be able to get all keys with options object as third argument
highestKey = yield dbBulkAddAll.dudes.add({ username: "fsdkljfd", email: "fjkljslk", find: "bulkAddAll" });
var allKeys2 = yield dbBulkAddAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1", find: "bulkAddAll" },
{ first: "Åke2", last: "Persbrant2", find: "bulkAddAll" }
], undefined, { allKeys: true });
var expectedKeys2 = [highestKey + 1, highestKey + 2];
deepEqual(allKeys2, expectedKeys2,
"Result of bulkAdd(objects, undefined, { allKeys: true }) operation was equal to [highestKey + 1, highestKey + 2]");
// should be able to get all keys with options object as third argument with some keys
var allKeys3 = yield dbBulkAddAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], ['sd5fs2df', 'dasfsd3fs7df'], { allKeys: true });
deepEqual(allKeys3, ['sd5fs2df', 'dasfsd3fs7df'],
"Result of bulkAdd(objects, ['sd5fs2df', 'dasfsd3fs7df'], { allKeys: true }) operation was equal to ['sd5fs2df', 'dasfsd3fs7df']");
// should return last key with 1 argument and options: { allKeys: false }
highestKey = yield dbBulkAddAll.dudes.add({ username: "fsdkljfd", email: "fjkljslk", find: "bulkAddAll" });
var lastKey = yield dbBulkAddAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], { allKeys: false });
equal(lastKey, highestKey + 2,
"Result of bulkAdd(objects, { allKeys: false }) operation was equal to highestKey + 2");
// should return last key with 2 arguments and options: { allKeys: false }
var lastKey = yield dbBulkAddAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], ['cv4btr45fbrt', 'b33vn3fytn'], { allKeys: false });
equal(lastKey, 'b33vn3fytn',
"Result of bulkAdd(objects, ['cv4btr45fbrt', 'b33vn3fytn'], { allKeys: false }) operation was equal to 'b33vn3fytn'");
// should return last key with 2 arguments and no options object
var lastKey = yield dbBulkAddAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], ['dfgd2vdfh4d', 'ty1jxdbd9']);
equal(lastKey, 'ty1jxdbd9',
"Result of bulkAdd(objects, ['dfgd2vdfh4d', 'ty1jxdbd9']) operation was equal to 'ty1jxdbd9'");
yield dbBulkAddAll.delete();
});
spawnedTest("bulkAdd-catching errors", function*() {
yield db.transaction("rw", db.users, function() {
var newUsers = [
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }, // Should fail
{ first: "Åke3", last: "Persbrant3", username: "aper3", email: ["[email protected]"] }
];
db.users.bulkAdd(newUsers).then(()=> {
ok(false, "Should not resolve when one operation failed");
}).catch(Dexie.BulkError, e=>{
ok(true, "Got BulkError: " + e.message);
equal(e.failures.length, 1, "One error due to a duplicate username: " + e.failures[0]);
});
// Now, since we catched the error, the transaction should continue living.
db.users.where("username").startsWith("aper").count(function(count) {
equal(count, 3, "Got three matches now when users are bulk-added");
});
});
equal(yield db.users.where("username").startsWith('aper').count(), 3, "Previous transaction committed");
var newUsersX = [
{first: "Xke1", last: "Persbrant1", username: "xper1", email: ["[email protected]"]},
{first: "Xke2", last: "Persbrant2", username: "xper2", email: ["[email protected]"]},
{first: "Xke2", last: "Persbrant2", username: "xper2", email: ["[email protected]"]}, // Should fail
{first: "Xke3", last: "Persbrant3", username: "xper3", email: ["[email protected]"]}
];
try {
yield db.transaction("rw", db.users, () => {
db.users.bulkAdd(newUsersX).then(()=> {
ok(false, "Should not resolve");
});
});
ok(false, "Should not come here");
} catch (e) {
ok(true, "Got: " + e);
}
equal(yield db.users.where('username').startsWith('xper').count(), 0, "0 users! Good, means that previous transaction did not commit");
yield db.users.bulkAdd(newUsersX).catch(e => {
ok(true, "Got error. Catching it should make the successors work.")
});
equal(yield db.users.where('username').startsWith('xper').count(), 3, "3 users! Good - means that previous operation catched and therefore committed");
var newUsersY = [
{first: "Yke1", last: "Persbrant1", username: "yper1", email: ["[email protected]"]},
{first: "Yke2", last: "Persbrant2", username: "yper2", email: ["[email protected]"]},
{first: "Yke2", last: "Persbrant2", username: "yper2", email: ["[email protected]"]}, // Should fail
{first: "Yke3", last: "Persbrant3", username: "yper3", email: ["[email protected]"]}
];
// Now check that catching the operation via try..catch should also make it succeed.
try {
yield db.users.bulkAdd(newUsersY);
} catch (e) {
ok(true, "Got: " + e);
}
equal(yield db.users.where('username').startsWith('yper').count(), 3, "3 users! Good - means that previous operation catched (via try..yield..catch this time, and therefore committed");
// Now check that catching and rethrowing should indeed make it fail
var newUsersZ = [
{first: "Zke1", last: "Persbrant1", username: "zper1", email: ["[email protected]"]},
{first: "Zke2", last: "Persbrant2", username: "zper2", email: ["[email protected]"]},
{first: "Zke2", last: "Persbrant2", username: "zper2", email: ["[email protected]"]}, // Should fail
{first: "Zke3", last: "Persbrant3", username: "zper3", email: ["[email protected]"]}
];
yield db.transaction('rw', db.users, function*() {
try {
yield db.users.bulkAdd(newUsersZ);
} catch (e) {
throw e;
}
}).catch(Dexie.BulkError, e => {
ok(true, "Got rethrown BulkError: " + e.stack);
});
equal(yield db.users.where('username').startsWith('zper').count(), 0, "0 users! Good - means that previous operation rethrown (via try..yield..catch--throw this time, and therefore not committed");
});
spawnedTest("bulkAdd-non-inbound-autoincrement", function*(){
yield db.folks.bulkAdd([
{ first: "Foo", last: "Bar"},
{ first: "Foo", last: "Bar2"},
{ first: "Foo", last: "Bar3"},
{ first: "Foo", last: "Bar4"}
]);
equal (yield db.folks.where('first').equals('Foo').count(), 4, "Should be 4 Foos");
equal (yield db.folks.where('last').equals('Bar').count(), 1, "Shoudl be 1 Bar");
});
spawnedTest("bulkAdd-catch sub transaction", function*(){
yield db.transaction('rw', db.users, ()=>{
var newUsers = [
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }, // Should fail
{ first: "Åke3", last: "Persbrant3", username: "aper3", email: ["[email protected]"] }
];
db.transaction('rw', db.users, ()=>{
db.users.bulkAdd(newUsers);
}).then(()=>{
ok(false, "Should not succeed with all these operations");
}).catch(e => {
equal(e.failures.length, 1, "Should get one failure");
});
}).catch(e => {
ok(true, "Outer transaction aborted due to inner transaction abort. This is ok: " + e);
});
equal(yield db.users.where('username').startsWith('aper').count(), 0, "0 users! Good, means that inner transaction did not commit");
});
spawnedTest("Issue #1280 - add() with auto-incrementing ID and CryptoKey", function* () {
if (!self?.crypto?.subtle) {
ok(true, "This browser doesnt have WebCrypto");
return;
}
var generatedKey = yield self.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 1024,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["encrypt", "decrypt"],
);
yield Dexie.delete("MyDatabaseToStoreCryptoKeys");
var db = new Dexie("MyDatabaseToStoreCryptoKeys");
db.version(1).stores({
keys: "++id",
});
var objToAdd = { key: generatedKey.privateKey };
ok(generatedKey.privateKey instanceof CryptoKey, "The CryptoKey object was generated correctly");
var id = yield db.keys.add(objToAdd);
ok(id != null, "The id we got was not nullish");
var storedObj = yield db.keys.get(id);
ok(storedObj.key instanceof CryptoKey, "The CryptoKey object exists in storage");
// Verify that update works
yield db.keys.update(id, {someOtherProp: 'x'});
storedObj = yield db.keys.get(id);
ok(storedObj.key instanceof CryptoKey, "The CryptoKey object is still a CryptoKey");
});
spawnedTest("bulkPut", function*(){
var highestKey = yield db.users.add({username: "fsdkljfd", email: ["fjkljslk"]});
ok(true, "Highest key was: " + highestKey);
// Delete test item.
yield db.users.delete(highestKey);
ok(true, "Deleted test item");
let existingFirstUserToReplace = yield db.users.get(idOfFirstUser);
equal (existingFirstUserToReplace.username, "dfahlander", "Existing user should be dfahlander");
var result = yield db.users.bulkPut([
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["[email protected]"] },
{ id: idOfFirstUser, first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] },
{ first: "Åke3", last: "Persbrant3", username: "aper3", email: ["[email protected]"] }
]);
equal (result, highestKey + 2, "Result of bulkPut() operation was equal to highestKey + 2");
let ourAddedUsers = yield db.users.where('username').startsWith("aper").toArray();
equal(ourAddedUsers.length, 3, "Should have put 3 users there (two additions and one replaced");
let replacedDfahlander = yield db.users.get(idOfFirstUser);
equal(replacedDfahlander.username, "aper2", "dfahlander Should now be aper2 instead");
});
spawnedTest("bulkPut-all-results", function* () {
var dbBulkPutAll = new Dexie("TestDBTableBulkPutAllResults");
dbBulkPutAll.version(1).stores({
users: "++id,first,last,find",
mates: "++id,first,last,find",
dudes: "++,first,last,find",
});
var highestKey = yield dbBulkPutAll.users.add({ first: "fsdkljfd", last: "fjkljslk", find: "bulkPutAll" });
// should be able to get all keys with options object as second argument (users)
var allKeys = yield dbBulkPutAll.users.bulkPut([
{ first: "Åke1", last: "Persbrant1", find: "bulkPutAll" },
{ id: highestKey, first: "Åke2", last: "Persbrant2", find: "bulkPutAll" },
{ first: "Åke3", last: "Persbrant3", find: "bulkPutAll" }
], { allKeys: true });
deepEqual(allKeys, [highestKey + 1, highestKey, highestKey + 2],
"Result of bulkAdd(objects, { allKeys: true }) operation was equal to [highestKey + 1, highestKey, highestKey + 2]");
let ourAddedUsers = yield dbBulkPutAll.users.where('find').startsWith("bulkPutAll").toArray();
equal(ourAddedUsers.length, 3, "Should have put 3 users there (two additions and one replaced");
let replacedRecord = yield dbBulkPutAll.users.get(highestKey);
equal(replacedRecord.last, "Persbrant2", "fjkljslk should now be Persbrant2 instead");
// should be able to get all keys with options object as third argument (mates)
highestKey = yield dbBulkPutAll.mates.add({ first: "fsdkljfd", last: "fjkljslk", find: "bulkPutAll" });
var allKeys2 = yield dbBulkPutAll.mates.bulkPut([
{ first: "Åke1", last: "Persbrant1", find: "bulkPutAll" },
{ id: highestKey, first: "Åke2", last: "Persbrant2", find: "bulkPutAll" },
{ first: "Åke3", last: "Persbrant3", find: "bulkPutAll" }
], undefined, { allKeys: true });
deepEqual(allKeys2, [highestKey + 1, highestKey, highestKey + 2],
"Result of bulkPut(objects, undefined, { allKeys: true }) operation was equal to [highestKey + 1, highestKey, highestKey + 2]");
let ourAddedUsers2 = yield dbBulkPutAll.mates.where('find').startsWith("bulkPutAll").toArray();
equal(ourAddedUsers2.length, 3, "Should have put 3 users there (two additions and one replaced");
let replacedRecord2 = yield dbBulkPutAll.mates.get(highestKey);
equal(replacedRecord2.last, "Persbrant2", "fjkljslk should now be Persbrant2 instead");
// should be able to get all keys with options object as third argument with keys array (dudes)
highestKey = yield dbBulkPutAll.dudes.add({ first: "fsdkljfd", last: "fjkljslk", find: "bulkPutAll" });
var allKeys3 = yield dbBulkPutAll.dudes.bulkPut([
{ first: "Åke1", last: "Persbrant1", find: "bulkPutAll" },
{ id: highestKey, first: "Åke2", last: "Persbrant2", find: "bulkPutAll" },
{ first: "Åke3", last: "Persbrant3", find: "bulkPutAll" }
], ['sd5fs2df', highestKey, 'dasfsd3fs7df'], { allKeys: true });
deepEqual(allKeys3, ['sd5fs2df', highestKey, 'dasfsd3fs7df'],
"Result of bulkPut(objects, ['sd5fs2df', highestKey, 'dasfsd3fs7df'], { allKeys: true }) operation was equal to ['sd5fs2df', highestKey, 'dasfsd3fs7df']");
let ourAddedUsers3 = yield dbBulkPutAll.dudes.where('find').startsWith("bulkPutAll").toArray();
equal(ourAddedUsers3.length, 3, "Should have put 3 users there (two additions and one replaced");
let replacedRecord3 = yield dbBulkPutAll.dudes.get(highestKey);
equal(replacedRecord3.last, "Persbrant2", "fjkljslk should now be Persbrant2 instead");
// should return last key with 1 argument and options: { allKeys: false }
highestKey = yield dbBulkPutAll.dudes.add({ username: "fsdkljfd", email: "fjkljslk", find: "bulkAddAll" });
var lastKey = yield dbBulkPutAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], { allKeys: false });
equal(lastKey, highestKey + 2,
"Result of bulkAdd(objects, { allKeys: false }) operation was equal to highestKey + 2");
// should return last key with 2 arguments and options: { allKeys: false }
var lastKey = yield dbBulkPutAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], ['cv4btr45fbrt', 'b33vn3fytn'], { allKeys: false });
equal(lastKey, 'b33vn3fytn',
"Result of bulkAdd(objects, ['cv4btr45fbrt', 'b33vn3fytn'], { allKeys: false }) operation was equal to 'b33vn3fytn'");
// should return last key with 2 arguments and no options object
var lastKey = yield dbBulkPutAll.dudes.bulkAdd([
{ first: "Åke1", last: "Persbrant1" },
{ first: "Åke2", last: "Persbrant2" }
], ['dfgd2vdfh4d', 'ty1jxdbd9']);
equal(lastKey, 'ty1jxdbd9',
"Result of bulkAdd(objects, ['dfgd2vdfh4d', 'ty1jxdbd9']) operation was equal to 'ty1jxdbd9'");
yield dbBulkPutAll.delete();
});
spawnedTest("bulkPut with overlapping objects", function*(){
yield db.users.bulkPut([{
id: "sdjls83",
first: "Daveious"
},{
id: "sdjls83",
last: "Olvono"
}]);
let theOne = yield db.users.get("sdjls83");
equal (theOne.last, "Olvono", "Last item is the one inserted");
ok (theOne.first === undefined, "Object doesnt have a first property");
});
spawnedTest("bulkPut-catching errors", function*() {
yield db.transaction("rw", db.users, function() {
var newUsers = [
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["[email protected]"] },
{ id: idOfLastUser, first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }, // update success
{ id: idOfFirstUser, first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }, // update should fail
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }, // Add should fail
{ first: "Åke3", last: "Persbrant3", username: "aper3", email: ["[email protected]"] }
];
db.users.bulkPut(newUsers).then(()=> {
ok(false, "Should not resolve when one operation failed");
}).catch(Dexie.BulkError, e=>{
ok(true, "Got BulkError: " + e.message);
equal(e.failures.length, 2, "Two errors due to a duplicate username: " + e.failures[0]);
});
// Now, since we catched the error, the transaction should continue living.
db.users.where("username").startsWith("aper").count(function(count) {
equal(count, 3, "Got three matches now when users are bulk-putted");
});
});
equal(yield db.users.where("username").startsWith('aper').count(), 3, "Previous transaction committed");
var newUsersX = [
{first: "Xke1", last: "Persbrant1", username: "xper1", email: ["[email protected]"]},
{id: idOfLastUser, first: "Xke2", last: "Persbrant2", username: "xper2", email: ["[email protected]"]},
{first: "Xke2", last: "Persbrant2", username: "xper2", email: ["[email protected]"]}, // Should fail (add)
{id: idOfFirstUser, first: "Xke2", last: "Persbrant2", username: "xper2", email: ["[email protected]"]}, // Should fail (update)
{first: "Xke3", last: "Persbrant3", username: "xper3", email: ["[email protected]"]}
];
try {
yield db.transaction("rw", db.users, () => {
db.users.bulkPut(newUsersX).then(()=> {
ok(false, "Should not resolve");
});
});
ok(false, "Should not come here");
} catch (e) {
ok(true, "Got: " + e);
}
equal(yield db.users.where('username').startsWith('xper').count(), 0, "0 users! Good, means that previous transaction did not commit");
yield db.users.bulkPut(newUsersX).catch(e => {
ok(true, "Got error. Catching it should make the successors work.")
});
equal(yield db.users.where('username').startsWith('xper').count(), 3,
"Should count to 3 users because previous operation was catched and therefore should have been committed");
var newUsersY = [
{first: "Yke1", last: "Persbrant1", username: "yper1", email: ["[email protected]"]},
{first: "Yke2", last: "Persbrant2", username: "yper2", email: ["[email protected]"]},
{id: idOfFirstUser, first: "Yke2", last: "Persbrant2", username: "yper2", email: ["[email protected]"]}, // Should fail
{first: "Yke2", last: "Persbrant2", username: "yper2", email: ["[email protected]"]}, // Should fail
{first: "Yke3", last: "Persbrant3", username: "yper3", email: ["[email protected]"]}
];
// Now check that catching the operation via try..catch should also make it succeed.
try {
yield db.users.bulkPut(newUsersY);
} catch (e) {
ok(true, "Got: " + e);
}
equal(yield db.users.where('username').startsWith('yper').count(), 3,
"Should count to 3 users because previous previous operation catched (via try..yield..catch this time, and therefore should have been committed");
// Now check that catching and rethrowing should indeed make it fail
var newUsersZ = [
{first: "Zke1", last: "Persbrant1", username: "zper1", email: ["[email protected]"]},
{first: "Zke2", last: "Persbrant2", username: "zper2", email: ["[email protected]"]},
{first: "Zke2", last: "Persbrant2", username: "zper2", email: ["[email protected]"]}, // Should fail
{id: idOfLastUser, first: "Zke2", last: "Persbrant2", username: "zper2", email: ["[email protected]"]}, // Should fail
{first: "Zke3", last: "Persbrant3", username: "zper3", email: ["[email protected]"]}
];
yield db.transaction('rw', db.users, function*() {
try {
yield db.users.bulkPut(newUsersZ);
} catch (e) {
throw e;
}
}).catch(Dexie.BulkError, e => {
ok(true, "Got rethrown BulkError: " + e.stack);
});
equal(yield db.users.where('username').startsWith('zper').count(), 0, "0 users! Good - means that previous operation rethrown (via try..yield..catch--throw this time, and therefore not committed");
});
spawnedTest("bulkPut-non-inbound-autoincrement", function*(){
yield db.folks.bulkPut([
{ first: "Foo", last: "Bar"},
{ first: "Foo", last: "Bar2"},
{ first: "Foo", last: "Bar3"},
{ first: "Foo", last: "Bar4"}
]);
equal (yield db.folks.where('first').equals('Foo').count(), 4, "Should be 4 Foos");
equal (yield db.folks.where('last').equals('Bar').count(), 1, "Should be 1 Bar");
});
spawnedTest("bulkPut - mixed inbound autoIncrement", function* () {
let lastId = yield db.users.bulkPut([
{ first: "Foo", last: "Bar"},
{ first: "Foo", last: "Bar2"},
{ first: "Foo", last: "Bar3"},
{ first: "Foo", last: "Bar4"}
]);
equal (yield db.users.where('first').equals('Foo').count(), 4, "Should be 4 Foos");
equal (yield db.users.where('last').equals('Bar').count(), 1, "Should be 1 Bar");
let newLastId = yield db.users.bulkPut([
{ id: lastId - 3, first: "Foo2", last: "BarA"}, // Will update "Foo Bar" to "Foo2 BarA"
{ first: "Foo2", last: "BarB"}, // Will create
{ id: lastId - 1, first: "Foo2", last: "BarC"}, // Will update "Foo Bar3" to "Foo2 BarC"
{ first: "Foo2", last: "BarD"} // Will create
]);
equal (newLastId, lastId + 2, "Should have incremented last ID twice now");
equal (yield db.users.where('first').equals('Foo').count(), 2, "Should be 2 Foos now");
equal (yield db.users.where('first').equals('Foo2').count(), 4, "Should be 4 Foo2s now");
let foo2s = yield db.users.where('first').equals('Foo2').toArray();
equal (foo2s[0].last, "BarA", "BarA should be first (updated previous ID)");
equal (foo2s[1].last, "BarC", "BarC should be second (updated previous ID");
equal (foo2s[2].last, "BarB", "BarB should be third (got new key)");
equal (foo2s[3].last, "BarD", "BarD should be forth (got new key)");
});
spawnedTest("bulkPut-catch sub transaction", function*(){
yield db.transaction('rw', db.users, ()=>{
var newUsers = [
{ first: "Åke1", last: "Persbrant1", username: "aper1", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] },
{ first: "Åke2", last: "Persbrant2", username: "aper2", email: ["[email protected]"] }, // Should fail
{ first: "Åke3", last: "Persbrant3", username: "aper3", email: ["[email protected]"] }
];
db.transaction('rw', db.users, ()=>{
db.users.bulkPut(newUsers);
}).then(()=>{
ok(false, "Should not succeed with all these operations");
}).catch(e => {
equal(e.failures.length, 1, "Should get one failure");
});
}).catch(e => {
ok(true, "Outer transaction aborted due to inner transaction abort. This is ok: " + e);
});
equal(yield db.users.where('username').startsWith('aper').count(), 0, "0 users! Good, means that inner transaction did not commit");
});
spawnedTest("bulkDelete", function*(){
let userKeys = yield db.users.orderBy('id').keys();
ok(userKeys.length > 0, "User keys found: " + userKeys.join(','));
yield db.users.bulkDelete(userKeys);
let userCount = yield db.users.count();
equal (userCount, 0, "Should be no users there now");
});
spawnedTest("bulkDelete - nonexisting keys", function*(){
let userKeys = ["nonexisting1", "nonexisting2", yield db.users.orderBy(':id').lastKey()];
yield db.users.bulkDelete(userKeys);
let userCount = yield db.users.count();
equal (userCount, 1, "Should be one user there now. (the other should have been deleted)");
});
spawnedTest("bulkDelete-faulty-key", function*(){
let userKeys = [{faulty: "ohyes"}];
yield db.users.bulkDelete(userKeys).then (()=>{
ok (false, "Should not succeed");
}).catch('DataError', e => {
ok (true, "Should get error: " + e);
});
});
asyncTest("delete", function () {
// Without transaction
db.users.get(idOfFirstUser, function (user) {
notEqual(user, null, "User with id 1 exists");
}).then(function () {
db.users.delete(1).then(function () {
db.users.get(1, function (user) {
equal(user, null, "User not found anymore");
start();
});
});
}).catch(function (e) {
ok(false, e);
start();
});
});
asyncTest("delete(using transaction)", function() {
// With transaction
db.transaction("rw", db.users, function () {
db.users.get(idOfFirstUser, function (user) {
notEqual(user, null, "User with id 1 exists");
});
db.users.delete(idOfFirstUser);
db.users.get(idOfFirstUser, function (user) {
equal(user, null, "User not found anymore");
});
}).catch(function (e) {
ok(false, e);
}).finally(start);
});
asyncTest("delete nonexisting item", 3, function () {
var numUsers;
db.users.count().then(function(count) {
numUsers = count;
ok(true, "Number of users before delete: " + count);
}).then(function() {
return db.users.delete("nonexisting key");
}).then(function(){
ok(true, "Success even though nothing was deleted");
}).then(function(){
return db.users.count();
}).then(function(count){
equal(numUsers, count, "Just verifying number of items in user table is still same");
}).catch(function (err) {
ok(false, "Got error: " + err);
}).finally (start);
});
asyncTest("clear", function () {
db.transaction("rw", "users", function () {
db.users.count(function (count) {
equal(count, 2, "There are 2 items in database before clearing it");
});
db.users.clear();
db.users.count(function (count) {
equal(count, 0, "There are 0 items in database after it has been cleared");
});
}).catch(function (e) {
ok(false, e);
}).finally(start);
});
spawnedTest("failReadonly", function*(){
yield db.transaction('r', 'users', function*() {
yield db.users.bulkAdd([{first: "Foo", last: "Bar"}]);
}).then(()=>{
ok(false, "Should not happen");
}).catch ('ReadOnlyError', e => {
ok(true, "Got ReadOnlyError: " + e.stack);
});
});
spawnedTest("failNotIncludedStore", function*(){
yield db.transaction('rw', 'folks', function*() {
yield db.users.bulkAdd([{first: "Foo", last: "Bar"}]);
}).then(()=>{
ok(false, "Should not happen");
}).catch ('NotFoundError', e => {
ok(true, "Got NotFoundError: " + e.stack);
});
});
asyncTest("failNotIncludedStoreTrans", () => {
db.transaction('rw', 'foodassaddas', ()=>{
}).then(()=>{
ok(false, "Should not happen");
}).catch ('NotFoundError', e => {
ok(true, "Got NotFoundError: " + e.stack);
}).catch (e => {
ok(false, "Oops: " + e.stack);
}).then(start);
});
// Must use this rather than QUnit's deepEqual() because that one fails on Safari when run via karma-browserstack-launcher
export function deepEqual(a, b, description) {
equal(JSON.stringify(a, null, 2), JSON.stringify(b, null, 2), description);
}
promisedTest("bulkGet()", async () => {
const bulkData = [];
for (let i=0; i<400; ++i) {
bulkData.push({id: i, first: "Foo"+i, last: "Bar" + i});
}
ok(`Putting ${bulkData.length} users into the table`);
await db.users.bulkPut(bulkData);
ok(`Done putting users. Now getting them using bulkGet()`);
const keys = bulkData.map(({id}) => id);
const retrieved = await db.users.bulkGet(keys);
deepEqual(retrieved, bulkData, "Put and retrieved should be the same");
ok("Now validating that is should be possible to request nonexisting keys but yet get all results in the order of the given keys");
const [u1, u2, u3, u4] = await db.users.bulkGet(["x", "y", 100, "z"]);
ok(u1 === undefined, "First result should be undefined, as there where no object with that key");
ok(u2 === undefined, "Second objects -''-");
ok(u3 && u3.first === 'Foo100', "Third should be Foo100");
ok(u4 === undefined, "Forth should be undefined");
});
promisedTest("bulkError by pos", async () => {
try {
const ids = await db.users.bulkAdd([
{ first: "foo1", last: "bar1", username: "foobar" },
{ first: "foo2", last: "bar2", username: "foobar" }, // should fail because username is unique idx
{ first: "foo3", last: "bar3", username: "foobar3" },
]);
ok(false, "Should not succeed");
} catch (bulkError) {
ok(bulkError instanceof Dexie.BulkError, "Got BulkError");
equal(bulkError.failures.length, 1, "Got one failure");
ok(!!bulkError.failures[0], "failures[0] is one Error");
ok(bulkError.failures[1] === undefined, "failures[1] is undefined");
equal(Object.keys(bulkError.failuresByPos).length, 1, "Got one key in failuresByPos");
equal(Object.keys(bulkError.failuresByPos)[0], 1, "Failure in position 1");
ok(bulkError.failuresByPos[0] === undefined, "failuresByPos[0] is undefined");
ok(!!bulkError.failuresByPos[1], "failuresByPos[1] is one Error");
}