-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgqt_test.go
358 lines (328 loc) · 9.14 KB
/
gqt_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package gqt_test
import (
"bytes"
"embed"
"fmt"
"io/fs"
"path/filepath"
"strings"
"testing"
"github.com/graph-guard/gqt/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v3"
)
//go:embed tests
var testsFS embed.FS
func TestParse(t *testing.T) {
type T struct {
Schema string `yaml:"schema"`
Template string `yaml:"template"`
ExpectAST map[string]any `yaml:"expect-ast"`
ExpectASTSchemaless map[string]any `yaml:"expect-ast(schemaless)"`
ExpectErrors []string `yaml:"expect-errors"`
ExpectErrorsSchemaless []string `yaml:"expect-errors(schemaless)"`
}
d, err := fs.ReadDir(testsFS, "tests")
require.NoError(t, err)
for _, do := range d {
fileName := do.Name()
if do.IsDir() {
t.Run(fileName, func(t *testing.T) {
t.Skipf("ignoring directory %q", fileName)
})
continue
}
if !strings.HasSuffix(fileName, ".yml") {
t.Run(fileName, func(t *testing.T) {
t.Skipf("ignoring file %q", fileName)
})
continue
}
f, err := testsFS.ReadFile(filepath.Join("tests", fileName))
require.NoError(t, err, "reading YAML test file")
t.Run(strings.TrimSuffix(fileName, ".yml"), func(t *testing.T) {
var ts T
{
d := yaml.NewDecoder(bytes.NewReader(f))
d.KnownFields(true)
if err := d.Decode(&ts); err != nil {
t.Fatal("parsing YAML test definition", err)
}
}
if ts.ExpectAST != nil && ts.ExpectErrors != nil {
t.Fatal("expecting both AST and errors in schema-aware mode")
} else if ts.ExpectASTSchemaless != nil &&
ts.ExpectErrorsSchemaless != nil {
t.Fatal("expecting both AST and errors in schema-less mode")
}
t.Run("schema", func(t *testing.T) {
p, err := gqt.NewParser([]gqt.Source{
{Name: "schema.graphqls", Content: ts.Schema},
})
require.NoError(t, err, "unexpected error while parsing schema")
opr, vars, errs := p.Parse([]byte(ts.Template))
compareErrors(t, ts.ExpectErrors, errs)
if len(ts.ExpectErrors) > 0 {
// Expect failure
require.Zero(t, vars)
require.Zero(t, opr)
} else {
// Expect success
var j bytes.Buffer
d := yaml.NewEncoder(&j)
d.SetIndent(2)
err := d.Encode(opr)
require.NoError(t, err)
var decoded map[string]any
require.NoError(t, yaml.Unmarshal(j.Bytes(), &decoded))
if !assert.ObjectsAreEqual(ts.ExpectAST, decoded) {
fmt.Println("actual:")
fmt.Println(j.String())
}
require.Equal(t, ts.ExpectAST, decoded)
}
})
t.Run("schemaless", func(t *testing.T) {
opr, vars, errs := gqt.Parse([]byte(ts.Template))
compareErrors(t, ts.ExpectErrorsSchemaless, errs)
if len(ts.ExpectErrorsSchemaless) > 0 {
// Expect failure
require.Zero(t, vars)
require.Zero(t, opr)
} else {
// Expect success
var j bytes.Buffer
d := yaml.NewEncoder(&j)
d.SetIndent(2)
err := d.Encode(opr)
require.NoError(t, err)
var decoded map[string]any
require.NoError(t, yaml.Unmarshal(j.Bytes(), &decoded))
if !assert.ObjectsAreEqual(ts.ExpectASTSchemaless, decoded) {
fmt.Println("actual(schemaless):")
fmt.Println(j.String())
}
require.Equal(t, ts.ExpectASTSchemaless, decoded)
}
})
})
}
}
//go:embed tests_optimize
var testsOptimizeFS embed.FS
func TestOptimize(t *testing.T) {
type T struct {
Schema string `yaml:"schema"`
Template string `yaml:"template"`
ExpectAST map[string]any `yaml:"expect-ast"`
}
d, err := fs.ReadDir(testsOptimizeFS, "tests_optimize")
require.NoError(t, err)
for _, do := range d {
fileName := do.Name()
if do.IsDir() {
t.Run(fileName, func(t *testing.T) {
t.Skipf("ignoring directory %q", fileName)
})
continue
}
if !strings.HasSuffix(fileName, ".yml") {
t.Run(fileName, func(t *testing.T) {
t.Skipf("ignoring file %q", fileName)
})
continue
}
f, err := testsOptimizeFS.ReadFile(
filepath.Join("tests_optimize", fileName),
)
require.NoError(t, err, "reading YAML test file")
t.Run(strings.TrimSuffix(fileName, ".yml"), func(t *testing.T) {
var ts T
{
d := yaml.NewDecoder(bytes.NewReader(f))
d.KnownFields(true)
if err := d.Decode(&ts); err != nil {
t.Fatal("parsing YAML test definition", err)
}
}
p, err := gqt.NewParser([]gqt.Source{
{Name: "schema.graphqls", Content: ts.Schema},
})
require.NoError(t, err, "unexpected error while parsing schema")
opr, _, errs := p.Parse([]byte(ts.Template))
if compareErrors(t, nil, errs); len(errs) > 0 {
return
}
opt := gqt.Optimize(opr)
var j bytes.Buffer
d := yaml.NewEncoder(&j)
d.SetIndent(2)
err = d.Encode(opt)
require.NoError(t, err)
var decoded map[string]any
require.NoError(t, yaml.Unmarshal(j.Bytes(), &decoded))
if !assert.ObjectsAreEqual(ts.ExpectAST, decoded) {
fmt.Println("actual:")
fmt.Println(j.String())
}
require.Equal(t, ts.ExpectAST, decoded)
})
}
}
func compareErrors(t *testing.T, expected []string, actual []gqt.Error) {
if len(expected) < 1 {
for _, act := range actual {
t.Errorf("unexpected error: %v", act)
}
return
}
for i, e := range expected {
if i >= len(actual) {
t.Errorf("missing error: %v", e)
continue
}
assert.Equal(t, e, actual[i].Error(), "at index %d", i)
}
if d := len(actual) - len(expected); d > 0 {
for _, act := range actual[d:] {
t.Errorf("unexpected error: %v", act)
}
}
}
func TestParseVariables(t *testing.T) {
input := `query {
f1(a: $b+$x, c=$c: $b) {
f2(b=$b:*, x=$unused: $c+$b, d: {o1=$x:*})
}
}`
opr, vars, errs := gqt.Parse([]byte(input))
require.Len(t, errs, 0, "unexpected errors: %v", errs)
require.NotNil(t, opr)
require.Len(t, vars, 4)
require.Contains(t, vars, "b")
require.Contains(t, vars, "c")
require.Contains(t, vars, "x")
require.Contains(t, vars, "unused")
// Check names
require.Equal(t, "b", vars["b"].Name)
require.Equal(t, "c", vars["c"].Name)
require.Equal(t, "x", vars["x"].Name)
require.Equal(t, "unused", vars["unused"].Name)
// Check parents
require.IsType(t, &gqt.Argument{}, vars["b"].Parent)
require.IsType(t, &gqt.Argument{}, vars["c"].Parent)
require.IsType(t, &gqt.Argument{}, vars["unused"].Parent)
require.IsType(t, &gqt.ObjectField{}, vars["x"].Parent)
require.Equal(t, "b", vars["b"].Parent.(*gqt.Argument).Name.Name)
require.Equal(t, "c", vars["c"].Parent.(*gqt.Argument).Name.Name)
require.Equal(t, "x", vars["unused"].Parent.(*gqt.Argument).Name.Name)
require.Equal(t, "o1", vars["x"].Parent.(*gqt.ObjectField).Name.Name)
// Check references
require.Len(t, vars["b"].References, 3)
require.Len(t, vars["c"].References, 1)
require.Len(t, vars["unused"].References, 0)
require.Len(t, vars["x"].References, 1)
require.Equal(
t, gqt.Location{Index: 16, Line: 2, Column: 9},
vars["b"].References[0].(*gqt.Variable).Location,
)
require.Equal(
t, gqt.Location{Index: 29, Line: 2, Column: 22},
vars["b"].References[1].(*gqt.Variable).Location,
)
require.Equal(
t, gqt.Location{Index: 63, Line: 3, Column: 29},
vars["b"].References[2].(*gqt.Variable).Location,
)
require.Equal(
t, gqt.Location{Index: 60, Line: 3, Column: 26},
vars["c"].References[0].(*gqt.Variable).Location,
)
require.Equal(
t, gqt.Location{Index: 19, Line: 2, Column: 12},
vars["x"].References[0].(*gqt.Variable).Location,
)
}
func TestParseNoSchema(t *testing.T) {
p, err := gqt.NewParser([]gqt.Source{})
require.NoError(t, err)
require.NotNil(t, p)
}
func TestParseErrEmpty(t *testing.T) {
opr, vars, errs := gqt.Parse([]byte(""))
require.Equal(t, []gqt.Error{
{
LocRange: gqt.LocRange{
Location: gqt.Location{
Index: 0, Line: 1, Column: 1,
},
},
Msg: "unexpected end of file, expected " +
"query, mutation, or subscription operation definition",
},
}, errs)
require.Nil(t, opr)
require.Zero(t, vars)
}
func TestParseErrSchema(t *testing.T) {
p, err := gqt.NewParser([]gqt.Source{
{
Name: "schema.graphqls",
Content: "type Query {}",
},
})
require.Error(t, err)
require.Equal(t,
"schema.graphqls:1: "+
"expected at least one definition, found }",
err.Error(),
)
require.Nil(t, p)
}
func TestErrorString(t *testing.T) {
e := gqt.Error{}
require.False(t, e.IsErr())
require.Zero(t, e.Error())
e.LocRange = gqt.LocRange{
Location: gqt.Location{0, 1, 1},
LocationEnd: gqt.LocationEnd{3, 1, 4},
}
e.Msg = "some error"
require.True(t, e.IsErr())
require.Equal(t, "1:1: some error", e.Error())
}
func TestNumber(t *testing.T) {
opr, _, errs := gqt.Parse([]byte("query { f(i:42, f:3.14) }"))
require.Nil(t, errs)
{
// i:42
ni := opr.Selections[0].(*gqt.SelectionField).Arguments[0].
Constraint.(*gqt.ConstrEquals).Value.(*gqt.Number)
{
i, ok := ni.Int()
require.Equal(t, 42, i)
require.True(t, ok)
}
{
f, ok := ni.Float()
require.Zero(t, f)
require.False(t, ok)
}
}
{
// f:3.14
ni := opr.Selections[0].(*gqt.SelectionField).Arguments[1].
Constraint.(*gqt.ConstrEquals).Value.(*gqt.Number)
{
i, ok := ni.Int()
require.Zero(t, i)
require.False(t, ok)
}
{
f, ok := ni.Float()
require.Equal(t, 3.14, f)
require.True(t, ok)
}
}
}