-
Notifications
You must be signed in to change notification settings - Fork 493
/
introspection_test.go
89 lines (80 loc) · 1.91 KB
/
introspection_test.go
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
package graphql_test
import (
"bytes"
"encoding/json"
"os"
"testing"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/social"
"github.com/graph-gophers/graphql-go/example/starwars"
)
func TestSchema_ToJSON(t *testing.T) {
t.Parallel()
type args struct {
Schema *graphql.Schema
}
type want struct {
JSON []byte
}
testTable := []struct {
Name string
Args args
Want want
}{
{
Name: "Social Schema",
Args: args{Schema: graphql.MustParseSchema(social.Schema, &social.Resolver{}, graphql.UseFieldResolvers())},
Want: want{JSON: mustReadFile("example/social/introspect.json")},
},
{
Name: "Star Wars Schema",
Args: args{Schema: graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{})},
Want: want{JSON: mustReadFile("example/starwars/introspect.json")},
},
{
Name: "Star Wars Schema without Resolver",
Args: args{Schema: graphql.MustParseSchema(starwars.Schema, nil)},
Want: want{JSON: mustReadFile("example/starwars/introspect.json")},
},
}
for _, tt := range testTable {
t.Run(tt.Name, func(t *testing.T) {
j, err := tt.Args.Schema.ToJSON()
if err != nil {
t.Fatalf("invalid schema %s", err.Error())
}
// Verify JSON to avoid red herring errors.
got, err := formatJSON(j)
if err != nil {
t.Fatalf("got: invalid JSON: %s", err)
}
want, err := formatJSON(tt.Want.JSON)
if err != nil {
t.Fatalf("want: invalid JSON: %s", err)
}
if !bytes.Equal(got, want) {
t.Logf("got: %s", got)
t.Logf("want: %s", want)
t.Fail()
}
})
}
}
func formatJSON(data []byte) ([]byte, error) {
var v interface{}
if err := json.Unmarshal(data, &v); err != nil {
return nil, err
}
formatted, err := json.Marshal(v)
if err != nil {
return nil, err
}
return formatted, nil
}
func mustReadFile(filename string) []byte {
b, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
return b
}