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(GraphQL): Add aggregate query at root level #6985

Merged
merged 6 commits into from Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 77 additions & 22 deletions graphql/e2e/auth/auth_test.go
Expand Up @@ -1011,24 +1011,54 @@ func TestRootFilter(t *testing.T) {
}
}

func TestRootCountQuery(t *testing.T) {
testCases := []TestCase{{
user: "user1",
role: "USER",
result: `{"aggregateColumn": {"count": 1}}`,
}, {
user: "user2",
role: "USER",
result: `{"aggregateColumn": {"count": 3}}`,
}, {
user: "user4",
role: "USER",
result: `{"aggregateColumn": {"count": 2}}`,
}}
func TestRootAggregateQuery(t *testing.T) {
testCases := []TestCase{
{
user: "user1",
role: "USER",
result: `
{
"aggregateColumn":
{
"count": 1,
"nameMin": "Column1",
"nameMax": "Column1"
}
}`,
},
{
user: "user2",
role: "USER",
result: `
{
"aggregateColumn":
{
"count": 3,
"nameMin": "Column1",
"nameMax": "Column3"
}
}`,
},
{
user: "user4",
role: "USER",
result: `
{
"aggregateColumn":
{
"count": 2,
"nameMin": "Column2",
"nameMax": "Column3"
}
}`,
},
}
query := `
query {
aggregateColumn {
count
nameMin
nameMax
}
}`

Expand All @@ -1040,9 +1070,9 @@ func TestRootCountQuery(t *testing.T) {
}

gqlResponse := params.ExecuteAsPost(t, common.GraphqlURL)
require.Nil(t, gqlResponse.Errors)
common.RequireNoGQLErrors(t, gqlResponse)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
require.JSONEq(t, tcase.result, string(gqlResponse.Data))
})
}
}
Expand Down Expand Up @@ -1108,16 +1138,41 @@ func TestRBACFilter(t *testing.T) {
}
}

func TestRBACFilterWithCountQuery(t *testing.T) {
func TestRBACFilterWithAggregateQuery(t *testing.T) {
testCases := []TestCase{
{role: "USER", result: `{"aggregateLog": null}`},
{result: `{"aggregateLog": null}`},
{role: "ADMIN", result: `{"aggregateLog": {"count": 2}}`}}
{
role: "USER",
result: `
{
"aggregateLog": null
}`,
},
{
result: `
{
"aggregateLog": null
}`,
},
{
role: "ADMIN",
result: `
{
"aggregateLog":
{
"count": 2,
"randomMin": "test",
"randomMax": "test"
}
}`,
},
}

query := `
query {
aggregateLog {
count
randomMin
randomMax
}
}
`
Expand All @@ -1130,9 +1185,9 @@ func TestRBACFilterWithCountQuery(t *testing.T) {
}

gqlResponse := params.ExecuteAsPost(t, common.GraphqlURL)
require.Nil(t, gqlResponse.Errors)
common.RequireNoGQLErrors(t, gqlResponse)

require.JSONEq(t, string(gqlResponse.Data), tcase.result)
require.JSONEq(t, tcase.result, string(gqlResponse.Data))
})
}
}
Expand Down
39 changes: 37 additions & 2 deletions graphql/e2e/auth/delete_mutation_test.go
Expand Up @@ -394,8 +394,43 @@ func TestDeleteRootFilter(t *testing.T) {

func TestDeleteRBACFilter(t *testing.T) {
testCases := []TestCase{
{role: "USER", result: `{"deleteLog":{"numUids":0, "msg":"No nodes were deleted", "log":[]}}`},
{role: "ADMIN", result: `{"deleteLog":{"numUids":2, "msg":"Deleted", "log":[{"logs":"Log1","random":"test"}, {"logs":"Log2","random":"test"}]}}`}}
{
role: "USER",
result: `
{
"deleteLog":
{
"numUids":0,
"msg":"No nodes were deleted",
"log":[]
}
}
`,
},
{
role: "ADMIN",
result: `
{
"deleteLog":
{
"numUids":2,
"msg":"Deleted",
"log":
[
{
"logs":"Log1",
"random":"test"
},
{
"logs":"Log2",
"random":"test"
}
]
}
}
`,
},
}

query := `
mutation ($logs: [ID!]) {
Expand Down
6 changes: 3 additions & 3 deletions graphql/e2e/common/common.go
Expand Up @@ -362,9 +362,9 @@ func RunAll(t *testing.T) {
t.Run("filter in queries with array for AND/OR", filterInQueriesWithArrayForAndOr)
t.Run("query geo near filter", queryGeoNearFilter)
t.Run("persisted query", persistedQuery)
t.Run("query count without filter", queryCountWithoutFilter)
t.Run("query count with filter", queryCountWithFilter)
t.Run("query count with alias", queryCountWithAlias)
t.Run("query aggregate without filter", queryAggregateWithoutFilter)
t.Run("query aggregate with filter", queryAggregateWithFilter)
t.Run("query aggregate with alias", queryAggregateWithAlias)
t.Run("query count at child level", queryCountAtChildLevel)
t.Run("query count at child level with filter", queryCountAtChildLevelWithFilter)
t.Run("query count at child level with multiple alias", queryCountAtChildLevelWithMultipleAlias)
Expand Down
62 changes: 54 additions & 8 deletions graphql/e2e/common/query.go
Expand Up @@ -1929,7 +1929,7 @@ func queryWithMultipleAliasOfSameField(t *testing.T) {
"p2": [
{
"title": "Random post",
"numLikes": 0
"numLikes": 1
}
]
}
Expand Down Expand Up @@ -2810,40 +2810,64 @@ func persistedQuery(t *testing.T) {
RequireNoGQLErrors(t, gqlResponse)
}

func queryCountWithFilter(t *testing.T) {
func queryAggregateWithFilter(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
aggregatePost (filter: {title : { anyofterms : "Introducing" }} ) {
count
numLikesMax
titleMin
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"count":1}}`,
`{
"aggregatePost":
{
"count":1,
"numLikesMax": 100,
"titleMin": "Introducing GraphQL in Dgraph"
}
}`,
string(gqlResponse.Data))

