-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Error when attempting to insert value into relationship field #632
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions
47
tests/integration/collection/create/one_to_many/simple_test.go
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,47 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// 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 create | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateSaveErrorsGivenValueInRelationField(t *testing.T) { | ||
doc, err := client.NewDocFromJSON( | ||
[]byte( | ||
`{ | ||
"Name": "Painted House", | ||
"Author": "ValueDoesntMatter" | ||
}`, | ||
), | ||
) | ||
if err != nil { | ||
assert.Fail(t, err.Error()) | ||
} | ||
|
||
test := testUtils.TestCase{ | ||
CollectionCalls: map[string][]func(client.Collection) error{ | ||
"book": []func(c client.Collection) error{ | ||
func(c client.Collection) error { | ||
return c.Save(context.Background(), doc) | ||
}, | ||
}, | ||
}, | ||
ExpectedError: "The given field does not exist", | ||
} | ||
|
||
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,36 @@ | ||
// Copyright 2022 Democratized Data Foundation | ||
// | ||
// 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 create | ||
|
||
import ( | ||
"testing" | ||
|
||
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection" | ||
) | ||
|
||
var schema = (` | ||
type book { | ||
Name: String | ||
Rating: Float | ||
Author: author | ||
} | ||
|
||
type author { | ||
Name: String | ||
Age: Int | ||
Verified: Boolean | ||
Published: [book] | ||
} | ||
`) | ||
|
||
func executeTestCase(t *testing.T, test testUtils.TestCase) { | ||
testUtils.ExecuteQueryTestCase(t, schema, 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
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
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 2022 Democratized Data Foundation | ||
// | ||
// 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 collection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
testUtils "github.com/sourcenetwork/defradb/tests/integration" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestCase struct { | ||
// docs is a map from Collection name, to a list | ||
// of docs in stringified JSON format | ||
Docs map[string][]string | ||
|
||
CollectionCalls map[string][]func(client.Collection) error | ||
|
||
// Any error expected to be returned by collection calls. | ||
ExpectedError string | ||
} | ||
|
||
type dbInfo interface { | ||
DB() client.DB | ||
} | ||
|
||
func ExecuteQueryTestCase( | ||
t *testing.T, | ||
schema string, | ||
testCase TestCase, | ||
) { | ||
var err error | ||
ctx := context.Background() | ||
|
||
var dbi dbInfo | ||
dbi, err = testUtils.NewBadgerMemoryDB(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
db := dbi.DB() | ||
|
||
err = db.AddSchema(ctx, schema) | ||
if assertError(t, err, testCase.ExpectedError) { | ||
return | ||
} | ||
|
||
setupDatabase(ctx, t, db, testCase) | ||
|
||
for collectionName, collectionCallSet := range testCase.CollectionCalls { | ||
col, err := db.GetCollectionByName(ctx, collectionName) | ||
if assertError(t, err, testCase.ExpectedError) { | ||
return | ||
} | ||
|
||
for _, collectionCall := range collectionCallSet { | ||
err := collectionCall(col) | ||
if assertError(t, err, testCase.ExpectedError) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
if testCase.ExpectedError != "" { | ||
assert.Fail(t, "Expected an error however none was raised.") | ||
} | ||
} | ||
|
||
func setupDatabase( | ||
ctx context.Context, | ||
t *testing.T, | ||
db client.DB, | ||
testCase TestCase, | ||
) { | ||
for collectionName, docs := range testCase.Docs { | ||
col, err := db.GetCollectionByName(ctx, collectionName) | ||
if assertError(t, err, testCase.ExpectedError) { | ||
return | ||
} | ||
|
||
for _, docStr := range docs { | ||
doc, err := client.NewDocFromJSON([]byte(docStr)) | ||
if assertError(t, err, testCase.ExpectedError) { | ||
return | ||
} | ||
err = col.Save(ctx, doc) | ||
if assertError(t, err, testCase.ExpectedError) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func assertError(t *testing.T, err error, expectedError string) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
if expectedError == "" { | ||
assert.NoError(t, err) | ||
return false | ||
} else { | ||
if !strings.Contains(err.Error(), expectedError) { | ||
assert.ErrorIs(t, err, fmt.Errorf(expectedError)) | ||
return false | ||
} | ||
return true | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test also got renamed to distinguish it from the Create tests, I lazily showed this into the (first?) commit