Skip to content

Commit 901cbb0

Browse files
committed
test(objectql,dogfood): record-currency suites type their query options and hold the find double's limit
Claude-Session: https://claude.ai/code/session_01Utp6CboxRwQPbQZnhxyuzt Co-authored-by: Claude <noreply@anthropic.com>
1 parent 77bc11a commit 901cbb0

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

‎packages/objectql/src/record-currency-guards.test.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// `record-currency-probes.dogfood.test.ts`.
1919

2020
import { describe, it, expect, beforeEach } from 'vitest';
21+
import type { EngineAggregateOptions, EngineUpdateOptions } from '@objectstack/spec/data';
2122
import { ObjectQL } from './engine.js';
2223

2324
function makeDriver() {
@@ -42,7 +43,8 @@ function makeDriver() {
4243
name: 'memory', version: '0.0.0', supports: {},
4344
async connect() {}, async disconnect() {}, async checkHealth() { return true; }, async execute() { return null; },
4445
async find(object: string, ast: any) {
45-
return Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where)).map((r) => ({ ...r }));
46+
const rows = Array.from(storeFor(object).values()).filter((r) => matches(r, ast?.where)).map((r) => ({ ...r }));
47+
return typeof ast?.limit === 'number' ? rows.slice(0, ast.limit) : rows;
4648
},
4749
async findOne(object: string, ast: any) {
4850
for (const r of storeFor(object).values()) if (matches(r, ast?.where)) return { ...r };
@@ -102,6 +104,9 @@ const INVALID_FIELD = { code: 'INVALID_FIELD', status: 400, httpStatus: 400 };
102104
const INVALID_METADATA = { code: 'INVALID_METADATA', status: 422, httpStatus: 422 };
103105

104106
const RECORD = { currencyMode: 'record', currencyField: 'currency_code' } as const;
107+
/** Typed at the call, never asserted (`check:query-options-erasure`). */
108+
const aggregateQuery = (query: EngineAggregateOptions): EngineAggregateOptions => query;
109+
const BATCH: EngineUpdateOptions = { where: { title: 'batch' }, multi: true };
105110

106111
const FX_INVOICE = {
107112
name: 'fx_invoice',
@@ -180,13 +185,13 @@ describe('a record-mode amount is stored with its currency code (ERR_CURRENCY_CO
180185
const a = await engine.insert('fx_invoice', { title: 'batch', amount: 1, currency_code: 'EUR' });
181186
const b = await engine.insert('fx_invoice', { title: 'batch' });
182187

183-
const err = await refusalOf(engine.update('fx_invoice', { amount: 9 }, { where: { title: 'batch' }, multi: true } as any));
188+
const err = await refusalOf(engine.update('fx_invoice', { amount: 9 }, BATCH));
184189
expect(envelopeOf(err)).toEqual(CODE_REQUIRED);
185190
expect(err?.message).toContain(`(record ${b.id})`);
186191
expect(storeFor('fx_invoice').get(a.id).amount).toBe(1);
187192

188193
// The control: the same write carrying the code.
189-
await engine.update('fx_invoice', { amount: 9, currency_code: 'EUR' }, { where: { title: 'batch' }, multi: true } as any);
194+
await engine.update('fx_invoice', { amount: 9, currency_code: 'EUR' }, BATCH);
190195
expect(storeFor('fx_invoice').get(b.id)).toMatchObject({ amount: 9, currency_code: 'EUR' });
191196
});
192197
});
@@ -202,10 +207,10 @@ describe('a raw aggregate over a record-mode amount groups by its code field (PR
202207
});
203208

