|
1 | 1 | import { expect, it, describe } from "bun:test"; |
2 | | -import { db, gen, prepare, sql } from "../case"; |
| 2 | +import { db, prepare, sql } from "../case"; |
3 | 3 | await prepare(sql` |
4 | | - -- name: AggEmails :one |
5 | | - SELECT array_agg(e.id::text)::text[] AS emails FROM emails e; |
| 4 | + -- name: AggEmailsOne :one |
| 5 | + SELECT array_agg(e.id)::text[] AS emails FROM emails e; |
| 6 | +
|
| 7 | + -- name: AggEmailsWithoutCastOne :one |
| 8 | + SELECT array_agg(e.id) AS emails FROM emails e; |
| 9 | +
|
| 10 | + -- name: AggEmailsMany :many |
| 11 | + SELECT 1 as foo, array_agg(e.id)::text[] AS emails FROM emails e; |
| 12 | +
|
| 13 | + -- name: AggEmailsWithoutCastMany :many |
| 14 | + SELECT 1 as foo, array_agg(e.id) AS emails FROM emails e; |
6 | 15 | `); |
7 | 16 |
|
8 | 17 | describe("aggregation", () => { |
9 | | - it("returns an array type", async () => { |
10 | | - const result = await gen().aggEmails(db); |
| 18 | + it("returns an array type in One mode", async () => { |
| 19 | + const result = await (await import("./aggregate_sql")).aggEmailsOne(db); |
| 20 | + |
| 21 | + result as string[]; |
| 22 | + |
| 23 | + expect(result).toHaveLength(3); |
| 24 | + expect(Array.isArray(result)).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns an array type in Many mode", async () => { |
| 28 | + const result = await (await import("./aggregate_sql")).aggEmailsMany(db); |
| 29 | + |
| 30 | + result[0].emails as string[]; |
| 31 | + |
| 32 | + expect(result[0].emails).toHaveLength(3); |
| 33 | + expect(Array.isArray(result)).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns an array type in a query without type cast in One mode", async () => { |
| 37 | + const result = await ( |
| 38 | + await import("./aggregate_sql") |
| 39 | + ).aggEmailsWithoutCastOne(db); |
| 40 | + |
| 41 | + result as string[]; |
11 | 42 |
|
12 | 43 | expect(result).toHaveLength(3); |
13 | 44 | expect(Array.isArray(result)).toBe(true); |
14 | 45 | }); |
| 46 | + |
| 47 | + it("returns an array type in a query without type cast in Many mode", async () => { |
| 48 | + const result = await ( |
| 49 | + await import("./aggregate_sql") |
| 50 | + ).aggEmailsWithoutCastMany(db); |
| 51 | + |
| 52 | + result[0].emails as string[]; |
| 53 | + |
| 54 | + expect(result[0].emails).toHaveLength(3); |
| 55 | + expect(Array.isArray(result)).toBe(true); |
| 56 | + }); |
15 | 57 | }); |
0 commit comments