@x5e/gink
Version:
an eventually consistent database
128 lines • 7.58 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const implementation_1 = require("../implementation");
const utils_1 = require("../implementation/utils");
it("test reset", async function () {
for (const store of [
new implementation_1.IndexedDbStore("Keyed.reset", true),
new implementation_1.MemoryStore(true),
]) {
const database = new implementation_1.Database(store);
await database.ready;
const box = await database.createBox();
const pairMap = await database.createPairMap();
const schema = await database.createDirectory();
await schema.set("a key", "a value");
await pairMap.set([box, schema], "a value");
const prop1 = await database.createProperty();
const prop2 = await database.createProperty();
await prop1.set(schema, "foo");
await prop2.set(schema, "bar");
const afterFirst = (0, utils_1.generateTimestamp)();
await schema.set("another key", "another value");
await pairMap.set([schema, box], "reversed");
await prop1.set(schema, "foo2");
await prop2.set(schema, "bar2");
(0, utils_1.ensure)((await schema.get("another key")) === "another value");
(0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value");
(0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed");
const afterbox = (0, utils_1.generateTimestamp)();
// Reset when first entry is still there
await schema.reset({ toTime: afterFirst });
(0, utils_1.ensure)((await schema.get("another key")) === undefined);
(0, utils_1.ensure)((await schema.get("a key")) === "a value");
(0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value");
(0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed");
// Properties should have also reset
(0, utils_1.ensure)((await prop1.get(schema)) === "foo");
(0, utils_1.ensure)((await prop2.get(schema)) === "bar");
// Reset to epoch
await schema.reset();
(0, utils_1.ensure)((await schema.get("another key")) === undefined);
(0, utils_1.ensure)((await schema.get("a key")) === undefined);
(0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value");
(0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed");
// Properties should have also reset
(0, utils_1.ensure)((await prop1.get(schema)) === undefined);
(0, utils_1.ensure)((await prop2.get(schema)) === undefined);
await schema.reset({ toTime: afterFirst, skipProperties: true });
(0, utils_1.ensure)((await schema.get("another key")) === undefined);
(0, utils_1.ensure)((await schema.get("a key")) === "a value");
(0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value");
(0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed");
// properties skipped, should still be undefined
(0, utils_1.ensure)((await prop1.get(schema)) === undefined);
(0, utils_1.ensure)((await prop2.get(schema)) === undefined);
await pairMap.reset({ toTime: afterFirst });
(0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value");
(0, utils_1.ensure)((await pairMap.get([schema, box])) === undefined);
await pairMap.reset({ toTime: afterbox });
(0, utils_1.ensure)((await pairMap.get([box, schema])) === "a value");
(0, utils_1.ensure)((await pairMap.get([schema, box])) === "reversed");
(0, utils_1.ensure)((await schema.get("another key")) === undefined);
(0, utils_1.ensure)((await schema.get("a key")) === "a value");
// Test recursive reset
const child = await database.createDirectory();
const childOfChild = await database.createDirectory();
await child.set("childOfChild", childOfChild);
await child.set("random key", "random");
await childOfChild.set("key", "value");
await schema.set("child", child);
const afterInit = (0, utils_1.generateTimestamp)();
await childOfChild.set("key", "changed");
await child.set("random key", "changed");
const afterChanged = (0, utils_1.generateTimestamp)();
await schema.reset({ toTime: afterInit, recurse: true });
(0, utils_1.ensure)((await childOfChild.get("key")) === "value");
(0, utils_1.ensure)((await child.get("random key")) === "random");
(0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory);
await schema.clear();
(0, utils_1.ensure)((await schema.size()) === 0);
// Reset after a clear
await schema.reset({ toTime: afterInit, recurse: true });
(0, utils_1.ensure)((await childOfChild.get("key")) === "value");
(0, utils_1.ensure)((await child.get("random key")) === "random");
(0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory);
// Same reset again, should not change anything
await schema.reset({ toTime: afterInit, recurse: true });
(0, utils_1.ensure)((await childOfChild.get("key")) === "value");
(0, utils_1.ensure)((await child.get("random key")) === "random");
(0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory);
// Make sure a deletion doesn't cause problems
await schema.delete("child");
(0, utils_1.ensure)((await schema.get("child")) === undefined);
await schema.reset({ toTime: afterInit, recurse: true });
(0, utils_1.ensure)((await childOfChild.get("key")) === "value");
(0, utils_1.ensure)((await child.get("random key")) === "random");
(0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory);
// Recurse = false should not reset children
await schema.reset({ toTime: afterChanged });
(0, utils_1.ensure)((await childOfChild.get("key")) === "value");
(0, utils_1.ensure)((await child.get("random key")) === "random");
(0, utils_1.ensure)((await schema.get("child")) instanceof implementation_1.Directory);
// Test circular references doesn't cause infinite loops
await box.set(schema);
await schema.set("circle", box);
const resetTo = (0, utils_1.generateTimestamp)();
await schema.set("circle", "not a box");
await schema.reset({ toTime: resetTo, recurse: true });
(0, utils_1.ensure)((await schema.get("circle")) instanceof implementation_1.Box);
(0, utils_1.ensure)((await box.get()) instanceof implementation_1.Directory);
// Test non-immediate reset
const arr = [1, 2, 3];
await schema.set("hmm", 2);
await schema.set(2, arr);
const afterNumbers = (0, utils_1.generateTimestamp)();
await schema.set("hmm", 20);
const bundler = new implementation_1.Bundler();
await schema.reset({ toTime: afterNumbers, bundlerOrComment: bundler });
(0, utils_1.ensure)((await schema.get("hmm")) === 20);
(0, utils_1.ensure)((0, lodash_1.isEqual)(await schema.get(2), arr));
await database.addBundler(bundler);
(0, utils_1.ensure)((await schema.get("hmm")) === 2, await schema.toJson());
(0, utils_1.ensure)((0, lodash_1.isEqual)(await schema.get(2), arr));
await store.close();
}
});
//# sourceMappingURL=Keyed.test.js.map