queryPostParams = &GraphQLParams{
Query: `query {
aggregatePost (filter: {title : { anyofterms : "Nothing" }} ) {
count
numLikesMax
titleMin
}
}`,
}

gqlResponse = queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"count":0}}`,
`{
"aggregatePost":
{
"count":0,
"numLikesMax": 0,
"titleMin": "0.000000"
}
}`,
string(gqlResponse.Data))
}

func queryCountWithoutFilter(t *testing.T) {
func queryAggregateWithoutFilter(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
aggregatePost {
titleMax
titleMin
numLikesSum
numLikesAvg
numLikesMax
numLikesMin
count
}
}`,
Expand All @@ -2852,23 +2876,45 @@ func queryCountWithoutFilter(t *testing.T) {
gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"count":4}}`,
`{
"aggregatePost":
{
"count":4,
"titleMax": "Random post",
"titleMin": "GraphQL doco",
"numLikesAvg": 66.25,
"numLikesMax": 100,
"numLikesMin": 1,
"numLikesSum": 265
}
}`,
string(gqlResponse.Data))
}

func queryCountWithAlias(t *testing.T) {
func queryAggregateWithAlias(t *testing.T) {
queryPostParams := &GraphQLParams{
Query: `query {
aggregatePost {
cnt: count
tmin : titleMin
tmax: titleMax
navg : numLikesAvg
}
}`,
}

gqlResponse := queryPostParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
testutil.CompareJSON(t,
`{"aggregatePost":{"cnt":4}}`,
`{
"aggregatePost":
{
"cnt":4,
"tmax": "Random post",
"tmin": "GraphQL doco",
"navg": 66.25
}
}`,
string(gqlResponse.Data))
}

Expand Down
2 changes: 1 addition & 1 deletion graphql/e2e/directives/test_data.json
Expand Up @@ -86,7 +86,7 @@
"text": "this post is not worth publishing",
"myPost.tags": ["Random"],
"test.dgraph.topic": "Random",
"myPost.numLikes": 0,
"myPost.numLikes": 1,
"myPost.numViews": 0,
"is_published": false,
"myPost.postType": "Fact",
Expand Down
2 changes: 1 addition & 1 deletion graphql/e2e/normal/test_data.json
Expand Up @@ -86,7 +86,7 @@
"Post.text": "this post is not worth publishing",
"Post.tags": ["Random"],
"Post.topic": "Random",
"Post.numLikes": 0,
"Post.numLikes": 1,
"Post.numViews": 0,
"Post.isPublished": false,
"Post.postType": "Fact",
Expand Down
2 changes: 2 additions & 0 deletions graphql/e2e/schema/generatedSchema.graphql
Expand Up @@ -274,6 +274,8 @@ type AddAuthorPayload {

type AuthorAggregateResult {
count: Int
nameMin: String
nameMax: String
}

type DeleteAuthorPayload {
Expand Down