Skip to content

Commit

Permalink
fix(datastore): Return error from RunAggregationQuery (#8222)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhshkh committed Jul 6, 2023
1 parent 3726e9f commit a9b67cf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datastore/client.go
Expand Up @@ -81,7 +81,7 @@ func (dc *datastoreClient) RunAggregationQuery(ctx context.Context, in *pb.RunAg
res, err = dc.c.RunAggregationQuery(ctx, in, opts...)
return err
})
return res, nil
return res, err
}

func (dc *datastoreClient) BeginTransaction(ctx context.Context, in *pb.BeginTransactionRequest, opts ...grpc.CallOption) (res *pb.BeginTransactionResponse, err error) {
Expand Down
77 changes: 77 additions & 0 deletions datastore/integration_test.go
Expand Up @@ -33,6 +33,7 @@ import (
"cloud.google.com/go/rpcreplay"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
pb "google.golang.org/genproto/googleapis/datastore/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -651,6 +652,82 @@ func TestIntegration_Filters(t *testing.T) {
})
}

func TestIntegration_AggregationQueries(t *testing.T) {
ctx := context.Background()
client := newTestClient(ctx, t)
defer client.Close()

parent := NameKey("SQParent", "TestIntegration_Filters"+suffix, nil)
now := timeNow.Truncate(time.Millisecond).Unix()
children := []*SQChild{
{I: 0, T: now, U: now},
{I: 1, T: now, U: now},
{I: 2, T: now, U: now},
{I: 3, T: now, U: now},
{I: 4, T: now, U: now},
{I: 5, T: now, U: now},
{I: 6, T: now, U: now},
{I: 7, T: now, U: now},
}

keys := make([]*Key, len(children))
for i := range keys {
keys[i] = IncompleteKey("SQChild", parent)
}
keys, err := client.PutMulti(ctx, keys, children)
if err != nil {
t.Fatalf("client.PutMulti: %v", err)
}
defer func() {
err := client.DeleteMulti(ctx, keys)
if err != nil {
t.Errorf("client.DeleteMulti: %v", err)
}
}()

baseQuery := NewQuery("SQChild").Ancestor(parent)
testCases := []struct {
desc string
aggQuery *AggregationQuery
wantFailure bool
wantErrMsg string
wantAggResult AggregationResult
}{
{
desc: "Count Failure - Missing index",
aggQuery: baseQuery.Filter("T>=", now).NewAggregationQuery().WithCount("count"),
wantFailure: true,
wantErrMsg: "no matching index found",
wantAggResult: nil,
},
{
desc: "Count Success",
aggQuery: baseQuery.Filter("T=", now).Filter("I>=", 3).NewAggregationQuery().WithCount("count"),
wantFailure: false,
wantErrMsg: "",
wantAggResult: map[string]interface{}{
"count": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: 5}},
},
},
}

for _, testCase := range testCases {
gotAggResult, gotErr := client.RunAggregationQuery(ctx, testCase.aggQuery)
gotFailure := gotErr != nil

if gotFailure != testCase.wantFailure ||
(gotErr != nil && !strings.Contains(gotErr.Error(), testCase.wantErrMsg)) {
t.Errorf("%q: Mismatch in error got: %v, want: %q", testCase.desc, gotErr, testCase.wantErrMsg)
continue
}
if !reflect.DeepEqual(gotAggResult, testCase.wantAggResult) {
t.Errorf("%q: Mismatch in aggregation result got: %v, want: %v", testCase.desc, gotAggResult, testCase.wantAggResult)
continue
}
}

}

type ckey struct{}

func TestIntegration_LargeQuery(t *testing.T) {
Expand Down

0 comments on commit a9b67cf

Please sign in to comment.