Skip to content

Commit 28e570c

Browse files
committed
test(service-analytics): pin the refusal status, a relationship path, and host-neutral words for the unnamed home field
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CbmDUh2Sx7fvaGgu7r
1 parent e430075 commit 28e570c

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎packages/services/service-analytics/src/__tests__/home-amount-consolidated-measure.test.ts‎

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*
2828
* Three stores: the draft-preview evaluator (in memory, over the very rows
2929
* the projection wrote, read back from the store), SQLite, and PostgreSQL.
30+
* The "memory" cell is that evaluator, ⛔ not `driver-memory` (whose test
31+
* consumers are a closed census, `check:driver-memory-census`).
3032
* The SQLite cell always runs. The PostgreSQL cell runs where
3133
* `OS_TEST_POSTGRES_URL` is set and is a named skip otherwise; CI provisions
3234
* that variable for this package in the Temporal Conformance job
@@ -95,6 +97,31 @@ const BARE_INVOICE = {
9597
},
9698
};
9799

100+
/** A line per invoice, whose cube reaches the invoice's amounts through a declared join. */
101+
const LINE = 'os27195_fx_line';
102+
const LINE_OBJECT = {
103+
name: LINE,
104+
label: 'FX invoice line',
105+
fields: {
106+
note: { name: 'note', type: 'text' as const },
107+
invoice: { name: 'invoice', type: 'lookup' as const, reference: OBJECT },
108+
},
109+
};
110+
const LINE_CUBE = {
111+
name: 'os27195_line_cube',
112+
title: 'Lines over their invoice amounts',
113+
sql: LINE,
114+
public: true,
115+
joins: { invoice: { name: OBJECT } },
116+
measures: {
117+
invoice_raw: { type: 'sum', sql: 'invoice.amount', label: 'Invoice amount (record currency)' },
118+
invoice_home: { type: 'sum', sql: 'invoice.amount_home', label: 'Invoice amount (home currency)' },
119+
},
120+
dimensions: {
121+
invoice_code: { type: 'string', sql: 'invoice.currency_code', label: 'Invoice currency' },
122+
},
123+
};
124+
98125
const HOME = 'CNY';
99126

