Skip to content
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 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,11 @@ func (c *collection) tryGetFieldKey(key core.PrimaryDataStoreKey, fieldName stri
func (c *collection) tryGetSchemaFieldID(fieldName string) (uint32, bool) {
for _, field := range c.desc.Schema.Fields {
if field.Name == fieldName {
if field.IsObject() || field.IsObjectArray() {
// We do not wish to match navigational properties, only
// fields directly on the collection.
return uint32(0), false
}
return uint32(field.ID), true
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/integration/collection/create/one_to_many/simple_test.go
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)
}
36 changes: 36 additions & 0 deletions tests/integration/collection/create/one_to_many/utils.go
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)
}
33 changes: 14 additions & 19 deletions tests/integration/collection/update/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"testing"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration"
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection"
"github.com/stretchr/testify/assert"
)

func TestSaveErrorsGivenUnknownField(t *testing.T) {
func TestUpdateSaveErrorsGivenUnknownField(t *testing.T) {
Copy link
Contributor Author

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

doc, err := client.NewDocFromJSON(
[]byte(
`{
Expand All @@ -43,25 +43,20 @@ func TestSaveErrorsGivenUnknownField(t *testing.T) {
assert.Fail(t, err.Error())
}

test := testUtils.QueryTestCase{
Description: "Simple query with no filter",
Query: `query {
users {
_key
Name
Age
}
test := testUtils.TestCase{
Docs: map[string][]string{
"users": {
`{
"Name": "John",
"Age": 21
}`,
Docs: map[int][]string{
0: {
(`{
"Name": "John",
"Age": 21
}`)},
},
},
UpdateFuncs: map[int]func(client.Collection) error{
0: func(c client.Collection) error {
return c.Save(context.Background(), doc)
CollectionCalls: map[string][]func(client.Collection) error{
"users": []func(c client.Collection) error{
func(c client.Collection) error {
return c.Save(context.Background(), doc)
},
},
},
ExpectedError: "The given field does not exist",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/collection/update/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package update
import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
testUtils "github.com/sourcenetwork/defradb/tests/integration/collection"
)

var userCollectionGQLSchema = (`
Expand All @@ -25,6 +25,6 @@ var userCollectionGQLSchema = (`
}
`)

func executeTestCase(t *testing.T, test testUtils.QueryTestCase) {
testUtils.ExecuteQueryTestCase(t, userCollectionGQLSchema, []string{"users"}, test)
func executeTestCase(t *testing.T, test testUtils.TestCase) {
testUtils.ExecuteQueryTestCase(t, userCollectionGQLSchema, test)
}
121 changes: 121 additions & 0 deletions tests/integration/collection/utils.go
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
}
}
19 changes: 0 additions & 19 deletions tests/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ type QueryTestCase struct {
// of changes in strinigied JSON format
Updates map[int]map[int][]string

UpdateFuncs map[int]func(client.Collection) error

Results []map[string]interface{}
// The expected content of an expected error
ExpectedError string
Expand Down Expand Up @@ -361,23 +359,6 @@ func ExecuteQueryTestCase(
}
}

collections := []client.Collection{}
for _, collectionName := range collectionNames {
col, err := dbi.db.GetCollectionByName(ctx, collectionName)
if assertError(t, test.Description, err, test.ExpectedError) {
return
}
collections = append(collections, col)
}

for collectionIndex, f := range test.UpdateFuncs {
collection := collections[collectionIndex]
err := f(collection)
if assertError(t, test.Description, err, test.ExpectedError) {
return
}
}

// We run the core query after the explicitly transactional ones to permit tests to query
// the commited result of the transactional queries
if test.Query != "" {
Expand Down