forked from sogko/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
465 lines (446 loc) · 11.9 KB
/
testutil.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package testutil
import (
"encoding/json"
"reflect"
"strconv"
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/parser"
"github.com/kr/pretty"
)
var (
Luke StarWarsChar
Vader StarWarsChar
Han StarWarsChar
Leia StarWarsChar
Tarkin StarWarsChar
Threepio StarWarsChar
Artoo StarWarsChar
HumanData map[int]StarWarsChar
DroidData map[int]StarWarsChar
StarWarsSchema graphql.Schema
humanType *graphql.Object
droidType *graphql.Object
)
type StarWarsChar struct {
Id string
Name string
Friends []StarWarsChar
AppearsIn []int
HomePlanet string
PrimaryFunction string
}
func init() {
Luke = StarWarsChar{
Id: "1000",
Name: "Luke Skywalker",
AppearsIn: []int{4, 5, 6},
HomePlanet: "Tatooine",
}
Vader = StarWarsChar{
Id: "1001",
Name: "Darth Vader",
AppearsIn: []int{4, 5, 6},
HomePlanet: "Tatooine",
}
Han = StarWarsChar{
Id: "1002",
Name: "Han Solo",
AppearsIn: []int{4, 5, 6},
}
Leia = StarWarsChar{
Id: "1003",
Name: "Leia Organa",
AppearsIn: []int{4, 5, 6},
HomePlanet: "Alderaa",
}
Tarkin = StarWarsChar{
Id: "1004",
Name: "Wilhuff Tarkin",
AppearsIn: []int{4},
}
Threepio = StarWarsChar{
Id: "2000",
Name: "C-3PO",
AppearsIn: []int{4, 5, 6},
PrimaryFunction: "Protocol",
}
Artoo = StarWarsChar{
Id: "2001",
Name: "R2-D2",
AppearsIn: []int{4, 5, 6},
PrimaryFunction: "Astromech",
}
Luke.Friends = append(Luke.Friends, []StarWarsChar{Han, Leia, Threepio, Artoo}...)
Vader.Friends = append(Luke.Friends, []StarWarsChar{Tarkin}...)
Han.Friends = append(Han.Friends, []StarWarsChar{Luke, Leia, Artoo}...)
Leia.Friends = append(Leia.Friends, []StarWarsChar{Luke, Han, Threepio, Artoo}...)
Tarkin.Friends = append(Tarkin.Friends, []StarWarsChar{Vader}...)
Threepio.Friends = append(Threepio.Friends, []StarWarsChar{Luke, Han, Leia, Artoo}...)
Artoo.Friends = append(Artoo.Friends, []StarWarsChar{Luke, Han, Leia}...)
HumanData = map[int]StarWarsChar{
1000: Luke,
1001: Vader,
1002: Han,
1003: Leia,
1004: Tarkin,
}
DroidData = map[int]StarWarsChar{
2000: Threepio,
2001: Artoo,
}
episodeEnum := graphql.NewEnum(graphql.EnumConfig{
Name: "Episode",
Description: "One of the films in the Star Wars Trilogy",
Values: graphql.EnumValueConfigMap{
"NEWHOPE": &graphql.EnumValueConfig{
Value: 4,
Description: "Released in 1977.",
},
"EMPIRE": &graphql.EnumValueConfig{
Value: 5,
Description: "Released in 1980.",
},
"JEDI": &graphql.EnumValueConfig{
Value: 6,
Description: "Released in 1983.",
},
},
})
characterInterface := graphql.NewInterface(graphql.InterfaceConfig{
Name: "Character",
Description: "A character in the Star Wars Trilogy",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The id of the character.",
},
"name": &graphql.Field{
Type: graphql.String,
Description: "The name of the character.",
},
"appearsIn": &graphql.Field{
Type: graphql.NewList(episodeEnum),
Description: "Which movies they appear in.",
},
},
ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
if character, ok := value.(StarWarsChar); ok {
id, _ := strconv.Atoi(character.Id)
human := GetHuman(id)
if human.Id != "" {
return humanType
}
}
return droidType
},
})
characterInterface.AddFieldConfig("friends", &graphql.Field{
Type: graphql.NewList(characterInterface),
Description: "The friends of the character, or an empty list if they have none.",
})
humanType = graphql.NewObject(graphql.ObjectConfig{
Name: "Human",
Description: "A humanoid creature in the Star Wars universe.",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The id of the human.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if human, ok := p.Source.(StarWarsChar); ok {
return human.Id, nil
}
return nil, nil
},
},
"name": &graphql.Field{
Type: graphql.String,
Description: "The name of the human.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if human, ok := p.Source.(StarWarsChar); ok {
return human.Name, nil
}
return nil, nil
},
},
"friends": &graphql.Field{
Type: graphql.NewList(characterInterface),
Description: "The friends of the human, or an empty list if they have none.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if human, ok := p.Source.(StarWarsChar); ok {
return human.Friends, nil
}
return []interface{}{}, nil
},
},
"appearsIn": &graphql.Field{
Type: graphql.NewList(episodeEnum),
Description: "Which movies they appear in.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if human, ok := p.Source.(StarWarsChar); ok {
return human.AppearsIn, nil
}
return nil, nil
},
},
"homePlanet": &graphql.Field{
Type: graphql.String,
Description: "The home planet of the human, or null if unknown.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if human, ok := p.Source.(StarWarsChar); ok {
return human.HomePlanet, nil
}
return nil, nil
},
},
},
Interfaces: []*graphql.Interface{
characterInterface,
},
})
droidType = graphql.NewObject(graphql.ObjectConfig{
Name: "Droid",
Description: "A mechanical creature in the Star Wars universe.",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Description: "The id of the droid.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if droid, ok := p.Source.(StarWarsChar); ok {
return droid.Id, nil
}
return nil, nil
},
},
"name": &graphql.Field{
Type: graphql.String,
Description: "The name of the droid.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if droid, ok := p.Source.(StarWarsChar); ok {
return droid.Name, nil
}
return nil, nil
},
},
"friends": &graphql.Field{
Type: graphql.NewList(characterInterface),
Description: "The friends of the droid, or an empty list if they have none.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if droid, ok := p.Source.(StarWarsChar); ok {
friends := []map[string]interface{}{}
for _, friend := range droid.Friends {
friends = append(friends, map[string]interface{}{
"name": friend.Name,
"id": friend.Id,
})
}
return droid.Friends, nil
}
return []interface{}{}, nil
},
},
"appearsIn": &graphql.Field{
Type: graphql.NewList(episodeEnum),
Description: "Which movies they appear in.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if droid, ok := p.Source.(StarWarsChar); ok {
return droid.AppearsIn, nil
}
return nil, nil
},
},
"primaryFunction": &graphql.Field{
Type: graphql.String,
Description: "The primary function of the droid.",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if droid, ok := p.Source.(StarWarsChar); ok {
return droid.PrimaryFunction, nil
}
return nil, nil
},
},
},
Interfaces: []*graphql.Interface{
characterInterface,
},
})
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"hero": &graphql.Field{
Type: characterInterface,
Args: graphql.FieldConfigArgument{
"episode": &graphql.ArgumentConfig{
Description: "If omitted, returns the hero of the whole saga. If " +
"provided, returns the hero of that particular episode.",
Type: episodeEnum,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return GetHero(p.Args["episode"]), nil
},
},
"human": &graphql.Field{
Type: humanType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "id of the human",
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return GetHuman(p.Args["id"].(int)), nil
},
},
"droid": &graphql.Field{
Type: droidType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "id of the droid",
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return GetDroid(p.Args["id"].(int)), nil
},
},
},
})
StarWarsSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
}
func GetHuman(id int) StarWarsChar {
if human, ok := HumanData[id]; ok {
return human
}
return StarWarsChar{}
}
func GetDroid(id int) StarWarsChar {
if droid, ok := DroidData[id]; ok {
return droid
}
return StarWarsChar{}
}
func GetHero(episode interface{}) interface{} {
if episode == 5 {
return Luke
}
return Artoo
}
// Test helper functions
func TestParse(t *testing.T, query string) *ast.Document {
astDoc, err := parser.Parse(parser.ParseParams{
Source: query,
Options: parser.ParseOptions{
// include source, for error reporting
NoSource: false,
},
})
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
return astDoc
}
func TestExecute(t *testing.T, ep graphql.ExecuteParams) *graphql.Result {
return graphql.Execute(ep)
}
func Diff(a, b interface{}) []string {
return pretty.Diff(a, b)
}
func ASTToJSON(t *testing.T, a ast.Node) interface{} {
b, err := json.Marshal(a)
if err != nil {
t.Fatalf("Failed to marshal Node %v", err)
}
var f interface{}
err = json.Unmarshal(b, &f)
if err != nil {
t.Fatalf("Failed to unmarshal Node %v", err)
}
return f
}
func ContainSubsetSlice(super []interface{}, sub []interface{}) bool {
if len(sub) == 0 {
return true
}
subLoop:
for _, subVal := range sub {
found := false
innerLoop:
for _, superVal := range super {
if subVal, ok := subVal.(map[string]interface{}); ok {
if superVal, ok := superVal.(map[string]interface{}); ok {
if ContainSubset(superVal, subVal) {
found = true
break innerLoop
} else {
continue
}
} else {
return false
}
}
if subVal, ok := subVal.([]interface{}); ok {
if superVal, ok := superVal.([]interface{}); ok {
if ContainSubsetSlice(superVal, subVal) {
found = true
break innerLoop
} else {
continue
}
} else {
return false
}
}
if reflect.DeepEqual(superVal, subVal) {
found = true
break innerLoop
}
}
if !found {
return false
} else {
continue subLoop
}
}
return true
}
func ContainSubset(super map[string]interface{}, sub map[string]interface{}) bool {
if len(sub) == 0 {
return true
}
for subKey, subVal := range sub {
if superVal, ok := super[subKey]; ok {
switch superVal := superVal.(type) {
case []interface{}:
if subVal, ok := subVal.([]interface{}); ok {
if !ContainSubsetSlice(superVal, subVal) {
return false
}
} else {
return false
}
case map[string]interface{}:
if subVal, ok := subVal.(map[string]interface{}); ok {
if !ContainSubset(superVal, subVal) {
return false
}
} else {
return false
}
default:
if !reflect.DeepEqual(superVal, subVal) {
return false
}
}
} else {
return false
}
}
return true
}
func EqualErrorMessage(expected, result *graphql.Result, i int) bool {
return expected.Errors[i].Message == result.Errors[i].Message
}