Debian Codegraph @

tests/validation/test_possible_fragment_spreads.py

raw
1 from functools import partial
2
3 from graphql.utilities import build_schema
4 from graphql.validation import PossibleFragmentSpreadsRule
5
6 from .harness import assert_validation_errors
7
8 test_schema = build_schema("""
9 interface Being {
10 name: String
11 }
12
13 interface Pet implements Being {
14 name: String
15 }
16
17 type Dog implements Being & Pet {
18 name: String
19 barkVolume: Int
20 }
21
22 type Cat implements Being & Pet {
23 name: String
24 meowVolume: Int
25 }
26
27 union CatOrDog = Cat | Dog
28
29 interface Intelligent {
30 iq: Int
31 }
32
33 type Human implements Being & Intelligent {
34 name: String
35 pets: [Pet]
36 iq: Int
37 }
38
39 type Alien implements Being & Intelligent {
40 name: String
41 iq: Int
42 }
43
44 union DogOrHuman = Dog | Human
45
46 union HumanOrAlien = Human | Alien
47
48 type Query {
49 catOrDog: CatOrDog
50 dogOrHuman: DogOrHuman
51 humanOrAlien: HumanOrAlien
52 }
53 """)
54
55
56 assert_errors = partial(
57 assert_validation_errors, PossibleFragmentSpreadsRule, schema=test_schema
58 )
59
60 assert_valid = partial(assert_errors, errors=[])
61
62
63 def describe_validate_possible_fragment_spreads():
64 def of_the_same_object():
65 assert_valid("""
66 fragment objectWithinObject on Dog { ...dogFragment }
67 fragment dogFragment on Dog { barkVolume }
68 """)
69
70 def of_the_same_object_inline_fragment():
71 assert_valid("""
72 fragment objectWithinObjectAnon on Dog { ... on Dog { barkVolume } }
73 """)
74
75 def object_into_implemented_interface():
76 assert_valid("""
77 fragment objectWithinInterface on Pet { ...dogFragment }
78 fragment dogFragment on Dog { barkVolume }
79 """)
80
81 def object_into_containing_union():
82 assert_valid("""
83 fragment objectWithinUnion on CatOrDog { ...dogFragment }
84 fragment dogFragment on Dog { barkVolume }
85 """)
86
87 def union_into_contained_object():
88 assert_valid("""
89 fragment unionWithinObject on Dog { ...catOrDogFragment }
90 fragment catOrDogFragment on CatOrDog { __typename }
91 """)
92
93 def union_into_overlapping_interface():
94 assert_valid("""
95 fragment unionWithinInterface on Pet { ...catOrDogFragment }
96 fragment catOrDogFragment on CatOrDog { __typename }
97 """)
98
99 def union_into_overlapping_union():
100 assert_valid("""
101 fragment unionWithinUnion on DogOrHuman { ...catOrDogFragment }
102 fragment catOrDogFragment on CatOrDog { __typename }
103 """)
104
105 def interface_into_implemented_object():
106 assert_valid("""
107 fragment interfaceWithinObject on Dog { ...petFragment }
108 fragment petFragment on Pet { name }
109 """)
110
111 def interface_into_overlapping_interface():
112 assert_valid("""
113 fragment interfaceWithinInterface on Pet { ...beingFragment }
114 fragment beingFragment on Being { name }
115 """)
116
117 def interface_into_overlapping_interface_in_inline_fragment():
118 assert_valid("""
119 fragment interfaceWithinInterface on Pet { ... on Being { name } }
120 """)
121
122 def interface_into_overlapping_union():
123 assert_valid("""
124 fragment interfaceWithinUnion on CatOrDog { ...petFragment }
125 fragment petFragment on Pet { name }
126 """)
127
128 def ignores_incorrect_type_caught_by_fragments_on_composite_types():
129 assert_valid("""
130 fragment petFragment on Pet { ...badInADifferentWay }
131 fragment badInADifferentWay on String { name }
132 """)
133
134 def ignores_unknown_fragments_caught_by_known_fragment_names():
135 assert_valid("""
136 fragment petFragment on Pet { ...UnknownFragment }
137 """)
138
139 def different_object_into_object():
140 assert_errors(
141 """
142 fragment invalidObjectWithinObject on Cat { ...dogFragment }
143 fragment dogFragment on Dog { barkVolume }
144 """,
145 [
146 {
147 "message": "Fragment 'dogFragment' cannot be spread here"
148 " as objects of type 'Cat' can never be of type 'Dog'.",
149 "locations": [(2, 57)],
150 },
151 ],
152 )
153
154 def different_object_into_object_in_inline_fragment():
155 assert_errors(
156 """
157 fragment invalidObjectWithinObjectAnon on Cat {
158 ... on Dog { barkVolume }
159 }
160 """,
161 [
162 {
163 "message": "Fragment cannot be spread here"
164 " as objects of type 'Cat' can never be of type 'Dog'.",
165 "locations": [(3, 15)],
166 },
167 ],
168 )
169
170 def object_into_not_implementing_interface():
171 assert_errors(
172 """
173 fragment invalidObjectWithinInterface on Pet { ...humanFragment }
174 fragment humanFragment on Human { pets { name } }
175 """,
176 [
177 {
178 "message": "Fragment 'humanFragment' cannot be spread here"
179 " as objects of type 'Pet' can never be of type 'Human'.",
180 "locations": [(2, 60)],
181 },
182 ],
183 )
184
185 def object_into_not_containing_union():
186 assert_errors(
187 """
188 fragment invalidObjectWithinUnion on CatOrDog { ...humanFragment }
189 fragment humanFragment on Human { pets { name } }
190 """,
191 [
192 {
193 "message": "Fragment 'humanFragment' cannot be spread here"
194 " as objects of type 'CatOrDog' can never be of type 'Human'.",
195 "locations": [(2, 61)],
196 },
197 ],
198 )
199
200 def union_into_not_contained_object():
201 assert_errors(
202 """
203 fragment invalidUnionWithinObject on Human { ...catOrDogFragment }
204 fragment catOrDogFragment on CatOrDog { __typename }
205 """,
206 [
207 {
208 "message": "Fragment 'catOrDogFragment' cannot be spread here"
209 " as objects of type 'Human' can never be of type 'CatOrDog'.",
210 "locations": [(2, 58)],
211 },
212 ],
213 )
214
215 def union_into_non_overlapping_interface():
216 assert_errors(
217 """
218 fragment invalidUnionWithinInterface on Pet { ...humanOrAlienFragment }
219 fragment humanOrAlienFragment on HumanOrAlien { __typename }
220 """,
221 [
222 {
223 "message": "Fragment 'humanOrAlienFragment' cannot be spread here"
224 " as objects of type 'Pet' can never be of type 'HumanOrAlien'.",
225 "locations": [(2, 59)],
226 },
227 ],
228 )
229
230 def union_into_non_overlapping_union():
231 assert_errors(
232 """
233 fragment invalidUnionWithinUnion on CatOrDog { ...humanOrAlienFragment }
234 fragment humanOrAlienFragment on HumanOrAlien { __typename }
235 """,
236 [
237 {
238 "message": "Fragment 'humanOrAlienFragment'"
239 " cannot be spread here as objects of type 'CatOrDog'"
240 " can never be of type 'HumanOrAlien'.",
241 "locations": [(2, 60)],
242 },
243 ],
244 )
245
246 def interface_into_non_implementing_object():
247 assert_errors(
248 """
249 fragment invalidInterfaceWithinObject on Cat { ...intelligentFragment }
250 fragment intelligentFragment on Intelligent { iq }
251 """,
252 [
253 {
254 "message": "Fragment 'intelligentFragment' cannot be spread here"
255 " as objects of type 'Cat' can never be of type 'Intelligent'.",
256 "locations": [(2, 60)],
257 },
258 ],
259 )
260
261 def interface_into_non_overlapping_interface():
262 assert_errors(
263 """
264 fragment invalidInterfaceWithinInterface on Pet {
265 ...intelligentFragment
266 }
267 fragment intelligentFragment on Intelligent { iq }
268 """,
269 [
270 {
271 "message": "Fragment 'intelligentFragment' cannot be spread here"
272 " as objects of type 'Pet' can never be of type 'Intelligent'.",
273 "locations": [(3, 15)],
274 },
275 ],
276 )
277
278 def interface_into_non_overlapping_interface_in_inline_fragment():
279 assert_errors(
280 """
281 fragment invalidInterfaceWithinInterfaceAnon on Pet {
282 ...on Intelligent { iq }
283 }
284 """,
285 [
286 {
287 "message": "Fragment cannot be spread here as objects"
288 " of type 'Pet' can never be of type 'Intelligent'.",
289 "locations": [(3, 15)],
290 },
291 ],
292 )
293
294 def interface_into_non_overlapping_union():
295 assert_errors(
296 """
297 fragment invalidInterfaceWithinUnion on HumanOrAlien { ...petFragment }
298 fragment petFragment on Pet { name }
299 """,
300 [
301 {
302 "message": "Fragment 'petFragment' cannot be spread here"
303 " as objects of type 'HumanOrAlien' can never be of type 'Pet'.",
304 "locations": [(2, 68)],
305 },
306 ],
307 )