From 91167dbea52e2110d7ca4e3eae0ff91e4160f9ba Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Fri, 13 Nov 2026 11:28:52 +0000 Subject: [PATCH] build(deps): adopt @antelopejs/interface-database 0.4.0 --- AGENTS.md | 2 +- README.md | 2 +- package.json | 2 +- pnpm-lock.yaml | 10 +- src/test-external/native.test.ts | 17 ++-- src/test-external/reads.test.ts | 62 +++++++---- src/test-external/refusals.test.ts | 16 +-- src/test-external/writes.test.ts | 158 ++++++++++++++++------------- src/test/atomic.test.ts | 127 +++++++++++++---------- src/test/contention.test.ts | 48 +++++---- src/test/duplicate-key.test.ts | 69 ++++++++----- src/test/indexes.test.ts | 27 ++--- src/test/nested-paths.test.ts | 53 ++++++---- src/test/typed-operations.test.ts | 133 ++++++++++++++---------- 14 files changed, 429 insertions(+), 297 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 6990575..fad4947 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,7 +11,7 @@ The PostgreSQL implementation of `@antelopejs/interface-database` and `@antelope ## Versioning -- Both implemented interfaces stay capped below the next minor (`interface-database` `>=0.3.3 <0.4.0`, `interface-database-catalog` `>=0.0.3 <0.1.0`), because a new minor can add required handlers. `pnpm lint` enforces it with `antelopejs-check-interface-ranges`. +- Both implemented interfaces stay capped below the next minor (`interface-database` `>=0.4.0 <0.5.0`, `interface-database-catalog` `>=0.0.3 <0.1.0`), because a new minor can add required handlers. `pnpm lint` enforces it with `antelopejs-check-interface-ranges`. - When a dependency range changes, update the compatibility table in `README.md` too. - A release that needs action from users gets an `### Upgrade notes` section in `CHANGELOG.md`, above the generated entries. diff --git a/README.md b/README.md index 15aa31a..cd8701d 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ This repository documents what is specific to the PostgreSQL backend: | Package | Supported | Tested with | | ---------------------------------------- | ---------------- | ----------- | | `@antelopejs/interface-core` | `>=0.1.1 <1.0.0` | `0.4.0` | -| `@antelopejs/interface-database` | `>=0.3.3 <0.4.0` | `0.3.3` | +| `@antelopejs/interface-database` | `>=0.4.0 <0.5.0` | `0.4.0` | | `@antelopejs/interface-database-catalog` | `>=0.0.3 <0.1.0` | `0.0.3` | diff --git a/package.json b/package.json index 85b60d9..97614b2 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ }, "dependencies": { "@antelopejs/interface-core": ">=0.1.1 <1.0.0", - "@antelopejs/interface-database": ">=0.3.3 <0.4.0", + "@antelopejs/interface-database": ">=0.4.0 <0.5.0", "@antelopejs/interface-database-catalog": ">=0.0.3 <0.1.0", "pg": "^8.22.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 81c4ab8..fc57c98 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,8 +12,8 @@ importers: specifier: '>=0.1.1 <1.0.0' version: 0.4.0 '@antelopejs/interface-database': - specifier: '>=0.3.3 <0.4.0' - version: 0.3.3(@antelopejs/interface-core@0.4.0) + specifier: '>=0.4.0 <0.5.0' + version: 0.4.0(@antelopejs/interface-core@0.4.0) '@antelopejs/interface-database-catalog': specifier: '>=0.0.3 <0.1.0' version: 0.0.3(@antelopejs/interface-core@0.4.0) @@ -94,8 +94,8 @@ packages: peerDependencies: '@antelopejs/interface-core': '>=0.2.4 <1.0.0' - '@antelopejs/interface-database@0.3.3': - resolution: {integrity: sha512-4NKCw+qc2BmisdPmh1i9ts6RHR6wOQz/TCnJtuwP8+a32PqTv2ZVZyHlLUX9RceKExqCQP6z90yXjtuqskRQVA==} + '@antelopejs/interface-database@0.4.0': + resolution: {integrity: sha512-cdVGd0qdHwkS3PHdbV9IJUyL3OoGjnmXoVbXdHPNmbwzmjUDkZQHFN2Y3b58SGsH11k3nS1EJLq0sTbKVhaKdQ==} peerDependencies: '@antelopejs/interface-core': '>=0.2.4 <1.0.0' @@ -2979,7 +2979,7 @@ snapshots: dependencies: '@antelopejs/interface-core': 0.4.0 - '@antelopejs/interface-database@0.3.3(@antelopejs/interface-core@0.4.0)': + '@antelopejs/interface-database@0.4.0(@antelopejs/interface-core@0.4.0)': dependencies: '@antelopejs/interface-core': 0.4.0 diff --git a/src/test-external/native.test.ts b/src/test-external/native.test.ts index 2bac7ac..4d8b51e 100644 --- a/src/test-external/native.test.ts +++ b/src/test-external/native.test.ts @@ -64,14 +64,15 @@ describe("an external key that its column cannot hold", () => { ["order_lines", '["x", "1"]'], ]) { it(`gets nothing for ${key} on ${table}, rather than failing the request`, async () => { - expect(await Table(table).get(key)).to.equal(undefined); + expect(await Table(table).get(key).run()).to.equal(undefined); }); } it("filters nothing for an enum label the type does not have", async () => { const total = await Table("accounts") .filter((row) => row.key("status").eq("archived")) - .count(); + .count() + .run(); expect(total).to.equal(0); }); }); @@ -117,7 +118,8 @@ describe("numbers read from an external relation's exact types", () => { it("sums a numeric column exactly", async () => { const total = await Table("accounts") .filter((row) => row.key("active").eq(true)) - .sum("revenue"); + .sum("revenue") + .run(); const [expected] = await Native<{ total: number }>( "SELECT sum(revenue)::double precision AS total FROM sales.accounts WHERE active", ); @@ -136,7 +138,8 @@ describe("numbers read from an external relation's exact types", () => { it(`keeps the meaning of '${operator} 5.5' on an integer column`, async () => { const total = await Table("accounts") .filter((row) => row.key("score")[method](5.5)) - .count(); + .count() + .run(); expect(total).to.equal( await NativeCount( `SELECT count(*) FROM sales.accounts WHERE score ${operator} 5.5`, @@ -162,10 +165,12 @@ describe("inequality against an external column's own type", () => { it(`answers ${field} ne ${JSON.stringify(value)} as the complement of eq, keeping NULL rows`, async () => { const unequal = await Table("flags") .filter((row) => row.key(field).ne(value)) - .count(); + .count() + .run(); const equal = await Table("flags") .filter((row) => row.key(field).eq(value)) - .count(); + .count() + .run(); expect(unequal).to.equal(expected); expect(unequal + equal).to.equal(2); }); diff --git a/src/test-external/reads.test.ts b/src/test-external/reads.test.ts index 0631abf..9324f49 100644 --- a/src/test-external/reads.test.ts +++ b/src/test-external/reads.test.ts @@ -43,7 +43,8 @@ describe("listing an external relation as a TableView does", () => { .filter((row) => row.key("status").eq("active")) .filter((row) => row.key("name").match("(?i)acct 1")) .slice(0, 5) - .pluck("_internal", "_id", "name", "status"); + .pluck("_internal", "_id", "name", "status") + .run(); const expected = await Native( `SELECT id::text AS "_id", name, status::text AS status FROM sales.accounts WHERE status = 'active' AND name ~* 'acct 1' ORDER BY name DESC LIMIT 5`, @@ -55,7 +56,8 @@ describe("listing an external relation as a TableView does", () => { const total = await Accounts() .filter((row) => row.key("status").eq("active")) .filter((row) => row.key("name").match("(?i)acct 1")) - .count(); + .count() + .run(); expect(total).to.equal( await NativeCount( "SELECT count(*) FROM sales.accounts WHERE status = 'active' AND name ~* 'acct 1'", @@ -67,7 +69,8 @@ describe("listing an external relation as a TableView does", () => { const total = await Accounts() .filter((row) => row.key("score").gt(97)) .filter((row) => row.key("revenue").ge(267000)) - .count(); + .count() + .run(); expect(total).to.equal( await NativeCount( "SELECT count(*) FROM sales.accounts WHERE score > 97 AND revenue >= 267000", @@ -91,7 +94,8 @@ describe("listing an external relation as a TableView does", () => { .ge(Day(day)) .and(row.key(column).lt(NextDay(day))), ) - .count(); + .count() + .run(); expect(total).to.equal( await NativeCount( `SELECT count(*) FROM sales.${table} @@ -106,7 +110,8 @@ describe("listing an external relation as a TableView does", () => { it("searches a date column through its ISO rendering, as a TableView search does", async () => { const total = await Accounts() .filter((row) => row.key("created_at").match("(?i)2026-01-02T0")) - .count(); + .count() + .run(); expect(total).to.equal(10); }); @@ -118,7 +123,8 @@ describe("listing an external relation as a TableView does", () => { .match("(?i)CHURN") .or(row.key("external_ref").match("(?i)^0")), ) - .count(); + .count() + .run(); expect(total).to.equal( await NativeCount( "SELECT count(*) FROM sales.accounts WHERE status::text ~* 'churn' OR external_ref::text ~* '^0'", @@ -135,7 +141,8 @@ describe("reading an external row by key", () => { it("gets a row by its bigint key as a string _id, with exact bigints and true instants", async () => { const row = await Accounts() .get("42") - .pluck("_id", "id", "big_counter", "signed_on", "last_seen"); + .pluck("_id", "id", "big_counter", "signed_on", "last_seen") + .run(); expect(row).to.deep.equal({ _id: "42", id: "42", @@ -149,7 +156,10 @@ describe("reading an external row by key", () => { const original = process.env.TZ; process.env.TZ = "America/New_York"; try { - const row = await Accounts().get("42").pluck("signed_on", "last_seen"); + const row = await Accounts() + .get("42") + .pluck("signed_on", "last_seen") + .run(); expect(row).to.deep.equal({ signed_on: new Date("2026-02-12T00:00:00.000Z"), last_seen: new Date("2026-01-01T00:42:00.000Z"), @@ -160,7 +170,7 @@ describe("reading an external row by key", () => { }); it("reads a whole row in row mode with _id first and every declared field", async () => { - const row = await Accounts().get("7"); + const row = await Accounts().get("7").run(); expect(Object.keys(row)[0]).to.equal("_id"); expect(row).to.include({ _id: "7", @@ -176,7 +186,7 @@ describe("reading an external row by key", () => { const [known] = await Native<{ id: string }>( "SELECT contact_id::text AS id FROM sales.contacts ORDER BY first_name LIMIT 1", ); - const rows = await Contacts().getAll([known.id, "not-a-uuid"], "_id"); + const rows = await Contacts().getAll([known.id, "not-a-uuid"], "_id").run(); expect(rows.map((row) => row._id)).to.deep.equal([known.id]); }); }); @@ -191,7 +201,8 @@ describe("reading across external relations", () => { .orderBy("first_name", "asc") .slice(0, 3) .lookup(Accounts().pluck("_internal", "_id", "name"), "account_id", "_id") - .pluck("first_name", "account_id"); + .pluck("first_name", "account_id") + .run(); const expected = await Native( `SELECT c.first_name, jsonb_build_object('_id', a.id::text, 'name', a.name) AS account_id FROM sales.contacts c JOIN sales.accounts a ON a.id = c.account_id @@ -216,7 +227,8 @@ describe("reading across external relations", () => { })) .group("bucket", (rows, group) => ({ x: group, y: rows.count() })) .orderBy("x", "asc") - .slice(0, 3); + .slice(0, 3) + .run(); const expected = await Native<{ x: string; y: string }>( `SELECT to_char(created_at AT TIME ZONE 'UTC' AT TIME ZONE '${PARIS}', 'YYYYMMDD') AS x, count(*) AS y FROM sales.contacts GROUP BY 1 ORDER BY 1 LIMIT 3`, @@ -227,11 +239,12 @@ describe("reading across external relations", () => { }); it("reads an undeclared key, such as the _internal data-api plucks, as absent", async () => { - const row = await Accounts().get("1").pluck("_internal", "name"); + const row = await Accounts().get("1").pluck("_internal", "name").run(); expect(row).to.deep.equal({ name: "Acct 1" }); const total = await Accounts() .filter((row) => row.key("nickname").eq("x")) - .count(); + .count() + .run(); expect(total).to.equal(0); }); }); @@ -246,17 +259,24 @@ describe("the _id of relations without a single-column primary key", () => { .instance() .table("order_lines") .orderBy("_id") - .slice(0, 1); + .slice(0, 1) + .run(); expect(first._id).to.equal('["0", "1"]'); const again = await sales .instance() .table("order_lines") - .get(String(first._id)); + .get(String(first._id)) + .run(); expect(again).to.deep.equal(first); }); it("answers _id from the unique index of a table without a primary key", async () => { - const row = await sales.instance().table("events").orderBy("at").nth(0); + const row = await sales + .instance() + .table("events") + .orderBy("at") + .nth(0) + .run(); if (row === null) throw new Error("the relation answered no row"); expect(row._id).to.equal(row.event_ref); }); @@ -264,10 +284,10 @@ describe("the _id of relations without a single-column primary key", () => { for (const relation of ["raw_log", "big_accounts", "daily_signups"]) { it(`counts ${relation}, which has no key, and its rows answer no _id`, async () => { const table = sales.instance().table(relation); - expect(await table.count()).to.equal( + expect(await table.count().run()).to.equal( await NativeCount(`SELECT count(*) FROM sales.${relation}`), ); - const [row] = await table.slice(0, 1); + const [row] = await table.slice(0, 1).run(); expect(row).to.not.have.property("_id"); }); } @@ -280,14 +300,14 @@ describe("instances over an external relation", () => { it("reads every row through CROSS_INSTANCE, since an external relation has no instances", async () => { expect( - await sales.instance(CROSS_INSTANCE).table("accounts").count(), + await sales.instance(CROSS_INSTANCE).table("accounts").count().run(), ).to.equal(20000); }); it("refuses a named instance rather than answer rows nobody scoped to it", async () => { let refusal: Error | undefined; try { - await sales.instance("tenant-1").table("accounts").count(); + await sales.instance("tenant-1").table("accounts").count().run(); } catch (err) { if (!(err instanceof Error)) throw err; refusal = err; diff --git a/src/test-external/refusals.test.ts b/src/test-external/refusals.test.ts index c010526..3527d21 100644 --- a/src/test-external/refusals.test.ts +++ b/src/test-external/refusals.test.ts @@ -73,7 +73,7 @@ describe("what an external relation refuses", () => { const expected = stage === "atomicMutation" ? stage : "may not write to"; it(`refuses ${stage} on a relation the connected role may not write, naming it, and writes nothing`, async () => { const message = await Refusal(async () => { - await write(); + await write().run(); }); expect(message).to.contain(expected).and.to.contain('"sales"."accounts"'); }); @@ -91,18 +91,18 @@ describe("what an external relation refuses", () => { it("refuses creating and destroying an instance", async () => { expect( await Refusal(async () => { - await sales.createInstance("t1"); + await sales.createInstance("t1").run(); }), ).to.contain("createInstance"); expect( await Refusal(async () => { - await sales.destroyInstance("t1"); + await sales.destroyInstance("t1").run(); }), ).to.contain("destroyInstance"); }); it("lists no instances", async () => { - expect(await sales.listInstances()).to.deep.equal([]); + expect(await sales.listInstances().run()).to.deep.equal([]); }); for (const { description, read } of [ @@ -121,14 +121,18 @@ describe("what an external relation refuses", () => { ]) { it(`refuses ${description} on a relation with no key, naming it`, async () => { const message = await Refusal(async () => { - await read(); + await read().run(); }); expect(message).to.contain('"sales"."raw_log"').and.to.contain("no _id"); }); } it("still reads a keyless relation by a field", async () => { - const rows = await sales.instance().table("raw_log").getAll(["r1"], "msg"); + const rows = await sales + .instance() + .table("raw_log") + .getAll(["r1"], "msg") + .run(); expect(rows).to.have.length(1); }); }); diff --git a/src/test-external/writes.test.ts b/src/test-external/writes.test.ts index bf61b55..858107d 100644 --- a/src/test-external/writes.test.ts +++ b/src/test-external/writes.test.ts @@ -86,9 +86,9 @@ describe("inserting into an external relation", () => { it("answers the database's generated keys in input order, each reading back its own row", async () => { const labels = Array.from({ length: 1000 }, (_, n) => `label-${999 - n}`); - const ids = await Table("w_serial").insert( - labels.map((label) => ({ label })), - ); + const ids = await Table("w_serial") + .insert(labels.map((label) => ({ label }))) + .run(); expect(ids).to.have.length(1000); const rows = await Native<{ id: string; label: string }>( "SELECT id::text AS id, label FROM sales.w_serial", @@ -98,8 +98,8 @@ describe("inserting into an external relation", () => { }); it("fills an omitted column with its DEFAULT and answers an identity key as _id", async () => { - const [id] = await Table("w_accounts").insert({ code: "d1" }); - const row = await Table("w_accounts").get(id); + const [id] = await Table("w_accounts").insert({ code: "d1" }).run(); + const row = await Table("w_accounts").get(id).run(); expect(row).to.include({ _id: id, id, @@ -111,22 +111,28 @@ describe("inserting into an external relation", () => { }); it("answers a key the database generates by default, and a list then shows the row under it", async () => { - const [account] = await Table("w_accounts").insert({ - code: "d2", - }); - const [sku] = await Table("w_items").insert({ - account_id: account, - qty: 3, - }); + const [account] = await Table("w_accounts") + .insert({ + code: "d2", + }) + .run(); + const [sku] = await Table("w_items") + .insert({ + account_id: account, + qty: 3, + }) + .run(); expect(sku).to.match(/^[0-9a-f-]{36}$/); - const listed = await Table("w_items").filter((row) => - row.key("account_id").eq(account), - ); + const listed = await Table("w_items") + .filter((row) => row.key("account_id").eq(account)) + .run(); expect(listed.map((row) => row._id)).to.deep.equal([sku]); }); it("writes a supplied composite _id into its key columns", async () => { - const ids = await Table("w_lines").insert({ _id: '["7", "2"]', qty: 1 }); + const ids = await Table("w_lines") + .insert({ _id: '["7", "2"]', qty: 1 }) + .run(); expect(ids).to.deep.equal(['["7", "2"]']); expect( await NativeCount( @@ -137,11 +143,13 @@ describe("inserting into an external relation", () => { it("refuses an _id and a key field that disagree, writing nothing", async () => { const failure = await Failure(async () => { - await Table("w_lines").insert({ - _id: '["8", "1"]', - order_id: 9, - line_no: 1, - }); + await Table("w_lines") + .insert({ + _id: '["8", "1"]', + order_id: 9, + line_no: 1, + }) + .run(); }); expect(failure.message).to.contain("order_id"); expect( @@ -164,7 +172,7 @@ describe("inserting into an external relation", () => { ] satisfies [string, Row, string][]) { it(`refuses ${description}, writing nothing`, async () => { const failure = await Failure(async () => { - await Table("w_accounts").insert(document); + await Table("w_accounts").insert(document).run(); }); expect(failure.message).to.contain(named); expect( @@ -177,20 +185,18 @@ describe("inserting into an external relation", () => { } it("upserts on the key: update keeps an unmentioned column, replace clears it", async () => { - await Table("w_lines").insert({ _id: '["20", "1"]', qty: 5 }); - await Table("w_lines").insert( - { _id: '["20", "1"]' }, - { conflict: "update" }, - ); + await Table("w_lines").insert({ _id: '["20", "1"]', qty: 5 }).run(); + await Table("w_lines") + .insert({ _id: '["20", "1"]' }, { conflict: "update" }) + .run(); expect( await NativeCount( "SELECT qty AS count FROM sales.w_lines WHERE order_id = 20", ), ).to.equal(5); - await Table("w_lines").insert( - { order_id: 20, line_no: 1 }, - { conflict: "replace" }, - ); + await Table("w_lines") + .insert({ order_id: 20, line_no: 1 }, { conflict: "replace" }) + .run(); const [row] = await Native<{ qty: number | null }>( "SELECT qty FROM sales.w_lines WHERE order_id = 20", ); @@ -216,7 +222,7 @@ describe("values an external column cannot hold", () => { for (const [table, document, field] of INVALID_VALUES) { it(`refuses ${JSON.stringify(document[field])} for ${table}.${field} before writing, naming the field`, async () => { const failure = await WriteFailure(async () => { - await Table(table).insert(document); + await Table(table).insert(document).run(); }); expect(failure.kind).to.equal("invalid"); expect(failure.message).to.contain(`sales.${table}.${field}`); @@ -236,19 +242,20 @@ describe("writing back what was read from an external relation", () => { it("changes no byte of what a lossy read cannot spell, and counts no changed row", async () => { const id = await Seeded("r1"); const before = await Stored(id); - const stored = await Table("w_accounts").get(id); - expect(await Table("w_accounts").get(id).update(stored)).to.equal(0); + const stored = await Table("w_accounts").get(id).run(); + expect(await Table("w_accounts").get(id).update(stored).run()).to.equal(0); expect(await Stored(id)).to.deep.equal(before); }); it("writes the one field an edit changed and keeps every other stored value", async () => { const id = await Seeded("r2"); const before = await Stored(id); - const stored = await Table("w_accounts").get(id); + const stored = await Table("w_accounts").get(id).run(); expect( await Table("w_accounts") .get(id) - .update({ ...stored, code: "r2b" }), + .update({ ...stored, code: "r2b" }) + .run(), ).to.equal(1); expect(await Stored(id)).to.deep.equal({ ...before, @@ -261,10 +268,13 @@ describe("writing back what was read from an external relation", () => { const id = await Seeded("r3"); const other = await Seeded("r4"); expect( - await Table("w_accounts").get(id).update({ _id: other, code: "zz" }), + await Table("w_accounts") + .get(id) + .update({ _id: other, code: "zz" }) + .run(), ).to.equal(0); expect( - await Table("w_accounts").get(id).update({ id: other, code: "zz" }), + await Table("w_accounts").get(id).update({ id: other, code: "zz" }).run(), ).to.equal(0); expect((await Stored(id)).code).to.equal("r3"); }); @@ -274,9 +284,9 @@ describe("writing back what was read from an external relation", () => { await Native("UPDATE sales.w_accounts SET email = 'a@b.c' WHERE id = $1", [ id, ]); - expect(await Table("w_accounts").get(id).replace({ code: "r5b" })).to.equal( - 1, - ); + expect( + await Table("w_accounts").get(id).replace({ code: "r5b" }).run(), + ).to.equal(1); const [row] = await Native( "SELECT email, code, upper_code, status::text AS status FROM sales.w_accounts WHERE id = $1", [id], @@ -312,12 +322,13 @@ describe("editing through a key field read lossily, as data-api's edit sends it" ]) { it(`edits ${table}, whose key field is not read as the key's text, and answers 1`, async () => { await Native(seed); - const stored = await Table(table).get(id); + const stored = await Table(table).get(id).run(); expect(stored._id).to.equal(id); expect( await Table(table) .get(id) - .update({ ...stored, [field]: "b" }), + .update({ ...stored, [field]: "b" }) + .run(), ).to.equal(1); expect( await NativeCount( @@ -335,15 +346,18 @@ describe("selecting the rows an external write changes", () => { }); it("updates rows selected by composite _id", async () => { - await Table("w_lines").insert([ - { _id: '["30", "1"]', qty: 1 }, - { _id: '["30", "2"]', qty: 1 }, - { _id: '["30", "3"]', qty: 1 }, - ]); + await Table("w_lines") + .insert([ + { _id: '["30", "1"]', qty: 1 }, + { _id: '["30", "2"]', qty: 1 }, + { _id: '["30", "3"]', qty: 1 }, + ]) + .run(); expect( await Table("w_lines") .getAll(['["30", "1"]', '["30", "3"]']) - .update({ qty: 9 }), + .update({ qty: 9 }) + .run(), ).to.equal(2); expect( await NativeCount( @@ -355,11 +369,12 @@ describe("selecting the rows an external write changes", () => { it("deletes by _id and by filter, answering the count", async () => { const id = await Seeded("r6"); await Seeded("r7"); - expect(await Table("w_accounts").get(id).delete()).to.equal(1); + expect(await Table("w_accounts").get(id).delete().run()).to.equal(1); expect( await Table("w_accounts") .filter((row) => row.key("code").eq("r7")) - .delete(), + .delete() + .run(), ).to.equal(1); expect( await NativeCount( @@ -405,20 +420,23 @@ describe("selecting the rows an external write changes", () => { const failure = await Failure(async () => { await Table("raw_log") .filter((row) => row.key("msg").eq("r1")) - .delete(); + .delete() + .run(); }); expect(failure.message).to.contain('"sales"."raw_log"'); }); it("still refuses atomicMutation, naming the stage", async () => { const failure = await Failure(async () => { - await Table("w_accounts").atomicMutation("1", { - type: "update", - revisionField: "code", - expectedRevision: "a", - nextRevision: "b", - patch: {}, - }); + await Table("w_accounts") + .atomicMutation("1", { + type: "update", + revisionField: "code", + expectedRevision: "a", + nextRevision: "b", + patch: {}, + }) + .run(); }); expect(failure.message).to.contain("atomicMutation"); }); @@ -431,9 +449,9 @@ describe("what the database refuses on a write to an external relation", () => { }); it("reports a duplicate value of a unique constraint", async () => { - await Table("w_accounts").insert({ code: "e1" }); + await Table("w_accounts").insert({ code: "e1" }).run(); const failure = await WriteFailure(async () => { - await Table("w_accounts").insert({ code: "e1" }); + await Table("w_accounts").insert({ code: "e1" }).run(); }); expect(failure.kind).to.equal("duplicate"); expect(failure.constraint).to.equal("w_accounts_code_key"); @@ -442,7 +460,7 @@ describe("what the database refuses on a write to an external relation", () => { it("reports a missing required field, naming it", async () => { const failure = await WriteFailure(async () => { - await Table("w_items").insert({ qty: null }); + await Table("w_items").insert({ qty: null }).run(); }); expect(failure.kind).to.equal("missing"); expect(failure.fields).to.deep.equal(["qty"]); @@ -450,25 +468,27 @@ describe("what the database refuses on a write to an external relation", () => { it("reports a reference to a row that does not exist", async () => { const failure = await WriteFailure(async () => { - await Table("w_items").insert({ account_id: "999999", qty: 1 }); + await Table("w_items").insert({ account_id: "999999", qty: 1 }).run(); }); expect(failure.kind).to.equal("reference"); }); it("reports deleting a row that is still referenced", async () => { - const [account] = await Table("w_accounts").insert({ - code: "e2", - }); - await Table("w_items").insert({ account_id: account, qty: 1 }); + const [account] = await Table("w_accounts") + .insert({ + code: "e2", + }) + .run(); + await Table("w_items").insert({ account_id: account, qty: 1 }).run(); const failure = await WriteFailure(async () => { - await Table("w_accounts").get(account).delete(); + await Table("w_accounts").get(account).delete().run(); }); expect(failure.kind).to.equal("referenced"); }); it("reports a broken check constraint", async () => { const failure = await WriteFailure(async () => { - await Table("w_accounts").insert({ code: "e3", revenue: -1 }); + await Table("w_accounts").insert({ code: "e3", revenue: -1 }).run(); }); expect(failure.kind).to.equal("check"); expect(failure.constraint).to.equal("w_accounts_revenue_check"); diff --git a/src/test/atomic.test.ts b/src/test/atomic.test.ts index 81d300e..5f019be 100644 --- a/src/test/atomic.test.ts +++ b/src/test/atomic.test.ts @@ -35,23 +35,25 @@ const update: AtomicUpdate = { }; async function Seed(document: Partial = {}) { - await table.insert({ _id: ID, value: "before", ...document }); + await table.insert({ _id: ID, value: "before", ...document }).run(); } describe("atomic mutation results and scope", () => { beforeEach(async () => { - await table.delete(); + await table.delete().run(); }); it("persists inserted overflow revisions and replaces nested fields as a whole", async () => { await Seed({ revision: "old", details: { removed: true } }); - expect((await table.get(ID))?.revision).to.equal("old"); - expect(await table.atomicMutation(ID, update)).to.equal("applied"); - expect(await table.get(ID)).to.include({ + expect((await table.get(ID).run())?.revision).to.equal("old"); + expect(await table.atomicMutation(ID, update).run()).to.equal("applied"); + expect(await table.get(ID).run()).to.include({ revision: "fresh", value: "after", }); - const rows = await table.filter((row) => row.key("revision").eq("fresh")); + const rows = await table + .filter((row) => row.key("revision").eq("fresh")) + .run(); expect(rows).to.have.length(1); expect(rows[0].revision).to.equal("fresh"); expect(rows[0].details).to.deep.equal({ kept: true }); @@ -62,12 +64,16 @@ describe("atomic mutation results and scope", () => { ...update, expectedRevision: { kind: "missing" } as const, }; - expect(await table.atomicMutation(ID, request)).to.equal("not-applied"); + expect(await table.atomicMutation(ID, request).run()).to.equal( + "not-applied", + ); await Seed({ revision: null }); - expect(await table.atomicMutation(ID, request)).to.equal("not-applied"); - await table.get(ID).update({ revision: undefined }); - expect(await table.atomicMutation(ID, request)).to.equal("applied"); - expect((await table.get(ID))?.revision).to.equal("fresh"); + expect(await table.atomicMutation(ID, request).run()).to.equal( + "not-applied", + ); + await table.get(ID).update({ revision: undefined }).run(); + expect(await table.atomicMutation(ID, request).run()).to.equal("applied"); + expect((await table.get(ID).run())?.revision).to.equal("fresh"); }); it("rejects missing-revision bootstrap on declared columns", async () => { @@ -78,7 +84,8 @@ describe("atomic mutation results and scope", () => { .atomicMutation(ID, { ...update, expectedRevision: { kind: "missing" }, - }); + }) + .run(); expect.fail("expected unsupported bootstrap"); } catch (error) { expect(String(error)).to.include( @@ -89,9 +96,9 @@ describe("atomic mutation results and scope", () => { it("supports the default instance and refuses cross-instance mutation", async () => { const defaults = schema.instance().table("records"); - await defaults.delete(); - await defaults.insert({ _id: ID, revision: "old" }); - expect(await defaults.atomicMutation(ID, update)).to.equal("applied"); + await defaults.delete().run(); + await defaults.insert({ _id: ID, revision: "old" }).run(); + expect(await defaults.atomicMutation(ID, update).run()).to.equal("applied"); expect(() => schema .instance(CROSS_INSTANCE) @@ -103,59 +110,71 @@ describe("atomic mutation results and scope", () => { describe("atomic scalar equality deletes", () => { beforeEach(async () => { - await table.delete(); + await table.delete().run(); }); it("compares declared dates by instant and leaves a changed date untouched", async () => { const declared = schema.instance().table("declared"); - await declared.delete(); + await declared.delete().run(); const updatedAt = new Date("2026-01-05T10:00:00+02:00"); - await declared.insert({ _id: ID, updatedAt }); + await declared.insert({ _id: ID, updatedAt }).run(); expect( - await declared.atomicMutation(ID, { - type: "deleteIfEqual", - field: "updatedAt", - expectedValue: new Date("2026-01-05T08:00:01Z"), - }), + await declared + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "updatedAt", + expectedValue: new Date("2026-01-05T08:00:01Z"), + }) + .run(), ).to.equal("not-applied"); expect( - await declared.atomicMutation(ID, { - type: "deleteIfEqual", - field: "updatedAt", - expectedValue: new Date("2026-01-05T08:00:00Z"), - }), + await declared + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "updatedAt", + expectedValue: new Date("2026-01-05T08:00:00Z"), + }) + .run(), ).to.equal("applied"); }); it("preserves scalar types in overflow equality and ignores missing fields", async () => { await Seed({ attempts: 7, active: true }); expect( - await table.atomicMutation(ID, { - type: "deleteIfEqual", - field: "updatedAt", - expectedValue: new Date(), - }), + await table + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "updatedAt", + expectedValue: new Date(), + }) + .run(), ).to.equal("not-applied"); expect( - await table.atomicMutation(ID, { - type: "deleteIfEqual", - field: "attempts", - expectedValue: "7", - }), + await table + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "attempts", + expectedValue: "7", + }) + .run(), ).to.equal("not-applied"); expect( - await table.atomicMutation(ID, { - type: "deleteIfEqual", - field: "active", - expectedValue: false, - }), + await table + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "active", + expectedValue: false, + }) + .run(), ).to.equal("not-applied"); expect( - await table.atomicMutation(ID, { - type: "deleteIfEqual", - field: "attempts", - expectedValue: 7, - }), + await table + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "attempts", + expectedValue: 7, + }) + .run(), ).to.equal("applied"); }); @@ -163,11 +182,13 @@ describe("atomic scalar equality deletes", () => { const updatedAt = new Date("2026-02-12T03:04:05Z"); await Seed({ updatedAt }); expect( - await table.atomicMutation(ID, { - type: "deleteIfEqual", - field: "updatedAt", - expectedValue: updatedAt, - }), + await table + .atomicMutation(ID, { + type: "deleteIfEqual", + field: "updatedAt", + expectedValue: updatedAt, + }) + .run(), ).to.equal("applied"); }); }); diff --git a/src/test/contention.test.ts b/src/test/contention.test.ts index 9a8bcd0..93b8054 100644 --- a/src/test/contention.test.ts +++ b/src/test/contention.test.ts @@ -101,12 +101,14 @@ async function Compete(query: StagedObject, winner?: StagedObject) { describe("legacy conditional writes under contention", () => { beforeEach(async () => { - await table.delete(); - await table.insert({ - _id: ID, - revision: "old", - value: "initial", - }); + await table.delete().run(); + await table + .insert({ + _id: ID, + revision: "old", + value: "initial", + }) + .run(); }); it("reproduces two different patches matching one stale revision", async () => { @@ -116,7 +118,7 @@ describe("legacy conditional writes under contention", () => { .update({ revision: "next", value: "writer-b" }), ); expect(result).to.equal(1); - expect((await table.get(ID))?.value).to.equal("writer-b"); + expect((await table.get(ID).run())?.value).to.equal("writer-b"); }); it("reproduces a stale conditional delete removing an updated row", async () => { @@ -124,18 +126,20 @@ describe("legacy conditional writes under contention", () => { table.filter((row) => row.key("revision").eq("old")).delete(), ); expect(result).to.equal(1); - expect(await table.get(ID)).to.equal(undefined); + expect(await table.get(ID).run()).to.equal(undefined); }); }); describe("atomic mutations under contention", () => { beforeEach(async () => { - await table.delete(); - await table.insert({ - _id: ID, - revision: "old", - value: "initial", - }); + await table.delete().run(); + await table + .insert({ + _id: ID, + revision: "old", + value: "initial", + }) + .run(); }); function Winner() { @@ -157,7 +161,7 @@ describe("atomic mutations under contention", () => { patch: { value: "writer-b" }, }); expect(await Compete(second, Winner())).to.equal("not-applied"); - const record = await table.get(ID); + const record = await table.get(ID).run(); expect(record?.revision).to.equal("next-a"); expect(record?.value).to.equal("writer-a"); }); @@ -169,7 +173,7 @@ describe("atomic mutations under contention", () => { expectedRevision: "old", }); expect(await Compete(second, Winner())).to.equal("not-applied"); - expect((await table.get(ID))?.value).to.equal("writer-a"); + expect((await table.get(ID).run())?.value).to.equal("writer-a"); }); it("rechecks scalar equality after a concurrent update", async () => { @@ -179,13 +183,13 @@ describe("atomic mutations under contention", () => { expectedValue: "initial", }); expect(await Compete(second, Winner())).to.equal("not-applied"); - expect((await table.get(ID))?.value).to.equal("writer-a"); + expect((await table.get(ID).run())?.value).to.equal("writer-a"); }); it("allows only one bootstrap of a missing overflow revision", async () => { const overflow = schema.instance("tenant").table("overflow"); - await overflow.delete(); - await overflow.insert({ _id: ID, value: "initial" }); + await overflow.delete().run(); + await overflow.insert({ _id: ID, value: "initial" }).run(); const request = { type: "update", revisionField: "revision", @@ -200,8 +204,8 @@ describe("atomic mutations under contention", () => { patch: { value: "writer-b" }, }); expect(await Compete(second, first)).to.equal("not-applied"); - expect((await overflow.get(ID))?.revision).to.equal("next-a"); - expect((await overflow.get(ID))?.value).to.equal("writer-a"); + expect((await overflow.get(ID).run())?.revision).to.equal("next-a"); + expect((await overflow.get(ID).run())?.value).to.equal("writer-a"); }); it("reports unknown when the committed response is lost without retrying", async () => { @@ -218,6 +222,6 @@ describe("atomic mutations under contention", () => { ); expect(outcome).to.equal("unknown"); expect(calls).to.equal(1); - expect((await table.get(ID))?.revision).to.equal("next-a"); + expect((await table.get(ID).run())?.revision).to.equal("next-a"); }); }); diff --git a/src/test/duplicate-key.test.ts b/src/test/duplicate-key.test.ts index 32b0bcc..89662c6 100644 --- a/src/test/duplicate-key.test.ts +++ b/src/test/duplicate-key.test.ts @@ -2,6 +2,7 @@ import { expect } from "chai"; import { DuplicateKeyError, IsDuplicateKeyError, + type Query as RecordedQuery, } from "@antelopejs/interface-database"; import { Schema, @@ -44,8 +45,8 @@ async function Fresh(schemaId: string): Promise> { ); const schema = new Schema(schemaId, DEFINITION); await WaitForIndexes(schemaId); - await schema.createInstance(ALPHA); - await schema.createInstance(BETA); + await schema.createInstance(ALPHA).run(); + await schema.createInstance(BETA).run(); return schema; } @@ -58,9 +59,9 @@ function Row(id: string, email: string, seat: number): Account { } /** The error a write was refused with, failing the test when it was not refused. */ -async function Refusal(write: PromiseLike): Promise { +async function Refusal(write: RecordedQuery): Promise { try { - await write; + await write.run(); } catch (err) { if (!(err instanceof Error)) throw err; return err; @@ -69,7 +70,7 @@ async function Refusal(write: PromiseLike): Promise { } async function expectDuplicate( - write: PromiseLike, + write: RecordedQuery, index: string, fields: string[], ): Promise { @@ -89,7 +90,9 @@ describe("duplicate keys", () => { }); it("refuses a second row with the same _id as a duplicate of _id", async () => { - await Accounts(schema).insert(Row("a", "a@x", 1)); + await Accounts(schema) + .insert(Row("a", "a@x", 1)) + .run(); await expectDuplicate( Accounts(schema).insert(Row("a", "other@x", 2)), "_id", @@ -98,14 +101,20 @@ describe("duplicate keys", () => { }); it("upserts on _id under a conflict mode instead of refusing", async () => { - await Accounts(schema).insert(Row("a", "a@x", 1)); - await Accounts(schema).insert(Row("a", "b@x", 2), { conflict: "update" }); - const row = await Accounts(schema).get("a"); + await Accounts(schema) + .insert(Row("a", "a@x", 1)) + .run(); + await Accounts(schema) + .insert(Row("a", "b@x", 2), { conflict: "update" }) + .run(); + const row = await Accounts(schema).get("a").run(); expect(row?.email).to.equal("b@x"); }); it("refuses an insert that clashes on a unique secondary index, conflict mode or not", async () => { - await Accounts(schema).insert(Row("a", "a@x", 1)); + await Accounts(schema) + .insert(Row("a", "a@x", 1)) + .run(); await expectDuplicate( Accounts(schema).insert(Row("b", "a@x", 2)), "email", @@ -116,11 +125,13 @@ describe("duplicate keys", () => { "email", ["email"], ); - expect(await Accounts(schema).count()).to.equal(1); + expect(await Accounts(schema).count().run()).to.equal(1); }); it("refuses an update and a replace that clash on a unique index", async () => { - await Accounts(schema).insert([Row("a", "a@x", 1), Row("b", "b@x", 2)]); + await Accounts(schema) + .insert([Row("a", "a@x", 1), Row("b", "b@x", 2)]) + .run(); await expectDuplicate( Accounts(schema).get("b").update({ email: "a@x" }), "email", @@ -133,19 +144,25 @@ describe("duplicate keys", () => { "teamSeat", ["team", "seat"], ); - const row = await Accounts(schema).get("b"); + const row = await Accounts(schema).get("b").run(); expect(row).to.include({ email: "b@x", seat: 2 }); }); it("refuses a compound key only when every field matches", async () => { - await Accounts(schema).insert(Row("a", "a@x", 1)); - await Accounts(schema).insert(Row("b", "b@x", 2)); - await Accounts(schema).insert({ - _id: "c", - email: "c@x", - team: "ops", - seat: 1, - }); + await Accounts(schema) + .insert(Row("a", "a@x", 1)) + .run(); + await Accounts(schema) + .insert(Row("b", "b@x", 2)) + .run(); + await Accounts(schema) + .insert({ + _id: "c", + email: "c@x", + team: "ops", + seat: 1, + }) + .run(); await expectDuplicate( Accounts(schema).insert(Row("d", "d@x", 1)), "teamSeat", @@ -154,8 +171,12 @@ describe("duplicate keys", () => { }); it("accepts the same key in two instances, crossInstance included", async () => { - await Accounts(schema, ALPHA).insert(Row("a", "a@x", 1)); - await Accounts(schema, BETA).insert(Row("a", "a@x", 1)); - expect(await Accounts(schema, BETA).count()).to.equal(1); + await Accounts(schema, ALPHA) + .insert(Row("a", "a@x", 1)) + .run(); + await Accounts(schema, BETA) + .insert(Row("a", "a@x", 1)) + .run(); + expect(await Accounts(schema, BETA).count().run()).to.equal(1); }); }); diff --git a/src/test/indexes.test.ts b/src/test/indexes.test.ts index 39ec93c..5e31c54 100644 --- a/src/test/indexes.test.ts +++ b/src/test/indexes.test.ts @@ -180,13 +180,13 @@ async function ReleaseWrites(client: PoolClient): Promise { async function Seed(schema: Schema, rowsPerInstance: number) { for (const instance of INSTANCES) { - await schema.createInstance(instance); + await schema.createInstance(instance).run(); const rows = Array.from({ length: rowsPerInstance }, (_, n) => ({ _id: `${instance}-${n}`, sku: `sku-${n}`, price: n, })); - await schema.instance(instance).table("items").insert(rows); + await schema.instance(instance).table("items").insert(rows).run(); } } @@ -199,13 +199,14 @@ function Lookup(schema: Schema, instance: InstanceId) { } async function ReadEverything(schema: Schema): Promise { - const scoped = await Lookup(schema, SCOPED_INSTANCE); - const crossInstance = await Lookup(schema, CROSS_INSTANCE); + const scoped = await Lookup(schema, SCOPED_INSTANCE).run(); + const crossInstance = await Lookup(schema, CROSS_INSTANCE).run(); const ordered = await schema .instance(CROSS_INSTANCE) .table("items") .orderBy("price") - .slice(0, INSTANCES.length); + .slice(0, INSTANCES.length) + .run(); return { scoped: [...scoped].sort(ById), crossInstance: [...crossInstance].sort(ById), @@ -405,9 +406,9 @@ describe("cross-instance reads of secondary indexes", () => { expect(scopedPlan).to.match(/items_sku\b/); expect(scopedPlan).to.not.contain("items_sku__g"); - const crossInstance = await Lookup(schema, CROSS_INSTANCE); + const crossInstance = await Lookup(schema, CROSS_INSTANCE).run(); const perInstance = await Promise.all( - INSTANCES.map((instance) => Lookup(schema, instance)), + INSTANCES.map((instance) => Lookup(schema, instance).run()), ); expect([...crossInstance].sort(ById)).to.deep.equal( perInstance.flat().sort(ById), @@ -424,12 +425,12 @@ describe("cross-instance reads of secondary indexes", () => { const scoped = schema.instance(SCOPED_INSTANCE).table("items"); const warnings = await CaptureWarnings(async () => { - await crossInstance.getAll(1, "price"); - await crossInstance.getAll(2, "price"); - await crossInstance.orderBy("price"); - await crossInstance.getAll(LOOKED_UP_SKU, "sku"); - await crossInstance.getAll(LOOKED_UP_SKU, "undeclared"); - await scoped.getAll(1, "price"); + await crossInstance.getAll(1, "price").run(); + await crossInstance.getAll(2, "price").run(); + await crossInstance.orderBy("price").run(); + await crossInstance.getAll(LOOKED_UP_SKU, "sku").run(); + await crossInstance.getAll(LOOKED_UP_SKU, "undeclared").run(); + await scoped.getAll(1, "price").run(); }); expect(warnings).to.have.lengthOf(1); diff --git a/src/test/nested-paths.test.ts b/src/test/nested-paths.test.ts index 451f892..bc75abf 100644 --- a/src/test/nested-paths.test.ts +++ b/src/test/nested-paths.test.ts @@ -105,7 +105,7 @@ function Table(table: K) { } async function Ids(stream: Stream): Promise { - return (await stream).map((book) => book._id); + return (await stream.run()).map((book) => book._id); } async function LookedUp( @@ -114,7 +114,8 @@ async function LookedUp( ): Promise<(string | undefined)[]> { const reviews = await Table("reviews") .orderBy("_id") - .lookup(Table(table), "bookTitle", otherKey); + .lookup(Table(table), "bookTitle", otherKey) + .run(); return reviews.map((review) => review.bookTitle?._id); } @@ -150,7 +151,7 @@ async function PlanOverIndexes(stream: Stream): Promise { async function InsertRejected(book: Book): Promise { try { - await Table("booksIndexed").insert(book); + await Table("booksIndexed").insert(book).run(); } catch (error) { if (IsDuplicateKeyError(error)) { return error; @@ -160,24 +161,27 @@ async function InsertRejected(book: Book): Promise { return expect.fail(`the insert of ${book._id} was not refused`); } +/** Recreates the schema and seeds every table the suite reads. */ +async function SeedTables(): Promise { + await Query( + `DROP SCHEMA IF EXISTS ${QuoteIdentifier(SchemaName(SCHEMA_ID))} CASCADE`, + ); + schema = new Schema(SCHEMA_ID, DEFINITION); + await WaitForIndexes(SCHEMA_ID); + await schema.createInstance(INSTANCE).run(); + await Table("books").insert(BOOKS).run(); + await Table("booksIndexed").insert(BOOKS).run(); + await Table("events").insert(EVENTS).run(); + await Table("reviews") + .insert(REVIEWED.map((bookTitle, n) => ({ _id: `review-${n}`, bookTitle }))) + .run(); +} + describe("dotted names read nested paths", () => { - before(async () => { - await Query( - `DROP SCHEMA IF EXISTS ${QuoteIdentifier(SchemaName(SCHEMA_ID))} CASCADE`, - ); - schema = new Schema(SCHEMA_ID, DEFINITION); - await WaitForIndexes(SCHEMA_ID); - await schema.createInstance(INSTANCE); - await Table("books").insert(BOOKS); - await Table("booksIndexed").insert(BOOKS); - await Table("events").insert(EVENTS); - await Table("reviews").insert( - REVIEWED.map((bookTitle, n) => ({ _id: `review-${n}`, bookTitle })), - ); - }); + before(SeedTables); after(async () => { - await schema.destroyInstance(INSTANCE); + await schema.destroyInstance(INSTANCE).run(); }); it("orderBy sorts by a path below a declared field", async () => { @@ -247,7 +251,7 @@ describe("dotted names read nested paths", () => { }); it("an index over a date below a declared field builds, refuses a duplicate, and the sort reads dates", async () => { - const sorted = await Table("events").orderBy("meta.at"); + const sorted = await Table("events").orderBy("meta.at").run(); expect(sorted.map((event) => event._id)).to.deep.equal([ "event-1", "event-2", @@ -255,7 +259,9 @@ describe("dotted names read nested paths", () => { ]); let refused: unknown; try { - await Table("events").insert({ ...EVENTS[0], _id: "event-copy" }); + await Table("events") + .insert({ ...EVENTS[0], _id: "event-copy" }) + .run(); } catch (error) { refused = error; } @@ -267,9 +273,12 @@ describe("dotted names read nested paths", () => { _id, meta: { at: null }, })); - await Table("events").insert(unset); + await Table("events").insert(unset).run(); expect( - await Table("events").getAll(["event-null-0", "event-null-1"]).count(), + await Table("events") + .getAll(["event-null-0", "event-null-1"]) + .count() + .run(), ).to.equal(2); }); diff --git a/src/test/typed-operations.test.ts b/src/test/typed-operations.test.ts index a145941..55c488e 100644 --- a/src/test/typed-operations.test.ts +++ b/src/test/typed-operations.test.ts @@ -40,112 +40,139 @@ function Rows() { return schema.instance(INSTANCE).table("rows"); } +/** Creates the instance and seeds the one row the suite reads. */ +async function SeedRow(): Promise { + await schema.createInstance(INSTANCE).run(); + await Rows() + .insert({ + _id: "ada", + name: "ada", + suffix: "!", + tags: ["a"], + more: ["b"], + age: 36, + bornAt: new Date(BORN_AT), + field: "name", + low: 0, + high: 10_000, + }) + .run(); +} + describe("the operations interface-database 0.3.0 types", () => { - before(async () => { - await schema.createInstance(INSTANCE).run(); - await Rows() - .insert({ - _id: "ada", - name: "ada", - suffix: "!", - tags: ["a"], - more: ["b"], - age: 36, - bornAt: new Date(BORN_AT), - field: "name", - low: 0, - high: 10_000, - }) - .run(); - }); + before(SeedRow); after(async () => { await schema.destroyInstance(INSTANCE).run(); }); it("add concatenates a string with a constant or a field", async () => { - assert.deepEqual(await Rows().map((row) => row.key("name").add("!")), [ - "ada!", - ]); assert.deepEqual( - await Rows().map((row) => row.key("name").add(row.key("suffix"))), + await Rows() + .map((row) => row.key("name").add("!")) + .run(), + ["ada!"], + ); + assert.deepEqual( + await Rows() + .map((row) => row.key("name").add(row.key("suffix"))) + .run(), ["ada!"], ); }); it("add concatenates a list with a constant or a field", async () => { - assert.deepEqual(await Rows().map((row) => row.key("tags").add(["x"])), [ - ["a", "x"], - ]); assert.deepEqual( - await Rows().map((row) => row.key("tags").add(row.key("more"))), + await Rows() + .map((row) => row.key("tags").add(["x"])) + .run(), + [["a", "x"]], + ); + assert.deepEqual( + await Rows() + .map((row) => row.key("tags").add(row.key("more"))) + .run(), [["a", "b"]], ); }); it("add still adds two numbers, constant or field", async () => { - assert.deepEqual(await Rows().map((row) => row.key("age").add(1)), [37]); assert.deepEqual( - await Rows().map((row) => row.key("age").add(row.key("age"))), + await Rows() + .map((row) => row.key("age").add(1)) + .run(), + [37], + ); + assert.deepEqual( + await Rows() + .map((row) => row.key("age").add(row.key("age"))) + .run(), [72], ); }); it("add answers null for a null operand on every receiver", async () => { assert.deepEqual( - await Rows().map((row) => ({ - number: row.key("age").add(null), - date: row.key("bornAt").add(null), - text: row.key("name").add(null), - list: row.key("tags").add(null), - })), + await Rows() + .map((row) => ({ + number: row.key("age").add(null), + date: row.key("bornAt").add(null), + text: row.key("name").add(null), + list: row.key("tags").add(null), + })) + .run(), [{ number: null, date: null, text: null, list: null }], ); }); it("during reads a number as milliseconds since the UNIX epoch", async () => { assert.deepEqual( - await Rows().map((row) => ({ - inside: row.key("bornAt").during(0, 10_000), - low: row.key("bornAt").during(BORN_AT, 10_000), - high: row.key("bornAt").during(0, BORN_AT), - fields: row.key("bornAt").during(row.key("low"), row.key("high")), - before: row.key("bornAt").during(6000, 10_000), - })), + await Rows() + .map((row) => ({ + inside: row.key("bornAt").during(0, 10_000), + low: row.key("bornAt").during(BORN_AT, 10_000), + high: row.key("bornAt").during(0, BORN_AT), + fields: row.key("bornAt").during(row.key("low"), row.key("high")), + before: row.key("bornAt").during(6000, 10_000), + })) + .run(), [{ inside: true, low: true, high: false, fields: true, before: false }], ); }); it("during still compares dates", async () => { assert.deepEqual( - await Rows().map((row) => - row.key("bornAt").during(new Date(0), new Date(10_000)), - ), + await Rows() + .map((row) => row.key("bornAt").during(new Date(0), new Date(10_000))) + .run(), [true], ); }); it("key reads the field a constant expression names", async () => { - assert.deepEqual(await Rows().key(ValueProxy.constant<"name">("name")), [ - "ada", - ]); + assert.deepEqual( + await Rows().key(ValueProxy.constant<"name">("name")).run(), + ["ada"], + ); }); it("min and max compare strings and dates", async () => { assert.deepEqual( - await Rows().map(() => ({ - min: ValueProxy.constant(["b", "a"]).min(), - max: ValueProxy.constant([new Date(5), new Date(9)]).max(), - })), + await Rows() + .map(() => ({ + min: ValueProxy.constant(["b", "a"]).min(), + max: ValueProxy.constant([new Date(5), new Date(9)]).max(), + })) + .run(), [{ min: "a", max: new Date(9) }], ); }); it("filter keeps a number unless it is 0", async () => { assert.deepEqual( - await Rows().map(() => - ValueProxy.constant([0, 1, 2]).filter((value) => value), - ), + await Rows() + .map(() => ValueProxy.constant([0, 1, 2]).filter((value) => value)) + .run(), [[1, 2]], ); });