@@ -17,9 +17,8 @@ describe('knex-flex-filter', () => {
1717 return 'bigint' ;
1818 case 'lastBuyBlockNumber' :
1919 return 'bigint' ;
20-
2120 default :
22- return undefined ;
21+ return '' ;
2322 }
2423 } ;
2524 done ( ) ;
@@ -136,6 +135,107 @@ describe('knex-flex-filter', () => {
136135 } ) ;
137136 } ) ;
138137
138+ describe ( 'when filtering an aggregate column' , ( ) => {
139+ let aggregatedQuery ;
140+ let isAggregateFn ;
141+ let preprocessor ;
142+ beforeEach ( ( ) => {
143+ aggregatedQuery = knex . table ( 'entities' ) . sum ( 'ownerId as ownerIdSum' ) . groupBy ( 'id' ) ;
144+ isAggregateFn = column => column === 'ownerIdSum' ;
145+ preprocessor = column => ( column === 'ownerIdSum' ? 'sum("ownerId")' : column ) ;
146+ } ) ;
147+
148+ it ( 'correctly filters by _eq' , async ( done ) => {
149+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_eq : 1 } , { castFn, isAggregateFn, preprocessor } ) ;
150+
151+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") = ?' ) ;
152+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ 1 ] ) ;
153+
154+ const result = await query ;
155+ expect ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) . toEqual ( 1 ) ;
156+ done ( ) ;
157+ } ) ;
158+
159+ it ( 'correctly filters by _gt' , async ( done ) => {
160+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_gt : 0 } , { castFn, isAggregateFn, preprocessor } ) ;
161+
162+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") > ?' ) ;
163+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ 0 ] ) ;
164+
165+ const result = await query ;
166+ expect ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) . toBeGreaterThan ( 0 ) ;
167+ done ( ) ;
168+ } ) ;
169+
170+ it ( 'correctly filters by _lt' , async ( done ) => {
171+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_lt : 2 } , { castFn, isAggregateFn, preprocessor } ) ;
172+
173+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") < ?' ) ;
174+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ 2 ] ) ;
175+
176+ const result = await query ;
177+ expect ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) . toBeLessThan ( 2 ) ;
178+ done ( ) ;
179+ } ) ;
180+
181+ it ( 'correctly filters by _in' , async ( done ) => {
182+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_in : [ 1 , 2 ] } , { castFn, isAggregateFn, preprocessor } ) ;
183+
184+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") = ANY(?)' ) ;
185+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ [ 1 , 2 ] ] ) ;
186+
187+ const result = await query ;
188+ expect ( [ 1 , 2 ] ) . toContain ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) ;
189+ done ( ) ;
190+ } ) ;
191+
192+ it ( 'correctly filters by _not' , async ( done ) => {
193+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_not : 2 } , { castFn, isAggregateFn, preprocessor } ) ;
194+
195+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") <> ?' ) ;
196+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ 2 ] ) ;
197+
198+ const result = await query ;
199+ expect ( result . map ( _schema => parseInt ( _schema . ownerIdSum , 10 ) ) ) . not . toContain ( 2 ) ;
200+ done ( ) ;
201+ } ) ;
202+
203+ it ( 'correctly filters by _gte' , async ( done ) => {
204+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_gte : 1 } , { castFn, isAggregateFn, preprocessor } ) ;
205+
206+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") >= ?' ) ;
207+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ 1 ] ) ;
208+
209+ const result = await query ;
210+ expect ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) . toBeGreaterThanOrEqual ( 1 ) ;
211+ done ( ) ;
212+ } ) ;
213+
214+ it ( 'correctly filters by _lte' , async ( done ) => {
215+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_lte : 1 } , { castFn, isAggregateFn, preprocessor } ) ;
216+
217+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") <= ?' ) ;
218+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ 1 ] ) ;
219+
220+ const result = await query ;
221+ expect ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) . toBeLessThanOrEqual ( 1 ) ;
222+ done ( ) ;
223+ } ) ;
224+
225+ // TODO @dmerrill 6: Fix this test as it's not passing
226+ xit ( 'correctly filters by _not_in' , async ( done ) => {
227+ const query = knexFlexFilter ( aggregatedQuery , { ownerIdSum_not_in : [ 2 , 3 ] } , { castFn, isAggregateFn, preprocessor } ) ;
228+ console . log ( query . toString ( ) ) ;
229+ expect ( query . _statements [ 2 ] . value . sql ) . toEqual ( 'sum("ownerId") <> ANY(?)' ) ;
230+ expect ( query . _statements [ 2 ] . value . bindings ) . toEqual ( [ [ 2 , 3 ] ] ) ;
231+
232+ const result = await query ;
233+ console . log ( 'result' , result ) ;
234+ expect ( [ 2 , 3 ] ) . not . toContain ( parseInt ( result [ 0 ] . ownerIdSum , 10 ) ) ;
235+ done ( ) ;
236+ } ) ;
237+ } ) ;
238+
139239 describe ( 'when filtering using the jsonb preprocessor' , ( ) => {
140240 const BLOCK_NUMBER = 5000 ;
141241
0 commit comments