1818// `record-currency-probes.dogfood.test.ts`.
1919
2020import { describe , it , expect , beforeEach } from 'vitest' ;
21+ import type { EngineAggregateOptions , EngineUpdateOptions } from '@objectstack/spec/data' ;
2122import { ObjectQL } from './engine.js' ;
2223
2324function 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 };
102104const INVALID_METADATA = { code : 'INVALID_METADATA' , status : 422 , httpStatus : 422 } ;
103105
104106const 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
106111const 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} ) ;
0 commit comments