feat: make queues durable by default and add seed helpers
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import type { Db } from "../src/driver.ts";
|
||||
import { addSeedData, removeSeedData, runSeedQuery } from "../src/seed.ts";
|
||||
|
||||
function fakeDb() {
|
||||
const calls: Array<{ sql: string; parameters: unknown[] }> = [];
|
||||
const db = {
|
||||
exec: async (sql: string, parameters: unknown[] = []) => {
|
||||
calls.push({ sql, parameters });
|
||||
return { changes: 1 };
|
||||
},
|
||||
} as unknown as Db;
|
||||
return { db, calls };
|
||||
}
|
||||
|
||||
test("seed helpers parameterize object rows and removals", async () => {
|
||||
const { db, calls } = fakeDb();
|
||||
await addSeedData(db, [{ code: "free", credits: 10 }, { code: "pro", credits: 20 }], "plans", {
|
||||
conflict: "ignore",
|
||||
});
|
||||
await removeSeedData(db, { code: "free" }, "plans");
|
||||
expect(calls[0]).toEqual({
|
||||
sql: "INSERT OR IGNORE INTO plans (code, credits) VALUES (?, ?)",
|
||||
parameters: ["free", 10],
|
||||
});
|
||||
expect(calls[2]).toEqual({ sql: "DELETE FROM plans WHERE code = ?", parameters: ["free"] });
|
||||
});
|
||||
|
||||
test("custom seed queries stay parameterized and unsafe identifiers are refused", async () => {
|
||||
const { db, calls } = fakeDb();
|
||||
await runSeedQuery(db, "UPDATE plans SET credits = ? WHERE code = ?", [50, "free"]);
|
||||
expect(calls[0]?.parameters).toEqual([50, "free"]);
|
||||
await expect(addSeedData(db, { id: 1 }, "plans; DROP TABLE plans")).rejects.toThrow(
|
||||
"safe SQL identifier",
|
||||
);
|
||||
await expect(removeSeedData(db, {}, "plans")).rejects.toThrow("match fields");
|
||||
});
|
||||
Reference in New Issue
Block a user