Skip to content

Commit

Permalink
TEMP
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Mar 22, 2024
1 parent d524320 commit 9a068d5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
62 changes: 62 additions & 0 deletions tests/integration/mutation/update/with_invalid_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 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 update

import (
"testing"

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

func TestMutationUpdate_WithInvalidID(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple update mutation with invalid document id",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
points: Float
}
`,
},
testUtils.CreateDoc{
Doc: `{
"name": "John",
"points": 42.1
}`,
},
testUtils.UpdateDoc{
CollectionID: 0,
DocID: -1,
Doc: `{
"points": 59
}`,
},
testUtils.Request{
Request: `query {
Users {
name
points
}
}`,
Results: []map[string]any{
{
"name": "John",
"points": float64(59),
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}
48 changes: 44 additions & 4 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ const (
// Instantiating lenses is expensive, and our tests do not benefit from a large number of them,
// so we explicitly set it to a low value.
lensPoolSize = 2

// Invalid doc string for testing invalid docID input.
invalidDocID = "bae-b769708d-f552-5c3d-a402-ccfd7ac7fb04"
)

const testJSONFile = "/test.json"
Expand Down Expand Up @@ -1287,6 +1290,21 @@ func updateDocViaColSave(
node client.P2P,
collections []client.Collection,
) error {
if action.DocID < 0 {
docID, err := client.NewDocIDFromString(invalidDocID)
if err != nil {
return err
}
collection := collections[action.CollectionID]
invalidDoc := client.NewDocWithID(docID, collection.Schema())
err = invalidDoc.SetWithJSON([]byte(action.Doc))
if err != nil {
return err
}
return collections[action.CollectionID].Save(s.ctx, invalidDoc)

Check failure on line 1305 in tests/integration/utils2.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unnecessary trailing newline (whitespace)
}

cachedDoc := s.documents[action.CollectionID][action.DocID]

doc, err := collections[action.CollectionID].Get(s.ctx, cachedDoc.ID(), true)
Expand All @@ -1300,8 +1318,8 @@ func updateDocViaColSave(
}

s.documents[action.CollectionID][action.DocID] = doc

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

Check failure on line 1322 in tests/integration/utils2.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unnecessary trailing newline (whitespace)
}

func updateDocViaColUpdate(
Expand All @@ -1310,6 +1328,20 @@ func updateDocViaColUpdate(
node client.P2P,
collections []client.Collection,
) error {
if action.DocID < 0 {
docID, err := client.NewDocIDFromString(invalidDocID)
if err != nil {
return err
}
collection := collections[action.CollectionID]
invalidDoc := client.NewDocWithID(docID, collection.Schema())
err = invalidDoc.SetWithJSON([]byte(action.Doc))
if err != nil {
return err
}
return collections[action.CollectionID].Update(s.ctx, invalidDoc)

Check failure on line 1343 in tests/integration/utils2.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unnecessary trailing newline (whitespace)
}
cachedDoc := s.documents[action.CollectionID][action.DocID]

doc, err := collections[action.CollectionID].Get(s.ctx, cachedDoc.ID(), true)
Expand All @@ -1323,8 +1355,8 @@ func updateDocViaColUpdate(
}

s.documents[action.CollectionID][action.DocID] = doc

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

Check failure on line 1359 in tests/integration/utils2.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unnecessary trailing newline (whitespace)
}

func updateDocViaGQL(
Expand All @@ -1333,7 +1365,15 @@ func updateDocViaGQL(
node client.P2P,
collections []client.Collection,
) error {
doc := s.documents[action.CollectionID][action.DocID]
var docIDStr string

if action.DocID < 0 {
docIDStr = invalidDocID
} else {
doc := s.documents[action.CollectionID][action.DocID]
docIDStr = doc.ID().String()
}

collection := collections[action.CollectionID]

input, err := jsonToGQL(action.Doc)
Expand All @@ -1346,7 +1386,7 @@ func updateDocViaGQL(
}
}`,
collection.Name().Value(),
doc.ID().String(),
docIDStr,
input,
)

Expand Down

0 comments on commit 9a068d5

Please sign in to comment.