204209
it('sum / avg / min / max ungrouped is refused INVALID_FIELD, with the route first', async () => {
205-
for (const fn of ['sum', 'avg', 'min', 'max']) {
206-
const err = await refusalOf(engine.aggregate('fx_invoice', {
210+
for (const fn of ['sum', 'avg', 'min', 'max'] as const) {
211+
const err = await refusalOf(engine.aggregate('fx_invoice', aggregateQuery({
207212
aggregations: [{ function: fn, field: 'amount', alias: 'v' }],
208-
} as any));
213+
})));
209214
expect(envelopeOf(err), fn).toEqual(INVALID_FIELD);
210215
expect(err?.field).toBe('amount');
211216
expect(err?.param).toBe('aggregations');
@@ -215,31 +220,32 @@ describe('a raw aggregate over a record-mode amount groups by its code field (PR
215220
});
216221

217222
it('grouped by a field other than the code is still refused', async () => {
218-
const err = await refusalOf(engine.aggregate('fx_invoice', {
223+
const err = await refusalOf(engine.aggregate('fx_invoice', aggregateQuery({
219224
groupBy: ['title'],
220225
aggregations: [{ function: 'sum', field: 'amount', alias: 'v' }],
221-
} as any));
226+
})));
222227
expect(envelopeOf(err)).toEqual(INVALID_FIELD);
223228
});
224229

225230
it('grouped by the code field answers one correct total per currency', async () => {
226-
for (const groupBy of [['currency_code'], [{ field: 'currency_code' }]]) {
227-
const rows = await engine.aggregate('fx_invoice', {
231+
const groupings: Array<EngineAggregateOptions['groupBy']> = [['currency_code'], [{ field: 'currency_code' }]];
232+
for (const groupBy of groupings) {
233+
const rows = await engine.aggregate('fx_invoice', aggregateQuery({
228234
groupBy,
229235
aggregations: [{ function: 'sum', field: 'amount', alias: 'total' }],
230-
} as any);
236+
}));
231237
const byCode = Object.fromEntries(rows.map((r: any) => [r.currency_code, r.total]));
232238
expect(byCode).toEqual({ USD: 100, EUR: 100 });
233239
}
234240
});
235241

236242
it('count, and a fixed-currency amount, are not judged', async () => {
237-
const rows = await engine.aggregate('fx_invoice', {
243+
const rows = await engine.aggregate('fx_invoice', aggregateQuery({
238244
aggregations: [
239245
{ function: 'count', field: 'amount', alias: 'n' },
240246
{ function: 'sum', field: 'amt_usd', alias: 'usd' },
241247
],
242-
} as any);
248+
}));
243249
expect(rows[0]).toMatchObject({ n: 2, usd: 100 });
244250
});
245251
});

‎packages/qa/dogfood/test/record-currency-probes.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2323
import { ObjectQL } from '@objectstack/objectql';
24+
import type { EngineAggregateOptions } from '@objectstack/spec/data';
2425
import { SqlDriver } from '@objectstack/driver-sql';
2526
import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm';
2627
import { installDefaultOrganization } from './default-organization.testkit.js';
@@ -37,6 +38,9 @@ const DRIVERS = [
3738
},
3839
];
3940

41+
/** Typed at the call, never asserted (`check:query-options-erasure`). */
42+
const aggregateQuery = (query: EngineAggregateOptions): EngineAggregateOptions => query;
43+
4044
const SYS = { context: { isSystem: true, userId: 'usr_system' } };
4145

4246
const RECORD = { currencyMode: 'record', currencyField: 'currency_code' } as const;
@@ -99,17 +103,17 @@ describe.each(DRIVERS)('record-currency probes: $name', ({ make }) => {
99103
await engine.insert('fx_invoice', { name: 'i1', currency_code: 'USD', amount: 100 }, SYS);
100104
await engine.insert('fx_invoice', { name: 'i2', currency_code: 'EUR', amount: 100 }, SYS);
101105

102-
for (const fn of ['sum', 'avg', 'min', 'max']) {
103-
const err = await refusalOf(() => engine.aggregate('fx_invoice', {
106+
for (const fn of ['sum', 'avg', 'min', 'max'] as const) {
107+
const err = await refusalOf(() => engine.aggregate('fx_invoice', aggregateQuery({
104108
aggregations: [{ function: fn, field: 'amount', alias: 'v' }],
105-
} as never, SYS));
109+
}), SYS));
106110
expect(envelope(err), fn).toEqual({ code: 'INVALID_FIELD', status: 400, field: 'amount' });
107111
}
108112

109-
const rows = await engine.aggregate('fx_invoice', {
113+
const rows = await engine.aggregate('fx_invoice', aggregateQuery({
110114
groupBy: ['currency_code'],
111115
aggregations: [{ function: 'sum', field: 'amount', alias: 'total' }],
112-
} as never, SYS);
116+
}), SYS);
113117
const byCode = Object.fromEntries((rows as Array<Record<string, unknown>>).map((r) => [r.currency_code, Number(r.total)]));
114118
expect(byCode).toEqual({ USD: 100, EUR: 100 });
115119
});

0 commit comments

Comments
 (0)