UNPKG

@x5e/gink

Version:

an eventually consistent database

184 lines 7.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../implementation/index"); const utils_1 = require("../implementation/utils"); it("create a box; set and get data in it", async function () { // set up the objects for (const store of [ new index_1.IndexedDbStore("Box.test1", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; const aBox = await instance.createBox(); // set a value await aBox.set("a value"); // check that the desired result exists in the database const result = await aBox.get(); (0, utils_1.ensure)(result === "a value", `result is ${result}`); // check the appropriate size const size1 = await aBox.size(); (0, utils_1.ensure)(size1 === 1); // set another value await aBox.set("another value"); const result2 = await aBox.get(); (0, utils_1.ensure)(result2 === "another value"); // Make sure the "clear" operation works as intended await aBox.clear(); const result3 = await aBox.get(); (0, utils_1.ensure)(result3 === undefined); const size3 = await aBox.size(); (0, utils_1.ensure)(size3 === 0, size3.toString()); } }); it("set a box in a bundler", async function () { // set up the objects for (const store of [ new index_1.IndexedDbStore("box.test2", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; const aBox = await instance.createBox(); // set the value in a bundler const bundler = new index_1.Bundler(); aBox.set("a value", bundler); // confirm that change isn't visible yet const size0 = await aBox.size(); (0, utils_1.ensure)(size0 === 0); await instance.addBundler(bundler); const size1 = await aBox.size(); (0, utils_1.ensure)(size1 === 1); } }); it("create a box and set in same CS", async function () { var _a, _b; // set up the objects for (const store of [ new index_1.IndexedDbStore("box.test3", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; // create a box and set in on CL const bundler = new index_1.Bundler(); const box = await instance.createBox(bundler); const change = await box.set("a value", bundler); await instance.addBundler(bundler); // make sure the change and the box have the same timestamp (0, utils_1.ensure)((_a = box.address) === null || _a === void 0 ? void 0 : _a.timestamp); (0, utils_1.ensure)(((_b = box.address) === null || _b === void 0 ? void 0 : _b.timestamp) === change.timestamp); const val = await box.get(); (0, utils_1.ensure)(val === "a value"); } }); it("set a value in a box then clear it", async function () { for (const store of [ new index_1.IndexedDbStore("box.test4", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; // put a value into the box const box = await instance.createBox(); await box.set("foo"); // make sure it's there const current = await box.get(); (0, utils_1.ensure)(current === "foo"); // clear the box await box.clear(); // make sure the contents are gone const after = await box.get(); (0, utils_1.ensure)(after === undefined); } }); it("Box.toJson", async function () { for (const store of [ new index_1.IndexedDbStore("box.toJson", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; // put a value into the box const box = await instance.createBox(); const directory = await instance.createDirectory(); await box.set(directory); const box2 = await instance.createBox(); await directory.set("cheese", box2); await box2.set("fries"); const asJson = await box.toJson(); (0, utils_1.ensure)(asJson === `{"cheese":"fries"}`); } }); it("Box.Store", async function () { for (const store of [ new index_1.IndexedDbStore("box.Store", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; // put a value into the box const box = await instance.createBox(); var date = new Date(); await box.set(date); var expectDate = await box.get(); (0, utils_1.ensure)((0, utils_1.isDate)(expectDate) && expectDate.getTime() === date.getTime()); var int = BigInt("137"); await box.set(int); var expectInt = await box.get(); (0, utils_1.ensure)(expectInt === int); var floating = 137; await box.set(floating); var expectFloating = await box.get(); (0, utils_1.ensure)(expectFloating === floating); } }); it("Box.reset", async function () { for (const store of [ new index_1.IndexedDbStore("box.reset", true), new index_1.MemoryStore(true), ]) { const instance = new index_1.Database(store); await instance.ready; const box = await instance.createBox(); const prop1 = await instance.createProperty(); const prop2 = await instance.createProperty(); await prop1.set(box, "foo"); await prop2.set(box, "bar"); await box.set("value 1"); const afterSet = (0, utils_1.generateTimestamp)(); await box.set("changed"); await prop1.set(box, "foo2"); await prop2.set(box, "bar2"); const afterSecond = (0, utils_1.generateTimestamp)(); await box.reset({ toTime: afterSet }); (0, utils_1.ensure)((await box.get()) === "value 1"); (0, utils_1.ensure)((await prop1.get(box)) === "foo"); (0, utils_1.ensure)((await prop2.get(box)) === "bar"); await box.reset(); (0, utils_1.ensure)((await box.get()) === undefined); (0, utils_1.ensure)((await prop1.get(box)) === undefined); (0, utils_1.ensure)((await prop2.get(box)) === undefined); await box.reset({ toTime: afterSecond, skipProperties: true }); (0, utils_1.ensure)((await box.get()) === "changed"); (0, utils_1.ensure)((await prop1.get(box)) === undefined); (0, utils_1.ensure)((await prop2.get(box)) === undefined); const dir = await instance.createDirectory(); await box.set(dir); await dir.set("cheese", "fries"); const resetTo = (0, utils_1.generateTimestamp)(); await dir.set("cheese", "no fries"); const noFries = (0, utils_1.generateTimestamp)(); (0, utils_1.ensure)((await dir.get("cheese")) === "no fries"); await box.reset({ toTime: resetTo, recurse: true }); (0, utils_1.ensure)((await dir.get("cheese")) === "fries"); await box.clear(); (0, utils_1.ensure)((await box.get()) === undefined); await box.reset({ toTime: resetTo }); (0, utils_1.ensure)(typeof (await box.get()) === "object"); (0, utils_1.ensure)((await dir.get("cheese")) === "fries"); await box.reset({ toTime: noFries, recurse: true }); (0, utils_1.ensure)((await dir.get("cheese")) === "no fries"); } }); //# sourceMappingURL=Box.test.js.map