query-builder.test.ts55 lines · main
| 1 | import { describe, expect, it } from 'bun:test'; |
| 2 | |
| 3 | import type { ProjectTx } from './db.js'; |
| 4 | import { buildDbClient } from './query-builder.js'; |
| 5 | |
| 6 | /** |
| 7 | * F1 — inline DELETE … RETURNING must return the deleted rows, matching the |
| 8 | * isolate executor (apps/runtime/src/isolate-runtime/loop.ts buildDeleteSql). |
| 9 | * Both executors agree: no .returning() → []; .returning() → RETURNING * rows; |
| 10 | * .returning([cols]) → RETURNING those columns. |
| 11 | */ |
| 12 | describe('inline DELETE … RETURNING', () => { |
| 13 | it('returns the deleted rows', async () => { |
| 14 | const calls: { sql: string; params: readonly unknown[] }[] = []; |
| 15 | const deletedRows = [{ id: 1, name: 'gone' }]; |
| 16 | const tx: ProjectTx = { |
| 17 | unsafe: async (sql, params = []) => { |
| 18 | calls.push({ sql, params }); |
| 19 | return /RETURNING/.test(sql) ? deletedRows : []; |
| 20 | }, |
| 21 | }; |
| 22 | const { db } = buildDbClient(tx); |
| 23 | |
| 24 | const rows = await db('widgets').delete().where({ id: 1 }).returning(); |
| 25 | |
| 26 | expect(rows).toEqual(deletedRows); |
| 27 | expect(calls[0]!.sql).toContain('DELETE FROM "widgets"'); |
| 28 | expect(calls[0]!.sql).toContain('RETURNING *'); |
| 29 | }); |
| 30 | |
| 31 | it('returns named columns when returning(cols) is given', async () => { |
| 32 | const calls: string[] = []; |
| 33 | const tx: ProjectTx = { |
| 34 | unsafe: async (sql) => { |
| 35 | calls.push(sql); |
| 36 | return [{ id: 1 }]; |
| 37 | }, |
| 38 | }; |
| 39 | const { db } = buildDbClient(tx); |
| 40 | |
| 41 | const rows = await db('widgets').delete().where({ id: 1 }).returning(['id']); |
| 42 | |
| 43 | expect(rows).toEqual([{ id: 1 }]); |
| 44 | expect(calls[0]!).toContain('RETURNING "id"'); |
| 45 | }); |
| 46 | |
| 47 | it('returns [] when returning() is not called', async () => { |
| 48 | const tx: ProjectTx = { unsafe: async () => [{ id: 1 }] }; |
| 49 | const { db } = buildDbClient(tx); |
| 50 | |
| 51 | const rows = await db('widgets').delete().where({ id: 1 }); |
| 52 | |
| 53 | expect(rows).toEqual([]); |
| 54 | }); |
| 55 | }); |