|
| 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