-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_selection_test.go
94 lines (76 loc) · 1.9 KB
/
parse_selection_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
package gqlyzer
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseSelection(t *testing.T) {
t.Run("without parameter", func(t *testing.T) {
l := Lexer{input: "SomeQuery"}
l.Reset()
l.push('\\')
s, err := l.parseSelection()
assert.NoError(t, err)
assert.Equal(t, "SomeQuery", s.Name)
})
t.Run("with subselection", func(t *testing.T) {
l := Lexer{input: `SomeQuery {
subQuery
}`}
l.Reset()
l.push('\\')
s, err := l.parseSelection()
assert.NoError(t, err)
assert.Equal(t, "SomeQuery", s.Name)
assert.Equal(t, "subQuery", s.InnerSelection["subQuery"].Name)
})
t.Run("with arguments", func(t *testing.T) {
l := Lexer{input: `SomeQuery(id: 123) {
subQuery
}`}
l.Reset()
l.push('\\')
s, err := l.parseSelection()
assert.NoError(t, err)
assert.Equal(t, "SomeQuery", s.Name)
assert.Equal(t, "subQuery", s.InnerSelection["subQuery"].Name)
assert.Equal(t, "id", s.Arguments["id"].Key)
})
}
func TestParseSelectionSet(t *testing.T) {
t.Run("with correct separator", func(t *testing.T) {
l := Lexer{input: `{
query1, query2
query3
}`}
l.Reset()
s, err := l.parseSelectionSet()
assert.NoError(t, err)
assert.Equal(t, "query1", s["query1"].Name)
assert.Equal(t, "query2", s["query2"].Name)
assert.Equal(t, "query3", s["query3"].Name)
})
t.Run("with incorrect separator", func(t *testing.T) {
l := Lexer{input: `{
query1 query2
query3
}`}
l.Reset()
_, err := l.parseSelectionSet()
assert.Error(t, err)
})
t.Run("with nested value", func(t *testing.T) {
l := Lexer{input: `{
query1(id: 123) {
query3
},
query2
}`}
l.Reset()
s, err := l.parseSelectionSet()
assert.NoError(t, err)
assert.Equal(t, "query1", s["query1"].Name)
assert.Equal(t, "query3", s["query1"].InnerSelection["query3"].Name)
assert.Equal(t, "id", s["query1"].Arguments["id"].Key)
assert.Equal(t, "query2", s["query2"].Name)
})
}