Skip to content

Commit

Permalink
test: Add test for successful one-one create mutation (#1215)
Browse files Browse the repository at this point in the history
* Migrate one-one tests to new framework

Might as well migrate the 4 existing tests before going through the hassle of adding new ones in a different (new) framework.

* Document documentation test

* Document documentation test

* Add one-one create mutation test
  • Loading branch information
AndrewSisley authored Mar 21, 2023
1 parent f0e2036 commit 56b89d7
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 57 deletions.
100 changes: 84 additions & 16 deletions tests/integration/mutation/one_to_one/create/with_simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@
package create

import (
"fmt"
"testing"

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

// This test documents incorrect behaviour. It should be possible to create author then book,
// linking in the second create step (like in [TestMutationCreateOneToOne]).
// https://github.com/sourcenetwork/defradb/issues/1213
func TestMutationCreateOneToOneWrongSide(t *testing.T) {
test := testUtils.RequestTestCase{
test := testUtils.TestCase{
Description: "One to one create mutation, from the wrong side",
Request: `mutation {
create_book(data: "{\"name\": \"Painted House\",\"author_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
_key
}
}`,
ExpectedError: "The given field does not exist",
Actions: []any{
testUtils.Request{
Request: `mutation {
create_book(data: "{\"name\": \"Painted House\",\"author_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
_key
}
}`,
ExpectedError: "The given field does not exist",
},
},
}

simpleTests.ExecuteTestCase(t, test)
Expand All @@ -34,16 +42,76 @@ func TestMutationCreateOneToOneWrongSide(t *testing.T) {
// Note: This test should probably not pass, as it contains a
// reference to a document that doesnt exist.
func TestMutationCreateOneToOneNoChild(t *testing.T) {
test := testUtils.RequestTestCase{
test := testUtils.TestCase{
Description: "One to one create mutation, from the wrong side",
Request: `mutation {
create_author(data: "{\"name\": \"John Grisham\",\"published_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
name
}
}`,
Results: []map[string]any{
{
"name": "John Grisham",
Actions: []any{
testUtils.Request{
Request: `mutation {
create_author(data: "{\"name\": \"John Grisham\",\"published_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
name
}
}`,
Results: []map[string]any{
{
"name": "John Grisham",
},
},
},
},
}
simpleTests.ExecuteTestCase(t, test)
}

func TestMutationCreateOneToOne(t *testing.T) {
bookKey := "bae-3d236f89-6a31-5add-a36a-27971a2eac76"

test := testUtils.TestCase{
Description: "One to one create mutation",
Actions: []any{
testUtils.Request{
Request: `mutation {
create_book(data: "{\"name\": \"Painted House\"}") {
_key
}
}`,
Results: []map[string]any{
{
"_key": bookKey,
},
},
},
testUtils.Request{
Request: fmt.Sprintf(
`mutation {
create_author(data: "{\"name\": \"John Grisham\",\"published_id\": \"%s\"}") {
name
}
}`,
bookKey),
Results: []map[string]any{
{
"name": "John Grisham",
},
},
},
testUtils.Request{
Request: `
query {
book {
name
author {
name
}
}
}`,
Results: []map[string]any{
{
"name": "Painted House",
"author": map[string]any{
"name": "John Grisham",
},
},
},
},
},
}
Expand Down
56 changes: 31 additions & 25 deletions tests/integration/mutation/one_to_one/update/with_simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,56 @@ import (
simpleTests "github.com/sourcenetwork/defradb/tests/integration/mutation/one_to_one"
)

// This test documents incorrect behaviour. It should be possible to update book to point to author.
// https://github.com/sourcenetwork/defradb/issues/1214
func TestMutationUpdateOneToOneWrongSide(t *testing.T) {
test := testUtils.RequestTestCase{
test := testUtils.TestCase{
Description: "One to one create mutation, from the wrong side",
Request: `mutation {
update_book(data: "{\"name\": \"Painted House\",\"author_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
_key
}
}`,
Docs: map[int][]string{
0: {
`{
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Theif Lord"
}`,
},
testUtils.Request{
Request: `mutation {
update_book(data: "{\"name\": \"Painted House\",\"author_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
_key
}
}`,
ExpectedError: "The given field does not exist",
},
},
ExpectedError: "The given field does not exist",
}

simpleTests.ExecuteTestCase(t, test)
}

// Note: This test should probably not pass, as it contains a
// reference to a document that doesnt exist.
func TestMutationUpdateOneToOneNoChild(t *testing.T) {
test := testUtils.RequestTestCase{
test := testUtils.TestCase{
Description: "One to one create mutation, from the wrong side",
Request: `mutation {
update_author(data: "{\"name\": \"John Grisham\",\"published_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
name
}
}`,
Docs: map[int][]string{
1: {
`{
Actions: []any{
testUtils.CreateDoc{
CollectionID: 1,
Doc: `{
"name": "John"
}`,
},
},
Results: []map[string]any{
{
"name": "John Grisham",
testUtils.Request{
Request: `mutation {
update_author(data: "{\"name\": \"John Grisham\",\"published_id\": \"bae-fd541c25-229e-5280-b44b-e5c2af3e374d\"}") {
name
}
}`,
Results: []map[string]any{
{
"name": "John Grisham",
},
},
},
},
}

simpleTests.ExecuteTestCase(t, test)
}
44 changes: 28 additions & 16 deletions tests/integration/mutation/one_to_one/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,33 @@ import (
testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

var bookAuthorGQLSchema = (`
type book {
name: String
rating: Float
author: author
}
func ExecuteTestCase(t *testing.T, test testUtils.TestCase) {
testUtils.ExecuteTestCase(
t,
[]string{"book", "author"},
testUtils.TestCase{
Description: test.Description,
Actions: append(
[]any{
testUtils.SchemaUpdate{
Schema: `
type book {
name: String
rating: Float
author: author
}
type author {
name: String
age: Int
verified: Boolean
published: book @primary
}
`)

func ExecuteTestCase(t *testing.T, test testUtils.RequestTestCase) {
testUtils.ExecuteRequestTestCase(t, bookAuthorGQLSchema, []string{"book", "author"}, test)
type author {
name: String
age: Int
verified: Boolean
published: book @primary
}
`,
},
},
test.Actions...,
),
},
)
}

0 comments on commit 56b89d7

Please sign in to comment.