Skip to content

Commit

Permalink
test: Allow assertion of AddSchema results (#2788)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2766

## Description

Allows assertion of AddSchema results.
  • Loading branch information
AndrewSisley committed Jun 28, 2024
1 parent 666e871 commit 30b40fd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
42 changes: 42 additions & 0 deletions tests/integration/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (

"github.com/sourcenetwork/immutable"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/sourcenetwork/defradb/client"
)

// AnyOf may be used as `Results` field where the value may
Expand Down Expand Up @@ -184,3 +187,42 @@ func areResultArraysEqual[S any](expected []S, actual any) bool {
}
return true
}

func assertCollectionDescriptions(
s *state,
expected []client.CollectionDescription,
actual []client.CollectionDescription,
) {
require.Equal(s.t, len(expected), len(actual))

for i, expected := range expected {
actual := actual[i]
if expected.ID != 0 {
require.Equal(s.t, expected.ID, actual.ID)
}
if expected.RootID != 0 {
require.Equal(s.t, expected.RootID, actual.RootID)
}
if expected.SchemaVersionID != "" {
require.Equal(s.t, expected.SchemaVersionID, actual.SchemaVersionID)
}

require.Equal(s.t, expected.Name, actual.Name)

if expected.Indexes != nil || len(actual.Indexes) != 0 {
// Dont bother asserting this if the expected is nil and the actual is nil/empty.
// This is to say each test action from having to bother declaring an empty slice (if there are no indexes)
require.Equal(s.t, expected.Indexes, actual.Indexes)
}

if expected.Sources != nil || len(actual.Sources) != 0 {
// Dont bother asserting this if the expected is nil and the actual is nil/empty.
// This is to say each test action from having to bother declaring an empty slice (if there are no sources)
require.Equal(s.t, expected.Sources, actual.Sources)
}

if expected.Fields != nil {
require.Equal(s.t, expected.Fields, actual.Fields)
}
}
}
11 changes: 11 additions & 0 deletions tests/integration/schema/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/sourcenetwork/immutable"

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

Expand All @@ -28,6 +29,16 @@ func TestSchemaSimpleCreatesSchemaGivenEmptyType(t *testing.T) {
Schema: `
type Users {}
`,
ExpectedResults: []client.CollectionDescription{
{
Name: immutable.Some("Users"),
Fields: []client.CollectionFieldDescription{
{
Name: request.DocIDFieldName,
},
},
},
},
},
testUtils.IntrospectionRequest{
Request: `
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ type SchemaUpdate struct {
// The schema update.
Schema string

// Optionally, the expected results.
//
// Each item will be compared individually, if ID, RootID, SchemaVersionID or Fields on the
// expected item are default they will not be compared with the actual.
//
// Assertions on Indexes and Sources will not distinguish between nil and empty (in order
// to allow their ommission in most cases).
ExpectedResults []client.CollectionDescription

// Any error expected from the action. Optional.
//
// String can be a partial, and the test will pass if an error is returned that
Expand Down
39 changes: 10 additions & 29 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,14 @@ func updateSchema(
action SchemaUpdate,
) {
for _, node := range getNodes(action.NodeID, s.nodes) {
_, err := node.AddSchema(s.ctx, action.Schema)
results, err := node.AddSchema(s.ctx, action.Schema)
expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)

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

if action.ExpectedResults != nil {
assertCollectionDescriptions(s, action.ExpectedResults, results)
}
}

// If the schema was updated we need to refresh the collection definitions.
Expand Down Expand Up @@ -1089,39 +1093,16 @@ func getCollections(
txn := getTransaction(s, node, action.TransactionID, "")
ctx := db.SetContextTxn(s.ctx, txn)
results, err := node.GetCollections(ctx, action.FilterOptions)
resultDescriptions := make([]client.CollectionDescription, len(results))
for i, col := range results {
resultDescriptions[i] = col.Description()
}

expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)

if !expectedErrorRaised {
require.Equal(s.t, len(action.ExpectedResults), len(results))

for i, expected := range action.ExpectedResults {
actual := results[i].Description()
if expected.ID != 0 {
require.Equal(s.t, expected.ID, actual.ID)
}
if expected.RootID != 0 {
require.Equal(s.t, expected.RootID, actual.RootID)
}
if expected.SchemaVersionID != "" {
require.Equal(s.t, expected.SchemaVersionID, actual.SchemaVersionID)
}

require.Equal(s.t, expected.Name, actual.Name)

if expected.Indexes != nil || len(actual.Indexes) != 0 {
// Dont bother asserting this if the expected is nil and the actual is nil/empty.
// This is to say each test action from having to bother declaring an empty slice (if there are no indexes)
require.Equal(s.t, expected.Indexes, actual.Indexes)
}

if expected.Sources != nil || len(actual.Sources) != 0 {
// Dont bother asserting this if the expected is nil and the actual is nil/empty.
// This is to say each test action from having to bother declaring an empty slice (if there are no sources)
require.Equal(s.t, expected.Sources, actual.Sources)
}
}
assertCollectionDescriptions(s, action.ExpectedResults, resultDescriptions)
}
}
}
Expand Down

0 comments on commit 30b40fd

Please sign in to comment.