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

test(i): Test CreateDoc tests with gql and Collection.Save #1837

Merged
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
2 changes: 1 addition & 1 deletion tests/integration/query/one_to_many/with_dockeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestQueryOneToManyWithChildDocKeys(t *testing.T) {
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`,
`{
"name": "A Time for Mercy",
"name": "The Firm",
"rating": 4.5,
"author_id": "bae-41598f0c-19bc-5da6-813b-e80f14a10df3"
}`,
Expand Down
9 changes: 8 additions & 1 deletion tests/integration/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type SchemaPatch struct {
}

// CreateDoc will attempt to create the given document in the given collection
// using the collection api.
// using the set [MutationType].
type CreateDoc struct {
// NodeID may hold the ID (index) of a node to apply this create to.
//
Expand All @@ -97,6 +97,13 @@ type CreateDoc struct {
// String can be a partial, and the test will pass if an error is returned that
// contains this string.
ExpectedError string

// If provided a value, SupportedMutationTypes will cause this test to be skipped
// if the active mutation type is not within the given set.
//
// This is to only be used in the very rare cases where we really do want behavioural
// differences between mutation types, or we need to temporarily document a bug.
SupportedMutationTypes immutable.Option[[]MutationType]
}

// DeleteDoc will attempt to delete the given document in the given collection
Expand Down
130 changes: 101 additions & 29 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,34 +1027,42 @@ func patchSchema(
refreshIndexes(s)
}

// createDoc creates a document using the collection api and caches it in the
// given documents slice.
// createDoc creates a document using the chosen [mutationType] and caches it in the
// test state object.
func createDoc(
s *state,
action CreateDoc,
) {
// All the docs should be identical, and we only need 1 copy so taking the last
// is okay.
skipIfMutationTypeUnsupported(s.t, action.SupportedMutationTypes)

var mutation func(*state, CreateDoc, *net.Node, []client.Collection) (*client.Document, error)

switch mutationType {
case CollectionSaveMutationType:
mutation = createDocViaColSave
case GQLRequestMutationType:
mutation = createDocViaGQL
default:
s.t.Fatalf("invalid mutationType: %v", mutationType)
}

var expectedErrorRaised bool
var doc *client.Document
actionNodes := getNodes(action.NodeID, s.nodes)
for nodeID, collections := range getNodeCollections(action.NodeID, s.collections) {
var err error
doc, err = client.NewDocFromJSON([]byte(action.Doc))
if AssertError(s.t, s.testCase.Description, err, action.ExpectedError) {
return
}

err = withRetry(
err := withRetry(
actionNodes,
nodeID,
func() error { return collections[action.CollectionID].Save(s.ctx, doc) },
func() error {
var err error
doc, err = mutation(s, action, actionNodes[nodeID], collections)
return err
},
)
if AssertError(s.t, s.testCase.Description, err, action.ExpectedError) {
return
}
expectedErrorRaised = AssertError(s.t, s.testCase.Description, err, action.ExpectedError)
}

assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, false)
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)

if action.CollectionID >= len(s.documents) {
// Expand the slice if required, so that the document can be accessed by collection index
Expand All @@ -1063,6 +1071,64 @@ func createDoc(
s.documents[action.CollectionID] = append(s.documents[action.CollectionID], doc)
}

func createDocViaColSave(
s *state,
action CreateDoc,
node *net.Node,
collections []client.Collection,
) (*client.Document, error) {
var err error
doc, err := client.NewDocFromJSON([]byte(action.Doc))
if err != nil {
return nil, err
}

return doc, collections[action.CollectionID].Save(s.ctx, doc)
}

func createDocViaGQL(
s *state,
action CreateDoc,
node *net.Node,
collections []client.Collection,
) (*client.Document, error) {
collection := collections[action.CollectionID]

escapedJson, err := json.Marshal(action.Doc)
require.NoError(s.t, err)

request := fmt.Sprintf(
`mutation {
create_%s(data: %s) {
_key
}
}`,
collection.Name(),
escapedJson,
)

db := getStore(s, node.DB, immutable.None[int](), action.ExpectedError)

result := db.ExecRequest(s.ctx, request)
if len(result.GQL.Errors) > 0 {
return nil, result.GQL.Errors[0]
}

resultantDocs, ok := result.GQL.Data.([]map[string]any)
if !ok || len(resultantDocs) == 0 {
return nil, nil
}

docKeyString := resultantDocs[0]["_key"].(string)
docKey, err := client.NewDocKeyFromString(docKeyString)
require.NoError(s.t, err)

doc, err := collection.Get(s.ctx, docKey, false)
require.NoError(s.t, err)

return doc, nil
}

// deleteDoc deletes a document using the collection api and caches it in the
// given documents slice.
func deleteDoc(
Expand Down Expand Up @@ -1093,19 +1159,7 @@ func updateDoc(
s *state,
action UpdateDoc,
) {
if action.SupportedMutationTypes.HasValue() {
var isTypeSupported bool
for _, supportedMutationType := range action.SupportedMutationTypes.Value() {
if supportedMutationType == mutationType {
isTypeSupported = true
break
}
}

if !isTypeSupported {
s.t.Skipf("test does not support given mutation type. Type: %s", mutationType)
}
}
skipIfMutationTypeUnsupported(s.t, action.SupportedMutationTypes)

var mutation func(*state, UpdateDoc, *net.Node, []client.Collection) error

Expand Down Expand Up @@ -1711,3 +1765,21 @@ func assertBackupContent(t *testing.T, expectedContent, filepath string) {
string(b),
)
}

// skipIfMutationTypeUnsupported skips the current test if the given supportedMutationTypes option has value
// and the active mutation type is not contained within that value set.
func skipIfMutationTypeUnsupported(t *testing.T, supportedMutationTypes immutable.Option[[]MutationType]) {
if supportedMutationTypes.HasValue() {
var isTypeSupported bool
for _, supportedMutationType := range supportedMutationTypes.Value() {
if supportedMutationType == mutationType {
isTypeSupported = true
break
}
}

if !isTypeSupported {
t.Skipf("test does not support given mutation type. Type: %s", mutationType)
}
}
}