@@ -319,7 +319,7 @@ describe('REST server tests', () => {
319319 expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
320320 expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user2' } ) ;
321321
322- // attribute filter
322+ // String filter
323323 r = await handler ( {
324324 method : 'get' ,
325325 path : '/user' ,
@@ -329,30 +329,53 @@ describe('REST server tests', () => {
329329 expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
330330 expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user1' } ) ;
331331
332- // filter to empty
333332 r = await handler ( {
334333 method : 'get' ,
335334 path : '/user' ,
336- query : { [ 'filter[id]' ] : 'user3' } ,
335+ query : { [ 'filter[email$contains]' ] : '1@abc' } ,
336+ prisma,
337+ } ) ;
338+ expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
339+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user1' } ) ;
340+
341+ r = await handler ( {
342+ method : 'get' ,
343+ path : '/user' ,
344+ query : { [ 'filter[email$contains]' ] : '1@bc' } ,
337345 prisma,
338346 } ) ;
339347 expect ( ( r . body as any ) . data ) . toHaveLength ( 0 ) ;
340348
341- // to-many relation collection filter
342349 r = await handler ( {
343350 method : 'get' ,
344351 path : '/user' ,
345- query : { [ 'filter[posts ]' ] : '2 ' } ,
352+ query : { [ 'filter[email$startsWith ]' ] : 'user1 ' } ,
346353 prisma,
347354 } ) ;
348355 expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
349- expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user2 ' } ) ;
356+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user1 ' } ) ;
350357
351- // multi filter
352358 r = await handler ( {
353359 method : 'get' ,
354360 path : '/user' ,
355- query : { [ 'filter[id]' ] : 'user1' , [ 'filter[posts]' ] : '2' } ,
361+ query : { [ 'filter[email$startsWith]' ] : 'ser1' } ,
362+ prisma,
363+ } ) ;
364+ expect ( ( r . body as any ) . data ) . toHaveLength ( 0 ) ;
365+
366+ r = await handler ( {
367+ method : 'get' ,
368+ path : '/user' ,
369+ query : { [ 'filter[email$endsWith]' ] : '1@abc.com' } ,
370+ prisma,
371+ } ) ;
372+ expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
373+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user1' } ) ;
374+
375+ r = await handler ( {
376+ method : 'get' ,
377+ path : '/user' ,
378+ query : { [ 'filter[email$endsWith]' ] : '1@abc' } ,
356379 prisma,
357380 } ) ;
358381 expect ( ( r . body as any ) . data ) . toHaveLength ( 0 ) ;
@@ -367,6 +390,41 @@ describe('REST server tests', () => {
367390 expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
368391 expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 2 } ) ;
369392
393+ r = await handler ( {
394+ method : 'get' ,
395+ path : '/post' ,
396+ query : { [ 'filter[viewCount$gt]' ] : '0' } ,
397+ prisma,
398+ } ) ;
399+ expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
400+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 2 } ) ;
401+
402+ r = await handler ( {
403+ method : 'get' ,
404+ path : '/post' ,
405+ query : { [ 'filter[viewCount$gte]' ] : '1' } ,
406+ prisma,
407+ } ) ;
408+ expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
409+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 2 } ) ;
410+
411+ r = await handler ( {
412+ method : 'get' ,
413+ path : '/post' ,
414+ query : { [ 'filter[viewCount$lt]' ] : '0' } ,
415+ prisma,
416+ } ) ;
417+ expect ( ( r . body as any ) . data ) . toHaveLength ( 0 ) ;
418+
419+ r = await handler ( {
420+ method : 'get' ,
421+ path : '/post' ,
422+ query : { [ 'filter[viewCount$lte]' ] : '0' } ,
423+ prisma,
424+ } ) ;
425+ expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
426+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 1 } ) ;
427+
370428 // Boolean filter
371429 r = await handler ( {
372430 method : 'get' ,
@@ -377,6 +435,42 @@ describe('REST server tests', () => {
377435 expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
378436 expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 2 } ) ;
379437
438+ // filter to empty
439+ r = await handler ( {
440+ method : 'get' ,
441+ path : '/user' ,
442+ query : { [ 'filter[id]' ] : 'user3' } ,
443+ prisma,
444+ } ) ;
445+ expect ( ( r . body as any ) . data ) . toHaveLength ( 0 ) ;
446+
447+ // to-many relation collection filter
448+ r = await handler ( {
449+ method : 'get' ,
450+ path : '/user' ,
451+ query : { [ 'filter[posts]' ] : '2' } ,
452+ prisma,
453+ } ) ;
454+ expect ( ( r . body as any ) . data ) . toHaveLength ( 1 ) ;
455+ expect ( ( r . body as any ) . data [ 0 ] ) . toMatchObject ( { id : 'user2' } ) ;
456+
457+ r = await handler ( {
458+ method : 'get' ,
459+ path : '/user' ,
460+ query : { [ 'filter[posts]' ] : '1,2,3' } ,
461+ prisma,
462+ } ) ;
463+ expect ( ( r . body as any ) . data ) . toHaveLength ( 2 ) ;
464+
465+ // multi filter
466+ r = await handler ( {
467+ method : 'get' ,
468+ path : '/user' ,
469+ query : { [ 'filter[id]' ] : 'user1' , [ 'filter[posts]' ] : '2' } ,
470+ prisma,
471+ } ) ;
472+ expect ( ( r . body as any ) . data ) . toHaveLength ( 0 ) ;
473+
380474 // to-one relation filter
381475 r = await handler ( {
382476 method : 'get' ,
@@ -420,6 +514,23 @@ describe('REST server tests', () => {
420514 } ,
421515 ] ,
422516 } ) ;
517+
518+ // invalid filter operation
519+ r = await handler ( {
520+ method : 'get' ,
521+ path : '/user' ,
522+ query : { [ 'filter[email$foo]' ] : '1' } ,
523+ prisma,
524+ } ) ;
525+ expect ( r . body ) . toMatchObject ( {
526+ errors : [
527+ {
528+ status : 400 ,
529+ code : 'invalid-filter' ,
530+ title : 'Invalid filter' ,
531+ } ,
532+ ] ,
533+ } ) ;
423534 } ) ;
424535
425536 it ( 'related data filtering' , async ( ) => {
0 commit comments