Skip to content

Commit

Permalink
feat: integrate keys and query
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jan 12, 2025
1 parent 3c37462 commit 8b6eabd
Show file tree
Hide file tree
Showing 4 changed files with 600 additions and 24 deletions.
140 changes: 140 additions & 0 deletions keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assert, assertEquals, setup, teardown } from "./_test_util.ts";
import { batchedAtomic } from "./batched_atomic.ts";
import { Filter, query } from "./query.ts";

import {
equals,
Expand Down Expand Up @@ -30,6 +31,28 @@ Deno.test({
},
});

Deno.test({
name: "keys - returns a list of keys from query",
async fn() {
const kv = await setup();
const res = await kv.atomic()
.set(["a"], "a")
.set(["a", "b"], "b")
.set(["a", "b", "c"], "c")
.set(["d"], "d")
.commit();
assert(res.ok);

const actual = await keys(
query(kv, { prefix: ["a"] })
.value("==", "b"),
);

assertEquals(actual, [["a", "b"]]);
return teardown();
},
});

Deno.test({
name: "unique - returns a list of unique sub-keys",
async fn() {
Expand All @@ -50,6 +73,33 @@ Deno.test({
},
});

Deno.test({
name: "unique - returns a list of unique sub-keys from query",
async fn() {
const kv = await setup();
const res = await kv.atomic()
.set(["a"], "a")
.set(["a", "b"], "b")
.set(["a", "b", "c"], "c")
.set(["a", "d", "f", "g"], "g")
.set(["e"], "e")
.commit();
assert(res.ok);

const actual = await unique(
query(kv, { prefix: ["a"] }).where(
Filter.or(
Filter.value("==", "b"),
Filter.value("==", "c"),
Filter.value("==", "g"),
),
),
);
assertEquals(actual, [["a", "b"], ["a", "d"]]);
return teardown();
},
});

Deno.test({
name: "unique - handles Uint8Array equality",
async fn() {
Expand Down Expand Up @@ -98,6 +148,40 @@ Deno.test({
},
});

Deno.test({
name: "uniqueCount - returns a list of unique sub-keys from query",
async fn() {
const kv = await setup();
const res = await kv.atomic()
.set(["a"], "a")
.set(["a", "b"], "b")
.set(["a", "b", "c"], "c")
.set(["a", "d", "f", "g"], "g")
.set(["a", "h"], "h")
.set(["e"], "e")
.commit();
assert(res.ok);

const actual = await uniqueCount(
query(kv, { prefix: ["a"] }).where(
Filter.or(
Filter.value("==", "b"),
Filter.value("==", "c"),
Filter.value("==", "g"),
Filter.value("==", "h"),
),
),
);

assertEquals(actual, [
{ key: ["a", "b"], count: 1 },
{ key: ["a", "d"], count: 1 },
{ key: ["a", "h"], count: 0 },
]);
return teardown();
},
});

Deno.test({
name: "uniqueCount - handles Uint8Array equality",
async fn() {
Expand Down Expand Up @@ -318,6 +402,62 @@ Deno.test({
},
});

Deno.test({
name: "tree - returns a correct tree structure from query",
async fn() {
const kv = await setup();
const res = await kv.atomic()
.set(["a"], "a")
.set(["a", "b"], "b")
.set(["a", "b", "c"], "c")
.set(["a", "d", "f", "g"], "g")
.set(["a", "h"], "h")
.set(["e"], "e")
.commit();
assert(res.ok);

const actual = await tree(
query(kv, { prefix: [] }).where(
Filter.or(
Filter.value("==", "a"),
Filter.value("==", "b"),
Filter.value("==", "c"),
Filter.value("==", "g"),
Filter.value("==", "h"),
Filter.value("==", "e"),
),
),
);

assertEquals(actual, {
children: [
{
part: "a",
hasValue: true,
children: [
{
part: "b",
hasValue: true,
children: [{ part: "c", hasValue: true }],
},
{
part: "d",
children: [{
part: "f",
children: [{ part: "g", hasValue: true }],
}],
},
{ part: "h", hasValue: true },
],
},
{ part: "e", hasValue: true },
],
});

return teardown();
},
});

Deno.test({
name: "partEquals",
fn() {
Expand Down
Loading

0 comments on commit 8b6eabd

Please sign in to comment.