|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * How `findWithWindowFunctions()` turns a `windowFunctions[]` entry into SQL. |
| 5 | + * |
| 6 | + * Nothing the caller sends may reach the statement as text: the function name |
| 7 | + * and the sort direction come from closed tables (anything else is refused with |
| 8 | + * `INVALID_QUERY` / 400), and `partitionBy` / `orderBy` fields are emitted as |
| 9 | + * quoted column references. Each refusal case below is a spelling that the |
| 10 | + * builder used to splice into the statement verbatim. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 14 | +import { SqlDriver } from './index.js'; |
| 15 | + |
| 16 | +describe('findWithWindowFunctions — compiling a windowFunctions[] entry', () => { |
| 17 | + let driver: SqlDriver; |
| 18 | + let knexInstance: any; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + driver = new SqlDriver({ |
| 22 | + client: 'better-sqlite3', |
| 23 | + connection: { filename: ':memory:' }, |
| 24 | + useNullAsDefault: true, |
| 25 | + }); |
| 26 | + knexInstance = (driver as any).knex; |
| 27 | + await knexInstance.schema.createTable('orders', (t: any) => { |
| 28 | + t.string('id').primary(); |
| 29 | + t.string('customer'); |
| 30 | + t.float('amount'); |
| 31 | + }); |
| 32 | + await knexInstance('orders').insert([ |
| 33 | + { id: '1', customer: 'Alice', amount: 10 }, |
| 34 | + { id: '2', customer: 'Bob', amount: 20 }, |
| 35 | + { id: '3', customer: 'Alice', amount: 30 }, |
| 36 | + ]); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(async () => { |
| 40 | + await knexInstance.destroy(); |
| 41 | + }); |
| 42 | + |
| 43 | + const expectInvalidQuery = async (promise: Promise<unknown>) => { |
| 44 | + const err: any = await promise.then( |
| 45 | + () => { |
| 46 | + throw new Error('expected the window-function read to be refused'); |
| 47 | + }, |
| 48 | + (e) => e, |
| 49 | + ); |
| 50 | + expect(err.code).toBe('INVALID_QUERY'); |
| 51 | + expect(err.status).toBe(400); |
| 52 | + }; |
| 53 | + |
| 54 | + describe('the function name is a closed set', () => { |
| 55 | + it.each(['row_number', 'RANK', 'Dense_Rank', 'percent_rank', 'CUME_DIST'])('compiles %s', async (fn) => { |
| 56 | + const rows = await driver.findWithWindowFunctions('orders', { |
| 57 | + windowFunctions: [{ function: fn, alias: 'w', orderBy: [{ field: 'amount', order: 'desc' }] }], |
| 58 | + }); |
| 59 | + expect(rows).toHaveLength(3); |
| 60 | + expect(rows.every((r: any) => r.w !== undefined)).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it.each([ |
| 64 | + ['an unsupported function', 'median'], |
| 65 | + ['a function that needs an argument', 'lag'], |
| 66 | + ['an expression in the name slot', 'row_number() over () as x, (select count(*) from orders) as n, row_number'], |
| 67 | + ])('refuses %s with INVALID_QUERY / 400', async (_label, fn) => { |
| 68 | + await expectInvalidQuery( |
| 69 | + driver.findWithWindowFunctions('orders', { windowFunctions: [{ function: fn, alias: 'w' }] }), |
| 70 | + ); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('the sort direction is asc or desc', () => { |
| 75 | + it('accepts either direction in any case, and defaults to asc', async () => { |
| 76 | + for (const order of ['asc', 'DESC', undefined] as const) { |
| 77 | + const rows = await driver.findWithWindowFunctions('orders', { |
| 78 | + windowFunctions: [{ function: 'row_number', alias: 'w', orderBy: [{ field: 'amount', order: order as any }] }], |
| 79 | + orderBy: [{ field: 'amount', order: 'asc' }], |
| 80 | + }); |
| 81 | + expect(rows.map((r: any) => r.w)).toEqual(order === 'DESC' ? [3, 2, 1] : [1, 2, 3]); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('refuses any other direction with INVALID_QUERY / 400', async () => { |
| 86 | + await expectInvalidQuery( |
| 87 | + driver.findWithWindowFunctions('orders', { |
| 88 | + windowFunctions: [ |
| 89 | + { function: 'row_number', alias: 'w', orderBy: [{ field: 'amount', order: 'desc, (select 1)' as any }] }, |
| 90 | + ], |
| 91 | + }), |
| 92 | + ); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('partitionBy / orderBy fields are quoted column references', () => { |
| 97 | + it('emits every field through the dialect quoting', () => { |
| 98 | + const sql = (driver as any).buildWindowFunction({ |
| 99 | + function: 'rank', |
| 100 | + alias: 'w', |
| 101 | + partitionBy: ['customer'], |
| 102 | + orderBy: [{ field: 'amount', order: 'desc' }], |
| 103 | + }); |
| 104 | + expect(sql).toBe('RANK() OVER (PARTITION BY `customer` ORDER BY `amount` DESC)'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('reads an expression in a field slot as a column name, never as SQL', async () => { |
| 108 | + // As text, `length(customer)` is a valid partition expression and the read |
| 109 | + // would succeed; as a quoted identifier it names no column. |
| 110 | + await expect( |
| 111 | + driver.findWithWindowFunctions('orders', { |
| 112 | + windowFunctions: [{ function: 'row_number', alias: 'w', partitionBy: ['length(customer)'] }], |
| 113 | + }), |
| 114 | + ).rejects.toThrow(/no such column/); |
| 115 | + await expect( |
| 116 | + driver.findWithWindowFunctions('orders', { |
| 117 | + windowFunctions: [{ function: 'row_number', alias: 'w', orderBy: [{ field: 'length(customer)' }] }], |
| 118 | + }), |
| 119 | + ).rejects.toThrow(/no such column/); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments