145 lines
3 KiB
TypeScript
145 lines
3 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { SchemaError } from "../src/error";
|
|
import { Prequel, Table } from "../src/index";
|
|
|
|
interface User {
|
|
user_id: number;
|
|
name: string;
|
|
}
|
|
|
|
test("constructor", () => {
|
|
const pq = new Prequel();
|
|
const users = pq.table<User>("Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
users.insertPartial({ name: "Yondu" });
|
|
|
|
expect(users.findAll().length).toBe(1);
|
|
});
|
|
|
|
test(".hasTable() knows if a table exists in the DB", () => {
|
|
const pq = new Prequel();
|
|
|
|
expect(pq.hasTable("Users")).toBeFalse();
|
|
expect(pq.hasTable("Notfound")).toBeFalse();
|
|
|
|
pq.table<User>("Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
expect(pq.hasTable("Users")).toBeTrue();
|
|
expect(pq.hasTable("Notfound")).toBeFalse();
|
|
});
|
|
|
|
test(".table() creates and attaches a table", () => {
|
|
const pq = new Prequel();
|
|
|
|
// Already has one table because of KV
|
|
expect(Object.keys(pq.tables)).toHaveLength(1);
|
|
|
|
const table = pq.table<User>("Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
expect(Object.keys(pq.tables)).toHaveLength(2);
|
|
expect(Object.values(pq.tables)).toContain(table);
|
|
});
|
|
|
|
test(".table() throws when creating a duplicate table", () => {
|
|
const pq = new Prequel();
|
|
pq.table<User>("Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
const oopsie = () => {
|
|
pq.table<User>("Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
};
|
|
|
|
expect(oopsie).toThrow(SchemaError);
|
|
});
|
|
|
|
test(".attachTable() attaches a table", () => {
|
|
const pq = new Prequel();
|
|
const table = new Table<User>(pq.db, "Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
expect(pq.hasTable("Users")).toBeFalse();
|
|
|
|
pq.attachTable(table);
|
|
|
|
expect(pq.hasTable("Users")).toBeTrue();
|
|
expect(Object.values(pq.tables)).toContain(table);
|
|
});
|
|
|
|
test(".attachTable() throws on duplicate table names", () => {
|
|
const pq = new Prequel();
|
|
pq.table<User>("Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
const secondTable = new Table<User>(pq.db, "Users", {
|
|
user_id: {
|
|
type: "INTEGER",
|
|
primary: true,
|
|
},
|
|
name: "TEXT",
|
|
});
|
|
|
|
const doAttachment = () => {
|
|
pq.attachTable(secondTable);
|
|
};
|
|
|
|
expect(doAttachment).toThrow(SchemaError);
|
|
});
|
|
|
|
test("kv store can set and retrieve arbitrary values", () => {
|
|
const pq = new Prequel();
|
|
|
|
pq.kv.put<number>("answer", 42);
|
|
pq.kv.put<{ enabled: boolean }>("config", { enabled: true });
|
|
pq.kv.put<string[]>("list", ["a", "b", "c"]);
|
|
|
|
expect(pq.kv.get<number>("answer")).toEqual(42);
|
|
expect(pq.kv.get<{ enabled: boolean }>("config")).toEqual({ enabled: true });
|
|
expect(pq.kv.get<string[]>("list")).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test(".query() creates a cached statement", () => {
|
|
const pq = new Prequel();
|
|
const query = pq.query("SELECT 1");
|
|
|
|
expect(query).toBeDefined();
|
|
expect(query).toBe(pq.query("SELECT 1"));
|
|
});
|