1919import static com .google .firebase .firestore .Filter .equalTo ;
2020import static com .google .firebase .firestore .Filter .greaterThan ;
2121import static com .google .firebase .firestore .Filter .inArray ;
22+ import static com .google .firebase .firestore .Filter .lessThan ;
23+ import static com .google .firebase .firestore .Filter .notEqualTo ;
2224import static com .google .firebase .firestore .Filter .notInArray ;
2325import static com .google .firebase .firestore .Filter .or ;
2426import static com .google .firebase .firestore .testutil .Assert .assertThrows ;
@@ -532,41 +534,11 @@ public void queryOrderByKeyBoundsMustBeStringsWithoutSlashes() {
532534 + "document path, but 'foo' is not because it contains an odd number of segments." );
533535 }
534536
535- @ Test
536- public void queriesWithDifferentInequalityFieldsFail () {
537- expectError (
538- () -> testCollection ().whereGreaterThan ("x" , 32 ).whereLessThan ("y" , "cat" ),
539- "All where filters with an inequality (notEqualTo, notIn, lessThan, "
540- + "lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the "
541- + "same field. But you have filters on 'x' and 'y'" );
542- }
543-
544- @ Test
545- public void queriesWithInequalityDifferentThanFirstOrderByFail () {
546- CollectionReference collection = testCollection ();
547- String reason =
548- "Invalid query. You have an inequality where filter (whereLessThan(), "
549- + "whereGreaterThan(), etc.) on field 'x' and so you must also have 'x' as "
550- + "your first orderBy() field, but your first orderBy() is currently on field 'y' "
551- + "instead." ;
552- expectError (() -> collection .whereGreaterThan ("x" , 32 ).orderBy ("y" ), reason );
553- expectError (() -> collection .orderBy ("y" ).whereGreaterThan ("x" , 32 ), reason );
554- expectError (() -> collection .whereGreaterThan ("x" , 32 ).orderBy ("y" ).orderBy ("x" ), reason );
555- expectError (() -> collection .orderBy ("y" ).orderBy ("x" ).whereGreaterThan ("x" , 32 ), reason );
556- expectError (() -> collection .orderBy ("y" ).orderBy ("x" ).whereNotEqualTo ("x" , 32 ), reason );
557- }
558-
559537 @ Test
560538 public void queriesWithMultipleNotEqualAndInequalitiesFail () {
561539 expectError (
562540 () -> testCollection ().whereNotEqualTo ("x" , 32 ).whereNotEqualTo ("x" , 33 ),
563541 "Invalid Query. You cannot use more than one '!=' filter." );
564-
565- expectError (
566- () -> testCollection ().whereNotEqualTo ("x" , 32 ).whereGreaterThan ("y" , 33 ),
567- "All where filters with an inequality (notEqualTo, notIn, lessThan, "
568- + "lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the "
569- + "same field. But you have filters on 'x' and 'y'" );
570542 }
571543
572544 @ Test
@@ -584,9 +556,7 @@ public void queriesWithNotEqualAndNotInFiltersFail() {
584556 public void queriesWithMultipleDisjunctiveFiltersFail () {
585557 expectError (
586558 () -> testCollection ().whereNotIn ("foo" , asList (1 , 2 )).whereNotIn ("bar" , asList (1 , 2 )),
587- "All where filters with an inequality (notEqualTo, notIn, lessThan, "
588- + "lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the "
589- + "same field. But you have filters on 'foo' and 'bar'" );
559+ "Invalid Query. You cannot use more than one 'not_in' filter." );
590560
591561 expectError (
592562 () ->
@@ -707,37 +677,71 @@ public void queriesUsingInAndDocumentIdMustHaveProperDocumentReferencesInArray()
707677 }
708678
709679 @ Test
710- public void testInvalidQueryFilters () {
680+ public void testConflictingOperatorsInCompositeFilters () {
711681 CollectionReference collection = testCollection ();
682+ // Composite queries can validate conflicting operators in multiple inequality.
683+ String reason = "Invalid Query. You cannot use more than one '!=' filter." ;
684+ expectError (
685+ () ->
686+ collection
687+ .where (
688+ or (
689+ and (lessThan ("a" , "b" ), inArray ("c" , Arrays .asList ("d" , "e" ))),
690+ and (equalTo ("e" , "f" ), notEqualTo ("g" , "h" ))))
691+ .where (
692+ or (
693+ and (greaterThan ("i" , "j" ), greaterThan ("k" , "l" )),
694+ and (notEqualTo ("o" , "p" ), lessThan ("q" , "r" )))),
695+ reason );
712696
713- // Multiple inequalities, one of which is inside a nested composite filter.
714- String reason =
715- "All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on 'c' and 'r'" ;
697+ reason = "Invalid Query. You cannot use more than one 'not_in' filter." ;
716698 expectError (
717699 () ->
718700 collection
719701 .where (
720702 or (
721- and (equalTo ("a" , "b" ), greaterThan ("c" , "d" )),
722- and (equalTo ("e" , "f" ), equalTo ("g" , "h" ))))
723- .where (greaterThan ("r" , "s" )),
703+ and (lessThan ("a" , "b" ), notInArray ("c" , Arrays .asList ("d" , "e" ))),
704+ and (equalTo ("e" , "f" ), lessThan ("g" , "j" ))))
705+ .where (
706+ or (
707+ and (greaterThan ("i" , "j" ), greaterThan ("k" , "l" )),
708+ and (notInArray ("o" , Arrays .asList ("p" , "q" )), lessThan ("q" , "r" )))),
724709 reason );
725710
726- // OrderBy and inequality on different fields. Inequality inside a nested composite filter.
727- reason =
728- "Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'c' and so you must also have 'c' as your first orderBy() field, but your first orderBy() is currently on field 'r' instead." ;
711+ reason = "Invalid Query. You cannot use 'not_in' filters with '!=' filters." ;
729712 expectError (
730713 () ->
731714 collection
732715 .where (
733716 or (
734- and (equalTo ("a" , "b" ), greaterThan ("c" , "d" )),
735- and (equalTo ("e" , "f" ), equalTo ("g" , "h" ))))
736- .orderBy ("r" ),
717+ and (lessThan ("a" , "b" ), greaterThan ("c" , "d" )),
718+ and (equalTo ("e" , "f" ), notEqualTo ("g" , "h" ))))
719+ .where (
720+ or (
721+ and (greaterThan ("i" , "j" ), greaterThan ("k" , "l" )),
722+ and (notInArray ("o" , Arrays .asList ("p" , "q" )), lessThan ("q" , "r" )))),
737723 reason );
738724
739- // Conflicting operations within a composite filter.
740725 reason = "Invalid Query. You cannot use 'not_in' filters with 'in' filters." ;
726+ expectError (
727+ () ->
728+ collection
729+ .where (
730+ or (
731+ and (lessThan ("a" , "b" ), inArray ("c" , Arrays .asList ("d" , "e" ))),
732+ and (equalTo ("e" , "f" ), lessThan ("g" , "j" ))))
733+ .where (
734+ or (
735+ and (greaterThan ("i" , "j" ), greaterThan ("k" , "l" )),
736+ and (notInArray ("o" , Arrays .asList ("p" , "q" )), lessThan ("q" , "r" )))),
737+ reason );
738+ }
739+
740+ @ Test
741+ public void testInvalidQueryFilters () {
742+ CollectionReference collection = testCollection ();
743+ // Conflicting operations within a composite filter.
744+ String reason = "Invalid Query. You cannot use 'not_in' filters with 'in' filters." ;
741745 expectError (
742746 () ->
743747 collection .where (
0 commit comments