Skip to content

Commit

Permalink
feat(firestore): support "!=" and "not-in" query operators (#3207)
Browse files Browse the repository at this point in the history
Firestore Version 7.21.0 - September 17, 2020 - introduced support for
the "!=" (NOT_EQUAL) and "not-in" (NOT_IN) query operators:
https://firebase.google.com/support/release-notes/js#version_7210_-_september_17_2020

Query operator documentation:
https://firebase.google.com/docs/firestore/query-data/queries

This patch adds support for these two new operators and adds test
cases for them.
  • Loading branch information
jacksgt committed Nov 17, 2020
1 parent c731dd5 commit 5c44019
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions firestore/integration_test.go
Expand Up @@ -651,6 +651,13 @@ func TestIntegration_QueryDocuments(t *testing.T) {
}{
{q, wants},
{q.Where("q", ">", 1), wants[2:]},
{q.Where("q", "<", 1), wants[:1]},
{q.Where("q", "==", 1), wants[1:2]},
{q.Where("q", "!=", 0), wants[1:]},
{q.Where("q", ">=", 1), wants[1:]},
{q.Where("q", "<=", 1), wants[:2]},
{q.Where("q", "in", []int{0, 1}), wants[:2]},
{q.Where("q", "not-in", []int{0, 1}), wants[2:]},
{q.WherePath([]string{"q"}, ">", 1), wants[2:]},
{q.Offset(1).Limit(1), wants[1:2]},
{q.StartAt(1), wants[1:]},
Expand Down
4 changes: 4 additions & 0 deletions firestore/query.go
Expand Up @@ -500,8 +500,12 @@ func (f filter) toProto() (*pb.StructuredQuery_Filter, error) {
op = pb.StructuredQuery_FieldFilter_GREATER_THAN_OR_EQUAL
case "==":
op = pb.StructuredQuery_FieldFilter_EQUAL
case "!=":
op = pb.StructuredQuery_FieldFilter_NOT_EQUAL
case "in":
op = pb.StructuredQuery_FieldFilter_IN
case "not-in":
op = pb.StructuredQuery_FieldFilter_NOT_IN
case "array-contains":
op = pb.StructuredQuery_FieldFilter_ARRAY_CONTAINS
case "array-contains-any":
Expand Down
12 changes: 11 additions & 1 deletion firestore/query_test.go
Expand Up @@ -143,11 +143,21 @@ func TestQueryToProto(t *testing.T) {
in: q.Where("a", "==", float32(math.NaN())),
want: &pb.StructuredQuery{Where: filtr([]string{"a"}, "==", math.NaN())},
},
{
desc: `q.Where("a", "!=", 3)`,
in: q.Where("a", "!=", 3),
want: &pb.StructuredQuery{Where: filtr([]string{"a"}, "!=", 3)},
},
{
desc: `q.Where("a", "in", []int{7, 8})`,
in: q.Where("a", "in", []int{7, 8}),
want: &pb.StructuredQuery{Where: filtr([]string{"a"}, "in", []int{7, 8})},
},
{
desc: `q.Where("a", "not-in", []int{9})`,
in: q.Where("a", "not-in", []int{9}),
want: &pb.StructuredQuery{Where: filtr([]string{"a"}, "not-in", []int{9})},
},
{
desc: `q.Where("c", "array-contains", 1)`,
in: q.Where("c", "array-contains", 1),
Expand Down Expand Up @@ -456,7 +466,7 @@ func TestQueryToProtoErrors(t *testing.T) {
q := coll.Query
for i, query := range []Query{
{}, // no collection ID
q.Where("x", "!=", 1), // invalid operator
q.Where("x", "<>", 1), // invalid operator
q.Where("~", ">", 1), // invalid path
q.WherePath([]string{"*", ""}, ">", 1), // invalid path
q.StartAt(1), // no OrderBy
Expand Down

0 comments on commit 5c44019

Please sign in to comment.