Skip to content

Commit

Permalink
Error when trying to save value to relation field
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jul 14, 2022
1 parent c60ceee commit 8cf0cb0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
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)
}

0 comments on commit 8cf0cb0

Please sign in to comment.