-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add group-by functionality to defra db
- Loading branch information
1 parent
6dc28c1
commit 82bf22e
Showing
11 changed files
with
1,753 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// 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 TestQueryOneToManyWithInnerJoinGroupNumber(t *testing.T) { | ||
tests := []testUtils.QueryTestCase{ | ||
{ | ||
Description: "One-to-many relation query from many side with group inside of join", | ||
Query: `query { | ||
author { | ||
name | ||
age | ||
published (groupBy: [rating]){ | ||
rating | ||
_group { | ||
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": "The Client", | ||
"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", | ||
"age": uint64(65), | ||
"published": []map[string]interface{}{ | ||
{ | ||
"rating": 4.5, | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"name": "The Client", | ||
}, | ||
{ | ||
"name": "A Time for Mercy", | ||
}, | ||
}, | ||
}, | ||
{ | ||
"rating": 4.9, | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"name": "Painted House", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"name": "Cornelia Funke", | ||
"age": uint64(62), | ||
"published": []map[string]interface{}{ | ||
{ | ||
"rating": 4.8, | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"name": "Theif Lord", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
executeTestCase(t, test) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
// 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 simple | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/db/tests" | ||
) | ||
|
||
func TestQuerySimpleWithGroupByStringWithGroupNumberFilter(t *testing.T) { | ||
test := testUtils.QueryTestCase{ | ||
Description: "Simple query with group by with child filter", | ||
Query: `query { | ||
users(groupBy: [Name]) { | ||
Name | ||
_group (filter: {Age: {_gt: 26}}){ | ||
Age | ||
} | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"Name": "John", | ||
"Age": 25 | ||
}`), | ||
(`{ | ||
"Name": "John", | ||
"Age": 32 | ||
}`), | ||
(`{ | ||
"Name": "Carlo", | ||
"Age": 55 | ||
}`), | ||
(`{ | ||
"Name": "Alice", | ||
"Age": 19 | ||
}`)}, | ||
}, | ||
Results: []map[string]interface{}{ | ||
{ | ||
"Name": "Alice", | ||
"_group": []map[string]interface{}{}, | ||
}, | ||
{ | ||
"Name": "John", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Age": uint64(32), | ||
}, | ||
}, | ||
}, | ||
{ | ||
"Name": "Carlo", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Age": uint64(55), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
func TestQuerySimpleWithGroupByStringWithGroupNumberWithParentFilter(t *testing.T) { | ||
test := testUtils.QueryTestCase{ | ||
Description: "Simple query with group by with number filter", | ||
Query: `query { | ||
users(groupBy: [Name], filter: {Age: {_gt: 26}}) { | ||
Name | ||
_group { | ||
Age | ||
} | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"Name": "John", | ||
"Age": 25 | ||
}`), | ||
(`{ | ||
"Name": "John", | ||
"Age": 32 | ||
}`), | ||
(`{ | ||
"Name": "Carlo", | ||
"Age": 55 | ||
}`), | ||
(`{ | ||
"Name": "Alice", | ||
"Age": 19 | ||
}`)}, | ||
}, | ||
Results: []map[string]interface{}{ | ||
{ | ||
"Name": "John", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Age": uint64(32), | ||
}, | ||
}, | ||
}, | ||
{ | ||
"Name": "Carlo", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Age": uint64(55), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} | ||
|
||
func TestQuerySimpleWithGroupByStringWithInnerGroupBooleanThenInnerNumberFilterThatExcludesAll(t *testing.T) { | ||
test := testUtils.QueryTestCase{ | ||
Description: "Simple query with group by string, with child group by boolean, with child number filter that excludes all records", | ||
Query: `query { | ||
users(groupBy: [Name]) { | ||
Name | ||
_group (groupBy: [Verified]){ | ||
Verified | ||
_group (filter: {Age: {_gt: 260}}) { | ||
Age | ||
} | ||
} | ||
} | ||
}`, | ||
Docs: map[int][]string{ | ||
0: { | ||
(`{ | ||
"Name": "John", | ||
"Age": 25, | ||
"Verified": true | ||
}`), | ||
(`{ | ||
"Name": "John", | ||
"Age": 32, | ||
"Verified": true | ||
}`), | ||
(`{ | ||
"Name": "John", | ||
"Age": 34, | ||
"Verified": false | ||
}`), | ||
(`{ | ||
"Name": "Carlo", | ||
"Age": 55, | ||
"Verified": true | ||
}`), | ||
(`{ | ||
"Name": "Alice", | ||
"Age": 19, | ||
"Verified": false | ||
}`)}, | ||
}, | ||
Results: []map[string]interface{}{ | ||
{ | ||
"Name": "John", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Verified": true, | ||
"_group": []map[string]interface{}{}, | ||
}, | ||
{ | ||
"Verified": false, | ||
"_group": []map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"Name": "Alice", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Verified": false, | ||
"_group": []map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"Name": "Carlo", | ||
"_group": []map[string]interface{}{ | ||
{ | ||
"Verified": true, | ||
"_group": []map[string]interface{}{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
executeTestCase(t, test) | ||
} |
Oops, something went wrong.