Skip to content

Commit c81583d

Browse files
committed
WIP: add sparsesnapshottree.test.ts
1 parent d35b183 commit c81583d

File tree

3 files changed

+238
-1
lines changed

3 files changed

+238
-1
lines changed

tests/database/datasnapshot.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getRandomNode } from "./helpers";
55
import { DataSnapshot } from "../../src/database/api/DataSnapshot";
66
import { Reference } from "../../src/database/api/Reference";
77

8-
describe.only("DataSnapshot Tests", function () {
8+
describe("DataSnapshot Tests", function () {
99
/** @return {!DataSnapshot} */
1010
var snapshotForJSON = function(json) {
1111
var dummyRef = <Reference>getRandomNode();

tests/database/path.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect } from "chai";
2+
import { Path } from "../../src/database/core/util/Path";
3+
4+
describe('Path Tests', function () {
5+
var expectGreater = function(left, right) {
6+
expect(Path.comparePaths(new Path(left), new Path(right))).to.equal(1)
7+
expect(Path.comparePaths(new Path(right), new Path(left))).to.equal(-1)
8+
};
9+
10+
var expectEqual = function(left, right) {
11+
expect(Path.comparePaths(new Path(left), new Path(right))).to.equal(0)
12+
};
13+
14+
it('contains() contains the path and any child path.', function () {
15+
expect(new Path('/').contains(new Path('/a/b/c'))).to.equal(true);
16+
expect(new Path('/a').contains(new Path('/a/b/c'))).to.equal(true);
17+
expect(new Path('/a/b').contains(new Path('/a/b/c'))).to.equal(true);
18+
expect(new Path('/a/b/c').contains(new Path('/a/b/c'))).to.equal(true);
19+
20+
expect(new Path('/a/b/c').contains(new Path('/a/b'))).to.equal(false);
21+
expect(new Path('/a/b/c').contains(new Path('/a'))).to.equal(false);
22+
expect(new Path('/a/b/c').contains(new Path('/'))).to.equal(false);
23+
24+
expect(new Path('/a/b/c').popFront().contains(new Path('/b/c'))).to.equal(true);
25+
expect(new Path('/a/b/c').popFront().contains(new Path('/b/c/d'))).to.equal(true);
26+
27+
expect(new Path('/a/b/c').contains(new Path('/b/c'))).to.equal(false);
28+
expect(new Path('/a/b/c').contains(new Path('/a/c/b'))).to.equal(false);
29+
30+
expect(new Path('/a/b/c').popFront().contains(new Path('/a/b/c'))).to.equal(false);
31+
expect(new Path('/a/b/c').popFront().contains(new Path('/b/c'))).to.equal(true);
32+
expect(new Path('/a/b/c').popFront().contains(new Path('/b/c/d'))).to.equal(true);
33+
});
34+
35+
it('popFront() returns the parent', function() {
36+
expect(new Path('/a/b/c').popFront().toString()).to.equal('/b/c')
37+
expect(new Path('/a/b/c').popFront().popFront().toString()).to.equal('/c');
38+
expect(new Path('/a/b/c').popFront().popFront().popFront().toString()).to.equal('/');
39+
expect(new Path('/a/b/c').popFront().popFront().popFront().popFront().toString()).to.equal('/');
40+
});
41+
42+
it('parent() returns the parent', function() {
43+
expect(new Path('/a/b/c').parent().toString()).to.equal('/a/b');
44+
expect(new Path('/a/b/c').parent().parent().toString()).to.equal('/a');
45+
expect(new Path('/a/b/c').parent().parent().parent().toString()).to.equal('/');
46+
expect(new Path('/a/b/c').parent().parent().parent().parent()).to.equal(null);
47+
});
48+
49+
it('comparePaths() works as expected', function() {
50+
expectEqual('/', '');
51+
expectEqual('/a', '/a');
52+
expectEqual('/a', '/a//');
53+
expectEqual('/a///b/b//', '/a/b/b');
54+
expectGreater('/b', '/a');
55+
expectGreater('/ab', '/a');
56+
expectGreater('/a/b', '/a');
57+
expectGreater('/a/b', '/a//');
58+
});
59+
});
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import { expect } from "chai";
2+
import { SparseSnapshotTree } from "../../src/database/core/SparseSnapshotTree";
3+
import { Path } from "../../src/database/core/util/Path";
4+
import { nodeFromJSON } from "../../src/database/core/snap/nodeFromJSON";
5+
import { ChildrenNode } from "../../src/database/core/snap/ChildrenNode";
6+
7+
describe("SparseSnapshotTree Tests", function () {
8+
it("Basic remember and find.", function () {
9+
var st = new SparseSnapshotTree();
10+
var path = new Path("a/b");
11+
var node = nodeFromJSON("sdfsd");
12+
13+
st.remember(path, node);
14+
expect(st.find(new Path("a/b")).isEmpty()).to.equal(false);
15+
expect(st.find(new Path("a"))).to.equal(null);
16+
});
17+
18+
19+
it("Find inside an existing snapshot", function () {
20+
var st = new SparseSnapshotTree();
21+
var path = new Path("t/tt");
22+
var node = nodeFromJSON({ a: "sdfsd", x: 5, "999i": true });
23+
node = node.updateImmediateChild("apples", nodeFromJSON({ "goats": 88 }));
24+
st.remember(path, node);
25+
26+
expect(st.find(new Path("t/tt")).isEmpty()).to.equal(false);
27+
expect(st.find(new Path("t/tt/a")).val()).to.equal("sdfsd");
28+
expect(st.find(new Path("t/tt/999i")).val()).to.equal(true);
29+
expect(st.find(new Path("t/tt/apples")).isEmpty()).to.equal(false);
30+
expect(st.find(new Path("t/tt/apples/goats")).val()).to.equal(88);
31+
});
32+
33+
34+
it("Write a snapshot inside a snapshot.", function () {
35+
var st = new SparseSnapshotTree();
36+
st.remember(new Path("t"), nodeFromJSON({ a: { b: "v" } }));
37+
st.remember(new Path("t/a/rr"), nodeFromJSON(19));
38+
expect(st.find(new Path("t/a/b")).val()).to.equal("v");
39+
expect(st.find(new Path("t/a/rr")).val()).to.equal(19);
40+
});
41+
42+
43+
it("Write a null value and confirm it is remembered.", function () {
44+
var st = new SparseSnapshotTree();
45+
st.remember(new Path("awq/fff"), nodeFromJSON(null));
46+
expect(st.find(new Path("awq/fff"))).to.equal(ChildrenNode.EMPTY_NODE);
47+
expect(st.find(new Path("awq/sdf"))).to.equal(null);
48+
expect(st.find(new Path("awq/fff/jjj"))).to.equal(ChildrenNode.EMPTY_NODE);
49+
expect(st.find(new Path("awq/sdf/sdf/q"))).to.equal(null);
50+
});
51+
52+
53+
it("Overwrite with null and confirm it is remembered.", function () {
54+
var st = new SparseSnapshotTree();
55+
st.remember(new Path("t"), nodeFromJSON({ a: { b: "v" } }));
56+
expect(st.find(new Path("t")).isEmpty()).to.equal(false);
57+
st.remember(new Path("t"), ChildrenNode.EMPTY_NODE);
58+
expect(st.find(new Path("t")).isEmpty()).to.equal(true);
59+
});
60+
61+
62+
it("Simple remember and forget.", function () {
63+
var st = new SparseSnapshotTree();
64+
st.remember(new Path("t"), nodeFromJSON({ a: { b: "v" } }));
65+
expect(st.find(new Path("t")).isEmpty()).to.equal(false);
66+
st.forget(new Path("t"));
67+
expect(st.find(new Path("t"))).to.equal(null);
68+
});
69+
70+
71+
it("Forget the root.", function () {
72+
var st = new SparseSnapshotTree();
73+
st.remember(new Path("t"), nodeFromJSON({ a: { b: "v" } }));
74+
expect(st.find(new Path("t")).isEmpty()).to.equal(false);
75+
st.forget(new Path(""));
76+
expect(st.find(new Path("t"))).to.equal(null);
77+
});
78+
79+
80+
it("Forget snapshot inside snapshot.", function () {
81+
var st = new SparseSnapshotTree();
82+
st.remember(new Path("t"), nodeFromJSON({ a: { b: "v", c: 9, art: false } }));
83+
expect(st.find(new Path("t/a/c")).isEmpty()).to.equal(false);
84+
expect(st.find(new Path("t")).isEmpty()).to.equal(false);
85+
86+
st.forget(new Path("t/a/c"));
87+
expect(st.find(new Path("t"))).to.equal(null);
88+
expect(st.find(new Path("t/a"))).to.equal(null);
89+
expect(st.find(new Path("t/a/b")).val()).to.equal("v");
90+
expect(st.find(new Path("t/a/c"))).to.equal(null);
91+
expect(st.find(new Path("t/a/art")).val()).to.equal(false);
92+
});
93+
94+
95+
it("Forget path shallower than snapshots.", function () {
96+
var st = new SparseSnapshotTree();
97+
st.remember(new Path("t/x1"), nodeFromJSON(false));
98+
st.remember(new Path("t/x2"), nodeFromJSON(true));
99+
st.forget(new Path("t"));
100+
expect(st.find(new Path("t"))).to.equal(null);
101+
});
102+
103+
104+
it("Iterate children.", function () {
105+
var st = new SparseSnapshotTree();
106+
st.remember(new Path("t"), nodeFromJSON({ b: "v", c: 9, art: false }));
107+
st.remember(new Path("q"), ChildrenNode.EMPTY_NODE);
108+
109+
var num = 0, gotT = false, gotQ = false;
110+
st.forEachChild(function(key, child) {
111+
num += 1;
112+
if (key === "t") {
113+
gotT = true;
114+
} else if (key === "q") {
115+
gotQ = true;
116+
} else {
117+
expect(false).to.equal(true);
118+
}
119+
});
120+
121+
expect(gotT).to.equal(true);
122+
expect(gotQ).to.equal(true);
123+
expect(num).to.equal(2);
124+
});
125+
126+
127+
it("Iterate trees.", function () {
128+
var st = new SparseSnapshotTree();
129+
130+
var count = 0;
131+
st.forEachTree(new Path(""), function(path, tree) {
132+
count += 1;
133+
});
134+
expect(count).to.equal(0);
135+
136+
st.remember(new Path("t"), nodeFromJSON(1));
137+
st.remember(new Path("a/b"), nodeFromJSON(2));
138+
st.remember(new Path("a/x/g"), nodeFromJSON(3));
139+
st.remember(new Path("a/x/null"), nodeFromJSON(null));
140+
141+
var num = 0, got1 = false, got2 = false, got3 = false, got4 = false;
142+
st.forEachTree(new Path("q"), function(path, node) {
143+
num += 1;
144+
var pathString = path.toString();
145+
if (pathString === "/q/t") {
146+
got1 = true;
147+
expect(node.val()).to.equal(1);
148+
} else if (pathString === "/q/a/b") {
149+
got2 = true;
150+
expect(node.val()).to.equal(2);
151+
} else if (pathString === "/q/a/x/g") {
152+
got3 = true;
153+
expect(node.val()).to.equal(3);
154+
} else if (pathString === "/q/a/x/null") {
155+
got4 = true;
156+
expect(node.val()).to.equal(null);
157+
} else {
158+
expect(false).to.equal(true);
159+
}
160+
});
161+
162+
expect(got1).to.equal(true);
163+
expect(got2).to.equal(true);
164+
expect(got3).to.equal(true);
165+
expect(got4).to.equal(true);
166+
expect(num).to.equal(4);
167+
});
168+
169+
it("Set leaf, then forget deeper path", function() {
170+
var st = new SparseSnapshotTree();
171+
172+
st.remember(new Path('foo'), nodeFromJSON('bar'));
173+
var safeToRemove = st.forget(new Path('foo/baz'));
174+
// it's not safe to remove this node
175+
expect(safeToRemove).to.equal(false);
176+
});
177+
178+
});

0 commit comments

Comments
 (0)