Debian Codegraph @

tests/validation/test_unique_directives_per_location.py

raw
1 from functools import partial
2 from typing import Any, Dict, List, Optional, Union
3
4 from graphql.error import GraphQLError
5 from graphql.language import parse
6 from graphql.type import GraphQLSchema
7 from graphql.utilities import extend_schema
8 from graphql.validation import UniqueDirectivesPerLocationRule
9 from graphql.validation.validate import validate_sdl
10
11 from .harness import assert_validation_errors, assert_sdl_validation_errors, test_schema
12
13 extension_sdl = """
14 directive @directive on FIELD | FRAGMENT_DEFINITION
15 directive @directiveA on FIELD | FRAGMENT_DEFINITION
16 directive @directiveB on FIELD | FRAGMENT_DEFINITION
17 directive @repeatable repeatable on FIELD | FRAGMENT_DEFINITION
18 """
19 schema_with_directives = extend_schema(test_schema, parse(extension_sdl))
20
21 assert_errors = partial(
22 assert_validation_errors,
23 UniqueDirectivesPerLocationRule,
24 schema=schema_with_directives,
25 )
26
27 assert_valid = partial(assert_errors, errors=[])
28
29 assert_sdl_errors = partial(
30 assert_sdl_validation_errors, UniqueDirectivesPerLocationRule
31 )
32
33
34 def assert_experimental_sdl_errors(
35 sdl_str: str,
36 errors: List[Union[GraphQLError, Dict[str, Any]]],
37 schema: Optional[GraphQLSchema] = None,
38 ) -> List[GraphQLError]:
39 doc = parse(sdl_str, experimental_directives_on_directive_definitions=True)
40 returned_errors = validate_sdl(doc, schema, [UniqueDirectivesPerLocationRule])
41 assert returned_errors == errors
42 return returned_errors
43
44
45 def describe_validate_directives_are_unique_per_location():
46 def no_directives():
47 assert_valid("""
48 {
49 field
50 }
51 """)
52
53 def unique_directives_in_different_locations():
54 assert_valid("""
55 fragment Test on Type @directiveA {
56 field @directiveB
57 }
58 """)
59
60 def unique_directives_in_same_locations():
61 assert_valid("""
62 fragment Test on Type @directiveA @directiveB {
63 field @directiveA @directiveB
64 }
65 """)
66
67 def same_directives_in_different_locations():
68 assert_valid("""
69 fragment Test on Type @directiveA {
70 field @directiveA
71 }
72 """)
73
74 def same_directives_in_similar_locations():
75 assert_valid("""
76 fragment Test on Type {
77 field @directive
78 field @directive
79 }
80 """)
81
82 def repeatable_directives_in_same_location():
83 assert_valid("""
84 fragment Test on Type @repeatable @repeatable {
85 field @repeatable @repeatable
86 }
87 """)
88
89 def unknown_directives_must_be_ignored():
90 assert_valid("""
91 type Test @unknown @unknown {
92 field: String! @unknown @unknown
93 }
94
95 extend type Test @unknown {
96 anotherField: String!
97 }
98 """)
99
100 def duplicate_directives_in_one_location():
101 assert_errors(
102 """
103 fragment Test on Type {
104 field @directive @directive
105 }
106 """,
107 [
108 {
109 "message": "The directive '@directive'"
110 " can only be used once at this location.",
111 "locations": [(3, 21), (3, 32)],
112 },
113 ],
114 )
115
116 def many_duplicate_directives_in_one_location():
117 assert_errors(
118 """
119 fragment Test on Type {
120 field @directive @directive @directive
121 }
122 """,
123 [
124 {
125 "message": "The directive '@directive'"
126 " can only be used once at this location.",
127 "locations": [(3, 21), (3, 32)],
128 },
129 {
130 "message": "The directive '@directive'"
131 " can only be used once at this location.",
132 "locations": [(3, 21), (3, 43)],
133 },
134 ],
135 )
136
137 def different_duplicate_directives_in_one_location():
138 assert_errors(
139 """
140 fragment Test on Type {
141 field @directiveA @directiveB @directiveA @directiveB
142 }
143 """,
144 [
145 {
146 "message": "The directive '@directiveA'"
147 " can only be used once at this location.",
148 "locations": [(3, 21), (3, 45)],
149 },
150 {
151 "message": "The directive '@directiveB'"
152 " can only be used once at this location.",
153 "locations": [(3, 33), (3, 57)],
154 },
155 ],
156 )
157
158 def different_duplicate_directives_in_many_locations():
159 assert_errors(
160 """
161 fragment Test on Type @directive @directive {
162 field @directive @directive
163 }
164 """,
165 [
166 {
167 "message": "The directive '@directive'"
168 " can only be used once at this location.",
169 "locations": [(2, 35), (2, 46)],
170 },
171 {
172 "message": "The directive '@directive'"
173 " can only be used once at this location.",
174 "locations": [(3, 21), (3, 32)],
175 },
176 ],
177 )
178
179 def duplicate_directives_on_sdl_definitions():
180 assert_sdl_errors(
181 """
182 directive @nonRepeatable on
183 SCHEMA | SCALAR | OBJECT | INTERFACE | UNION | INPUT_OBJECT
184
185 schema @nonRepeatable @nonRepeatable { query: Dummy }
186
187 scalar TestScalar @nonRepeatable @nonRepeatable
188 type TestObject @nonRepeatable @nonRepeatable
189 interface TestInterface @nonRepeatable @nonRepeatable
190 union TestUnion @nonRepeatable @nonRepeatable
191 input TestInput @nonRepeatable @nonRepeatable
192 """,
193 [
194 {
195 "message": "The directive '@nonRepeatable'"
196 " can only be used once at this location.",
197 "locations": [(5, 20), (5, 35)],
198 },
199 {
200 "message": "The directive '@nonRepeatable'"
201 " can only be used once at this location.",
202 "locations": [(7, 31), (7, 46)],
203 },
204 {
205 "message": "The directive '@nonRepeatable'"
206 " can only be used once at this location.",
207 "locations": [(8, 29), (8, 44)],
208 },
209 {
210 "message": "The directive '@nonRepeatable'"
211 " can only be used once at this location.",
212 "locations": [(9, 37), (9, 52)],
213 },
214 {
215 "message": "The directive '@nonRepeatable'"
216 " can only be used once at this location.",
217 "locations": [(10, 29), (10, 44)],
218 },
219 {
220 "message": "The directive '@nonRepeatable'"
221 " can only be used once at this location.",
222 "locations": [(11, 29), (11, 44)],
223 },
224 ],
225 )
226
227 def duplicate_directives_on_sdl_extensions():
228 assert_sdl_errors(
229 """
230 directive @nonRepeatable on
231 SCHEMA | SCALAR | OBJECT | INTERFACE | UNION | INPUT_OBJECT
232
233 extend schema @nonRepeatable @nonRepeatable
234
235 extend scalar TestScalar @nonRepeatable @nonRepeatable
236 extend type TestObject @nonRepeatable @nonRepeatable
237 extend interface TestInterface @nonRepeatable @nonRepeatable
238 extend union TestUnion @nonRepeatable @nonRepeatable
239 extend input TestInput @nonRepeatable @nonRepeatable
240 """,
241 [
242 {
243 "message": "The directive '@nonRepeatable'"
244 " can only be used once at this location.",
245 "locations": [(5, 27), (5, 42)],
246 },
247 {
248 "message": "The directive '@nonRepeatable'"
249 " can only be used once at this location.",
250 "locations": [(7, 38), (7, 53)],
251 },
252 {
253 "message": "The directive '@nonRepeatable'"
254 " can only be used once at this location.",
255 "locations": [(8, 36), (8, 51)],
256 },
257 {
258 "message": "The directive '@nonRepeatable'"
259 " can only be used once at this location.",
260 "locations": [(9, 44), (9, 59)],
261 },
262 {
263 "message": "The directive '@nonRepeatable'"
264 " can only be used once at this location.",
265 "locations": [(10, 36), (10, 51)],
266 },
267 {
268 "message": "The directive '@nonRepeatable'"
269 " can only be used once at this location.",
270 "locations": [(11, 36), (11, 51)],
271 },
272 ],
273 )
274
275 def duplicate_directives_between_sdl_definitions_and_extensions():
276 assert_sdl_errors(
277 """
278 directive @nonRepeatable on SCHEMA
279
280 schema @nonRepeatable { query: Dummy }
281 extend schema @nonRepeatable
282 """,
283 [
284 {
285 "message": "The directive '@nonRepeatable'"
286 " can only be used once at this location.",
287 "locations": [(4, 20), (5, 27)],
288 },
289 ],
290 )
291
292 assert_sdl_errors(
293 """
294 directive @nonRepeatable on SCALAR
295
296 scalar TestScalar @nonRepeatable
297 extend scalar TestScalar @nonRepeatable
298 scalar TestScalar @nonRepeatable
299 """,
300 [
301 {
302 "message": "The directive '@nonRepeatable'"
303 " can only be used once at this location.",
304 "locations": [(4, 31), (5, 38)],
305 },
306 {
307 "message": "The directive '@nonRepeatable'"
308 " can only be used once at this location.",
309 "locations": [(4, 31), (6, 31)],
310 },
311 ],
312 )
313
314 assert_sdl_errors(
315 """
316 directive @nonRepeatable on OBJECT
317
318 extend type TestObject @nonRepeatable
319 type TestObject @nonRepeatable
320 extend type TestObject @nonRepeatable
321 """,
322 [
323 {
324 "message": "The directive '@nonRepeatable'"
325 " can only be used once at this location.",
326 "locations": [(4, 36), (5, 29)],
327 },
328 {
329 "message": "The directive '@nonRepeatable'"
330 " can only be used once at this location.",
331 "locations": [(4, 36), (6, 36)],
332 },
333 ],
334 )
335
336 def duplicate_directives_on_directive_definitions():
337 assert_experimental_sdl_errors(
338 """
339 directive @nonRepeatable on DIRECTIVE_DEFINITION
340
341 directive @testDirective @nonRepeatable @nonRepeatable on FIELD_DEFINITION
342 """,
343 [
344 {
345 "message": "The directive '@nonRepeatable'"
346 " can only be used once at this location.",
347 "locations": [(4, 38), (4, 53)],
348 },
349 ],
350 )
351
352 def duplicate_directives_on_directive_extensions():
353 assert_experimental_sdl_errors(
354 """
355 directive @nonRepeatable on DIRECTIVE_DEFINITION
356
357 extend directive @testDirective @nonRepeatable @nonRepeatable
358 """,
359 [
360 {
361 "message": "The directive '@nonRepeatable'"
362 " can only be used once at this location.",
363 "locations": [(4, 45), (4, 60)],
364 },
365 ],
366 )
367
368 def duplicate_directives_between_directive_definitions_and_extensions():
369 assert_experimental_sdl_errors(
370 """
371 directive @nonRepeatable on DIRECTIVE_DEFINITION
372
373 directive @testDirective @nonRepeatable on FIELD_DEFINITION
374 extend directive @testDirective @nonRepeatable
375 """,
376 [
377 {
378 "message": "The directive '@nonRepeatable'"
379 " can only be used once at this location.",
380 "locations": [(4, 38), (5, 45)],
381 },
382 ],
383 )
384
385 def duplicate_directives_between_directive_extensions():
386 assert_experimental_sdl_errors(
387 """
388 directive @nonRepeatable on DIRECTIVE_DEFINITION
389
390 extend directive @testDirective @nonRepeatable
391 extend directive @testDirective @nonRepeatable
392 """,
393 [
394 {
395 "message": "The directive '@nonRepeatable'"
396 " can only be used once at this location.",
397 "locations": [(4, 45), (5, 45)],
398 },
399 ],
400 )