From f93839558d528f600b10fbe816914e7ff91fca64 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 8 Jun 2017 12:15:45 +0200 Subject: aa trees --- src/memidb/memidb-test.ts | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/memidb/memidb-test.ts (limited to 'src/memidb/memidb-test.ts') diff --git a/src/memidb/memidb-test.ts b/src/memidb/memidb-test.ts new file mode 100644 index 000000000..8f8498a6e --- /dev/null +++ b/src/memidb/memidb-test.ts @@ -0,0 +1,114 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + +import {test} from "ava"; +import * as memidb from "./memidb"; + +test.cb("db open", (t) => { + let ncb = 0; + const idb = new memidb.MemoryIDBFactory(); + const req = idb.open("testdb"); + let called = false; + req.onupgradeneeded = (evt) => { + ncb += 1; + called = true; + t.is(req.result, evt.target); + t.is(evt.oldVersion, 0); + t.is(evt.newVersion, 1); + t.truthy(req.result); + t.pass(); + } + req.onsuccess = (evt) => { + t.is(ncb, 1); + t.is(req.result, evt.target); + t.truthy(req.result); + t.end(); + } +}); + +test.cb("store creation", (t) => { + const idb = new memidb.MemoryIDBFactory(); + const req = idb.open("testdb"); + req.onupgradeneeded = (evt) => { + const db: IDBDatabase = req.result; + + const store1 = db.createObjectStore("b-store"); + t.is(store1.name, "b-store"); + t.deepEqual(Array.from(db.objectStoreNames), ["b-store"]); + + const store2 = db.createObjectStore("a-store"); + t.is(store2.name, "a-store"); + t.deepEqual(Array.from(db.objectStoreNames), ["a-store", "b-store"]); + + const store3 = db.createObjectStore("c-store"); + t.is(store3.name, "c-store"); + t.deepEqual(Array.from(db.objectStoreNames), ["a-store", "b-store", "c-store"]); + t.pass(); + } + req.onsuccess = (evt) => { + t.end(); + } +}); + + +test.cb("put and get", (t) => { + const idb = new memidb.MemoryIDBFactory(); + const req = idb.open("testdb"); + req.onupgradeneeded = (evt) => { + const db: IDBDatabase = req.result; + const store1 = db.createObjectStore("mystore"); + store1.put({answer: 42}, "a"); + } + req.onsuccess = (evt) => { + t.end() + } +}); + + +test("key path evaluation", (t) => { + const obj = { + a: { + b: { + c: 42, + }, + }, + b: "hello", + "": "spam", + arr: ["foo", "bar"], + } + t.deepEqual(memidb.evaluateKeyPath(obj, ""), obj); + t.deepEqual(memidb.evaluateKeyPath(obj, "a.b.c"), 42); + t.deepEqual(memidb.evaluateKeyPath(obj, "a.b"), {c: 42}); + t.deepEqual(memidb.evaluateKeyPath(obj, "foo"), undefined); + t.deepEqual(memidb.evaluateKeyPath(obj, ["a.b.c", "foo"]), undefined); + t.deepEqual(memidb.evaluateKeyPath(obj, ["a.b.c", "b"]), [42, "hello"]); + t.deepEqual(memidb.evaluateKeyPath(obj, "arr.0"), "foo"); + t.deepEqual(memidb.evaluateKeyPath(obj, "."), "spam"); +}); + +test("key path evaluation with replacement", (t) => { + const obj: any = { + a: { + b: { + c: 42, + }, + }, + } + memidb.evaluateKeyPath(obj, "a.b.c", 24); + t.is(obj.a.b.c, 24); + memidb.evaluateKeyPath(obj, "a.b", 24); + t.is(obj.a.b, 24); +}); -- cgit v1.2.3