@@ -1020,13 +1020,22 @@ func TestIntegration_QueryDocuments_WhereEntity(t *testing.T) {
10201020 })
10211021}
10221022
1023+ func reverseSlice (s []map [string ]interface {}) []map [string ]interface {} {
1024+ reversed := make ([]map [string ]interface {}, len (s ))
1025+ for i , j := 0 , len (s )- 1 ; i <= j ; i , j = i + 1 , j - 1 {
1026+ reversed [i ] = s [j ]
1027+ reversed [j ] = s [i ]
1028+ }
1029+ return reversed
1030+ }
1031+
10231032func TestIntegration_QueryDocuments (t * testing.T ) {
10241033 ctx := context .Background ()
10251034 coll := integrationColl (t )
10261035 h := testHelper {t }
10271036 var wants []map [string ]interface {}
10281037 var createdDocRefs []* DocumentRef
1029- for i := 0 ; i < 3 ; i ++ {
1038+ for i := 0 ; i < 8 ; i ++ {
10301039 doc := coll .NewDoc ()
10311040 createdDocRefs = append (createdDocRefs , doc )
10321041
@@ -1037,46 +1046,69 @@ func TestIntegration_QueryDocuments(t *testing.T) {
10371046 }
10381047 q := coll .Select ("q" )
10391048 for i , test := range []struct {
1040- q Query
1041- want []map [string ]interface {}
1042- orderBy bool // Some query types do not allow ordering.
1049+ desc string
1050+ q Query
1051+ want []map [string ]interface {}
1052+ orderBy bool // Some query types do not allow ordering.
1053+ orderByDir Direction
10431054 }{
1044- {q , wants , true },
1045- {q .Where ("q" , ">" , 1 ), wants [2 :], true },
1046- {q .Where ("q" , "<" , 1 ), wants [:1 ], true },
1047- {q .Where ("q" , "==" , 1 ), wants [1 :2 ], false },
1048- {q .Where ("q" , "!=" , 0 ), wants [1 :], true },
1049- {q .Where ("q" , ">=" , 1 ), wants [1 :], true },
1050- {q .Where ("q" , "<=" , 1 ), wants [:2 ], true },
1051- {q .Where ("q" , "in" , []int {0 }), wants [:1 ], false },
1052- {q .Where ("q" , "not-in" , []int {0 , 1 }), wants [2 :], true },
1053- {q .WherePath ([]string {"q" }, ">" , 1 ), wants [2 :], true },
1054- {q .Offset (1 ).Limit (1 ), wants [1 :2 ], true },
1055- {q .StartAt (1 ), wants [1 :], true },
1056- {q .StartAfter (1 ), wants [2 :], true },
1057- {q .EndAt (1 ), wants [:2 ], true },
1058- {q .EndBefore (1 ), wants [:1 ], true },
1059- {q .LimitToLast (2 ), wants [1 :], true },
1060- {q .EndBefore (2 ).LimitToLast (2 ), wants [:2 ], true },
1061- {q .StartAt (1 ).EndBefore (2 ).LimitToLast (3 ), wants [1 :2 ], true },
1055+ {"Without filters" , q , wants , true , 0 },
1056+ {"> filter" , q .Where ("q" , ">" , 1 ), wants [2 :], true , Asc },
1057+ {"< filter" , q .Where ("q" , "<" , 1 ), wants [:1 ], true , Asc },
1058+ {"== filter" , q .Where ("q" , "==" , 1 ), wants [1 :2 ], false , 0 },
1059+ {"!= filter" , q .Where ("q" , "!=" , 0 ), wants [1 :], true , Asc },
1060+ {">= filter" , q .Where ("q" , ">=" , 1 ), wants [1 :], true , Asc },
1061+ {"<= filter" , q .Where ("q" , "<=" , 1 ), wants [:2 ], true , Asc },
1062+ {"in filter" , q .Where ("q" , "in" , []int {0 }), wants [:1 ], false , 0 },
1063+ {"not-in filter" , q .Where ("q" , "not-in" , []int {0 , 1 }), wants [2 :], true , Asc },
1064+ {"WherePath" , q .WherePath ([]string {"q" }, ">" , 1 ), wants [2 :], true , Asc },
1065+ {"Offset with Limit" , q .Offset (1 ).Limit (1 ), wants [1 :2 ], true , Asc },
1066+ {"StartAt" , q .StartAt (1 ), wants [1 :], true , Asc },
1067+ {"StartAfter" , q .StartAfter (1 ), wants [2 :], true , Asc },
1068+ {"EndAt" , q .EndAt (1 ), wants [:2 ], true , Asc },
1069+ {"EndBefore" , q .EndBefore (1 ), wants [:1 ], true , Asc },
1070+ {"Open range with DESC order" , q .StartAfter (6 ).EndBefore (2 ), reverseSlice (wants [3 :6 ]), true , Desc },
1071+ {"LimitToLast" , q .LimitToLast (2 ), wants [len (wants )- 2 :], true , Asc },
1072+ {"StartAfter with LimitToLast" , q .StartAfter (2 ).LimitToLast (2 ), wants [len (wants )- 2 :], true , Asc },
1073+ {"StartAt with LimitToLast" , q .StartAt (2 ).LimitToLast (2 ), wants [len (wants )- 2 :], true , Asc },
1074+ {"EndBefore with LimitToLast" , q .EndBefore (7 ).LimitToLast (2 ), wants [5 :7 ], true , Asc },
1075+ {"EndAt with LimitToLast" , q .EndAt (7 ).LimitToLast (2 ), wants [6 :8 ], true , Asc },
1076+ {"LimitToLast greater than no. of results" , q .StartAt (1 ).EndBefore (2 ).LimitToLast (3 ), wants [1 :2 ], true , Asc },
1077+ {"Closed range with LimitToLast ASC order" , q .StartAt (2 ).EndAt (6 ).LimitToLast (2 ), wants [5 :7 ], true , Asc },
1078+ {"Left closed right open range with LimitToLast ASC order" , q .StartAt (2 ).EndBefore (6 ).LimitToLast (2 ), wants [4 :6 ], true , Asc },
1079+ {"Left open right closed with LimitToLast ASC order" , q .StartAfter (2 ).EndAt (6 ).LimitToLast (2 ), wants [5 :7 ], true , Asc },
1080+ {"Open range with LimitToLast ASC order" , q .StartAfter (2 ).EndBefore (6 ).LimitToLast (2 ), wants [4 :6 ], true , Asc },
1081+ {"Closed range with LimitToLast DESC order" , q .StartAt (6 ).EndAt (2 ).LimitToLast (2 ), reverseSlice (wants [2 :4 ]), true , Desc },
1082+ {"Left closed right open range with LimitToLast DESC order" , q .StartAt (6 ).EndBefore (2 ).LimitToLast (2 ), reverseSlice (wants [3 :5 ]), true , Desc },
1083+ {"Left open right closed with LimitToLast DESC order" , q .StartAfter (6 ).EndAt (2 ).LimitToLast (2 ), reverseSlice (wants [2 :4 ]), true , Desc },
1084+ {"Open range with LimitToLast DESC order" , q .StartAfter (6 ).EndBefore (2 ).LimitToLast (2 ), reverseSlice (wants [3 :5 ]), true , Desc },
10621085 } {
10631086 if test .orderBy {
1064- test .q = test .q .OrderBy ("q" , Asc )
1087+ test .q = test .q .OrderBy ("q" , test . orderByDir )
10651088 }
10661089 gotDocs , err := test .q .Documents (ctx ).GetAll ()
10671090 if err != nil {
1068- t .Errorf ("#%d: %+v: %v" , i , test .q , err )
1091+ t .Errorf ("#%d %v : %+v: %v" , i , test . desc , test .q , err )
10691092 continue
10701093 }
10711094 if len (gotDocs ) != len (test .want ) {
1072- t .Errorf ("#%d: %+v: got %d docs, want %d" , i , test .q , len (gotDocs ), len (test .want ))
1095+ t .Errorf ("#%d %v : %+v: got %d docs, want %d" , i , test . desc , test .q , len (gotDocs ), len (test .want ))
10731096 continue
10741097 }
1098+
1099+ fmt .Printf ("test.want: %+v\n " , test .want )
1100+
1101+ docsEqual := true
1102+ docsNotEqualErr := ""
10751103 for j , g := range gotDocs {
10761104 if got , want := g .Data (), test .want [j ]; ! testEqual (got , want ) {
1077- t .Errorf ("#%d: %+v, #%d: got\n %+v\n want\n %+v" , i , test .q , j , got , want )
1105+ docsNotEqualErr += fmt .Sprintf ("\n \t #%d: got %+v want %+v" , j , got , want )
1106+ docsEqual = false
10781107 }
10791108 }
1109+ if ! docsEqual {
1110+ t .Errorf ("#%d %v: %+v %v" , i , test .desc , test .q , docsNotEqualErr )
1111+ }
10801112 }
10811113 _ , err := coll .Select ("q" ).Where ("x" , "==" , 1 ).OrderBy ("q" , Asc ).Documents (ctx ).GetAll ()
10821114 codeEq (t , "Where and OrderBy on different fields without an index" , codes .FailedPrecondition , err )
0 commit comments