Skip to content

Commit

Permalink
WIP/FIXUP - expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jul 11, 2022
1 parent 66ae0a4 commit a2f7807
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 17 deletions.
19 changes: 2 additions & 17 deletions query/graphql/parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ func parseSelect(rootType parserTypes.SelectionType, field *ast.Field, index int

// parse arguments
for _, argument := range field.Arguments {
prop, astValue := getArgumentKeyValue(field, argument)
prop := argument.Name.Value
astValue := argument.Value

// parse filter
if prop == parserTypes.FilterClause {
Expand Down Expand Up @@ -302,22 +303,6 @@ func parseSelect(rootType parserTypes.SelectionType, field *ast.Field, index int
return slct, err
}

// getArgumentKeyValue returns the relevant arguement name and value for the given field-argument
// Note: this function will likely need some rework when adding more aggregate options (e.g. limit)
func getArgumentKeyValue(field *ast.Field, argument *ast.Argument) (string, ast.Value) {
if _, isAggregate := parserTypes.Aggregates[field.Name.Value]; isAggregate {
switch innerProps := argument.Value.(type) {
case *ast.ObjectValue:
for _, innerV := range innerProps.Fields {
if innerV.Name.Value == parserTypes.FilterClause {
return parserTypes.FilterClause, innerV.Value
}
}
}
}
return argument.Name.Value, argument.Value
}

func getFieldAlias(field *ast.Field) string {
if field.Alias == nil {
return field.Name.Value
Expand Down
49 changes: 49 additions & 0 deletions tests/integration/query/simple/with_count_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package simple

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestQuerySimpleWithCountWithFilter(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query, count with filter",
Query: `query {
_count(users: {filter: {Age: {_gt: 26}}})
}`,
Docs: map[int][]string{
0: {
`{
"Name": "John",
"Age": 21
}`,
`{
"Name": "Bob",
"Age": 30
}`,
`{
"Name": "Alice",
"Age": 32
}`,
},
},
Results: []map[string]interface{}{
{
"_count": 2,
},
},
}

executeTestCase(t, test)
}
49 changes: 49 additions & 0 deletions tests/integration/query/simple/with_sum_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package simple

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestQuerySimpleWithSumWithFilter(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple query, sum with filter",
Query: `query {
_sum(users: {field: Age, filter: {Age: {_gt: 26}}})
}`,
Docs: map[int][]string{
0: {
`{
"Name": "John",
"Age": 21
}`,
`{
"Name": "Bob",
"Age": 30
}`,
`{
"Name": "Alice",
"Age": 32
}`,
},
},
Results: []map[string]interface{}{
{
"_sum": int64(62),
},
},
}

executeTestCase(t, test)
}

0 comments on commit a2f7807

Please sign in to comment.