@@ -9,6 +9,7 @@ describe('queryFilter()', function () {
99
1010 context ( 'when use default configurations' , function ( ) {
1111 context ( 'when query is empty' , function ( ) {
12+
1213 it ( 'should return req.query as default middleware options' , function ( done ) {
1314 const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : { } } )
1415 const res = httpMocks . createResponse ( )
@@ -113,6 +114,21 @@ describe('queryFilter()', function () {
113114 } )
114115 } )
115116
117+
118+ context ( 'when query filters param value is a object' , function ( ) {
119+ it ( 'should return req.query as default middleware options' , function ( done ) {
120+
121+ const query = { value : { and : 123 } }
122+ const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
123+ const res = httpMocks . createResponse ( )
124+
125+ qs ( { } ) ( req , res , function next ( ) {
126+ validate ( req , default_options )
127+ } )
128+ done ( )
129+ } )
130+ } )
131+
116132 context ( 'when query contains simple filters param' , function ( ) {
117133 it ( 'should return req.query with set field params' , function ( done ) {
118134 const expect_filters = { name : 'lucas' , age : 30 }
@@ -200,15 +216,15 @@ describe('queryFilter()', function () {
200216 } )
201217
202218 context ( 'when query contains date filters param' , function ( ) {
203- it ( 'should return req.query with set date_start params as date' , function ( done ) {
219+ it ( 'should return req.query with set start_at params as date' , function ( done ) {
204220 const expect_filters = {
205221 $and : [
206222 { created_at : { $lt : new Date ( ) . toISOString ( ) } } ,
207223 { created_at : { $gte : '2018-12-05T00:00:00.000Z' } }
208224 ]
209225 }
210226
211- const query = { date_start : '2018-12-05' }
227+ const query = { start_at : '2018-12-05' }
212228 const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
213229 const res = httpMocks . createResponse ( )
214230
@@ -221,15 +237,15 @@ describe('queryFilter()', function () {
221237 done ( )
222238 } )
223239
224- it ( 'should return req.query with set date_start params as dateTime' , function ( done ) {
240+ it ( 'should return req.query with set start_at params as dateTime' , function ( done ) {
225241 const expect_filters = {
226242 $and : [
227243 { created_at : { $lt : new Date ( ) . toISOString ( ) } } ,
228244 { created_at : { $gte : '2018-12-05T00:00:00.000Z' } }
229245 ]
230246 }
231247
232- const query = { date_start : '2018-12-05T00:00:00' }
248+ const query = { start_at : '2018-12-05T00:00:00' }
233249 const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
234250 const res = httpMocks . createResponse ( )
235251
@@ -242,15 +258,15 @@ describe('queryFilter()', function () {
242258 done ( )
243259 } )
244260
245- it ( 'should return req.query with set date_start and date_end params as date' , function ( done ) {
261+ it ( 'should return req.query with set start_at and end_at params as date' , function ( done ) {
246262
247263 const expect_filters = {
248264 $and : [
249265 { created_at : { $lt : '2018-12-11T00:00:00.000Z' } } ,
250266 { created_at : { $gte : '2018-12-01T00:00:00.000Z' } } ]
251267 }
252268
253- const query = { date_start : '2018-12-01' , date_end : '2018-12-11' }
269+ const query = { start_at : '2018-12-01' , end_at : '2018-12-11' }
254270 const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
255271 const res = httpMocks . createResponse ( )
256272
@@ -263,15 +279,15 @@ describe('queryFilter()', function () {
263279 done ( )
264280 } )
265281
266- it ( 'should return req.query with set date_start and date_end params as dateTime' , function ( done ) {
282+ it ( 'should return req.query with set start_at and end_at params as dateTime' , function ( done ) {
267283
268284 const expect_filters = {
269285 $and : [
270286 { created_at : { $lt : '2018-12-11T00:00:00.000Z' } } ,
271287 { created_at : { $gte : '2018-12-01T00:00:00.000Z' } } ]
272288 }
273289
274- const query = { date_start : '2018-12-01T00:00:00' , date_end : '2018-12-11T00:00:00' }
290+ const query = { start_at : '2018-12-01T00:00:00' , end_at : '2018-12-11T00:00:00' }
275291 const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
276292 const res = httpMocks . createResponse ( )
277293
@@ -284,15 +300,14 @@ describe('queryFilter()', function () {
284300 done ( )
285301 } )
286302
287- it ( 'should return req.query with set period and date_end param' , function ( done ) {
288-
303+ it ( 'should return req.query with set period and end_at param' , function ( done ) {
289304 const expect_filters = {
290305 $and : [
291306 { created_at : { $lt : new Date ( '2018-12-09' ) . toISOString ( ) } } ,
292307 { created_at : { $gte : new Date ( '2018-11-10' ) . toISOString ( ) } }
293308 ]
294309 }
295- const query = { period : '1m' , date_end : '2018-12-09' }
310+ const query = { period : '1m' , end_at : '2018-12-09' }
296311 const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
297312 const res = httpMocks . createResponse ( )
298313
@@ -402,6 +417,31 @@ describe('queryFilter()', function () {
402417 } )
403418 done ( )
404419 } )
420+
421+ it ( 'should return req.query with today start_at for invalid period' , function ( done ) {
422+
423+ const now = new Date ( )
424+ const today = dateToString ( new Date ( now . getFullYear ( ) , now . getMonth ( ) , now . getDate ( ) + 1 ) )
425+ const beforeToday = dateToString ( new Date ( now . getFullYear ( ) - 1 , now . getMonth ( ) , now . getDate ( ) ) )
426+ const expect_filters = {
427+ $and : [
428+ { created_at : { $lt : new Date ( today ) . toISOString ( ) } } ,
429+ { created_at : { $gte : new Date ( beforeToday ) . toISOString ( ) } }
430+ ]
431+ }
432+
433+ const query = { period : '12' }
434+ const req = httpMocks . createRequest ( { method : 'GET' , url : '/' , query : query } )
435+ const res = httpMocks . createResponse ( )
436+
437+ const options = JSON . parse ( JSON . stringify ( default_options ) )
438+ options . default . filters = expect_filters
439+
440+ qs ( { } ) ( req , res , function next ( ) {
441+ validateFilterWithDate ( req , options )
442+ } )
443+ done ( )
444+ } )
405445 } )
406446
407447 context ( 'when use custom options' , function ( ) {
0 commit comments