Skip to content

Commit

Permalink
test: Add new setup for testing explain functionality (sourcenetwork#949
Browse files Browse the repository at this point in the history
)

- Resolves sourcenetwork#521

- Description:
This PR adds the testing setup for the `explain` feature to be more flexible and easier to manage going forward.

This new setup will handle all explain types: `simple`, `debug`, `execute`, `predict`.

The PR introduces three different types of test cases for asserting the results from an `explain` request. These can be asserted in one test case or smaller separate cases:
1) Being able to assert full explain graph or just quickly see the full graph to debug (like before).
2) Only matching the plan node ordering.
3) Targeting specific attributes only (option to also select their children nodes, or just it's attributes)
  • Loading branch information
shahzadlone authored Nov 27, 2022
1 parent 6d3a58d commit 297ce43
Show file tree
Hide file tree
Showing 32 changed files with 3,051 additions and 2,112 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ endif
test:
gotestsum --format pkgname -- ./... -race -shuffle=on

# Only build the tests (don't execute them).
.PHONY: test\:build
test\:build:
gotestsum --format pkgname -- ./... -race -shuffle=on -run=nope

.PHONY: test\:ci
test\:ci:
DEFRA_BADGER_MEMORY=true DEFRA_BADGER_FILE=true $(MAKE) test:names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package simple
package test_explain_simple

import (
"testing"
Expand All @@ -22,18 +22,18 @@ func TestExplainQuerySimpleOnFieldDirective_BadUsage(t *testing.T) {
Description: "Explain a query by providing the directive on wrong location (field).",

Query: `query {
users @explain {
_key
Name
Age
}
}`,
author @explain {
_key
name
age
}
}`,

Docs: map[int][]string{
0: {
2: {
`{
"Name": "John",
"Age": 21
"name": "John",
"age": 21
}`,
},
},
Expand All @@ -48,21 +48,24 @@ func TestExplainQuerySimpleOnFieldDirective_BadUsage(t *testing.T) {
func TestExplainQuerySimple(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain a query with no filter",

Query: `query @explain {
users {
_key
Name
Age
}
}`,
author {
_key
name
age
}
}`,

Docs: map[int][]string{
0: {
2: {
`{
"Name": "John",
"Age": 21
"name": "John",
"age": 21
}`,
},
},

Results: []dataMap{
{
"explain": dataMap{
Expand All @@ -71,12 +74,12 @@ func TestExplainQuerySimple(t *testing.T) {
"filter": nil,
"scanNode": dataMap{
"filter": nil,
"collectionID": "1",
"collectionName": "users",
"collectionID": "3",
"collectionName": "author",
"spans": []dataMap{
{
"start": "/1",
"end": "/2",
"start": "/3",
"end": "/4",
},
},
},
Expand All @@ -93,20 +96,23 @@ func TestExplainQuerySimple(t *testing.T) {
func TestExplainQuerySimpleWithAlias(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain a query with alias, no filter",

Query: `query @explain {
users {
username: Name
age: Age
}
}`,
author {
username: name
age: age
}
}`,

Docs: map[int][]string{
0: {
2: {
`{
"Name": "John",
"Age": 21
"name": "John",
"age": 21
}`,
},
},

Results: []dataMap{
{
"explain": dataMap{
Expand All @@ -115,12 +121,12 @@ func TestExplainQuerySimpleWithAlias(t *testing.T) {
"filter": nil,
"scanNode": dataMap{
"filter": nil,
"collectionID": "1",
"collectionName": "users",
"collectionID": "3",
"collectionName": "author",
"spans": []dataMap{
{
"start": "/1",
"end": "/2",
"start": "/3",
"end": "/4",
},
},
},
Expand All @@ -137,24 +143,27 @@ func TestExplainQuerySimpleWithAlias(t *testing.T) {
func TestExplainQuerySimpleWithMultipleRows(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain a query with no filter, mutiple rows",

Query: `query @explain {
users {
Name
Age
}
}`,
author {
name
age
}
}`,

Docs: map[int][]string{
0: {
2: {
`{
"Name": "John",
"Age": 21
"name": "John",
"age": 21
}`,
`{
"Name": "Bob",
"Age": 27
"name": "Bob",
"age": 27
}`,
},
},

Results: []dataMap{
{
"explain": dataMap{
Expand All @@ -163,12 +172,12 @@ func TestExplainQuerySimpleWithMultipleRows(t *testing.T) {
"filter": nil,
"scanNode": dataMap{
"filter": nil,
"collectionID": "1",
"collectionName": "users",
"collectionID": "3",
"collectionName": "author",
"spans": []dataMap{
{
"start": "/1",
"end": "/2",
"start": "/3",
"end": "/4",
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,41 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package create
package test_explain_simple

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
simpleTests "github.com/sourcenetwork/defradb/tests/integration/mutation/simple"
)

type dataMap = map[string]any

func TestExplainMutationCreateSimple(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain simple create mutation.",

Query: `mutation @explain {
create_user(data: "{\"name\": \"John\",\"age\": 27,\"points\": 42.1,\"verified\": true}") {
_key
name
age
}
}`,
create_author(data: "{\"name\": \"Shahzad Lone\",\"age\": 27,\"verified\": true}") {
_key
name
age
}
}`,

Results: []dataMap{
{
"explain": dataMap{
"createNode": dataMap{
"data": dataMap{
"age": float64(27),
"name": "John",
"points": 42.1,
"name": "Shahzad Lone",
"verified": true,
},
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "1",
"collectionName": "user",
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{},
},
Expand All @@ -60,25 +56,25 @@ func TestExplainMutationCreateSimple(t *testing.T) {
ExpectedError: "",
}

simpleTests.ExecuteTestCase(t, test)
executeTestCase(t, test)
}

func TestExplainMutationCreateSimpleDoesNotCreateDocGivenDuplicate(t *testing.T) {
test := testUtils.QueryTestCase{
Description: "Explain simple create mutation, where document already exists.",

Query: `mutation @explain {
create_user(data: "{\"name\": \"John\",\"age\": 27}") {
_key
name
age
}
}`,
create_author(data: "{\"name\": \"Shahzad Lone\",\"age\": 27}") {
_key
name
age
}
}`,

Docs: map[int][]string{
0: {
2: {
`{
"name": "John",
"name": "Shahzad Lone",
"age": 27
}`,
},
Expand All @@ -90,14 +86,14 @@ func TestExplainMutationCreateSimpleDoesNotCreateDocGivenDuplicate(t *testing.T)
"createNode": dataMap{
"data": dataMap{
"age": float64(27),
"name": "John",
"name": "Shahzad Lone",
},
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "1",
"collectionName": "user",
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{},
},
Expand All @@ -111,5 +107,5 @@ func TestExplainMutationCreateSimpleDoesNotCreateDocGivenDuplicate(t *testing.T)
ExpectedError: "",
}

simpleTests.ExecuteTestCase(t, test)
executeTestCase(t, test)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package test_explain
package test_explain_simple

import (
"testing"
Expand Down
Loading

0 comments on commit 297ce43

Please sign in to comment.