|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `aggregate()` and `distinct()` honour `external.columnMap` (ADR-0015 §18). |
| 5 | + * |
| 6 | + * `find()`, `count()` and every WHERE have mapped a federated object's local |
| 7 | + * field to its remote column since columnMap landed (`remoteColumn` in |
| 8 | + * `findRows`, `applyFilters`). The two analytic doors did not: they named the |
| 9 | + * FIELD verbatim in GROUP BY, in the aggregand and in `SELECT DISTINCT`, so |
| 10 | + * the remote table was asked for a column it does not have. Measured before |
| 11 | + * the fix on SQLite (this file's fixture) as the driver's unattributed-column |
| 12 | + * refusal. |
| 13 | + * |
| 14 | + * The rule pinned here: identifiers are the REMOTE column; output keys, |
| 15 | + * presentation and the per-field registries stay the FIELD. |
| 16 | + */ |
| 17 | + |
| 18 | +import { describe, it, expect, afterAll, beforeAll } from 'vitest'; |
| 19 | +import { rmSync } from 'node:fs'; |
| 20 | +import { tmpdir } from 'node:os'; |
| 21 | +import { join } from 'node:path'; |
| 22 | +import { SqlDriver } from './index.js'; |
| 23 | + |
| 24 | +const REMOTE = { |
| 25 | + name: 'legacy_deal', |
| 26 | + fields: { |
| 27 | + deal_no: { type: 'text' }, |
| 28 | + region_code: { type: 'text' }, |
| 29 | + amt: { type: 'number' }, |
| 30 | + closed_dt: { type: 'date' }, |
| 31 | + won_flag: { type: 'boolean' }, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +const COLUMN_MAP = { deal_no: 'id', region_code: 'region', amt: 'amount', closed_dt: 'closed_on', won_flag: 'won' }; |
| 36 | +const LOCAL_FIELDS = { |
| 37 | + region: { type: 'text' }, |
| 38 | + amount: { type: 'number' }, |
| 39 | + closed_on: { type: 'date' }, |
| 40 | + won: { type: 'boolean' }, |
| 41 | +}; |
| 42 | + |
| 43 | +let file: string; |
| 44 | +let driver: any; |
| 45 | + |
| 46 | +beforeAll(async () => { |
| 47 | + file = join(tmpdir(), `os-cm-agg-${process.pid}-${Date.now()}.db`); |
| 48 | + const fx = new SqlDriver({ client: 'better-sqlite3', connection: { filename: file }, useNullAsDefault: true }) as any; |
| 49 | + fx.name = 'fx'; |
| 50 | + await fx.connect(); |
| 51 | + await fx.initObjects([REMOTE]); |
| 52 | + await fx.create('legacy_deal', { deal_no: 'd1', region_code: 'NA', amt: 100, closed_dt: '2026-01-10', won_flag: true }); |
| 53 | + await fx.create('legacy_deal', { deal_no: 'd2', region_code: 'EU', amt: 40, closed_dt: '2026-01-20', won_flag: false }); |
| 54 | + await fx.create('legacy_deal', { deal_no: 'd3', region_code: 'EU', amt: 60, closed_dt: '2026-04-02', won_flag: true }); |
| 55 | + await fx.disconnect(); |
| 56 | + |
| 57 | + driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: file }, useNullAsDefault: true, schemaMode: 'external' } as any) as any; |
| 58 | + driver.name = 'extds'; |
| 59 | + await driver.connect(); |
| 60 | + driver.registerExternalObject({ name: 'deal', external: { remoteName: 'legacy_deal', columnMap: COLUMN_MAP }, fields: LOCAL_FIELDS }); |
| 61 | +}); |
| 62 | + |
| 63 | +afterAll(async () => { |
| 64 | + await driver?.disconnect(); |
| 65 | + if (file) rmSync(file, { force: true }); |
| 66 | +}); |
| 67 | + |
| 68 | +const byRegion = (rows: any[]) => [...rows].sort((a, b) => String(a.region).localeCompare(String(b.region))); |
| 69 | + |
| 70 | +describe('aggregate() over a columnMap-ed object', () => { |
| 71 | + it('groups by the remote column and keys the group under the field', async () => { |
| 72 | + const rows = await driver.aggregate('deal', { |
| 73 | + groupBy: ['region'], |
| 74 | + aggregations: [{ function: 'sum', field: 'amount', alias: 'total' }, { function: 'count', alias: 'n' }], |
| 75 | + }); |
| 76 | + expect(byRegion(rows)).toEqual([ |
| 77 | + { region: 'EU', total: 100, n: 2 }, |
| 78 | + { region: 'NA', total: 100, n: 1 }, |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('an aliased structured group key lands under its alias', async () => { |
| 83 | + const rows = await driver.aggregate('deal', { |
| 84 | + groupBy: [{ field: 'region', alias: 'r' }], |
| 85 | + aggregations: [{ function: 'max', field: 'amount', alias: 'top' }], |
| 86 | + }); |
| 87 | + expect([...rows].sort((a: any, b: any) => a.r.localeCompare(b.r))).toEqual([ |
| 88 | + { r: 'EU', top: 60 }, |
| 89 | + { r: 'NA', top: 100 }, |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('a date-bucketed group reads the remote column and keeps the field registry (calendar day)', async () => { |
| 94 | + const rows = await driver.aggregate('deal', { |
| 95 | + groupBy: [{ field: 'closed_on', dateGranularity: 'month' }], |
| 96 | + aggregations: [{ function: 'count', alias: 'n' }], |
| 97 | + }); |
| 98 | + expect([...rows].sort((a: any, b: any) => a.closed_on.localeCompare(b.closed_on))).toEqual([ |
| 99 | + { closed_on: '2026-01', n: 2 }, |
| 100 | + { closed_on: '2026-04', n: 1 }, |
| 101 | + ]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('min/max over a mapped column take the field presentation; count_distinct maps too', async () => { |
| 105 | + const [row] = await driver.aggregate('deal', { |
| 106 | + aggregations: [ |
| 107 | + { function: 'min', field: 'closed_on', alias: 'first' }, |
| 108 | + { function: 'count_distinct', field: 'region', alias: 'regions' }, |
| 109 | + ], |
| 110 | + }); |
| 111 | + expect(row).toEqual({ first: '2026-01-10', regions: 2 }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('the WHERE and the GROUP BY agree on the mapping', async () => { |
| 115 | + const rows = await driver.aggregate('deal', { |
| 116 | + where: { won: true }, |
| 117 | + groupBy: ['region'], |
| 118 | + aggregations: [{ function: 'sum', field: 'amount', alias: 'total' }], |
| 119 | + }); |
| 120 | + expect(byRegion(rows)).toEqual([ |
| 121 | + { region: 'EU', total: 60 }, |
| 122 | + { region: 'NA', total: 100 }, |
| 123 | + ]); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe('distinct() over a columnMap-ed object', () => { |
| 128 | + it('reads the remote column and answers its values', async () => { |
| 129 | + expect([...(await driver.distinct('deal', 'region'))].sort()).toEqual(['EU', 'NA']); |
| 130 | + }); |
| 131 | + |
| 132 | + it('presents by the field registry (a mapped boolean reads as a boolean)', async () => { |
| 133 | + expect([...(await driver.distinct('deal', 'won'))].sort()).toEqual([false, true]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('with a filter on another mapped field', async () => { |
| 137 | + expect(await driver.distinct('deal', 'region', { amount: { $gt: 50 } })).toEqual(expect.arrayContaining(['EU', 'NA'])); |
| 138 | + expect(await driver.distinct('deal', 'region', { amount: { $lt: 50 } })).toEqual(['EU']); |
| 139 | + }); |
| 140 | +}); |
0 commit comments