also looking at this
fix(client): the cursor predicate is a where term, not a second WHERE (#1090)
#1098
2 files
+213
-18
| @@ -0,0 +1,141 @@ | ||
| 1 | /** | |
| 2 | * Regression coverage for stacksjs/bun-query-builder#1090. | |
| 3 | * | |
| 4 | * `cursorPaginate` appended its cursor predicate with an unconditional `WHERE` | |
| 5 | * keyword instead of continuing an existing one with `AND`. Any filter already | |
| 6 | * on the builder produced two top-level `WHERE`s and a parse error — on every | |
| 7 | * dialect, not just the SQLite wrapper. | |
| 8 | * | |
| 9 | * `chunkById` and `eachById` both delegate to it, so `.where(x).chunkById(...)` | |
| 10 | * — the natural way to walk a filtered table in batches — had never worked. | |
| 11 | * | |
| 12 | * The timing is what kept this hidden: page one is fetched with no cursor, so | |
| 13 | * no predicate is emitted and it succeeds. The failure only lands on page two. | |
| 14 | * A fixture that fits in a single chunk passes, which is why the existing | |
| 15 | * pagination tests never caught it. Every test here therefore either forces a | |
| 16 | * non-null cursor or spans more than one chunk. | |
| 17 | */ | |
| 18 | ||
| 19 | import { SQL } from 'bun' | |
| 20 | import { afterAll, beforeAll, describe, expect, it } from 'bun:test' | |
| 21 | import { buildDatabaseSchema, createQueryBuilder } from '../src' | |
| 22 | ||
| 23 | const schema = buildDatabaseSchema({ | |
| 24 | Row: { | |
| 25 | name: 'Row', | |
| 26 | table: 'cp_rows', | |
| 27 | primaryKey: 'id', | |
| 28 | attributes: { | |
| 29 | id: { validation: { rule: {} } }, | |
| 30 | b: { validation: { rule: {} } }, | |
| 31 | g: { validation: { rule: {} } }, | |
| 32 | }, | |
| 33 | }, | |
| 34 | } as any) | |
| 35 | ||
| 36 | let sql: SQL | |
| 37 | ||
| 38 | beforeAll(async () => { | |
| 39 | sql = new SQL('sqlite://:memory:') | |
| 40 | await sql.unsafe('CREATE TABLE cp_rows (id INTEGER PRIMARY KEY, b INTEGER, g INTEGER)') | |
| 41 | // ids 1-20; `b` alternates 1/0 so a filter halves the set; `g` groups by 5. | |
| 42 | for (let i = 1; i <= 20; i++) | |
| 43 | await sql.unsafe('INSERT INTO cp_rows VALUES (?, ?, ?)', [i, i % 2, Math.ceil(i / 5)]) | |
| 44 | }) | |
| 45 | ||
| 46 | afterAll(async () => { | |
| 47 | await sql?.end() | |
| 48 | }) | |
| 49 | ||
| 50 | const q = () => createQueryBuilder<typeof schema>({ schema, sql }).selectFrom('cp_rows').selectAll() as any | |
| 51 | ||
| 52 | describe('cursorPaginate continues an existing WHERE (#1090)', () => { | |
| 53 | it('pages a filtered query instead of throwing', async () => { | |
| 54 | const page = await q().where('b', '=', 1).cursorPaginate(3, 5, 'id') | |
| 55 | ||
| 56 | // Was: SQLiteError: near "WHERE": syntax error. | |
| 57 | expect(page.data.map((r: any) => r.id)).toEqual([7, 9, 11]) | |
| 58 | // The builder's own filter must still apply — a fix that merely stopped | |
| 59 | // throwing by dropping the WHERE would return 6, 7, 8 here. | |
| 60 | expect(page.data.every((r: any) => r.b === 1)).toBe(true) | |
| 61 | }) | |
| 62 | ||
| 63 | it('pages an unfiltered query exactly as before', async () => { | |
| 64 | const page = await q().cursorPaginate(3, 5, 'id') | |
| 65 | ||
| 66 | expect(page.data.map((r: any) => r.id)).toEqual([6, 7, 8]) | |
| 67 | }) | |
| 68 | ||
| 69 | it('honours a descending cursor alongside a filter', async () => { | |
| 70 | const page = await q().where('b', '=', 1).cursorPaginate(3, 15, 'id', 'desc') | |
| 71 | ||
| 72 | expect(page.data.map((r: any) => r.id)).toEqual([13, 11, 9]) | |
| 73 | }) | |
| 74 | ||
| 75 | it('honours a composite cursor alongside a filter', async () => { | |
| 76 | const page = await q().where('b', '=', 1).cursorPaginate(3, [2, 7], ['g', 'id']) | |
| 77 | ||
| 78 | expect(page.data.map((r: any) => r.id)).toEqual([9, 11, 13]) | |
| 79 | }) | |
| 80 | ||
| 81 | it('carries a grouped OR filter into the cursor query', async () => { | |
| 82 | // Guards the interaction with #1083: the builder's WHERE is rendered from | |
| 83 | // the term list, so the cursor predicate has to be ANDed against the whole | |
| 84 | // group rather than against its last term. | |
| 85 | const page = await q().where('id', '<=', 4).orWhere('id', '>=', 18).cursorPaginate(10, 1, 'id') | |
| 86 | ||
| 87 | expect(page.data.map((r: any) => r.id)).toEqual([2, 3, 4, 18, 19, 20]) | |
| 88 | }) | |
| 89 | }) | |
| 90 | ||
| 91 | describe('chunkById/eachById over a filtered query (#1090)', () => { | |
| 92 | it('chunkById walks every matching row', async () => { | |
| 93 | const seen: number[] = [] | |
| 94 | await q().where('b', '=', 1).chunkById(3, 'id', (rows: any[]) => { | |
| 95 | seen.push(...rows.map(r => r.id)) | |
| 96 | }) | |
| 97 | ||
| 98 | // Was: threw on the second chunk. Note this spans four chunks — a fixture | |
| 99 | // that fitted in one would have passed against the bug. | |
| 100 | expect(seen).toEqual([1, 3, 5, 7, 9, 11, 13, 15, 17, 19]) | |
| 101 | }) | |
| 102 | ||
| 103 | it('eachById visits every matching row', async () => { | |
| 104 | const seen: number[] = [] | |
| 105 | await q().where('b', '=', 1).eachById(3, 'id', (row: any) => { | |
| 106 | seen.push(row.id) | |
| 107 | }) | |
| 108 | ||
| 109 | expect(seen).toEqual([1, 3, 5, 7, 9, 11, 13, 15, 17, 19]) | |
| 110 | }) | |
| 111 | ||
| 112 | it('chunkById is unchanged on an unfiltered query', async () => { | |
| 113 | const seen: number[] = [] | |
| 114 | await q().chunkById(3, 'id', (rows: any[]) => { | |
| 115 | seen.push(...rows.map(r => r.id)) | |
| 116 | }) | |
| 117 | ||
| 118 | expect(seen).toEqual(Array.from({ length: 20 }, (_, i) => i + 1)) | |
| 119 | }) | |
| 120 | }) | |
| 121 | ||
| 122 | describe('cursorPaginate argument validation (#1090)', () => { | |
| 123 | it('refuses to be combined with orderBy', async () => { | |
| 124 | // A cursor predicate `col > ?` only means "the rows after this one" if the | |
| 125 | // result is sorted by that column, so a second ordering makes the cursor | |
| 126 | // meaningless. This always emitted a duplicate ORDER BY and failed to | |
| 127 | // parse; it was invisible because the duplicate-WHERE error fired first. | |
| 128 | await expect(q().where('b', '=', 1).orderBy('id').cursorPaginate(3, 5, 'id')) | |
| 129 | .rejects.toThrow(/cannot be combined with orderBy/) | |
| 130 | }) | |
| 131 | ||
| 132 | it('refuses a non-positive page size', async () => { | |
| 133 | await expect(q().cursorPaginate(0, 1, 'id')).rejects.toThrow(/expected positive integer/) | |
| 134 | }) | |
| 135 | ||
| 136 | it('refuses a column that is not an identifier', async () => { | |
| 137 | await expect(q().cursorPaginate(3, 1, 'id; DROP TABLE cp_rows')).rejects.toThrow(/Invalid identifier/) | |
| 138 | // And the table is still there. | |
| 139 | expect((await sql.unsafe('SELECT count(*) as c FROM cp_rows'))[0].c).toBe(20) | |
| 140 | }) | |
| 141 | }) | |