Repository navigation
Expand file tree
/
Copy pathResultPathTest.groovy
More file actions
305 lines (233 loc) · 8.23 KB
/
Copy pathResultPathTest.groovy
File metadata and controls
305 lines (233 loc) · 8.23 KB
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
package graphql.execution
import graphql.AssertException
import graphql.ExceptionWhileDataFetching
import graphql.ExecutionInput
import graphql.GraphQLError
import graphql.SerializationError
import graphql.TestUtil
import graphql.schema.DataFetcher
import spock.lang.Specification
import spock.lang.Unroll
class ResultPathTest extends Specification {
@Unroll
"unit test toList works as expected : #actual"() {
expect:
actual.toList() == expected
where:
actual || expected
ResultPath.rootPath() || []
ResultPath.rootPath().segment("A") || ["A"]
ResultPath.rootPath().segment("A").segment(1).segment("B") || ["A", 1, "B"]
ResultPath.rootPath().segment("A").segment("B").segment(1) || ["A", "B", 1]
}
@Unroll
"unit test toString works as expected : #actual"() {
expect:
actual.toString() == expected
where:
actual || expected
ResultPath.rootPath() || ""
ResultPath.rootPath().segment("A") || "/A"
ResultPath.rootPath().segment("A").segment(1).segment("B") || "/A[1]/B"
ResultPath.rootPath().segment("A").segment("B").segment(1) || "/A/B[1]"
}
@Unroll
"unit test sibling works as expected : #actual"() {
expect:
actual.toList() == expected
where:
actual || expected
ResultPath.rootPath() || []
ResultPath.rootPath().segment("A").sibling("B") || ["B"]
ResultPath.rootPath().segment("A").segment(1).segment("B").sibling("C") || ["A", 1, "C"]
}
@Unroll
"unit test getLevel works as expected : #actual"() {
expect:
actual.getLevel() == expected
where:
actual || expected
ResultPath.rootPath() || 0
ResultPath.rootPath().segment("A") || 1
ResultPath.rootPath().segment("A").segment("B") || 2
ResultPath.rootPath().segment("A").segment(1).segment("B") || 2
ResultPath.rootPath().segment("A").segment("B").segment(1) || 2
}
def "full integration test of path support"() {
when:
def spec = """
type Query {
f1 : String
f2 : [Test]
f3 : Float
f4 : NonNullType
}
type Test {
sub1 : String
sub2 : String
}
type NonNullType {
nonNullField : String!
}
"""
def f1Fetcher = { env -> throw new RuntimeException("error") } as DataFetcher
def f2Fetcher = { env -> [false, true, false] } as DataFetcher
def f3Fetcher = { env -> "This is not a float" } as DataFetcher
def sub1Fetcher = { env -> "staticValue" } as DataFetcher
def sub2Fetcher = { env ->
boolean willThrow = env.getSource()
if (willThrow) {
throw new RuntimeException("error")
}
return "no error"
} as DataFetcher
def f4Fetcher = { env -> "Some Value" } as DataFetcher
def nonNullFieldFetcher = { env -> null } as DataFetcher
def graphQL = TestUtil.graphQL(spec,
["Query" :
[
"f1": f1Fetcher,
"f2": f2Fetcher,
"f3": f3Fetcher,
"f4": f4Fetcher
],
"Test" :
[
"sub1": sub1Fetcher,
"sub2": sub2Fetcher
],
"NonNullType":
[
"nonNullField": nonNullFieldFetcher
]
]).build()
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query("""
{
f1
f2 {
sub1
sub2
}
aliasedF3 : f3
f4 {
nonNullField
}
}
""")
.build()
List<GraphQLError> errors = graphQL.execute(executionInput).getErrors()
then:
errors.size() == 4
def error = errors.get(0) as ExceptionWhileDataFetching
["f1"] == error.getPath()
def error2 = errors.get(1) as ExceptionWhileDataFetching
["f2", 1, "sub2"] == error2.getPath()
def error3 = errors.get(2) as SerializationError
["aliasedF3"] == error3.getPath()
def error4 = errors.get(3) as NonNullableFieldWasNullError
["f4", "nonNullField"] == error4.getPath()
}
def "test best case parsing"() {
expect:
ResultPath.parse(pathString).toList() == expectedList
where:
pathString | expectedList
"" | []
null | []
"/a" | ["a"]
"/a/b" | ["a", "b"]
" /a/b " | ["a", "b"]
"/a/b[0]/c[1]" | ["a", "b", 0, "c", 1]
}
def "test worst case parsing"() {
when:
ResultPath.parse(badPathString)
then:
thrown(AssertException)
where:
badPathString | _
"a" | _
"a/b" | _
"a/b[x]" | _
"a/b[0" | _
"a/b[0/c[1]" | _
"a/b[0]c[1/" | _
"/" | _
}
@Unroll
def "test from test fromList #inputList and #expectedString"() {
expect:
ResultPath.fromList(inputList).toString() == expectedString
where:
expectedString | inputList
"" | []
"/a" | ["a"]
"/a/b" | ["a", "b"]
"/a/b[0]/c[1]" | ["a", "b", 0, "c", 1]
}
def "get path without list end"() {
when:
def path = ResultPath.fromList(["a", "b", 9])
path = path.getPathWithoutListEnd()
then:
path.toList() == ["a", "b"]
when:
path = path.getPathWithoutListEnd()
then:
path.toList() == ["a", "b"]
}
def "pass any other object than string or int"(){
when:
ResultPath.fromList(["a", "b", true])
then:
notThrown(ClassCastException)
}
def "can append paths"() {
when:
def path = ResultPath.fromList(["a", "b", 0])
def path2 = ResultPath.fromList(["x", "y", 9])
def newPath = path.append(path2)
then:
newPath.toList() == ["a", "b", 0, "x", "y", 9]
when:
newPath = ResultPath.rootPath().append(path2)
then:
newPath.toList() == ["x", "y", 9]
when:
newPath = path2.append(ResultPath.rootPath())
then:
newPath.toList() == ["x", "y", 9]
when:
newPath = ResultPath.rootPath().append(ResultPath.rootPath())
then:
newPath.toList() == []
}
def "replace support"() {
when:
def path = ResultPath.fromList(["a", "b", 0])
def newPath = path.replaceSegment(1)
then:
newPath.toList() == ["a", "b", 1]
when:
newPath = path.replaceSegment("x")
then:
newPath.toList() == ["a", "b", "x"]
when:
newPath = path.replaceSegment(99)
then:
newPath.toList() == ["a", "b", 99]
when:
ResultPath.rootPath().replaceSegment(1)
then:
thrown(AssertException)
when:
ResultPath.rootPath().replaceSegment("x")
then:
thrown(AssertException)
when:
newPath = ResultPath.parse("/a/b[1]").replaceSegment("x")
then:
newPath.toList() == ["a", "b", "x"]
}
}