@@ -548,10 +548,10 @@ describe("filters", () => {
548548 it ( "should apply search index filters correctly" , ( ) => {
549549 const mockBuilder = new MockSearchBuilder ( ) ;
550550 const searchString = "test search" ;
551- const searchIndexFilters = {
552- userId : { enabled : true , value : "user123" } ,
553- organizationId : { enabled : true , value : 42 } ,
554- } ;
551+ const searchIndexFilters = [
552+ { field : "userId" , enabled : true , value : "user123" } ,
553+ { field : "organizationId" , enabled : true , value : 42 } ,
554+ ] ;
555555 const selectedIndex : SearchIndex = {
556556 indexDescriptor : "testSearchIndex" ,
557557 searchField : "description" ,
@@ -572,38 +572,13 @@ describe("filters", () => {
572572 ] ) ;
573573 } ) ;
574574
575- it ( "should handle empty filters object" , ( ) => {
576- const mockBuilder = new MockSearchBuilder ( ) ;
577- const searchString = "test search" ;
578- const searchIndexFilters = { } ;
579- const selectedIndex : SearchIndex = {
580- indexDescriptor : "testSearchIndex" ,
581- searchField : "description" ,
582- filterFields : [ "userId" , "organizationId" ] ,
583- } ;
584-
585- applySearchIndexFilters (
586- mockBuilder as any ,
587- searchString ,
588- searchIndexFilters ,
589- selectedIndex ,
590- ) ;
591-
592- expect ( mockBuilder . operations ) . toEqual ( [
593- { op : "search" , field : "description" , value : "test search" } ,
594- ] ) ;
595- } ) ;
596-
597575 it ( "should only apply filters for fields that exist in the filter" , ( ) => {
598576 const mockBuilder = new MockSearchBuilder ( ) ;
599577 const searchString = "test search" ;
600- const searchIndexFilters = {
601- userId : {
602- enabled : true ,
603- value : "user123" ,
604- } ,
578+ const searchIndexFilters = [
579+ { field : "userId" , enabled : true , value : "user123" } ,
605580 // organizationId is missing
606- } ;
581+ ] ;
607582 const selectedIndex : SearchIndex = {
608583 indexDescriptor : "testSearchIndex" ,
609584 searchField : "description" ,
@@ -626,16 +601,18 @@ describe("filters", () => {
626601 it ( "should only apply filters for enabled fields" , ( ) => {
627602 const mockBuilder = new MockSearchBuilder ( ) ;
628603 const searchString = "test search" ;
629- const searchIndexFilters = {
630- userId : {
604+ const searchIndexFilters = [
605+ {
606+ field : "userId" ,
631607 enabled : true ,
632608 value : "user123" ,
633609 } ,
634- organizationId : {
610+ {
611+ field : "organizationId" ,
635612 enabled : false ,
636613 value : "org123" ,
637614 } ,
638- } ;
615+ ] ;
639616 const selectedIndex : SearchIndex = {
640617 indexDescriptor : "testSearchIndex" ,
641618 searchField : "description" ,
@@ -658,7 +635,6 @@ describe("filters", () => {
658635 it ( "should handle search index with no filter fields" , ( ) => {
659636 const mockBuilder = new MockSearchBuilder ( ) ;
660637 const searchString = "test search" ;
661- const searchIndexFilters = { } ;
662638 const selectedIndex : SearchIndex = {
663639 indexDescriptor : "testSearchIndex" ,
664640 searchField : "description" ,
@@ -668,7 +644,7 @@ describe("filters", () => {
668644 applySearchIndexFilters (
669645 mockBuilder as any ,
670646 searchString ,
671- searchIndexFilters ,
647+ [ ] ,
672648 selectedIndex ,
673649 ) ;
674650
@@ -805,10 +781,10 @@ describe("filters", () => {
805781
806782 const result = validateSearchIndexFilter (
807783 indexName ,
808- {
809- userId : { enabled : true , value : BigInt ( 123 ) } ,
810- organization : { enabled : true , value : "exampleOrg" } ,
811- } ,
784+ [
785+ { field : "userId" , enabled : true , value : BigInt ( 123 ) } ,
786+ { field : "organization" , enabled : true , value : "exampleOrg" } ,
787+ ] ,
812788 selectedIndex ,
813789 "asc" ,
814790 ) ;
@@ -821,7 +797,7 @@ describe("filters", () => {
821797
822798 const result = validateSearchIndexFilter (
823799 indexName ,
824- { } ,
800+ [ ] ,
825801 selectedIndex ,
826802 "asc" ,
827803 ) ;
@@ -841,15 +817,15 @@ describe("filters", () => {
841817
842818 const result = validateSearchIndexFilter (
843819 indexName ,
844- {
845- userId : { enabled : true , value : BigInt ( 123 ) } ,
846- organization : { enabled : true , value : "exampleOrg" } ,
847- } ,
820+ [
821+ { field : "userId" , enabled : true , value : BigInt ( 123 ) } ,
822+ { field : "organization" , enabled : true , value : "exampleOrg" } ,
823+ ] ,
848824 selectedIndex ,
849825 "asc" ,
850826 ) ;
851827 expect ( result ) . toEqual ( {
852- filter : - 1 ,
828+ filter : 1 ,
853829 error :
854830 "Invalid index filter selection: found a filter for field `organization` which is not part of the filter fields of the search index `testIndex`." ,
855831 } ) ;
@@ -865,17 +841,17 @@ describe("filters", () => {
865841
866842 const result = validateSearchIndexFilter (
867843 indexName ,
868- {
869- userId : { enabled : true , value : BigInt ( 123 ) } ,
870- organization : { enabled : false , value : "exampleOrg" } ,
871- } ,
844+ [
845+ { field : "userId" , enabled : true , value : BigInt ( 123 ) } ,
846+ { field : "organization" , enabled : false , value : "exampleOrg" } ,
847+ ] ,
872848 selectedIndex ,
873849 "asc" ,
874850 ) ;
875851 expect ( result ) . toEqual ( undefined ) ;
876852 } ) ;
877853
878- it ( "should return error when a search index filter references the search field" , ( ) => {
854+ it ( "should return an error when a search index filter references the search field" , ( ) => {
879855 const indexName = "testIndex" ;
880856 const selectedIndex : SearchIndex = {
881857 indexDescriptor : "testSearchIndex" ,
@@ -885,19 +861,66 @@ describe("filters", () => {
885861
886862 const result = validateSearchIndexFilter (
887863 indexName ,
888- {
889- description : { enabled : true , value : "test" } ,
890- } ,
864+ [
865+ {
866+ field : "description" ,
867+ enabled : true ,
868+ value : "test" ,
869+ } ,
870+ ] ,
891871 selectedIndex ,
892872 "asc" ,
893873 ) ;
894874 expect ( result ) . toEqual ( {
895- filter : - 1 ,
875+ filter : 0 ,
896876 error :
897877 "Invalid index filter selection: found a filter for field `description` which is not part of the filter fields of the search index `testIndex`." ,
898878 } ) ;
899879 } ) ;
900880
881+ it ( "should return error when there are two indexes for the same field" , ( ) => {
882+ const indexName = "testIndex" ;
883+ const selectedIndex : SearchIndex = {
884+ indexDescriptor : "testSearchIndex" ,
885+ searchField : "description" ,
886+ filterFields : [ "userId" ] ,
887+ } ;
888+
889+ const result = validateSearchIndexFilter (
890+ indexName ,
891+ [
892+ { field : "userId" , enabled : true , value : BigInt ( 123 ) } ,
893+ { field : "userId" , enabled : true , value : BigInt ( 123 ) } ,
894+ ] ,
895+ selectedIndex ,
896+ "asc" ,
897+ ) ;
898+ expect ( result ) . toEqual ( {
899+ filter : 0 ,
900+ error : "Invalid filter: there are multiple filters for field `userId`." ,
901+ } ) ;
902+ } ) ;
903+
904+ it ( "should not return an error when there are two indexes for the same field but only one is enabled" , ( ) => {
905+ const indexName = "testIndex" ;
906+ const selectedIndex : SearchIndex = {
907+ indexDescriptor : "testSearchIndex" ,
908+ searchField : "description" ,
909+ filterFields : [ "userId" , "organizationId" ] ,
910+ } ;
911+
912+ const result = validateSearchIndexFilter (
913+ indexName ,
914+ [
915+ { field : "userId" , enabled : true , value : BigInt ( 123 ) } ,
916+ { field : "userId" , enabled : false , value : BigInt ( 456 ) } ,
917+ ] ,
918+ selectedIndex ,
919+ "asc" ,
920+ ) ;
921+ expect ( result ) . toEqual ( undefined ) ;
922+ } ) ;
923+
901924 it ( "should return error when querying a search index in descending order" , ( ) => {
902925 const indexName = "testIndex" ;
903926 const selectedIndex : SearchIndex = {
@@ -908,7 +931,7 @@ describe("filters", () => {
908931
909932 const result = validateSearchIndexFilter (
910933 indexName ,
911- { } ,
934+ [ ] ,
912935 selectedIndex ,
913936 "desc" ,
914937 ) ;
0 commit comments