-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
58 lines (51 loc) · 1.32 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Database from "bun:sqlite";
import { test, expect, mock } from "bun:test";
import KV from ".";
const instance = mock((table?: string) => {
const database = new Database();
return {
database,
kv: new KV(database, table),
};
});
const random = mock(() => Math.random().toString());
function exists(database: Database, table: string) {
return (
database
.prepare(
`
SELECT EXISTS (
SELECT name FROM sqlite_schema
WHERE type='table' AND name='${table}'
)
`
)
.values()[0][0] == 1
);
}
test("can create kv table", () => {
const { database } = instance();
expect(exists(database, "kv")).toBeTrue();
});
test("can use custom table", () => {
const { database } = instance("bacon");
expect(exists(database, "kv")).toBeFalse();
expect(exists(database, "bacon")).toBeTrue();
});
test("can add & remove data from custom table", () => {
const { kv } = instance("cool_kids_table");
const value = random();
kv.set("test", value);
expect(kv.get("test")).toBe(value);
kv.remove("test");
expect(kv.get("test")).toBeUndefined();
});
test("can update data from custom table", () => {
const { kv } = instance("cool_kids_table");
const value = random();
const updated = value + 5;
kv.set("test", value);
expect(kv.get("test")).toBe(value);
kv.set("test", updated);
expect(kv.get("test")).toBe(updated);
});