testTablesCheckTheirRows

tcmts.server.test.testTablesCheckTheirRows · #7vgqwozy · term

A table declared with the library's table knows its row type: rows are checked against it, found by their fields, updated atomically, and a duplicate of a unique field is a 409.

import type { HttpError } from "#codebase/tcmts/server";
import { pantry } from "#codebase/tcmts/server/test";
import assert from "node:assert/strict";

export const testTablesCheckTheirRows = async () => {
  const id = await pantry.insert({ item: "flour", count: 2 });
  await pantry.put("rice", { item: "rice", count: 1 });
  assert.deepEqual(await pantry.get(id), { item: "flour", count: 2 });
  assert.equal(await pantry.count({ item: "rice" }), 1);
  assert.deepEqual(
    (await pantry.find({}, { orderBy: "count" })).map((r) => r.value.item),
    ["rice", "flour"],
  );
  assert.equal((await pantry.update("rice", (r) => ({ ...r, count: r.count + 1 })))?.count, 2);
  await assert.rejects(
    pantry.put("x", { item: "salt" } as unknown as { item: string; count: number }),
    /table "server_test_pantry": /,
  );
  await assert.rejects(
    pantry.put("y", { item: "rice", count: 5 }),
    (e: HttpError) => e.status === 409,
  );
  assert.equal(await pantry.delete("rice"), true);
}

uses