forked from Khan/genqlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
85 lines (70 loc) · 2.23 KB
/
parse_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
package generate
import (
"path/filepath"
"sort"
"testing"
"github.com/vektah/gqlparser/v2/ast"
)
var (
parseDataDir = "testdata/parsing"
parseErrorsDir = "testdata/parsing-errors"
)
func sortQueries(queryDoc *ast.QueryDocument) {
sort.Slice(queryDoc.Operations, func(i, j int) bool {
return queryDoc.Operations[i].Name < queryDoc.Operations[j].Name
})
sort.Slice(queryDoc.Fragments, func(i, j int) bool {
return queryDoc.Fragments[i].Name < queryDoc.Fragments[j].Name
})
}
func getTestQueries(t *testing.T, ext string) *ast.QueryDocument {
graphqlQueries, err := getQueries(
parseDataDir, []string{filepath.Join(parseDataDir, "*."+ext)})
if err != nil {
t.Fatal(err)
}
// The different file-types may have the operations/fragments in a
// different order.
sortQueries(graphqlQueries)
return graphqlQueries
}
// TestParse tests that query-extraction from different language source files
// produces equivalent results. We do not test the results it produces (that's
// covered by TestGenerate), just that they are equivalent in different
// languages (since TestGenerate only uses .graphql as input).
// TODO: redo this as more standard snapshot tests?
func TestParse(t *testing.T) {
extensions := []string{"go"}
graphqlQueries := getTestQueries(t, "graphql")
// check it's at least non-empty
if len(graphqlQueries.Operations) == 0 || len(graphqlQueries.Fragments) == 0 {
t.Fatalf("Didn't find any queries in *.graphql files")
}
sortQueries(graphqlQueries)
for _, ext := range extensions {
t.Run(ext, func(t *testing.T) {
queries := getTestQueries(t, ext)
got, want := ast.Dump(graphqlQueries), ast.Dump(queries)
if got != want {
// TODO: nice diffing
t.Errorf("got:\n%v\nwant:\n%v\n", got, want)
}
})
}
}
// TestParseErrors tests that query-extraction from different language source files
// produces appropriate errors if your query is invalid.
func TestParseErrors(t *testing.T) {
extensions := []string{"graphql", "go"}
for _, ext := range extensions {
t.Run(ext, func(t *testing.T) {
g, err := getQueries(
parseErrorsDir,
[]string{filepath.Join(parseErrorsDir, "*."+ext)})
if err == nil {
t.Errorf("expected error from getQueries(*.%v)", ext)
t.Logf("%#v", g)
}
})
}
}