diff --git a/client/document.go b/client/document.go index cc15d45673..3c890dd24a 100644 --- a/client/document.go +++ b/client/document.go @@ -241,11 +241,30 @@ func validateFieldSchema(val any, field FieldDefinition) (NormalValue, error) { if err != nil { return nil, err } + + _, err = NewDocIDFromString(v) + if err != nil { + return nil, err + } + return NewNormalString(v), nil } switch field.Kind { - case FieldKind_DocID, FieldKind_NILLABLE_STRING, FieldKind_NILLABLE_BLOB: + case FieldKind_DocID: + v, err := getString(val) + if err != nil { + return nil, err + } + + _, err = NewDocIDFromString(v) + if err != nil { + return nil, err + } + + return NewNormalString(v), nil + + case FieldKind_NILLABLE_STRING, FieldKind_NILLABLE_BLOB: v, err := getString(val) if err != nil { return nil, err @@ -692,6 +711,16 @@ func (doc *Document) Set(field string, value any) error { if !exists { return NewErrFieldNotExist(field) } + + if fd.Kind == FieldKind_DocID && strings.HasSuffix(field, request.RelatedObjectID) { + objFieldName := strings.TrimSuffix(field, request.RelatedObjectID) + ofd, exists := doc.collectionDefinition.GetFieldByName(objFieldName) + if exists && !ofd.IsPrimaryRelation { + return NewErrCannotSetRelationFromSecondarySide(field) + } + + } + if fd.Kind.IsObject() && !fd.Kind.IsArray() { if !strings.HasSuffix(field, request.RelatedObjectID) { field = field + request.RelatedObjectID diff --git a/client/errors.go b/client/errors.go index 81ebf2e3f5..caf2fc5c10 100644 --- a/client/errors.go +++ b/client/errors.go @@ -34,6 +34,7 @@ const ( errCanNotTurnNormalValueIntoArray string = "can not turn normal value into array" errCanNotMakeNormalNilFromFieldKind string = "can not make normal nil from field kind" errFailedToParseKind string = "failed to parse kind" + errCannotSetRelationFromSecondarySide string = "cannot set relation from secondary side" ) // Errors returnable from this package. @@ -190,3 +191,7 @@ func ReviveError(message string) error { return fmt.Errorf("%s", message) } } + +func NewErrCannotSetRelationFromSecondarySide(name string) error { + return errors.New(errCannotSetRelationFromSecondarySide, errors.NewKV("Name", name)) +} diff --git a/internal/request/graphql/schema/generate.go b/internal/request/graphql/schema/generate.go index f326a8232a..d057d79d54 100644 --- a/internal/request/graphql/schema/generate.go +++ b/internal/request/graphql/schema/generate.go @@ -566,6 +566,14 @@ func (g *Generator) buildMutationInputTypes(collections []client.CollectionDefin continue } + if field.Kind == client.FieldKind_DocID && strings.HasSuffix(field.Name, request.RelatedObjectID) { + objFieldName := strings.TrimSuffix(field.Name, request.RelatedObjectID) + ofd, exists := collection.GetFieldByName(objFieldName) + if exists && !ofd.IsPrimaryRelation { + continue + } + } + var ttype gql.Type if field.Kind.IsObject() { if field.Kind.IsArray() { diff --git a/tests/integration/mutation/create/field_kinds/one_to_many/with_alias_test.go b/tests/integration/mutation/create/field_kinds/one_to_many/with_alias_test.go index fe66f58d98..7b43fe8499 100644 --- a/tests/integration/mutation/create/field_kinds/one_to_many/with_alias_test.go +++ b/tests/integration/mutation/create/field_kinds/one_to_many/with_alias_test.go @@ -94,35 +94,6 @@ func TestMutationCreateOneToMany_AliasedRelationNameNonExistingRelationManySide_ } executeTestCase(t, test) } -func TestMutationCreateOneToMany_AliasedRelationNameInvalidIDManySide_CreatedDoc(t *testing.T) { - test := testUtils.TestCase{ - Description: "One to many create mutation, invalid id, from the many side, with alias", - Actions: []any{ - testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Painted House", - "author": "ValueDoesntMatter" - }`, - }, - testUtils.Request{ - Request: `query { - Book { - name - } - }`, - Results: map[string]any{ - "Book": []map[string]any{ - { - "name": "Painted House", - }, - }, - }, - }, - }, - } - executeTestCase(t, test) -} func TestMutationCreateOneToMany_AliasedRelationNameToLinkFromManySide(t *testing.T) { test := testUtils.TestCase{ diff --git a/tests/integration/mutation/create/field_kinds/one_to_one/with_simple_test.go b/tests/integration/mutation/create/field_kinds/one_to_one/with_simple_test.go index fabced3505..c366e4f459 100644 --- a/tests/integration/mutation/create/field_kinds/one_to_one/with_simple_test.go +++ b/tests/integration/mutation/create/field_kinds/one_to_one/with_simple_test.go @@ -73,23 +73,6 @@ func TestMutationCreateOneToOneNoChild(t *testing.T) { executeTestCase(t, test) } -func TestMutationCreateOneToOne_NonExistingRelationSecondarySide_Error(t *testing.T) { - test := testUtils.TestCase{ - Description: "One to one create mutation, from the secondary side", - Actions: []any{ - testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Painted House", - "author_id": "bae-be6d8024-4953-5a92-84b4-f042d25230c6" - }`, - ExpectedError: "document not found or not authorized to access", - }, - }, - } - executeTestCase(t, test) -} - func TestMutationCreateOneToOne(t *testing.T) { test := testUtils.TestCase{ Description: "One to one create mutation", @@ -168,51 +151,10 @@ func TestMutationCreateOneToOneSecondarySide(t *testing.T) { testUtils.CreateDoc{ CollectionID: 0, DocMap: map[string]any{ - "name": "Painted House", - "author_id": testUtils.NewDocIndex(1, 0), - }, - }, - testUtils.Request{ - Request: ` - query { - Author { - name - published { - name - } - } - }`, - Results: map[string]any{ - "Author": []map[string]any{ - { - "name": "John Grisham", - "published": map[string]any{ - "name": "Painted House", - }, - }, - }, - }, - }, - testUtils.Request{ - Request: ` - query { - Book { - name - author { - name - } - } - }`, - Results: map[string]any{ - "Book": []map[string]any{ - { - "name": "Painted House", - "author": map[string]any{ - "name": "John Grisham", - }, - }, - }, + "name": "Painted House", + "author": testUtils.NewDocIndex(1, 0), }, + ExpectedError: "cannot set relation from secondary side", }, }, } @@ -250,34 +192,3 @@ func TestMutationCreateOneToOne_ErrorsGivenRelationAlreadyEstablishedViaPrimary( executeTestCase(t, test) } - -func TestMutationCreateOneToOne_ErrorsGivenRelationAlreadyEstablishedViaSecondary(t *testing.T) { - test := testUtils.TestCase{ - Description: "One to one create mutation, errors due to link already existing, secondary side", - Actions: []any{ - testUtils.CreateDoc{ - CollectionID: 1, - Doc: `{ - "name": "John Grisham" - }`, - }, - testUtils.CreateDoc{ - CollectionID: 0, - DocMap: map[string]any{ - "name": "Painted House", - "author_id": testUtils.NewDocIndex(1, 0), - }, - }, - testUtils.CreateDoc{ - CollectionID: 0, - DocMap: map[string]any{ - "name": "Golestan", - "author_id": testUtils.NewDocIndex(1, 0), - }, - ExpectedError: "target document is already linked to another document.", - }, - }, - } - - executeTestCase(t, test) -} diff --git a/tests/integration/mutation/update/field_kinds/one_to_many/simple_test.go b/tests/integration/mutation/update/field_kinds/one_to_many/simple_test.go index e8ac15e4da..7c6d33af56 100644 --- a/tests/integration/mutation/update/field_kinds/one_to_many/simple_test.go +++ b/tests/integration/mutation/update/field_kinds/one_to_many/simple_test.go @@ -72,8 +72,6 @@ func TestMutationUpdateOneToMany_RelationIDToLinkFromSingleSide_Error(t *testing executeTestCase(t, test) } -// Note: This test should probably not pass, as it contains a -// reference to a document that doesnt exist. func TestMutationUpdateOneToMany_InvalidRelationIDToLinkFromManySide(t *testing.T) { author1ID := "bae-a47f80ab-1c30-53b3-9dac-04a4a3fda77e" invalidAuthorID := "bae-35953ca-518d-9e6b-9ce6cd00eff5" @@ -106,42 +104,7 @@ func TestMutationUpdateOneToMany_InvalidRelationIDToLinkFromManySide(t *testing. }`, invalidAuthorID, ), - }, - testUtils.Request{ - Request: `query { - Author { - name - published { - name - } - } - }`, - Results: map[string]any{ - "Author": []map[string]any{ - { - "name": "John Grisham", - "published": []map[string]any{}, - }, - }, - }, - }, - testUtils.Request{ - Request: `query { - Book { - name - author { - name - } - } - }`, - Results: map[string]any{ - "Book": []map[string]any{ - { - "name": "Painted House", - "author": nil, // Linked to incorrect id - }, - }, - }, + ExpectedError: "uuid: incorrect UUID length 30 in string", }, }, } diff --git a/tests/integration/mutation/update/field_kinds/one_to_many/with_alias_test.go b/tests/integration/mutation/update/field_kinds/one_to_many/with_alias_test.go index 1f0375e6ba..79dfd2c37f 100644 --- a/tests/integration/mutation/update/field_kinds/one_to_many/with_alias_test.go +++ b/tests/integration/mutation/update/field_kinds/one_to_many/with_alias_test.go @@ -100,42 +100,7 @@ func TestMutationUpdateOneToMany_InvalidAliasRelationNameToLinkFromManySide_GQL( }`, invalidAuthorID, ), - }, - testUtils.Request{ - Request: `query { - Author { - name - published { - name - } - } - }`, - Results: map[string]any{ - "Author": []map[string]any{ - { - "name": "John Grisham", - "published": []map[string]any{}, - }, - }, - }, - }, - testUtils.Request{ - Request: `query { - Book { - name - author { - name - } - } - }`, - Results: map[string]any{ - "Book": []map[string]any{ - { - "name": "Painted House", - "author": nil, // Linked to incorrect id - }, - }, - }, + ExpectedError: "uuid: incorrect UUID length 30 in string", }, }, } @@ -143,8 +108,6 @@ func TestMutationUpdateOneToMany_InvalidAliasRelationNameToLinkFromManySide_GQL( executeTestCase(t, test) } -// Note: This test should probably not pass, as it contains a -// reference to a document that doesnt exist. func TestMutationUpdateOneToMany_InvalidAliasRelationNameToLinkFromManySide_Collection(t *testing.T) { author1ID := "bae-a47f80ab-1c30-53b3-9dac-04a4a3fda77e" invalidAuthorID := "bae-35953ca-518d-9e6b-9ce6cd00eff5" @@ -177,24 +140,7 @@ func TestMutationUpdateOneToMany_InvalidAliasRelationNameToLinkFromManySide_Coll }`, invalidAuthorID, ), - }, - testUtils.Request{ - Request: `query { - Book { - name - author { - name - } - } - }`, - Results: map[string]any{ - "Book": []map[string]any{ - { - "name": "Painted House", - "author": nil, - }, - }, - }, + ExpectedError: "uuid: incorrect UUID length 30 in string", }, }, } diff --git a/tests/integration/mutation/update/field_kinds/one_to_one/with_self_ref_test.go b/tests/integration/mutation/update/field_kinds/one_to_one/with_self_ref_test.go index dd630c0721..77087f2186 100644 --- a/tests/integration/mutation/update/field_kinds/one_to_one/with_self_ref_test.go +++ b/tests/integration/mutation/update/field_kinds/one_to_one/with_self_ref_test.go @@ -106,94 +106,3 @@ func TestMutationUpdateOneToOne_SelfReferencingFromPrimary(t *testing.T) { testUtils.ExecuteTestCase(t, test) } - -func TestMutationUpdateOneToOne_SelfReferencingFromSecondary(t *testing.T) { - user1ID := "bae-a86ab69e-a2be-54b9-b66e-4e30d6778ffe" - - test := testUtils.TestCase{ - Description: "One to one update mutation, self referencing from secondary", - - Actions: []any{ - testUtils.SchemaUpdate{ - Schema: ` - type User { - name: String - boss: User - underling: User @primary - } - `, - }, - testUtils.CreateDoc{ - Doc: `{ - "name": "John" - }`, - }, - testUtils.CreateDoc{ - Doc: `{ - "name": "Fred" - }`, - }, - testUtils.UpdateDoc{ - DocID: 1, - Doc: fmt.Sprintf( - `{ - "boss_id": "%s" - }`, - user1ID, - ), - }, - testUtils.Request{ - Request: ` - query { - User { - name - boss { - name - } - } - }`, - Results: map[string]any{ - "User": []map[string]any{ - { - "name": "Fred", - "boss": map[string]any{ - "name": "John", - }, - }, - { - "name": "John", - "boss": nil, - }, - }, - }, - }, - testUtils.Request{ - Request: ` - query { - User { - name - underling { - name - } - } - }`, - Results: map[string]any{ - "User": []map[string]any{ - { - "name": "Fred", - "underling": nil, - }, - { - "name": "John", - "underling": map[string]any{ - "name": "Fred", - }, - }, - }, - }, - }, - }, - } - - testUtils.ExecuteTestCase(t, test) -} diff --git a/tests/integration/mutation/update/field_kinds/one_to_one/with_simple_test.go b/tests/integration/mutation/update/field_kinds/one_to_one/with_simple_test.go index b2b3859d2d..8d06bae2fd 100644 --- a/tests/integration/mutation/update/field_kinds/one_to_one/with_simple_test.go +++ b/tests/integration/mutation/update/field_kinds/one_to_one/with_simple_test.go @@ -161,52 +161,11 @@ func TestMutationUpdateOneToOneSecondarySide(t *testing.T) { DocID: 0, Doc: fmt.Sprintf( `{ - "author_id": "%s" + "author": "%s" }`, authorID, ), - }, - testUtils.Request{ - Request: ` - query { - Book { - name - author { - name - } - } - }`, - Results: map[string]any{ - "Book": []map[string]any{ - { - "name": "Painted House", - "author": map[string]any{ - "name": "John Grisham", - }, - }, - }, - }, - }, - testUtils.Request{ - Request: ` - query { - Author { - name - published { - name - } - } - }`, - Results: map[string]any{ - "Author": []map[string]any{ - { - "name": "John Grisham", - "published": map[string]any{ - "name": "Painted House", - }, - }, - }, - }, + ExpectedError: "cannot set relation from secondary side", }, }, } @@ -214,34 +173,30 @@ func TestMutationUpdateOneToOneSecondarySide(t *testing.T) { } func TestMutationUpdateOneToOne_RelationIDToLinkFromPrimarySide(t *testing.T) { - author1ID := "bae-53eff350-ad8e-532c-b72d-f95c4f47909c" - bookID := "bae-89d64ba1-44e3-5d75-a610-7226077ece48" + bookID := "bae-dafb74e9-2bf1-5f12-aea9-967814592bad" test := testUtils.TestCase{ Description: "One to one update mutation using relation id from single side (wrong)", Actions: []any{ testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, Doc: `{ - "name": "John Grisham" + "name": "Painted House" }`, }, + testUtils.CreateDoc{ + CollectionID: 1, + DocMap: map[string]any{ + "name": "John Grisham", + "published_id": testUtils.NewDocIndex(0, 0), + }, + }, testUtils.CreateDoc{ CollectionID: 1, Doc: `{ "name": "New Shahzad" }`, }, - testUtils.CreateDoc{ - CollectionID: 0, - Doc: fmt.Sprintf( - `{ - "name": "Painted House", - "author_id": "%s" - }`, - author1ID, - ), - }, testUtils.UpdateDoc{ CollectionID: 1, DocID: 1, @@ -260,7 +215,6 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromPrimarySide(t *testing.T) { } func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T) { - author1ID := "bae-53eff350-ad8e-532c-b72d-f95c4f47909c" author2ID := "bae-c058cfd4-259f-5b08-975d-106f13a143d5" test := testUtils.TestCase{ @@ -280,13 +234,9 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T) }, testUtils.CreateDoc{ CollectionID: 0, - Doc: fmt.Sprintf( - `{ - "name": "Painted House", - "author_id": "%s" - }`, - author1ID, - ), + Doc: `{ + "name": "Painted House" + }`, }, testUtils.UpdateDoc{ CollectionID: 0, @@ -297,7 +247,7 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T) }`, author2ID, ), - ExpectedError: "target document is already linked to another document.", + ExpectedError: "cannot set relation from secondary side", }, }, } @@ -306,79 +256,34 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T) } func TestMutationUpdateOneToOne_InvalidLengthRelationIDToLink_Error(t *testing.T) { - author1ID := "bae-53eff350-ad8e-532c-b72d-f95c4f47909c" invalidLenSubID := "35953ca-518d-9e6b-9ce6cd00eff5" - invalidAuthorID := "bae-" + invalidLenSubID + invalidBookID := "bae-" + invalidLenSubID test := testUtils.TestCase{ Description: "One to one update mutation using invalid relation id", Actions: []any{ testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, Doc: `{ - "name": "John Grisham" + "name": "Painted House" }`, }, - testUtils.CreateDoc{ - CollectionID: 0, - Doc: fmt.Sprintf( - `{ - "name": "Painted House", - "author_id": "%s" - }`, - author1ID, - ), - }, - testUtils.UpdateDoc{ - CollectionID: 0, - DocID: 0, - Doc: fmt.Sprintf( - `{ - "author_id": "%s" - }`, - invalidAuthorID, - ), - ExpectedError: "uuid: incorrect UUID length 30 in string \"" + invalidLenSubID + "\"", - }, - }, - } - - executeTestCase(t, test) -} - -func TestMutationUpdateOneToOne_InvalidRelationIDToLinkFromSecondarySide_Error(t *testing.T) { - author1ID := "bae-53eff350-ad8e-532c-b72d-f95c4f47909c" - invalidAuthorID := "bae-2edb7fdd-cad7-5ad4-9c7d-6920245a96ee" - - test := testUtils.TestCase{ - Description: "One to one update mutation using relation id from secondary side", - Actions: []any{ testUtils.CreateDoc{ CollectionID: 1, Doc: `{ "name": "John Grisham" }`, }, - testUtils.CreateDoc{ - CollectionID: 0, - Doc: fmt.Sprintf( - `{ - "name": "Painted House", - "author_id": "%s" - }`, - author1ID, - ), - }, testUtils.UpdateDoc{ - CollectionID: 0, + CollectionID: 1, DocID: 0, Doc: fmt.Sprintf( `{ - "author_id": "%s" + "published_id": "%s" }`, - invalidAuthorID, + invalidBookID, ), - ExpectedError: "document not found or not authorized to access", + ExpectedError: "uuid: incorrect UUID length 30 in string \"" + invalidLenSubID + "\"", }, }, } diff --git a/tests/integration/query/one_to_one/with_group_related_id_alias_test.go b/tests/integration/query/one_to_one/with_group_related_id_alias_test.go index 0ae9548536..4581914473 100644 --- a/tests/integration/query/one_to_one/with_group_related_id_alias_test.go +++ b/tests/integration/query/one_to_one/with_group_related_id_alias_test.go @@ -34,29 +34,29 @@ func TestQueryOneToOneWithGroupRelatedIDAlias(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Painted House" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "John Grisham", + }, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Go Guide for Rust developers" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "Andrew Lone", + }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "John Grisham", - "published_id": testUtils.NewDocIndex(0, 0), + "name": "Painted House", + "author_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "Andrew Lone", - "published_id": testUtils.NewDocIndex(0, 1), + "name": "Go Guide for Rust developers", + "author_id": testUtils.NewDocIndex(1, 1), }, }, testUtils.Request{ @@ -74,24 +74,24 @@ func TestQueryOneToOneWithGroupRelatedIDAlias(t *testing.T) { Results: map[string]any{ "Book": []map[string]any{ { - "author_id": "bae-fc7bf08d-9117-5acd-8b49-bc7431b1b238", + "author_id": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55", "author": map[string]any{ - "name": "John Grisham", + "name": "Andrew Lone", }, "_group": []map[string]any{ { - "name": "Painted House", + "name": "Go Guide for Rust developers", }, }, }, { - "author_id": "bae-fcb12812-4c38-574e-bc8b-91b37ee6cd9b", + "author_id": "bae-ee5973cf-73c3-558f-8aec-8b590b8e77cf", "author": map[string]any{ - "name": "Andrew Lone", + "name": "John Grisham", }, "_group": []map[string]any{ { - "name": "Go Guide for Rust developers", + "name": "Painted House", }, }, }, diff --git a/tests/integration/query/one_to_one/with_group_related_id_test.go b/tests/integration/query/one_to_one/with_group_related_id_test.go index 5b1aa09dce..aa3c0e9c17 100644 --- a/tests/integration/query/one_to_one/with_group_related_id_test.go +++ b/tests/integration/query/one_to_one/with_group_related_id_test.go @@ -34,29 +34,29 @@ func TestQueryOneToOneWithGroupRelatedID(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Painted House" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "John Grisham", + }, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Go Guide for Rust developers" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "Andrew Lone", + }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "John Grisham", - "published_id": testUtils.NewDocIndex(0, 0), + "name": "Painted House", + "author_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "John Grisham", - "published_id": testUtils.NewDocIndex(0, 1), + "name": "Go Guide for Rust developers", + "author_id": testUtils.NewDocIndex(1, 1), }, }, testUtils.Request{ @@ -71,18 +71,18 @@ func TestQueryOneToOneWithGroupRelatedID(t *testing.T) { Results: map[string]any{ "Book": []map[string]any{ { - "author_id": "bae-fc7bf08d-9117-5acd-8b49-bc7431b1b238", + "author_id": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55", "_group": []map[string]any{ { - "name": "Painted House", + "name": "Go Guide for Rust developers", }, }, }, { - "author_id": "bae-f2dcf043-d24d-5885-9a0a-60196094c782", + "author_id": "bae-ee5973cf-73c3-558f-8aec-8b590b8e77cf", "_group": []map[string]any{ { - "name": "Go Guide for Rust developers", + "name": "Painted House", }, }, }, diff --git a/tests/integration/query/one_to_one_multiple/simple_test.go b/tests/integration/query/one_to_one_multiple/simple_test.go index 4696db5dcf..5ab21e7543 100644 --- a/tests/integration/query/one_to_one_multiple/simple_test.go +++ b/tests/integration/query/one_to_one_multiple/simple_test.go @@ -143,18 +143,6 @@ func TestQueryOneToOneMultiple_FromMixedPrimaryAndSecondary(t *testing.T) { } `, }, - testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Old Publisher" - }`, - }, - testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "New Publisher" - }`, - }, testUtils.CreateDoc{ CollectionID: 1, Doc: `{ @@ -170,17 +158,29 @@ func TestQueryOneToOneMultiple_FromMixedPrimaryAndSecondary(t *testing.T) { testUtils.CreateDoc{ CollectionID: 2, DocMap: map[string]any{ - "name": "Painted House", - "publisher_id": testUtils.NewDocIndex(0, 0), - "author_id": testUtils.NewDocIndex(1, 0), + "name": "Painted House", + "author_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ CollectionID: 2, DocMap: map[string]any{ - "name": "Theif Lord", - "publisher_id": testUtils.NewDocIndex(0, 1), - "author_id": testUtils.NewDocIndex(1, 1), + "name": "Theif Lord", + "author_id": testUtils.NewDocIndex(1, 1), + }, + }, + testUtils.CreateDoc{ + CollectionID: 0, + DocMap: map[string]any{ + "name": "Old Publisher", + "printed_id": testUtils.NewDocIndex(2, 0), + }, + }, + testUtils.CreateDoc{ + CollectionID: 0, + DocMap: map[string]any{ + "name": "New Publisher", + "printed_id": testUtils.NewDocIndex(2, 1), }, }, testUtils.Request{ @@ -248,43 +248,43 @@ func TestQueryOneToOneMultiple_FromSecondary(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Old Publisher" - }`, + CollectionID: 2, + DocMap: map[string]any{ + "name": "Painted House", + }, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "New Publisher" - }`, + CollectionID: 2, + DocMap: map[string]any{ + "name": "Theif Lord", + }, }, testUtils.CreateDoc{ - CollectionID: 1, - Doc: `{ - "name": "John Grisham" - }`, + CollectionID: 0, + DocMap: map[string]any{ + "name": "Old Publisher", + "printed_id": testUtils.NewDocIndex(2, 0), + }, }, testUtils.CreateDoc{ - CollectionID: 1, - Doc: `{ - "name": "Cornelia Funke" - }`, + CollectionID: 0, + DocMap: map[string]any{ + "name": "New Publisher", + "printed_id": testUtils.NewDocIndex(2, 1), + }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 1, DocMap: map[string]any{ - "name": "Painted House", - "publisher_id": testUtils.NewDocIndex(0, 0), - "author_id": testUtils.NewDocIndex(1, 0), + "name": "John Grisham", + "published_id": testUtils.NewDocIndex(2, 0), }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 1, DocMap: map[string]any{ - "name": "Theif Lord", - "publisher_id": testUtils.NewDocIndex(0, 1), - "author_id": testUtils.NewDocIndex(1, 1), + "name": "Cornelia Funke", + "published_id": testUtils.NewDocIndex(2, 1), }, }, testUtils.Request{ @@ -302,21 +302,21 @@ func TestQueryOneToOneMultiple_FromSecondary(t *testing.T) { Results: map[string]any{ "Book": []map[string]any{ { - "name": "Theif Lord", + "name": "Painted House", "publisher": map[string]any{ - "name": "New Publisher", + "name": "Old Publisher", }, "author": map[string]any{ - "name": "Cornelia Funke", + "name": "John Grisham", }, }, { - "name": "Painted House", + "name": "Theif Lord", "publisher": map[string]any{ - "name": "Old Publisher", + "name": "New Publisher", }, "author": map[string]any{ - "name": "John Grisham", + "name": "Cornelia Funke", }, }, }, diff --git a/tests/integration/query/one_to_one_to_many/simple_test.go b/tests/integration/query/one_to_one_to_many/simple_test.go index 3e65193cc7..e360d7a698 100644 --- a/tests/integration/query/one_to_one_to_many/simple_test.go +++ b/tests/integration/query/one_to_one_to_many/simple_test.go @@ -115,16 +115,16 @@ func TestQueryOneToOneToManyFromSecondaryOnOneToMany(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Indicator1" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "Observable1", + }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "Observable1", - "indicator_id": testUtils.NewDocIndex(0, 0), + "name": "Indicator1", + "observable_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ @@ -192,16 +192,16 @@ func TestQueryOneToOneToManyFromSecondaryOnOneToOne(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Indicator1" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "Observable1", + }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "Observable1", - "indicator_id": testUtils.NewDocIndex(0, 0), + "name": "Indicator1", + "observable_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ diff --git a/tests/integration/query/one_to_one_to_one/simple_test.go b/tests/integration/query/one_to_one_to_one/simple_test.go index 0486d3db77..e338d0f47e 100644 --- a/tests/integration/query/one_to_one_to_one/simple_test.go +++ b/tests/integration/query/one_to_one_to_one/simple_test.go @@ -40,43 +40,43 @@ func TestQueryOneToOneToOne(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Old Publisher" - }`, + CollectionID: 2, + DocMap: map[string]any{ + "name": "John Grisham", + }, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "New Publisher" - }`, + CollectionID: 2, + DocMap: map[string]any{ + "name": "Cornelia Funke", + }, }, testUtils.CreateDoc{ CollectionID: 1, DocMap: map[string]any{ - "name": "Painted House", - "publisher_id": testUtils.NewDocIndex(0, 0), + "name": "Painted House", + "author_id": testUtils.NewDocIndex(2, 0), }, }, testUtils.CreateDoc{ CollectionID: 1, DocMap: map[string]any{ - "name": "Theif Lord", - "publisher_id": testUtils.NewDocIndex(0, 1), + "name": "Theif Lord", + "author_id": testUtils.NewDocIndex(2, 1), }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 0, DocMap: map[string]any{ - "name": "John Grisham", - "published_id": testUtils.NewDocIndex(1, 0), + "name": "Old Publisher", + "printed_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 0, DocMap: map[string]any{ - "name": "Cornelia Funke", - "published_id": testUtils.NewDocIndex(1, 1), + "name": "New Publisher", + "printed_id": testUtils.NewDocIndex(1, 1), }, }, testUtils.Request{ @@ -156,31 +156,31 @@ func TestQueryOneToOneToOneSecondaryThenPrimary(t *testing.T) { }`, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 2, DocMap: map[string]any{ - "name": "Painted House", - "publisher_id": testUtils.NewDocIndex(0, 0), + "name": "John Grisham", }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 2, DocMap: map[string]any{ - "name": "Theif Lord", - "publisher_id": testUtils.NewDocIndex(0, 1), + "name": "Cornelia Funke", }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 1, DocMap: map[string]any{ - "name": "John Grisham", - "published_id": testUtils.NewDocIndex(1, 0), + "name": "Painted House", + "publisher_id": testUtils.NewDocIndex(0, 0), + "author_id": testUtils.NewDocIndex(2, 0), }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 1, DocMap: map[string]any{ - "name": "Cornelia Funke", - "published_id": testUtils.NewDocIndex(1, 1), + "name": "Theif Lord", + "publisher_id": testUtils.NewDocIndex(0, 1), + "author_id": testUtils.NewDocIndex(2, 1), }, }, testUtils.Request{ @@ -248,29 +248,29 @@ func TestQueryOneToOneToOnePrimaryThenSecondary(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Old Publisher" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "Painted House", + }, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "New Publisher" - }`, + CollectionID: 1, + DocMap: map[string]any{ + "name": "Theif Lord", + }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "Painted House", - "publisher_id": testUtils.NewDocIndex(0, 0), + "name": "Old Publisher", + "printed_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ - CollectionID: 1, + CollectionID: 0, DocMap: map[string]any{ - "name": "Theif Lord", - "publisher_id": testUtils.NewDocIndex(0, 1), + "name": "New Publisher", + "printed_id": testUtils.NewDocIndex(1, 1), }, }, testUtils.CreateDoc{ @@ -302,20 +302,20 @@ func TestQueryOneToOneToOnePrimaryThenSecondary(t *testing.T) { Results: map[string]any{ "Publisher": []map[string]any{ { - "name": "Old Publisher", + "name": "New Publisher", "printed": map[string]any{ - "name": "Painted House", + "name": "Theif Lord", "author": map[string]any{ - "name": "John Grisham", + "name": "Cornelia Funke", }, }, }, { - "name": "New Publisher", + "name": "Old Publisher", "printed": map[string]any{ - "name": "Theif Lord", + "name": "Painted House", "author": map[string]any{ - "name": "Cornelia Funke", + "name": "John Grisham", }, }, }, diff --git a/tests/integration/query/one_to_one_to_one/with_order_test.go b/tests/integration/query/one_to_one_to_one/with_order_test.go index 4c241cc281..a5835c8527 100644 --- a/tests/integration/query/one_to_one_to_one/with_order_test.go +++ b/tests/integration/query/one_to_one_to_one/with_order_test.go @@ -40,43 +40,43 @@ func TestQueryOneToOneToOneWithNestedOrder(t *testing.T) { `, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "Old Publisher" - }`, + CollectionID: 2, + DocMap: map[string]any{ + "name": "John Grisham", + }, }, testUtils.CreateDoc{ - CollectionID: 0, - Doc: `{ - "name": "New Publisher" - }`, + CollectionID: 2, + DocMap: map[string]any{ + "name": "Cornelia Funke", + }, }, testUtils.CreateDoc{ CollectionID: 1, DocMap: map[string]any{ - "name": "Painted House", - "publisher_id": testUtils.NewDocIndex(0, 0), + "name": "Painted House", + "author_id": testUtils.NewDocIndex(2, 0), }, }, testUtils.CreateDoc{ CollectionID: 1, DocMap: map[string]any{ - "name": "Theif Lord", - "publisher_id": testUtils.NewDocIndex(0, 1), + "name": "Theif Lord", + "author_id": testUtils.NewDocIndex(2, 1), }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 0, DocMap: map[string]any{ - "name": "John Grisham", - "published_id": testUtils.NewDocIndex(1, 0), + "name": "Old Publisher", + "printed_id": testUtils.NewDocIndex(1, 0), }, }, testUtils.CreateDoc{ - CollectionID: 2, + CollectionID: 0, DocMap: map[string]any{ - "name": "Cornelia Funke", - "published_id": testUtils.NewDocIndex(1, 1), + "name": "New Publisher", + "printed_id": testUtils.NewDocIndex(1, 1), }, }, testUtils.Request{ diff --git a/tests/integration/schema/updates/add/field/kind/doc_id_test.go b/tests/integration/schema/updates/add/field/kind/doc_id_test.go index be62192a1f..45b0471a81 100644 --- a/tests/integration/schema/updates/add/field/kind/doc_id_test.go +++ b/tests/integration/schema/updates/add/field/kind/doc_id_test.go @@ -72,7 +72,7 @@ func TestSchemaUpdatesAddFieldKindDocIDWithCreate(t *testing.T) { CollectionID: 0, Doc: `{ "name": "John", - "foo": "nhgfdsfd" + "foo": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55" }`, }, testUtils.Request{ @@ -86,7 +86,7 @@ func TestSchemaUpdatesAddFieldKindDocIDWithCreate(t *testing.T) { "Users": []map[string]any{ { "name": "John", - "foo": "nhgfdsfd", + "foo": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55", }, }, }, @@ -118,7 +118,7 @@ func TestSchemaUpdatesAddFieldKindDocIDSubstitutionWithCreate(t *testing.T) { CollectionID: 0, Doc: `{ "name": "John", - "foo": "nhgfdsfd" + "foo": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55" }`, }, testUtils.Request{ @@ -132,7 +132,7 @@ func TestSchemaUpdatesAddFieldKindDocIDSubstitutionWithCreate(t *testing.T) { "Users": []map[string]any{ { "name": "John", - "foo": "nhgfdsfd", + "foo": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55", }, }, }, diff --git a/tests/predefined/gen_predefined_test.go b/tests/predefined/gen_predefined_test.go index a32c261ce7..fc68681b4e 100644 --- a/tests/predefined/gen_predefined_test.go +++ b/tests/predefined/gen_predefined_test.go @@ -233,55 +233,6 @@ func TestGeneratePredefinedFromSchema_OneToOneToOnePrimary(t *testing.T) { } } -func TestGeneratePredefinedFromSchema_TwoPrimaryToOneMiddle(t *testing.T) { - schema := ` - type User { - name: String - device: Device - } - type Device { - model: String - owner: User @primary - specs: Specs @primary - } - type Specs { - OS: String - device: Device - }` - - docs, err := CreateFromSDL(schema, DocsList{ - ColName: "User", - Docs: []map[string]any{ - { - "name": "John", - "device": map[string]any{ - "model": "iPhone", - "specs": map[string]any{ - "OS": "iOS", - }, - }, - }, - }, - }) - assert.NoError(t, err) - - colDefMap, err := gen.ParseSDL(schema) - require.NoError(t, err) - - specsDoc := mustAddDocIDToDoc(map[string]any{"OS": "iOS"}, colDefMap["Specs"]) - userDoc := mustAddDocIDToDoc(map[string]any{"name": "John"}, colDefMap["User"]) - deviceDoc := mustAddDocIDToDoc(map[string]any{ - "model": "iPhone", - "specs_id": specsDoc[request.DocIDFieldName], - "owner_id": userDoc[request.DocIDFieldName], - }, colDefMap["Device"]) - - errorMsg := assertDocs([]map[string]any{userDoc, deviceDoc, specsDoc}, docs) - if errorMsg != "" { - t.Error(errorMsg) - } -} - func TestGeneratePredefinedFromSchema_OneToTwoPrimary(t *testing.T) { schema := ` type User {