100127
/** "1 home unit = rate units of the foreign currency" (ADR-0144 D7 parameter 3), so home = amount ÷ rate. */
@@ -210,7 +237,7 @@ for (const cell of CELLS) {
210237
engine = new ObjectQL({ logger: quiet } as never);
211238
engine.registerDriver(driver as never, true);
212239
await engine.init();
213-
for (const object of [RATE_OBJECT, INVOICE, BARE_INVOICE]) engine.registry.registerObject(object as never);
240+
for (const object of [RATE_OBJECT, INVOICE, BARE_INVOICE, LINE_OBJECT]) engine.registry.registerObject(object as never);
214241
await engine.syncSchemas();
215242
await installDefaultOrganization(engine as never);
216243
engine.setHomeLocalizationResolver(async () => ({ currency: HOME }));
@@ -221,16 +248,17 @@ for (const cell of CELLS) {
221248
for (const row of ROWS) {
222249
await engine.insert(BARE, { id: row.id, currency_code: row.currency_code, amount: row.amount } as never, SYS as never);
223250
}
251+
for (const row of ROWS) await engine.insert(LINE, { id: `l_${row.id}`, note: row.id, invoice: row.id } as never, SYS as never);
224252
stored = (await engine.find(OBJECT, { context: SYS.context } as never)) as Array<Record<string, unknown>>;
225253

226254
const realExecute = (engine as any).execute.bind(engine);
227255
(engine as any).execute = (sql: unknown, opts?: { object?: string }) => {
228-
if (opts?.object === OBJECT || opts?.object === BARE) reads.rawSql += 1;
256+
if (opts?.object === OBJECT || opts?.object === BARE || opts?.object === LINE) reads.rawSql += 1;
229257
return realExecute(sql, opts);
230258
};
231259
const realAggregate = engine.aggregate.bind(engine);
232260
(engine as any).aggregate = (object: string, ...rest: unknown[]) => {
233-
if (object === OBJECT || object === BARE) reads.aggregate += 1;
261+
if (object === OBJECT || object === BARE || object === LINE) reads.aggregate += 1;
234262
return (realAggregate as any)(object, ...rest);
235263
};
236264

@@ -246,7 +274,7 @@ for (const cell of CELLS) {
246274
] as const) {
247275
const registered: Record<string, unknown> = {};
248276
const provided: Record<string, unknown> = { data: engine, protocol: pendingSeedProtocol };
249-
await new AnalyticsServicePlugin({ ...(caps ? { queryCapabilities: caps } : {}) } as never).init({
277+
await new AnalyticsServicePlugin({ cubes: [LINE_CUBE], ...(caps ? { queryCapabilities: caps } : {}) } as never).init({
250278
getService: (name: string) => provided[name] ?? registered[name],
251279
registerService: (name: string, svc: unknown) => { registered[name] = svc; },
252280
replaceService: (name: string, svc: unknown) => { registered[name] = svc; },
@@ -287,6 +315,7 @@ for (const cell of CELLS) {
287315
// The grand total over `raw_total` drops the code, so the door refuses
288316
// it; the home field alone is served under the same grand total.
289317
expect((result as Refusal).code).toBe('INVALID_FIELD');
318+
expect((result as Refusal).status).toBe(400);
290319
expect((result as Refusal).member).toBe('raw_total');
291320

292321
const served = await services[face]!.queryDataset(
@@ -321,12 +350,13 @@ for (const cell of CELLS) {
321350
expect(reads.aggregate - before.aggregate).toBe(0);
322351
});
323352

324-
it('an amount declaring no homeAmount reads the declaration to add, and the grouping', async () => {
353+
it('an amount declaring no homeAmount reads the home-field remedy without a name, and the grouping', async () => {
325354
const err = await refusalOf(services[face]!.queryDataset(BARE_DATASET, { measures: ['raw_total'] }, CTX));
326355
expect(err.code, err.message).toBe('INVALID_FIELD');
356+
expect(err.status).toBe(400);
327357
expect(err.object).toBe(BARE);
328358
expect(err.message).toContain(
329-
`declare \`currencyConfig.homeAmount\` on 'amount' of object '${BARE}' and point the measure at its home field`,
359+
`point the measure at the home field of 'amount' (the field its \`currencyConfig.homeAmount.field\` names on object '${BARE}'; declare \`homeAmount\` there first if it has none)`,
330360
);
331361
expect(err.message).toContain("add the dimension over 'currency_code' to the query");
332362
});
@@ -336,11 +366,31 @@ for (const cell of CELLS) {
336366
expect(Number((served.rows[0] as Record<string, unknown>).amount_home_sum)).toBe(HOME_TOTAL);
337367
const err = await refusalOf(services[face]!.query({ cube: OBJECT, measures: ['amount_sum'] } as never, CTX));
338368
expect(err.code, err.message).toBe('INVALID_FIELD');
369+
expect(err.status).toBe(400);
339370
expect(err.message).toContain("point the measure at 'amount_home'");
340371
});
372+
373+
it('over a relationship path, the ungrouped raw amount is refused naming the joined home field and code field', async () => {
374+
const before = { ...reads };
375+
const err = await refusalOf(services[face]!.query({ cube: LINE_CUBE.name, measures: ['invoice_raw'] } as never, CTX));
376+
expect(err.code, err.message).toBe('INVALID_FIELD');
377+
expect(err.status).toBe(400);
378+
expect(err.member).toBe('invoice_raw');
379+
expect(err.field).toBe('invoice.amount');
380+
expect(err.object).toBe(OBJECT);
381+
expect(err.message).toContain(`point the measure at 'invoice.amount_home', the home-currency amount object '${OBJECT}' declares for 'amount'`);
382+
expect(err.message).toContain("add the dimension over 'invoice.currency_code' to the query");
383+
expect(reads.rawSql - before.rawSql).toBe(0);
384+
expect(reads.aggregate - before.aggregate).toBe(0);
385+
});
341386
});
342387
}
343388

389+
it('over a relationship path, a sum over the joined home field is served: the home-currency total (native strategy)', async () => {
390+
const served = await services.native!.query({ cube: LINE_CUBE.name, measures: ['invoice_home'] } as never, CTX);
391+
expect(Number((served.rows[0] as Record<string, unknown>).invoice_home)).toBe(HOME_TOTAL);
392+
});
393+
344394
describe('the draft preview (the in-memory evaluator, over the stored rows)', () => {
345395
it('a sum over the home field answers the home-currency total, ungrouped and grouped by the code', async () => {
346396
const kpi = await services.native!.queryDataset(DATASET, { measures: ['home_total'] }, CTX, { previewDrafts: true });
@@ -360,6 +410,7 @@ for (const cell of CELLS) {
360410
services.native!.queryDataset(DATASET, { measures: ['raw_total'] }, CTX, { previewDrafts: true }),
361411
);
362412
expect(err.code, err.message).toBe('INVALID_FIELD');
413+
expect(err.status).toBe(400);
363414
expect(err.member).toBe('raw_total');
364415
expect(err.message).toContain("point the measure at 'amount_home'");
365416
});

‎packages/services/service-analytics/src/cube-record-currency-door.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ export function assertRecordCurrencyAggregatesGrouped(
109109
const homeRemedy = homeField !== undefined
110110
? `point the measure at '${prefix}${homeField}', the home-currency amount object '${target.object}' `
111111
+ `declares for '${target.column}' (\`homeAmount.field\`), for one total in the home currency`
112-
: `for one total in the home currency, declare \`currencyConfig.homeAmount\` on '${target.column}' `
113-
+ `of object '${target.object}' and point the measure at its home field`;
112+
: `for one total in the home currency, point the measure at the home field of '${target.column}' `
113+
+ `(the field its \`currencyConfig.homeAmount.field\` names on object '${target.object}'; declare `
114+
+ '`homeAmount` there first if it has none)';
114115
const err = invalidMemberError(
115116
`Measure '${member}' on cube '${cubeName}' takes the ${measure.type} of field '${target.path}', `
116117
+ `a record-currency amount whose currency is each record's '${codeField}', but the query does not `

0 commit comments

Comments
 (0)