Skip to content

Commit

Permalink
feat: Add count aggregate support (#102)
Browse files Browse the repository at this point in the history
* Add count aggregate support to object querys

* Add count aggregate support to commit queries
  • Loading branch information
AndrewSisley committed Jan 13, 2022
1 parent 6765046 commit 5369942
Show file tree
Hide file tree
Showing 18 changed files with 1,181 additions and 66 deletions.
43 changes: 43 additions & 0 deletions db/tests/query/all_commits/with_count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 Source Inc.
//
// 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 all_commits

import (
"testing"

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

func TestQueryAllCommitsSingleDAGWithLinkCount(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Simple latest commits query",
Query: `query {
allCommits(dockey: "bae-52b9170d-b77a-5887-b877-cbdbb99b009f") {
cid
_count(field: links)
}
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"Age": 21
}`)},
},
Results: []map[string]interface{}{
{
"cid": "bafkreiercmxn6e3qryxvuped5pplg733c5fj6gjypj5wykk63ouvcfb25m",
"_count": 2,
},
},
}

executeTestCase(t, test)
}
97 changes: 97 additions & 0 deletions db/tests/query/one_to_many/with_count_limit_offset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2020 Source Inc.
//
// 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 one_to_many

import (
"testing"

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

func TestQueryOneToManyWithCountAndLimitAndOffset(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "One-to-many relation query from many side with count and limit and offset",
Query: `query {
author {
name
_count(field: published)
published(limit: 2, offset: 1) {
name
}
}
}`,
Docs: map[int][]string{
//books
0: {
(`{
"name": "Painted House",
"rating": 4.9,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "A Time for Mercy",
"rating": 4.5,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "The Firm",
"rating": 4.1,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "The Pelican Brief",
"rating": 4.0,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "Theif Lord",
"rating": 4.8,
"author_id": "bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04"
}`),
},
//authors
1: {
// bae-41598f0c-19bc-5da6-813b-e80f14a10df3
(`{
"name": "John Grisham",
"age": 65,
"verified": true
}`),
// bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04
(`{
"name": "Cornelia Funke",
"age": 62,
"verified": false
}`),
},
},
Results: []map[string]interface{}{
{
"name": "John Grisham",
"_count": 4,
"published": []map[string]interface{}{
{
"name": "The Pelican Brief",
},
{
"name": "Painted House",
},
},
},
{
"name": "Cornelia Funke",
"_count": 1,
"published": []map[string]interface{}{},
},
},
}

executeTestCase(t, test)
}
88 changes: 88 additions & 0 deletions db/tests/query/one_to_many/with_count_limit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2020 Source Inc.
//
// 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 one_to_many

import (
"testing"

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

func TestQueryOneToManyWithCountAndLimit(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "One-to-many relation query from many side with count and limit",
Query: `query {
author {
name
_count(field: published)
published(limit: 1) {
name
}
}
}`,
Docs: map[int][]string{
//books
0: { // bae-fd541c25-229e-5280-b44b-e5c2af3e374d
(`{
"name": "Painted House",
"rating": 4.9,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "A Time for Mercy",
"rating": 4.5,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "Theif Lord",
"rating": 4.8,
"author_id": "bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04"
}`),
},
//authors
1: {
// bae-41598f0c-19bc-5da6-813b-e80f14a10df3
(`{
"name": "John Grisham",
"age": 65,
"verified": true
}`),
// bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04
(`{
"name": "Cornelia Funke",
"age": 62,
"verified": false
}`),
},
},
Results: []map[string]interface{}{
{
"name": "John Grisham",
"_count": 2,
"published": []map[string]interface{}{
{
"name": "Painted House",
},
},
},
{
"name": "Cornelia Funke",
"_count": 1,
"published": []map[string]interface{}{
{
"name": "Theif Lord",
},
},
},
},
}

executeTestCase(t, test)
}
104 changes: 104 additions & 0 deletions db/tests/query/one_to_many/with_count_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2020 Source Inc.
//
// 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 one_to_many

import (
"testing"

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

func TestQueryOneToManyWithCount(t *testing.T) {
tests := []testUtils.QueryTestCase{
{
Description: "One-to-many relation query from many side with count, no child records",
Query: `query {
author {
name
_count(field: published)
}
}`,
Docs: map[int][]string{
//authors
1: {
(`{
"name": "John Grisham",
"age": 65,
"verified": true
}`),
},
},
Results: []map[string]interface{}{
{
"name": "John Grisham",
"_count": 0,
},
},
},
{
Description: "One-to-many relation query from many side with count",
Query: `query {
author {
name
_count(field: published)
}
}`,
Docs: map[int][]string{
//books
0: { // bae-fd541c25-229e-5280-b44b-e5c2af3e374d
(`{
"name": "Painted House",
"rating": 4.9,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "A Time for Mercy",
"rating": 4.5,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`),
(`{
"name": "Theif Lord",
"rating": 4.8,
"author_id": "bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04"
}`),
},
//authors
1: {
// bae-41598f0c-19bc-5da6-813b-e80f14a10df3
(`{
"name": "John Grisham",
"age": 65,
"verified": true
}`),
// bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04
(`{
"name": "Cornelia Funke",
"age": 62,
"verified": false
}`),
},
},
Results: []map[string]interface{}{
{
"name": "John Grisham",
"_count": 2,
},
{
"name": "Cornelia Funke",
"_count": 1,
},
},
},
}

for _, test := range tests {
executeTestCase(t, test)
}
}
40 changes: 40 additions & 0 deletions db/tests/query/one_to_many_multiple/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2020 Source Inc.
//
// 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 one_to_many_multiple

import (
"testing"

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

var bookAuthorGQLSchema = (`
type article {
name: String
author: author
}
type book {
name: String
author: author
}
type author {
name: String
age: Int
verified: Boolean
books: [book]
articles: [article]
}
`)

func executeTestCase(t *testing.T, test testUtils.QueryTestCase) {
testUtils.ExecuteQueryTestCase(t, bookAuthorGQLSchema, []string{"article", "book", "author"}, test)
}
Loading

0 comments on commit 5369942

Please sign in to comment.