File: schema_internal_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 (644 lines) | stat: -rw-r--r-- 16,983 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package schema

import (
	"reflect"
	"testing"
	"text/scanner"

	"github.com/graph-gophers/graphql-go/ast"
	"github.com/graph-gophers/graphql-go/errors"
	"github.com/graph-gophers/graphql-go/internal/common"
)

func TestParseSchemaDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.SchemaDefinition
		err         *errors.QueryError
	}

	tests := []testCase{
		{
			description: "Parses sdl without schema definition",
			definition: `
			type Query {
				hello: String!
			}
			`,
			expected: &ast.SchemaDefinition{Present: false},
		},
		{
			description: "Schema definition present",
			definition: `
			schema {
				query: Query
			}
			type Query{
				hello: String!
			}
			`,
			expected: &ast.SchemaDefinition{Present: true, Loc: errors.Location{Line: 2, Column: 11}},
		},
		{
			description: "Schema definition present and has comment",
			definition: `
			"""
			My cool schema.
			"""
			schema {
				query: Query
			}
			type Query{
				hello: String!
			}
			`,
			expected: &ast.SchemaDefinition{
				Desc:    "My cool schema.",
				Present: true,
				Loc:     errors.Location{Line: 5, Column: 11},
			},
		},
		{
			description: "Schema definition present with comment and directives",
			definition: `
			"""
			My cool schema.
			"""
			schema @dir1(arg1: "Val1", arg2: 5) {
				query: Query
			}
			type Query{
				hello: String!
			}
			`,
			expected: &ast.SchemaDefinition{
				Desc: "My cool schema.",
				Directives: ast.DirectiveList{
					&ast.Directive{
						Arguments: ast.ArgumentList{
							{
								Name: ast.Ident{
									Name: "arg1",
									Loc:  errors.Location{Line: 5, Column: 17},
								},
								Value: &ast.PrimitiveValue{
									Type: scanner.String,
									Text: `"Val1"`,
									Loc:  errors.Location{Line: 5, Column: 23},
								},
							},
							{
								Name: ast.Ident{
									Name: "arg2",
									Loc:  errors.Location{Line: 5, Column: 31},
								},
								Value: &ast.PrimitiveValue{
									Type: scanner.Int,
									Text: "5",
									Loc:  errors.Location{Line: 5, Column: 37},
								},
							},
						},
						Name: ast.Ident{
							Name: "dir1",
							Loc:  errors.Location{Line: 5, Column: 11},
						},
					},
				},
				Loc:     errors.Location{Line: 5, Column: 11},
				Present: true,
			},
		},
		{
			description: "Schema definition present with directives",
			definition: `
			schema @dir3(a: 5) @dir4(b: 1) {
				query: Query
			}
			type Query{
				hello: String!
			}
			`,
			expected: &ast.SchemaDefinition{
				Directives: ast.DirectiveList{
					&ast.Directive{
						Arguments: ast.ArgumentList{
							{
								Name: ast.Ident{
									Name: "a",
									Loc:  errors.Location{Line: 2, Column: 17},
								},
								Value: &ast.PrimitiveValue{
									Type: scanner.Int,
									Text: "5",
									Loc:  errors.Location{Line: 2, Column: 20},
								},
							},
						},
						Name: ast.Ident{
							Name: "dir3",
							Loc:  errors.Location{Line: 2, Column: 11},
						},
					},
					&ast.Directive{
						Arguments: ast.ArgumentList{
							{
								Name: ast.Ident{
									Name: "b",
									Loc:  errors.Location{Line: 2, Column: 29},
								},
								Value: &ast.PrimitiveValue{
									Type: scanner.Int,
									Text: "1",
									Loc:  errors.Location{Line: 2, Column: 32},
								},
							},
						},
						Name: ast.Ident{
							Name: "dir4",
							Loc:  errors.Location{Line: 2, Column: 23},
						},
					},
				},
				Loc:     errors.Location{Line: 2, Column: 11},
				Present: true,
			},
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.SchemaDefinition
			lex := common.NewLexer(test.definition, true)
			parse := func() {
				s := New()
				parseSchema(s, lex)
				actual = &s.SchemaDefinition
			}
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareSchemaDefinitions(t, test.expected, actual)
		})
	}
}

func TestParseInterfaceDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.InterfaceTypeDefinition
		err         *errors.QueryError
	}

	tests := []testCase{{
		description: "Parses simple interface",
		definition:  "Greeting { field: String }",
		expected: &ast.InterfaceTypeDefinition{
			Name:   "Greeting",
			Loc:    errors.Location{Line: 1, Column: 1},
			Fields: ast.FieldsDefinition{&ast.FieldDefinition{Name: "field"}},
		},
	}}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.InterfaceTypeDefinition
			lex := setup(t, test.definition)

			parse := func() { actual = parseInterfaceDef(lex) }
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareInterfaces(t, test.expected, actual)
		})
	}
}

// TestParseObjectDef tests the logic for parsing object types from the schema definition as
// written in `parseObjectDef()`.
func TestParseObjectDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.ObjectTypeDefinition
		err         *errors.QueryError
	}

	tests := []testCase{{
		description: "Parses type inheriting single interface",
		definition:  "Hello implements World { field: String }",
		expected:    &ast.ObjectTypeDefinition{Name: "Hello", Loc: errors.Location{Line: 1, Column: 1}, InterfaceNames: []string{"World"}},
	}, {
		description: "Parses type inheriting multiple interfaces",
		definition:  "Hello implements Wo & rld { field: String }",
		expected:    &ast.ObjectTypeDefinition{Name: "Hello", Loc: errors.Location{Line: 1, Column: 1}, InterfaceNames: []string{"Wo", "rld"}},
	}, {
		description: "Parses type inheriting multiple interfaces with leading ampersand",
		definition:  "Hello implements & Wo & rld { field: String }",
		expected:    &ast.ObjectTypeDefinition{Name: "Hello", Loc: errors.Location{Line: 1, Column: 1}, InterfaceNames: []string{"Wo", "rld"}},
	}, {
		description: "Allows legacy SDL interfaces",
		definition:  "Hello implements Wo, rld { field: String }",
		expected:    &ast.ObjectTypeDefinition{Name: "Hello", Loc: errors.Location{Line: 1, Column: 1}, InterfaceNames: []string{"Wo", "rld"}},
	}}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.ObjectTypeDefinition
			lex := setup(t, test.definition)

			parse := func() { actual = parseObjectDef(lex) }
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareObjects(t, test.expected, actual)
		})
	}
}

func TestParseUnionDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.Union
		err         *errors.QueryError
	}

	tests := []testCase{
		{
			description: "Parses a union",
			definition:  "Foo = Bar | Qux | Quux",
			expected: &ast.Union{
				Name:      "Foo",
				TypeNames: []string{"Bar", "Qux", "Quux"},
				Loc:       errors.Location{Line: 1, Column: 1},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.Union
			lex := setup(t, test.definition)

			parse := func() { actual = parseUnionDef(lex) }
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareUnions(t, test.expected, actual)
		})
	}
}

func TestParseEnumDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.EnumTypeDefinition
		err         *errors.QueryError
	}

	tests := []testCase{
		{
			description: "parses EnumTypeDefinition on single line",
			definition:  "Foo { BAR QUX }",
			expected: &ast.EnumTypeDefinition{
				Name: "Foo",
				EnumValuesDefinition: []*ast.EnumValueDefinition{
					{
						EnumValue: "BAR",
						Loc:       errors.Location{Line: 1, Column: 7},
					},
					{
						EnumValue: "QUX",
						Loc:       errors.Location{Line: 1, Column: 11},
					},
				},
				Loc: errors.Location{Line: 1, Column: 1},
			},
		},
		{
			description: "parses EnumtypeDefinition with new lines",
			definition: `Foo { 
				BAR
				QUX
			}`,
			expected: &ast.EnumTypeDefinition{
				Name: "Foo",
				EnumValuesDefinition: []*ast.EnumValueDefinition{
					{
						EnumValue: "BAR",
						Loc:       errors.Location{Line: 2, Column: 5},
					},
					{
						EnumValue: "QUX",
						Loc:       errors.Location{Line: 3, Column: 5},
					},
				},
				Loc: errors.Location{Line: 1, Column: 1},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.EnumTypeDefinition
			lex := setup(t, test.definition)

			parse := func() { actual = parseEnumDef(lex) }
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareEnumTypeDefs(t, test.expected, actual)
		})
	}
}

func TestParseDirectiveDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.DirectiveDefinition
		err         *errors.QueryError
	}

	tests := []*testCase{
		{
			description: "parses DirectiveDefinition",
			definition:  "@Foo on FIELD",
			expected: &ast.DirectiveDefinition{
				Name:      "Foo",
				Loc:       errors.Location{Line: 1, Column: 2},
				Locations: []string{"FIELD"},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.DirectiveDefinition
			lex := setup(t, test.definition)

			parse := func() { actual = parseDirectiveDef(lex) }
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareDirectiveDefinitions(t, test.expected, actual)
		})
	}
}

func TestParseInputDef(t *testing.T) {
	type testCase struct {
		description string
		definition  string
		expected    *ast.InputObject
		err         *errors.QueryError
	}

	tests := []testCase{
		{
			description: "parses an input object type definition",
			definition:  "Foo { qux: String }",
			expected: &ast.InputObject{
				Name:   "Foo",
				Values: nil,
				Loc:    errors.Location{Line: 1, Column: 1},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
			var actual *ast.InputObject
			lex := setup(t, test.definition)

			parse := func() { actual = parseInputDef(lex) }
			err := lex.CatchSyntaxError(parse)

			compareErrors(t, test.err, err)
			compareInputObjectTypeDefinition(t, test.expected, actual)
		})
	}
}

func compareDirectiveDefinitions(t *testing.T, expected *ast.DirectiveDefinition, actual *ast.DirectiveDefinition) {
	t.Helper()

	if expected.Name != actual.Name {
		t.Fatalf("wrong DirectiveDefinition name: want %q, got %q", expected.Name, actual.Name)
	}

	if !reflect.DeepEqual(expected.Locations, actual.Locations) {
		t.Errorf("wrong DirectiveDefinition locations: want %v, got %v", expected.Locations, actual.Locations)
	}

	compareLoc(t, "DirectiveDefinition", expected.Loc, actual.Loc)
}

func compareInputObjectTypeDefinition(t *testing.T, expected, actual *ast.InputObject) {
	t.Helper()

	if expected.Name != actual.Name {
		t.Fatalf("wrong InputObject name: want %q, got %q", expected.Name, actual.Name)
	}

	compareLoc(t, "InputObjectTypeDefinition", expected.Loc, actual.Loc)
}

func compareEnumTypeDefs(t *testing.T, expected, actual *ast.EnumTypeDefinition) {
	t.Helper()

	if expected.Name != actual.Name {
		t.Fatalf("wrong EnumTypeDefinition name: want %q, got %q", expected.Name, actual.Name)
	}

	compareLoc(t, "EnumValueDefinition", expected.Loc, actual.Loc)

	for i, definition := range expected.EnumValuesDefinition {
		expectedValue, expectedLoc := definition.EnumValue, definition.Loc
		actualDef := actual.EnumValuesDefinition[i]

		if expectedValue != actualDef.EnumValue {
			t.Fatalf("wrong EnumValue: want %q, got %q", expectedValue, actualDef.EnumValue)
		}

		compareLoc(t, "EnumValue "+expectedValue, expectedLoc, actualDef.Loc)
	}
}

func compareLoc(t *testing.T, typeName string, expected, actual errors.Location) {
	t.Helper()
	if expected != actual {
		t.Errorf("wrong location on %s: want %v, got %v", typeName, expected, actual)
	}
}

func compareErrors(t *testing.T, expected, actual *errors.QueryError) {
	t.Helper()

	switch {
	case expected != nil && actual != nil:
		if expected.Message != actual.Message {
			t.Fatalf("wanted error message %q, got %q", expected.Message, actual.Message)
		}
		// TODO: Check error locations are as expected.

	case expected != nil && actual == nil:
		t.Fatalf("missing expected error: %q", expected)

	case expected == nil && actual != nil:
		t.Fatalf("got unexpected error: %q", actual)
	}
}

func compareInterfaces(t *testing.T, expected, actual *ast.InterfaceTypeDefinition) {
	t.Helper()

	if expected.Name != actual.Name {
		t.Errorf("wrong interface name: want %q, got %q", expected.Name, actual.Name)
	}

	compareLoc(t, "InterfaceTypeDefinition", expected.Loc, actual.Loc)

	if len(expected.Fields) != len(actual.Fields) {
		t.Fatalf("wanted %d field definitions, got %d", len(expected.Fields), len(actual.Fields))
	}

	for i, f := range expected.Fields {
		if f.Name != actual.Fields[i].Name {
			t.Errorf("fields[%d]: wrong field name: want %q, got %q", i, f.Name, actual.Fields[i].Name)
		}
	}
}

