Skip to content

Commit

Permalink
fix spurious test failures caused by #81
Browse files Browse the repository at this point in the history
  • Loading branch information
gmac committed Nov 4, 2021
1 parent bc9a9b9 commit 62b21ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
34 changes: 32 additions & 2 deletions plan_fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"encoding/json"
"sort"
"testing"
"context"
"fmt"

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

"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/99designs/gqlgen/graphql"
)

type PlanTestFixture struct {
Expand Down Expand Up @@ -261,7 +264,7 @@ var PlanTestFixture6 = &PlanTestFixture{
}


func (f *PlanTestFixture) Check(t *testing.T, query, expectedJSON string) {
func (f *PlanTestFixture) Plan(t *testing.T, query string) *QueryPlan {
t.Helper()
schema := gqlparser.MustLoadSchema(&ast.Source{Name: "fixture", Input: f.Schema})
operation := gqlparser.MustLoadQuery(schema, query)
Expand All @@ -273,7 +276,34 @@ func (f *PlanTestFixture) Check(t *testing.T, query, expectedJSON string) {
}})
require.NoError(t, err)
actual.SortSteps()
assert.JSONEq(t, expectedJSON, jsonMustMarshal(actual))
return actual
}

func (f *PlanTestFixture) Check(t *testing.T, query string, expectedJSON string) {
assert.JSONEq(t, expectedJSON, jsonMustMarshal(f.Plan(t, query)))
}

func (f *PlanTestFixture) CheckUnorderedRootFieldSelections(t *testing.T, query string, expectedSelections []string) {
ctx := graphql.WithOperationContext(context.Background(), &graphql.OperationContext{
Variables: map[string]interface{}{},
})

result := f.Plan(t, query)
rootField := result.RootSteps[0].SelectionSet[0].(*ast.Field)

assert.Equal(t, len(rootField.SelectionSet), len(expectedSelections))

for _, expectedSelection := range expectedSelections {
var foundSelection string
expectedSelection = fmt.Sprintf("{ %s }", expectedSelection)
for _, selection := range rootField.SelectionSet {
if expectedSelection == formatSelectionSetSingleLine(ctx, nil, []ast.Selection{selection}) {
foundSelection = expectedSelection
break
}
}
assert.Equal(t, expectedSelection, foundSelection)
}
}

type ByServiceURL []*QueryPlanStep
Expand Down
46 changes: 14 additions & 32 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,18 +492,12 @@ func TestQueryPlanExpandAbstractTypesWithPossibleBoundaryIds(t *testing.T) {
name
}
}`
plan := `{
"RootSteps": [
{
"ServiceURL": "A",
"ParentType": "Query",
"SelectionSet": "{ animals { ... on Snake { _id: id } ... on Lion { _id: id } name } }",
"InsertionPoint": null,
"Then": null
}
]
}`
PlanTestFixture3.Check(t, query, plan)
rootFieldSelections := []string{
"name",
"... on Lion { _id: id }",
"... on Snake { _id: id }",
}
PlanTestFixture3.CheckUnorderedRootFieldSelections(t, query, rootFieldSelections)
}

func TestQueryPlanInlineFragmentSpreadOfInterface(t *testing.T) {
Expand All @@ -519,26 +513,14 @@ func TestQueryPlanInlineFragmentSpreadOfInterface(t *testing.T) {
}
}
}`
plan := `{
"RootSteps": [
{
"ServiceURL": "A",
"ParentType": "Query",
"SelectionSet": "{ animals { ... on Snake { _id: id } ... on Lion { _id: id } name ... on Lion { maneColor __typename } ... on Snake { _id: id __typename } } }",
"InsertionPoint": null,
"Then": [
{
"ServiceURL": "B",
"ParentType": "Snake",
"SelectionSet": "{ _id: id venomous }",
"InsertionPoint": ["animals"],
"Then": null
}
]
}
]
}`
PlanTestFixture3.Check(t, query, plan)
rootFieldSelections := []string{
"name",
"... on Lion { _id: id }",
"... on Snake { _id: id }",
"... on Lion { maneColor __typename }",
"... on Snake { _id: id __typename }",
}
PlanTestFixture3.CheckUnorderedRootFieldSelections(t, query, rootFieldSelections)
}

func TestQueryPlanSkipDirective(t *testing.T) {
Expand Down

0 comments on commit 62b21ae

Please sign in to comment.