File: validation_test.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,516 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (113 lines) | stat: -rw-r--r-- 3,628 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package validation_test

import (
	"encoding/json"
	"os"
	"reflect"
	"sort"
	"testing"

	"github.com/graph-gophers/graphql-go/ast"
	"github.com/graph-gophers/graphql-go/errors"
	"github.com/graph-gophers/graphql-go/internal/query"
	"github.com/graph-gophers/graphql-go/internal/schema"
	"github.com/graph-gophers/graphql-go/internal/validation"
)

type Schema struct {
	ID  string
	SDL string
}

type Test struct {
	Name   string
	Rule   string
	Schema string
	Query  string
	Vars   map[string]interface{}
	Errors []*errors.QueryError
}

func TestValidate(t *testing.T) {
	skip := map[string]struct{}{
		// Minor issue: reporting extra error under PossibleFragmentSpreadsRule which is not intended
		"Validate: Possible fragment spreads/ignores incorrect type (caught by FragmentsOnCompositeTypesRule)": {},
		// graphql-js test case parses SDL as if it was a query here, which fails since we only accept a query
		"Validate: Directives Are Unique Per Location/unknown directives must be ignored": {},
		// The meta schema always includes the standard types, so this isn't applicable
		"Validate: Known type names/references to standard scalars that are missing in schema": {},
		// Ignore tests using experimental @stream
		"Validate: Overlapping fields can be merged/different stream directive label":               {},
		"Validate: Overlapping fields can be merged/different stream directive initialCount":        {},
		"Validate: Overlapping fields can be merged/different stream directive first missing args":  {},
		"Validate: Overlapping fields can be merged/different stream directive second missing args": {},
		"Validate: Overlapping fields can be merged/different stream directive extra argument":      {},
		"Validate: Overlapping fields can be merged/mix of stream and no stream":                    {},
	}

	f, err := os.Open("testdata/tests.json")
	if err != nil {
		t.Fatal(err)
	}

	var testData struct {
		Schemas []Schema
		Tests   []*Test
	}
	if err := json.NewDecoder(f).Decode(&testData); err != nil {
		t.Fatal(err)
	}

	schemas := make(map[string]*ast.Schema, len(testData.Schemas))
	for _, sc := range testData.Schemas {
		s := schema.New()

		s.Directives["oneOf"] = &ast.DirectiveDefinition{
			// graphql-js includes support for @oneOf, currently in RFC
			// This is not available in graphql-go, nor is it expected to be unless the RFC is accepted
			// See https://github.com/graphql/graphql-js/pull/3513 & https://github.com/graphql/graphql-spec/pull/825/
			Name:      "oneOf",
			Desc:      "Indicates exactly one field must be supplied and this field must not be `null`.",
			Locations: []string{"INPUT_OBJECT"},
		}

		err := schema.Parse(s, sc.SDL, false)
		if err != nil {
			t.Fatal(err)
		}
		schemas[sc.ID] = s
	}

	for _, test := range testData.Tests {
		t.Run(test.Name, func(t *testing.T) {
			if _, ok := skip[test.Name]; ok {
				t.Skip("Test ignored")
			}

			d, err := query.Parse(test.Query)
			if err != nil {
				t.Fatalf("failed to parse query: %s", err)
			}
			errs := validation.Validate(schemas[test.Schema], d, test.Vars, 0, 0)
			got := []*errors.QueryError{}
			for _, err := range errs {
				if err.Rule == test.Rule {
					err.Rule = ""
					got = append(got, err)
				}
			}
			sortLocations(test.Errors)
			sortLocations(got)
			if !reflect.DeepEqual(test.Errors, got) {
				t.Errorf("wrong errors for rule %q\nexpected: %v\ngot:      %v", test.Rule, test.Errors, got)
			}
		})
	}
}

func sortLocations(errs []*errors.QueryError) {
	for _, err := range errs {
		locs := err.Locations
		sort.Slice(locs, func(i, j int) bool { return locs[i].Before(locs[j]) })
	}
}