-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Error when attempting to insert value into relationship field (#632
) * Simplify collection integration tests Decouples them from thequery integration tests, allowing for a more focused interface and the removal of some complication from the query tests too. * Error when trying to save value to relation field
- Loading branch information
1 parent
ebe32e3
commit cce2f17
Showing
7 changed files
with
226 additions
and
41 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
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