From 56b89d7ddd69c765aec2a7dec301fcb4ae2ad7c3 Mon Sep 17 00:00:00 2001 From: AndrewSisley Date: Tue, 21 Mar 2023 18:33:12 -0400 Subject: [PATCH] test: Add test for successful one-one create mutation (#1215) * 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 --- .../one_to_one/create/with_simple_test.go | 100 +++++++++++++++--- .../one_to_one/update/with_simple_test.go | 56 +++++----- .../integration/mutation/one_to_one/utils.go | 44 +++++--- 3 files changed, 143 insertions(+), 57 deletions(-) diff --git a/tests/integration/mutation/one_to_one/create/with_simple_test.go b/tests/integration/mutation/one_to_one/create/with_simple_test.go index 95a0dc0f7b..2d60c2058f 100644 --- a/tests/integration/mutation/one_to_one/create/with_simple_test.go +++ b/tests/integration/mutation/one_to_one/create/with_simple_test.go @@ -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) @@ -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", + }, + }, + }, }, }, } diff --git a/tests/integration/mutation/one_to_one/update/with_simple_test.go b/tests/integration/mutation/one_to_one/update/with_simple_test.go index 54b7949088..e564e74ae9 100644 --- a/tests/integration/mutation/one_to_one/update/with_simple_test.go +++ b/tests/integration/mutation/one_to_one/update/with_simple_test.go @@ -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) } diff --git a/tests/integration/mutation/one_to_one/utils.go b/tests/integration/mutation/one_to_one/utils.go index f0d8dfe4fc..41dbb4a764 100644 --- a/tests/integration/mutation/one_to_one/utils.go +++ b/tests/integration/mutation/one_to_one/utils.go @@ -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..., + ), + }, + ) }