func compareUnions(t *testing.T, expected, actual *ast.Union) {
	t.Helper()

	if expected.Name != actual.Name {
		t.Errorf("wrong object name: want %q, got %q", expected.Name, actual.Name)
	}

	if !reflect.DeepEqual(expected, actual) {
		t.Errorf("wrong type names: want %v, got %v", expected.TypeNames, actual.TypeNames)
	}
}

func compareObjects(t *testing.T, expected, actual *ast.ObjectTypeDefinition) {
	t.Helper()

	if expected.Name != actual.Name {
		t.Errorf("wrong object name: want %q, got %q", expected.Name, actual.Name)
	}

	if len(expected.InterfaceNames) != len(actual.InterfaceNames) {
		t.Fatalf(
			"wrong number of interface names: want %s, got %s",
			expected.InterfaceNames,
			actual.InterfaceNames,
		)
	}

	for i, expectedName := range expected.InterfaceNames {
		actualName := actual.InterfaceNames[i]
		if expectedName != actualName {
			t.Errorf("wrong interface name: want %q, got %q", expectedName, actualName)
		}
	}
}

func compareSchemaDefinitions(t *testing.T, expected, actual *ast.SchemaDefinition) {
	t.Helper()

	if expected.Present != actual.Present {
		t.Errorf("wrong boolean Present: want %v, got %v", expected.Present, actual.Present)
	}

	if expected.Desc != actual.Desc {
		t.Errorf("wrong schema Desc: want %q, got %q", expected.Desc, actual.Desc)
	}

	if len(expected.RootOperationTypes) != len(actual.RootOperationTypes) {
		t.Fatalf(
			"wrong number of root operations: want %d, got %d",
			len(expected.RootOperationTypes),
			len(actual.RootOperationTypes),
		)
	}

	for name, expectedOp := range expected.RootOperationTypes {
		actualOp := actual.RootOperationTypes[name]
		if actualOp != expectedOp {
			t.Errorf("wrong root operation name: want %q, got %q", actualOp, expectedOp)
		}
	}

	compareDirectiveList(t, "SchemaDef", expected.Directives, actual.Directives)

	compareLoc(t, "SchemaDef ", expected.Loc, actual.Loc)
}

func compareDirectiveList(t *testing.T, target string, expectedList, actualList ast.DirectiveList) {
	if len(expectedList) != len(actualList) {
		t.Fatalf(
			"wrong number of schema directives on %s: want %d, got %d",
			target,
			len(expectedList),
			len(actualList),
		)
	}

	for i, expected := range expectedList {
		actual := actualList[i]
		if !reflect.DeepEqual(expectedList, actualList) {
			if expected.Name.Name != actual.Name.Name {
				t.Errorf("wrong directive name: want %q, got %q", expected.Name.Name, actual.Name.Name)
			}

			target := "directive " + expected.Name.Name + " on SchemaDefinition"
			compareLoc(t, target, expected.Name.Loc, actual.Name.Loc)
			compareArgumentList(t, target, expected.Arguments, actual.Arguments)
		}
	}
}

func compareArgumentList(t *testing.T, target string, expectedList, actualList ast.ArgumentList) {
	if len(expectedList) != len(actualList) {
		t.Fatalf(
			"wrong number of arguments on %s: want %d, got %d",
			target,
			len(expectedList),
			len(actualList),
		)
	}

	for i, expected := range expectedList {
		actual := actualList[i]
		if !reflect.DeepEqual(expectedList, actualList) {
			if expected.Name.Name != actual.Name.Name {
				t.Errorf("wrong argument name on %s: want %q, got %q", target, expected.Name.Name, actual.Name.Name)
			}

			if expected.Value.String() != actual.Value.String() {
				t.Errorf("wrong argument value on %s: want %q, got %q", target, expected.Value, actual.Value)
			}

			compareDirectiveList(t, "argument "+expected.Name.Name+" on "+target, expected.Directives, actual.Directives)

			compareLoc(t, "argument "+expected.Name.Name+" on "+target, expected.Name.Loc, actual.Name.Loc)

			compareLoc(t, "value on argument "+expected.Name.Name+" on "+target, expected.Value.Location(), actual.Value.Location())
		}
	}
}

func setup(t *testing.T, def string) *common.Lexer {
	t.Helper()

	lex := common.NewLexer(def, false)
	lex.ConsumeWhitespace()

	return lex
}