diff options
author | Florian Dold <florian@dold.me> | 2021-12-15 02:37:03 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-12-15 02:38:14 +0100 |
commit | e84a1789af2a0292128807b86649a45c4da0a51c (patch) | |
tree | cfeeb7505e6c40e778029ba0a05d0d472aad91a5 /packages/idb-bridge/src/MemoryBackend.test.ts | |
parent | 2237058bcc8f589b51137fe5f8e3ed53039e25db (diff) | |
download | wallet-core-e84a1789af2a0292128807b86649a45c4da0a51c.tar.xz |
idb-bridge: faster indices, various correctness fixes and tests
Diffstat (limited to 'packages/idb-bridge/src/MemoryBackend.test.ts')
-rw-r--r-- | packages/idb-bridge/src/MemoryBackend.test.ts | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/packages/idb-bridge/src/MemoryBackend.test.ts b/packages/idb-bridge/src/MemoryBackend.test.ts index 292f1b495..ac1a9d908 100644 --- a/packages/idb-bridge/src/MemoryBackend.test.ts +++ b/packages/idb-bridge/src/MemoryBackend.test.ts @@ -23,6 +23,12 @@ import { BridgeIDBRequest, BridgeIDBTransaction, } from "./bridge-idb"; +import { + IDBCursorDirection, + IDBCursorWithValue, + IDBKeyRange, + IDBValidKey, +} from "./idbtypes.js"; import { MemoryBackend } from "./MemoryBackend"; function promiseFromRequest(request: BridgeIDBRequest): Promise<any> { @@ -104,6 +110,7 @@ test("Spec: Example 1 Part 2", async (t) => { test("Spec: Example 1 Part 3", async (t) => { const backend = new MemoryBackend(); + backend.enableTracing = true; const idb = new BridgeIDBFactory(backend); const request = idb.open("library"); @@ -348,3 +355,184 @@ test("export", async (t) => { t.is(exportedData2.databases["library"].schema.databaseVersion, 42); t.pass(); }); + +test("range queries", async (t) => { + const backend = new MemoryBackend(); + backend.enableTracing = true; + const idb = new BridgeIDBFactory(backend); + + const request = idb.open("mydb"); + request.onupgradeneeded = () => { + const db = request.result; + const store = db.createObjectStore("bla", { keyPath: "x" }); + store.createIndex("by_y", "y"); + store.createIndex("by_z", "z"); + }; + + const db: BridgeIDBDatabase = await promiseFromRequest(request); + + t.is(db.name, "mydb"); + + const tx = db.transaction("bla", "readwrite"); + + const store = tx.objectStore("bla"); + + store.put({ x: 0, y: "a" }); + store.put({ x: 2, y: "a" }); + store.put({ x: 4, y: "b" }); + store.put({ x: 8, y: "b" }); + store.put({ x: 10, y: "c" }); + store.put({ x: 12, y: "c" }); + + await promiseFromTransaction(tx); + + async function doCursorStoreQuery( + range: IDBKeyRange | IDBValidKey | undefined, + direction: IDBCursorDirection | undefined, + expected: any[], + ): Promise<void> { + const tx = db.transaction("bla", "readwrite"); + const store = tx.objectStore("bla"); + const vals: any[] = []; + + const req = store.openCursor(range, direction); + while (1) { + await promiseFromRequest(req); + const cursor: IDBCursorWithValue = req.result; + if (!cursor) { + break; + } + cursor.continue(); + vals.push(cursor.value); + } + + await promiseFromTransaction(tx); + + t.deepEqual(vals, expected); + } + + async function doCursorIndexQuery( + range: IDBKeyRange | IDBValidKey | undefined, + direction: IDBCursorDirection | undefined, + expected: any[], + ): Promise<void> { + const tx = db.transaction("bla", "readwrite"); + const store = tx.objectStore("bla"); + const index = store.index("by_y"); + const vals: any[] = []; + + const req = index.openCursor(range, direction); + while (1) { + await promiseFromRequest(req); + const cursor: IDBCursorWithValue = req.result; + if (!cursor) { + break; + } + cursor.continue(); + vals.push(cursor.value); + } + + await promiseFromTransaction(tx); + + t.deepEqual(vals, expected); + } + + await doCursorStoreQuery(undefined, undefined, [ + { + x: 0, + y: "a", + }, + { + x: 2, + y: "a", + }, + { + x: 4, + y: "b", + }, + { + x: 8, + y: "b", + }, + { + x: 10, + y: "c", + }, + { + x: 12, + y: "c", + }, + ]); + + await doCursorStoreQuery( + BridgeIDBKeyRange.bound(0, 12, true, true), + undefined, + [ + { + x: 2, + y: "a", + }, + { + x: 4, + y: "b", + }, + { + x: 8, + y: "b", + }, + { + x: 10, + y: "c", + }, + ], + ); + + await doCursorIndexQuery( + BridgeIDBKeyRange.bound("a", "c", true, true), + undefined, + [ + { + x: 4, + y: "b", + }, + { + x: 8, + y: "b", + }, + ], + ); + + await doCursorIndexQuery(undefined, "nextunique", [ + { + x: 0, + y: "a", + }, + { + x: 4, + y: "b", + }, + { + x: 10, + y: "c", + }, + ]); + + await doCursorIndexQuery(undefined, "prevunique", [ + { + x: 10, + y: "c", + }, + { + x: 4, + y: "b", + }, + { + x: 0, + y: "a", + }, + ]); + + db.close(); + + t.pass(); +}); |