forked from shuttl-io/go-graphql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryPart_test.go
142 lines (131 loc) · 3.54 KB
/
queryPart_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
package graphql
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCanConvertBasicQueryPartToString(t *testing.T) {
assert := assert.New(t)
qp := NewQueryPart("some_string")
assert.Equal("some_string\n", qp.String())
}
func TestCanMakeSubfieldsRight(t *testing.T) {
assert := assert.New(t)
qp := NewQueryPart("some_string")
qp.SubFields = append(qp.SubFields,
NewQueryPart("some_field_one"),
NewQueryPart("some_field_two"),
NewQueryPart("some_field_three"),
)
assert.Equal(`some_string{
some_field_one
some_field_two
some_field_three
}
`, qp.String())
qp2 := NewQueryPart("some_field_three")
qp2.SubFields = append(qp2.SubFields,
NewQueryPart("sub_sub_one"),
NewQueryPart("sub_sub_two"),
NewQueryPart("sub_sub_three"),
)
qp = NewQueryPart("some_string")
qp.SubFields = append(qp.SubFields,
NewQueryPart("some_field_one"),
NewQueryPart("some_field_two"),
qp2,
)
assert.Equal(`some_string{
some_field_one
some_field_two
some_field_three{
sub_sub_one
sub_sub_two
sub_sub_three
}
}
`, qp.String())
}
func TestCanStringifyWithArgs(t *testing.T) {
assert := assert.New(t)
qp := NewQueryPart("some_string")
qp.markArgAsNeeded("some_arg")
qp.markArgAsNeeded("some_arg2")
qp.requiredArgs = []string{"some_arg", "some_arg2"}
assert.Equal("some_string(some_arg:$some_arg, some_arg2:$some_arg2)\n", qp.String())
}
func TestCanStringifySubfieldsWithArgs(t *testing.T) {
assert := assert.New(t)
qp := NewQueryPart("some_string")
qp.Arguments["some_arg"] = ""
qp.Arguments["some_arg2"] = ""
qp.markArgAsNeeded("some_arg")
qp.markArgAsNeeded("some_arg2")
qp.SubFields = append(qp.SubFields,
NewQueryPart("some_field_one"),
NewQueryPart("some_field_two"),
NewQueryPart("some_field_three"),
)
assert.Equal(`some_string(some_arg:$some_arg, some_arg2:$some_arg2){
some_field_one
some_field_two
some_field_three
}
`, qp.String())
}
func TestCanCollectRequiredArgs(t *testing.T) {
assert := assert.New(t)
qp2 := NewQueryPart("some_field_three")
qp2.SubFields = append(qp2.SubFields,
NewQueryPart("sub_sub_one"),
NewQueryPart("sub_sub_two"),
NewQueryPart("sub_sub_three"),
)
qp := NewQueryPart("some_string")
qp.Arguments["some_args"] = "String"
qp.Arguments["some_arg2"] = "String"
qp2.Arguments["some_int"] = "Int!"
qp2.Arguments["some_bool"] = "Bool"
qp.SubFields = append(qp.SubFields,
NewQueryPart("some_field_one"),
NewQueryPart("some_field_two"),
qp2,
)
qp.markArgAsNeeded("some_args")
qp.markArgAsNeeded("some_int")
qp.markArgAsNeeded("some_bool")
args := qp.collectArgs()
assert.Len(args, 3)
assert.Equal([]string{"$some_args:String", "$some_int:Int!", "$some_bool:Bool"}, args)
}
func TestAllTogetherNow(t *testing.T) {
assert := assert.New(t)
qp2 := NewQueryPart("some_field_three")
qp2.SubFields = append(qp2.SubFields,
NewQueryPart("sub_sub_one"),
NewQueryPart("sub_sub_two"),
NewQueryPart("sub_sub_three"),
)
qp := NewQueryPart("some_string")
qp.Arguments["some_args"] = "String"
qp.Arguments["some_arg2"] = "String"
qp2.Arguments["some_int"] = "Int!"
qp2.Arguments["some_bool"] = "Bool"
qp.SubFields = append(qp.SubFields,
NewQueryPart("some_field_one"),
NewQueryPart("some_field_two"),
qp2,
)
qp.markArgAsNeeded("some_args")
qp.markArgAsNeeded("some_int")
qp.markArgAsNeeded("some_bool")
assert.Equal(`some_string(some_args:$some_args){
some_field_one
some_field_two
some_field_three(some_int:$some_int, some_bool:$some_bool){
sub_sub_one
sub_sub_two
sub_sub_three
}
}
`, qp.String())
}