Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datastore): EntityFilter for AND/OR queries #7589

Merged
merged 9 commits into from
Apr 3, 2023
24 changes: 12 additions & 12 deletions datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func TestQueryConstruction(t *testing.T) {
q: NewQuery("Foo").Filter("foo >", 7),
exp: &Query{
kind: "Foo",
filter: []filter{
{
filter: []EntityFilter{
PropertyFilter{
FieldName: "foo",
Op: greaterThan,
Operator: string(greaterThan),
Value: 7,
},
},
Expand All @@ -61,10 +61,10 @@ func TestQueryConstruction(t *testing.T) {
q: NewQuery("Foo").Filter("foo=", 6),
exp: &Query{
kind: "Foo",
filter: []filter{
{
filter: []EntityFilter{
PropertyFilter{
FieldName: "foo",
Op: equal,
Operator: string(equal),
Value: 6,
},
},
Expand All @@ -76,10 +76,10 @@ func TestQueryConstruction(t *testing.T) {
q: NewQuery("Foo").Filter(" foo< ", 8),
exp: &Query{
kind: "Foo",
filter: []filter{
{
filter: []EntityFilter{
PropertyFilter{
FieldName: "foo",
Op: lessThan,
Operator: string(lessThan),
Value: 8,
},
},
Expand All @@ -91,10 +91,10 @@ func TestQueryConstruction(t *testing.T) {
q: NewQuery("Foo").Filter("foo >=", 9),
exp: &Query{
kind: "Foo",
filter: []filter{
{
filter: []EntityFilter{
PropertyFilter{
FieldName: "foo",
Op: greaterEq,
Operator: string(greaterEq),
Value: 9,
},
},
Expand Down
64 changes: 64 additions & 0 deletions datastore/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,70 @@ func testSmallQueries(ctx context.Context, t *testing.T, client *Client, parent
}
}

func TestIntegration_FilterEntity(t *testing.T) {
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.Background()
client := newTestClient(ctx, t)
defer client.Close()

parent := NameKey("SQParent", "TestIntegration_Filters"+suffix, nil)
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
now := timeNow.Truncate(time.Millisecond).Unix()
tomorrow := timeNow.Truncate(time.Millisecond).AddDate(0, 0, 1).Unix()
children := []*SQChild{
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
{I: 0, J: 99, T: tomorrow, U: now},
{I: 1, J: 98, T: tomorrow, U: now},
{I: 2, J: 97, T: tomorrow, U: now},
{I: 3, J: 96, T: now, U: now},
{I: 4, J: 95, T: now, U: now},
{I: 5, J: 94, T: now, U: now},
{I: 6, J: 93, T: now, U: now},
{I: 7, J: 92, T: now, U: now},
}
baseQuery := NewQuery("SQChild").Ancestor(parent)
testSmallQueries(ctx, t, client, parent, children, []SQTestCase{
{
desc: "I>1",
q: baseQuery.Filter("T=", now).FilterEntity(
PropertyFilter{FieldName: "I", Operator: ">", Value: 1}),
wantCount: 5,
wantSum: 3 + 4 + 5 + 6 + 7 + 96 + 95 + 94 + 93 + 92,
},
{
desc: "I<=1 or I >= 6",
q: baseQuery.Filter("T=", now).FilterEntity(OrFilter{
[]EntityFilter{
PropertyFilter{FieldName: "I", Operator: "<", Value: 4},
PropertyFilter{FieldName: "I", Operator: ">=", Value: 6},
},
}),
wantCount: 3,
wantSum: 3 + 6 + 7 + 92 + 93 + 96,
},
{
desc: "(T = now) and (((J > 97) and (T = tomorrow)) or (J < 94))",
q: baseQuery.FilterEntity(
AndFilter{
Filters: []EntityFilter{
OrFilter{
Filters: []EntityFilter{
AndFilter{
[]EntityFilter{
PropertyFilter{FieldName: "J", Operator: ">", Value: 97},
PropertyFilter{FieldName: "T", Operator: "=", Value: tomorrow},
},
},
PropertyFilter{FieldName: "J", Operator: "<", Value: 94},
},
},
PropertyFilter{FieldName: "T", Operator: "=", Value: now},
},
},
),
wantCount: 2,
wantSum: 6 + 7 + 92 + 93,
},
})
}

func TestIntegration_Filters(t *testing.T) {
ctx := context.Background()
client := newTestClient(ctx, t)
Expand Down
Loading