-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.mjs
119 lines (86 loc) · 4.11 KB
/
tests.mjs
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
import { strict as assert } from 'assert';
import { CoreStore } from "./cbdb.mjs";
const obj1 = {thing:1},
obj2 = {thing:2},
obj3 = {currentValue:0},
obj3v2 = {differentValue:true},
obj3b = {currentValue:42},
obj3c = {currentValue:-1},
obj4 = {another:1},
obj5 = {another:1};
let db = new CoreStore([obj1,obj2,obj3]);
// TODO: do we really want to guarantee order? check for each object instead? [others below…]
assert.deepEqual(db.objects, [obj1,obj2,obj3], "Should contain the objects we gave it.");
assert.equal(db.changes.length, 0, "Shouldn't be any changes off the bat.");
let notifyCount = 0;
const countNotifications = () => {
++notifyCount;
};
const unsubscribe = db.subscribe(countNotifications);
db.replace(obj3, obj3b);
assert.equal(db.changes.length, 1, "Should have a change.");
assert.equal(notifyCount, 1, "Subscriber callback should have fired.");
assert.deepEqual(db.changes, [{current:obj3b, original:obj3}], "Change should expose correct information.");
const unsubscribe2 = db.subscribe(countNotifications);
notifyCount = 0;
db.replace(null, obj4);
assert.equal(db.objects.length, 4, "Should have an additional object now.");
assert.equal(notifyCount, 2, "Subscriber callback should have fired twice (once for each subscriber).");
assert.deepEqual(db.changes, [{current:obj3b, original:obj3},{current:obj4,original:null}], "Changes should be up-to-date.");
assert.throws(() => {
db.replace(null, obj4);
}, "Shouldn't be allowed to add duplicate copies of an object.");
db.replace(obj4, null);
assert.deepEqual(db.changes, [{current:obj3b, original:obj3}], "Changes should filter out temporary local objects.");
assert.doesNotThrow(() => {
db.replace(null, obj4);
}, "Should be able to restore an earlier object if not a duplicate.");
db.updateOriginal(obj4, obj4);
assert.deepEqual(db.changes, [{current:obj3b, original:obj3}], "Changes shouldn't include local objects that are identical to their originals.");
notifyCount = 0;
unsubscribe();
db.replace(null, obj5);
assert.equal(db.objects.length, 5, "Should have an additional object despite same content.");
assert.equal(notifyCount, 1, "Subscriber callback should have fired once (for remaining subscriber).");
assert.doesNotThrow(() => {
unsubscribe2();
unsubscribe2();
}, "No problem calling unsubscribe helper multiple times");
notifyCount = 0;
db.replace(obj3b, obj3c);
assert.equal(notifyCount, 0, "No notifications should have been received.");
assert.equal(db.objects.length, 5, "Number of objects shouldn't change.");
assert.notEqual(db.objects.indexOf(obj3c), -1, "New object should be listed.");
assert.equal(db.objects.indexOf(obj3b), -1, "Old object should NOT be listed.");
let objectsBefore = db.objects,
changesBefore = db.changes;
db.updateOriginal(obj3, obj3v2);
assert.deepEqual(db.objects, objectsBefore, "Objects shouldn't change when updating a locally-replaced original.");
assert.notDeepEqual(db.changes.map(d => d.original), changesBefore.map(d => d.original), "Original fields of changes should differ.");
assert.deepEqual(db.changes.map(d => d.current), changesBefore.map(d => d.current), "Current fields of changes shouldn't differ.");
db.resolve(obj3);
assert.notEqual(db.objects.indexOf(obj3v2), -1, "Replaced version of original object should now be exposed.");
assert.equal(db.changes.length, changesBefore.length - 1, "Should have one less change after local resolution.");
console.log("Seems legit.");
import { TripleStore } from "./cbdb.mjs";
let kv = new TripleStore(Object.entries({
thing1: 1,
thing2: {a:true, b:false}
}));
//console.log(kv._db.objects);
assert.equal(kv.get('thing1'), 1);
assert.equal(kv.has('thing2'), true);
kv.set('thing1', "one");
assert.equal(kv.get('thing1'), "one");
kv.update('thing2', {b:true});
assert.deepEqual(kv.get('thing2'), {a:true, b:true});
kv.delete('thing2');
assert.equal(kv.has('thing2'), false);
kv.set('thing2', ["a","b"]);
assert.equal(kv.has('thing2'), true);
kv.set('thing2', null, false);
assert.deepEqual(kv.get('thing2'), ["a","b"]);
kv.delete('thing2', false);
//console.log(kv._db.changes);
//console.log(kv._byKey);
console.log("The legit abides.");