Skip to content

Commit

Permalink
fix(datastore): adds nil check to AggregationQuery (#7376)
Browse files Browse the repository at this point in the history
* fix(datastore): adds nil check to  RunAggregationQuery
  • Loading branch information
Eric Schmidt committed Feb 13, 2023
1 parent f7fe4f6 commit c43b9ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions datastore/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,14 @@ func (c *Client) Run(ctx context.Context, q *Query) *Iterator {

// RunAggregationQuery gets aggregation query (e.g. COUNT) results from the service.
func (c *Client) RunAggregationQuery(ctx context.Context, aq *AggregationQuery) (AggregationResult, error) {
if aq == nil {
return nil, errors.New("datastore: aggregation query cannot be nil")
}

if aq.query == nil {
return nil, errors.New("datastore: aggregation query must include nested query")
}

if len(aq.aggregationQueries) == 0 {
return nil, errors.New("datastore: aggregation query must contain one or more operators (e.g. count)")
}
Expand Down
30 changes: 30 additions & 0 deletions datastore/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,3 +721,33 @@ func TestAggregationQuery(t *testing.T) {
t.Errorf("want: %v\ngot: %v\n", want, cv)
}
}

func TestAggregationQueryIsNil(t *testing.T) {
client := &Client{
client: &fakeClient{
aggQueryFn: func(req *pb.RunAggregationQueryRequest) (*pb.RunAggregationQueryResponse, error) {
return fakeRunAggregationQuery(req)
},
},
}

var q Query
aq := q.NewAggregationQuery()
_, err := client.RunAggregationQuery(context.Background(), aq)
if err == nil {
t.Fatal(err)
}

q2 := NewQuery("Gopher")
aq2 := q2.NewAggregationQuery()
_, err = client.RunAggregationQuery(context.Background(), aq2)
if err == nil {
t.Fatal(err)
}

aq3 := q2.NewAggregationQuery().WithCount("")
_, err = client.RunAggregationQuery(context.Background(), aq3)
if err == nil {
t.Fatal(err)
}
}

0 comments on commit c43b9ed

Please sign in to comment.