Skip to content

Commit

Permalink
refactor(i): Collection tests (sourcenetwork#2861)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#2860 

## Description

This PR refactors the collection tests to use the test integration test
framework. It also adds a new `UpdateWithFilter` test action.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

(*replace*) Describe the tests performed to verify the changes. Provide
instructions to reproduce them.

Specify the platform(s) on which this was tested:
- MacOS
  • Loading branch information
nasdf authored Jul 26, 2024
1 parent 12b7624 commit e422058
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 286 deletions.
10 changes: 8 additions & 2 deletions cli/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package cli

import (
"encoding/json"

"github.com/spf13/cobra"

"github.com/sourcenetwork/defradb/client"
Expand Down Expand Up @@ -48,8 +50,12 @@ Example: update private docID, with identity:
}

switch {
case filter != "" && updater != "":
res, err := col.UpdateWithFilter(cmd.Context(), filter, updater)
case filter != "" || updater != "":
var filterValue any
if err := json.Unmarshal([]byte(filter), &filterValue); err != nil {
return err
}
res, err := col.UpdateWithFilter(cmd.Context(), filterValue, updater)
if err != nil {
return err
}
Expand Down
34 changes: 16 additions & 18 deletions tests/integration/collection/update/simple/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ package update
import (
"testing"

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

var userCollectionGQLSchema = `
var schema = `
type Users {
name: String
age: Int
Expand All @@ -27,19 +25,19 @@ var userCollectionGQLSchema = `
}
`

var colDefMap = make(map[string]client.CollectionDefinition)

func init() {
c, err := testUtils.ParseSDL(userCollectionGQLSchema)
if err != nil {
panic(err)
}
u := c["Users"]
u.Schema.Root = "bafkreiclkqkxhq3xu3sz5fqcixykk2qfpva5asj3elcaqyxscax66ok4za"
c["Users"] = u
colDefMap = c
}

func executeTestCase(t *testing.T, test testUtilsCol.TestCase) {
testUtilsCol.ExecuteRequestTestCase(t, userCollectionGQLSchema, test)
func executeTestCase(t *testing.T, test testUtils.TestCase) {
testUtils.ExecuteTestCase(
t,
testUtils.TestCase{
Description: test.Description,
Actions: append(
[]any{
testUtils.SchemaUpdate{
Schema: schema,
},
},
test.Actions...,
),
},
)
}
257 changes: 107 additions & 150 deletions tests/integration/collection/update/simple/with_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,193 +11,150 @@
package update

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

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

func TestUpdateWithInvalidFilterType(t *testing.T) {
func TestUpdateWithInvalidFilterType_ReturnsError(t *testing.T) {
test := testUtils.TestCase{
Description: "Test update users with invalid filter type",
Docs: map[string][]string{},
CollectionCalls: map[string][]func(client.Collection) error{
"Users": []func(c client.Collection) error{
func(c client.Collection) error {
ctx := context.Background()
// test with an invalid filter type
_, err := c.UpdateWithFilter(
ctx,
t,
`{"name": "Eric"}`,
)
return err
},
Actions: []any{
testUtils.UpdateWithFilter{
CollectionID: 0,
Filter: t,
Updater: `{"name": "Eric"}`,
ExpectedError: "invalid filter",
},
},
ExpectedError: "invalid filter",
}

executeTestCase(t, test)
}

func TestUpdateWithEmptyFilter(t *testing.T) {
func TestUpdateWithEmptyFilter_ReturnsError(t *testing.T) {
test := testUtils.TestCase{
Description: "Test update users with empty filter",
Docs: map[string][]string{},
CollectionCalls: map[string][]func(client.Collection) error{
"Users": []func(c client.Collection) error{
func(c client.Collection) error {
ctx := context.Background()
// test with an empty filter
_, err := c.UpdateWithFilter(
ctx,
"",
`{"name": "Eric"}`,
)
return err
},
Actions: []any{
testUtils.UpdateWithFilter{
CollectionID: 0,
Filter: "",
Updater: `{"name": "Eric"}`,
ExpectedError: "invalid filter",
},
},
ExpectedError: "invalid filter",
}

executeTestCase(t, test)
}

func TestUpdateWithFilter(t *testing.T) {
docStr := `{
"name": "John",
"age": 21
}`

doc, err := client.NewDocFromJSON([]byte(docStr), colDefMap["Users"])
if err != nil {
assert.Fail(t, err.Error())
func TestUpdateWithInvalidJSON_ReturnsError(t *testing.T) {
test := testUtils.TestCase{
Description: "Test update users with filter and invalid JSON",
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "John",
"age": 21
}`,
},
testUtils.UpdateWithFilter{
CollectionID: 0,
Filter: `{name: {_eq: "John"}}`,
Updater: `{name: "Eric"}`,
ExpectedError: "cannot parse JSON: cannot parse object",
},
},
}

filter := `{name: {_eq: "John"}}`
executeTestCase(t, test)
}

tests := []testUtils.TestCase{
{
Description: "Test update users with filter and invalid JSON",
Docs: map[string][]string{
"Users": {docStr},
func TestUpdateWithInvalidUpdater_ReturnsError(t *testing.T) {
test := testUtils.TestCase{
Description: "Test update users with filter and invalid updator",
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "John",
"age": 21
}`,
},
CollectionCalls: map[string][]func(client.Collection) error{
"Users": []func(c client.Collection) error{
func(c client.Collection) error {
ctx := context.Background()
_, err := c.UpdateWithFilter(
ctx,
filter,
`{name: "Eric"}`,
)
return err
},
},
testUtils.UpdateWithFilter{
CollectionID: 0,
Filter: `{name: {_eq: "John"}}`,
Updater: `"name: Eric"`,
ExpectedError: "the updater of a document is of invalid type",
},
ExpectedError: "cannot parse JSON: cannot parse object",
}, {
Description: "Test update users with filter and invalid updator",
Docs: map[string][]string{
"Users": {docStr},
},
}

executeTestCase(t, test)
}

func TestUpdateWithPatch_DoesNothing(t *testing.T) {
test := testUtils.TestCase{
Description: "Test update users with filter and patch updator (not implemented so no change)",
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "John",
"age": 21
}`,
},
CollectionCalls: map[string][]func(client.Collection) error{
"Users": []func(c client.Collection) error{
func(c client.Collection) error {
ctx := context.Background()
_, err := c.UpdateWithFilter(
ctx,
filter,
`"name: Eric"`,
)
return err
},
},
testUtils.UpdateWithFilter{
CollectionID: 0,
Filter: `{name: {_eq: "John"}}`,
Updater: `[{"name": "Eric"}, {"name": "Sam"}]`,
SkipLocalUpdateEvent: true,
},
ExpectedError: "the updater of a document is of invalid type",
}, {
Description: "Test update users with filter and patch updator (not implemented so no change)",
Docs: map[string][]string{
"Users": {docStr},
testUtils.Request{
Request: `query{
Users {
name
}
}`,
Results: []map[string]any{
{"name": "John"},
},
},
CollectionCalls: map[string][]func(client.Collection) error{
"Users": []func(c client.Collection) error{
func(c client.Collection) error {
ctx := context.Background()
_, err := c.UpdateWithFilter(
ctx,
filter,
`[
{
"name": "Eric"
}, {
"name": "Sam"
}
]`,
)
if err != nil {
return err
}

d, err := c.Get(ctx, doc.ID(), false)
if err != nil {
return err
}

name, err := d.Get("name")
if err != nil {
return err
}
},
}

assert.Equal(t, "John", name)
executeTestCase(t, test)
}

return nil
},
},
func TestUpdateWithFilter_Succeeds(t *testing.T) {
test := testUtils.TestCase{
Description: "Test update users with filter",
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "John",
"age": 21
}`,
},
}, {
Description: "Test update users with filter",
Docs: map[string][]string{
"Users": {docStr},
testUtils.UpdateWithFilter{
CollectionID: 0,
Filter: `{name: {_eq: "John"}}`,
Updater: `{"name": "Eric"}`,
},
CollectionCalls: map[string][]func(client.Collection) error{
"Users": []func(c client.Collection) error{
func(c client.Collection) error {
ctx := context.Background()
_, err := c.UpdateWithFilter(
ctx,
filter,
`{"name": "Eric"}`,
)
if err != nil {
return err
}

d, err := c.Get(ctx, doc.ID(), false)
if err != nil {
return err
}

name, err := d.Get("name")
if err != nil {
return err
}

assert.Equal(t, "Eric", name)

return nil
},
testUtils.Request{
Request: `query{
Users {
name
}
}`,
Results: []map[string]any{
{"name": "Eric"},
},
},
},
}

for _, test := range tests {
executeTestCase(t, test)
}
executeTestCase(t, test)
}
Loading

0 comments on commit e422058

Please sign in to comment.