Files
bun-sqlite/tests/sql-values.test.ts
tbenandClaude Opus 5.5 938a1da71d Add queue tables, quote SQL identifiers (v1.3.0)
- isQueue table option adds queue columns and a (status, available_at)
  index; BunSQLite.queue exposes add, claim, complete, fail and
  release_stale with atomic claims
- Set busy_timeout on connections so workers in separate processes wait
  instead of failing with SQLITE_BUSY
- Quote table and column names with backticks in generated SQL, so
  reserved words (to, order, group) work and unknown columns error
  instead of matching as string literals
- Tests for queue ops, multi-process claims and reserved-word names

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 16:29:14 +01:00

62 lines
1.8 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import sqlInsertGenerator, { sqlValue } from "../src/utils/sql-insert-generator";
describe("sqlValue", () => {
test("booleans become 0 / 1", () => {
expect(sqlValue(false)).toBe(0);
expect(sqlValue(true)).toBe(1);
});
test("0 and empty strings are kept", () => {
expect(sqlValue(0)).toBe(0);
expect(sqlValue("")).toBe("");
});
test("undefined becomes NULL", () => {
expect(sqlValue(undefined)).toBeNull();
});
});
describe("sqlInsertGenerator", () => {
test("builds a multi-row insert", () => {
const res = sqlInsertGenerator({
tableName: "items",
data: [
{ name: "a", active: false, qty: 0 },
{ name: "b", note: "" },
],
});
expect(res?.query).toBe(
"INSERT INTO `items` (`name`,`active`,`qty`,`note`) VALUES (?,?,?,?),(?,?,?,?)",
);
expect(res?.values).toEqual(["a", 0, 0, null, "b", null, null, ""]);
});
test("function values are called once", () => {
let calls = 0;
const res = sqlInsertGenerator({
tableName: "items",
data: [
{
emb: () => {
calls++;
return { value: "[1,2]", placeholder: "vec_f32(?)" };
},
},
],
});
expect(calls).toBe(1);
expect(res?.query).toBe("INSERT INTO `items` (`emb`) VALUES (vec_f32(?))");
expect(res?.values).toEqual(["[1,2]"]);
});
test("rejects injected column names", () => {
expect(() =>
sqlInsertGenerator({
tableName: "items",
data: [{ "name) VALUES (1); --": "x" }],
}),
).toThrow();
});
});