Files
bun-mariadb/tests/sql-values.test.ts
T
tbenandClaude Opus 5.5 ddf431e866 Add tests, schema sync safeguards, and query/handler improvements
- Add bun test suite: SQL generator and value unit tests, plus MariaDB
  integration tests gated on BUN_MARIADB_TEST_DB
- schema: add --dry-run and require confirmation (or --yes) before
  dropping tables/columns; block unapproved destructive statements
- Always AND targetId with the query, regardless of searchOperator
- Initialize lazily on first call; init throws instead of exiting
- Generate IN (?, ?, ...) lists for IN / NOT IN
- Share value binding between insert and update (booleans, embeddings,
  function values, "" -> NULL for non-text columns)
- Keep Buffers and convert BigInt/Date without a JSON round-trip
- DbSQL: report success for empty SELECT results
- Run update() in a transaction with row locking
- Key the client cache on the full connection config
- Consolidate identifier quoting, drop dead code, merge column lookup
- Read CLI version from package.json, pin @types/bun, chmod CLI on compile
- Rebuild dist

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 02:27:59 +01:00

56 lines
2.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import sqlInsertGenerator from "../src/utils/sql-insert-generator";
import sqlValuePlaceholder from "../src/utils/sql-value-placeholder";
describe("sqlValuePlaceholder", () => {
test("booleans become 0 / 1", () => {
expect(sqlValuePlaceholder({ value: false })).toEqual({ placeholder: "?", value: 0 });
expect(sqlValuePlaceholder({ value: true })).toEqual({ placeholder: "?", value: 1 });
});
test("0 is kept", () => {
expect(sqlValuePlaceholder({ value: 0 }).value).toBe(0);
});
test("embeddings use VEC_FromText", () => {
expect(sqlValuePlaceholder({ value: [0.1, 0.2] })).toEqual({
placeholder: "VEC_FromText(?)",
value: "[0.1,0.2]",
});
});
test("empty string: kept for text columns, NULL otherwise", () => {
expect(sqlValuePlaceholder({ value: "", field: { fieldName: "t", dataType: "VARCHAR" } }).value).toBe("");
expect(sqlValuePlaceholder({ value: "", field: { fieldName: "n", dataType: "INT" } }).value).toBeNull();
expect(sqlValuePlaceholder({ value: "" }).value).toBeNull();
});
test("function values are called once", () => {
let calls = 0;
const res = sqlValuePlaceholder({
value: () => {
calls++;
return { value: "POINT(1 2)", placeholder: "ST_GeomFromText(?)" };
},
});
expect(calls).toBe(1);
expect(res).toEqual({ placeholder: "ST_GeomFromText(?)", value: "POINT(1 2)" });
});
});
describe("sqlInsertGenerator", () => {
test("builds a multi-row insert with quoted identifiers", () => {
const res = sqlInsertGenerator({
tableName: "items",
data: [
{ name: "a", active: false, qty: 0 },
{ name: "b", emb: [1, 2] },
],
});
expect(res?.query).toBe(
"INSERT INTO `items` (`name`,`active`,`qty`,`emb`) VALUES (?,?,?,?),(?,?,?,VEC_FromText(?))",
);
expect(res?.values).toEqual(["a", 0, 0, null, "b", null, null, "[1,2]"]);
});
});