Debian Codegraph @

tests/utilities/test_ast_to_dict.py

raw
1 from graphql.language import parse, FieldNode, NameNode, OperationType, SelectionSetNode
2 from graphql.utilities import ast_to_dict
3
4
5 def describe_ast_to_disc():
6 def converts_name_node_to_dict():
7 node = NameNode(value="test")
8 res = ast_to_dict(node)
9 assert res == {"kind": "name", "value": "test"}
10 assert list(res)[0] == "kind"
11 assert ast_to_dict(node, locations=True) == res
12 assert node.to_dict() == res
13 assert node.to_dict(locations=True) == res
14
15 def converts_two_name_nodes_to_list():
16 nodes = [NameNode(value="foo"), NameNode(value="bar")]
17 res = ast_to_dict(nodes)
18 assert ast_to_dict(nodes, locations=True) == res
19 assert res == [
20 {"kind": "name", "value": "foo"},
21 {"kind": "name", "value": "bar"},
22 ]
23
24 def converts_operation_type_to_its_value():
25 assert ast_to_dict(OperationType.QUERY) == "query"
26
27 def keeps_all_other_leaf_nodes():
28 assert ast_to_dict(None) is None # type: ignore
29 assert ast_to_dict(42) == 42 # type: ignore
30 assert ast_to_dict("foo") == "foo" # type: ignore
31 ast = {"foo": "bar"}
32 assert ast_to_dict(ast) is ast # type: ignore
33
34 def converts_recursive_ast_to_recursive_dict():
35 field = FieldNode(name="foo", arguments=(), selection_set=())
36 ast = SelectionSetNode(selections=(field,))
37 field.selection_set = ast
38 res = ast_to_dict(ast)
39 assert res == {
40 "kind": "selection_set",
41 "selections": [
42 {
43 "kind": "field",
44 "name": "foo",
45 "alias": None,
46 "arguments": [],
47 "directives": None,
48 "selection_set": res,
49 }
50 ],
51 }
52
53 def converts_simple_schema_to_dict():
54 ast = parse("""
55 type Query {
56 me: User
57 }
58
59 type User {
60 id: ID
61 name: String
62 }
63 """)
64 res = ast_to_dict(ast)
65 assert ast.to_dict() == res
66 assert res == {
67 "definitions": [
68 {
69 "description": None,
70 "directives": [],
71 "fields": [
72 {
73 "arguments": [],
74 "description": None,
75 "directives": [],
76 "kind": "field_definition",
77 "name": {"kind": "name", "value": "me"},
78 "type": {
79 "kind": "named_type",
80 "name": {"kind": "name", "value": "User"},
81 },
82 }
83 ],
84 "interfaces": [],
85 "kind": "object_type_definition",
86 "name": {"kind": "name", "value": "Query"},
87 },
88 {
89 "description": None,
90 "directives": [],
91 "fields": [
92 {
93 "arguments": [],
94 "description": None,
95 "directives": [],
96 "kind": "field_definition",
97 "name": {"kind": "name", "value": "id"},
98 "type": {
99 "kind": "named_type",
100 "name": {"kind": "name", "value": "ID"},
101 },
102 },
103 {
104 "arguments": [],
105 "description": None,
106 "directives": [],
107 "kind": "field_definition",
108 "name": {"kind": "name", "value": "name"},
109 "type": {
110 "kind": "named_type",
111 "name": {"kind": "name", "value": "String"},
112 },
113 },
114 ],
115 "interfaces": [],
116 "kind": "object_type_definition",
117 "name": {"kind": "name", "value": "User"},
118 },
119 ],
120 "kind": "document",
121 }
122 assert list(res)[0] == "kind"
123
124 def converts_simple_schema_to_dict_with_locations():
125 ast = parse("""
126 type Query {
127 me: User
128 }
129
130 type User {
131 id: ID
132 name: String
133 }
134 """)
135 res = ast_to_dict(ast, locations=True)
136 assert ast.to_dict(locations=True) == res
137 assert res == {
138 "definitions": [
139 {
140 "description": None,
141 "directives": [],
142 "fields": [
143 {
144 "arguments": [],
145 "description": None,
146 "directives": [],
147 "kind": "field_definition",
148 "loc": {"end": 48, "start": 40},
149 "name": {
150 "kind": "name",
151 "loc": {"end": 42, "start": 40},
152 "value": "me",
153 },
154 "type": {
155 "kind": "named_type",
156 "loc": {"end": 48, "start": 44},
157 "name": {
158 "kind": "name",
159 "loc": {"end": 48, "start": 44},
160 "value": "User",
161 },
162 },
163 }
164 ],
165 "interfaces": [],
166 "kind": "object_type_definition",
167 "loc": {"end": 62, "start": 13},
168 "name": {
169 "kind": "name",
170 "loc": {"end": 23, "start": 18},
171 "value": "Query",
172 },
173 },
174 {
175 "description": None,
176 "directives": [],
177 "fields": [
178 {
179 "arguments": [],
180 "description": None,
181 "directives": [],
182 "kind": "field_definition",
183 "loc": {"end": 108, "start": 102},
184 "name": {
185 "kind": "name",
186 "loc": {"end": 104, "start": 102},
187 "value": "id",
188 },
189 "type": {
190 "kind": "named_type",
191 "loc": {"end": 108, "start": 106},
192 "name": {
193 "kind": "name",
194 "loc": {"end": 108, "start": 106},
195 "value": "ID",
196 },
197 },
198 },
199 {
200 "arguments": [],
201 "description": None,
202 "directives": [],
203 "kind": "field_definition",
204 "loc": {"end": 135, "start": 123},
205 "name": {
206 "kind": "name",
207 "loc": {"end": 127, "start": 123},
208 "value": "name",
209 },
210 "type": {
211 "kind": "named_type",
212 "loc": {"end": 135, "start": 129},
213 "name": {
214 "kind": "name",
215 "loc": {"end": 135, "start": 129},
216 "value": "String",
217 },
218 },
219 },
220 ],
221 "interfaces": [],
222 "kind": "object_type_definition",
223 "loc": {"end": 149, "start": 76},
224 "name": {
225 "kind": "name",
226 "loc": {"end": 85, "start": 81},
227 "value": "User",
228 },
229 },
230 ],
231 "kind": "document",
232 "loc": {"end": 162, "start": 0},
233 }
234 keys = list(res)
235 assert keys[0] == "kind"
236 assert keys[-1] == "loc"
237 assert list(res["loc"]) == ["start", "end"]
238
239 def converts_simple_query_to_dict():
240 ast = parse("""
241 query HeroForEpisode($ep: Episode!) {
242 hero(episode: $ep) {
243 name
244 ... on Droid {
245 primaryFunction
246 }
247 ... on Human {
248 height
249 }
250 }
251 }
252 """)
253 res = ast_to_dict(ast)
254 assert ast.to_dict() == res
255 assert res == {
256 "definitions": [
257 {
258 "description": None,
259 "directives": [],
260 "kind": "operation_definition",
261 "name": {"kind": "name", "value": "HeroForEpisode"},
262 "operation": "query",
263 "selection_set": {
264 "kind": "selection_set",
265 "selections": [
266 {
267 "alias": None,
268 "arguments": [
269 {
270 "kind": "argument",
271 "name": {"kind": "name", "value": "episode"},
272 "value": {
273 "kind": "variable",
274 "name": {"kind": "name", "value": "ep"},
275 },
276 }
277 ],
278 "directives": [],
279 "kind": "field",
280 "name": {"kind": "name", "value": "hero"},
281 "selection_set": {
282 "kind": "selection_set",
283 "selections": [
284 {
285 "alias": None,
286 "arguments": [],
287 "directives": [],
288 "kind": "field",
289 "name": {"kind": "name", "value": "name"},
290 "selection_set": None,
291 },
292 {
293 "directives": [],
294 "kind": "inline_fragment",
295 "selection_set": {
296 "kind": "selection_set",
297 "selections": [
298 {
299 "alias": None,
300 "arguments": [],
301 "directives": [],
302 "kind": "field",
303 "name": {
304 "kind": "name",
305 "value": "primaryFunction",
306 },
307 "selection_set": None,
308 }
309 ],
310 },
311 "type_condition": {
312 "kind": "named_type",
313 "name": {
314 "kind": "name",
315 "value": "Droid",
316 },
317 },
318 },
319 {
320 "directives": [],
321 "kind": "inline_fragment",
322 "selection_set": {
323 "kind": "selection_set",
324 "selections": [
325 {
326 "alias": None,
327 "arguments": [],
328 "directives": [],
329 "kind": "field",
330 "name": {
331 "kind": "name",
332 "value": "height",
333 },
334 "selection_set": None,
335 }
336 ],
337 },
338 "type_condition": {
339 "kind": "named_type",
340 "name": {
341 "kind": "name",
342 "value": "Human",
343 },
344 },
345 },
346 ],
347 },
348 }
349 ],
350 },
351 "variable_definitions": [
352 {
353 "default_value": None,
354 "description": None,
355 "directives": [],
356 "kind": "variable_definition",
357 "type": {
358 "kind": "non_null_type",
359 "type": {
360 "kind": "named_type",
361 "name": {"kind": "name", "value": "Episode"},
362 },
363 },
364 "variable": {
365 "kind": "variable",
366 "name": {"kind": "name", "value": "ep"},
367 },
368 }
369 ],
370 }
371 ],
372 "kind": "document",
373 }
374 assert list(res)[0] == "kind"
375
376 def converts_simple_query_to_dict_with_locations():
377 ast = parse("""
378 query HeroForEpisode($ep: Episode!) {
379 hero(episode: $ep) {
380 name
381 ... on Droid {
382 primaryFunction
383 }
384 ... on Human {
385 height
386 }
387 }
388 }
389 """)
390 res = ast_to_dict(ast, locations=True)
391 assert ast.to_dict(locations=True) == res
392 assert res == {
393 "definitions": [
394 {
395 "description": None,
396 "directives": [],
397 "kind": "operation_definition",
398 "loc": {"end": 293, "start": 13},
399 "name": {
400 "kind": "name",
401 "loc": {"end": 33, "start": 19},
402 "value": "HeroForEpisode",
403 },
404 "operation": "query",
405 "selection_set": {
406 "kind": "selection_set",
407 "loc": {"end": 293, "start": 49},
408 "selections": [
409 {
410 "alias": None,
411 "arguments": [
412 {
413 "kind": "argument",
414 "loc": {"end": 82, "start": 70},
415 "name": {
416 "kind": "name",
417 "loc": {"end": 77, "start": 70},
418 "value": "episode",
419 },
420 "value": {
421 "kind": "variable",
422 "loc": {"end": 82, "start": 79},
423 "name": {
424 "kind": "name",
425 "loc": {"end": 82, "start": 80},
426 "value": "ep",
427 },
428 },
429 }
430 ],
431 "directives": [],
432 "kind": "field",
433 "loc": {"end": 279, "start": 65},
434 "name": {
435 "kind": "name",
436 "loc": {"end": 69, "start": 65},
437 "value": "hero",
438 },
439 "selection_set": {
440 "kind": "selection_set",
441 "loc": {"end": 279, "start": 84},
442 "selections": [
443 {
444 "alias": None,
445 "arguments": [],
446 "directives": [],
447 "kind": "field",
448 "loc": {"end": 106, "start": 102},
449 "name": {
450 "kind": "name",
451 "loc": {"end": 106, "start": 102},
452 "value": "name",
453 },
454 "selection_set": None,
455 },
456 {
457 "directives": [],
458 "kind": "inline_fragment",
459 "loc": {"end": 189, "start": 123},
460 "selection_set": {
461 "kind": "selection_set",
462 "loc": {"end": 189, "start": 136},
463 "selections": [
464 {
465 "alias": None,
466 "arguments": [],
467 "directives": [],
468 "kind": "field",
469 "loc": {
470 "end": 171,
471 "start": 156,
472 },
473 "name": {
474 "kind": "name",
475 "loc": {
476 "end": 171,
477 "start": 156,
478 },
479 "value": "primaryFunction",
480 },
481 "selection_set": None,
482 }
483 ],
484 },
485 "type_condition": {
486 "kind": "named_type",
487 "loc": {"end": 135, "start": 130},
488 "name": {
489 "kind": "name",
490 "loc": {"end": 135, "start": 130},
491 "value": "Droid",
492 },
493 },
494 },
495 {
496 "directives": [],
497 "kind": "inline_fragment",
498 "loc": {"end": 263, "start": 206},
499 "selection_set": {
500 "kind": "selection_set",
501 "loc": {"end": 263, "start": 219},
502 "selections": [
503 {
504 "alias": None,
505 "arguments": [],
506 "directives": [],
507 "kind": "field",
508 "loc": {
509 "end": 245,
510 "start": 239,
511 },
512 "name": {
513 "kind": "name",
514 "loc": {
515 "end": 245,
516 "start": 239,
517 },
518 "value": "height",
519 },
520 "selection_set": None,
521 }
522 ],
523 },
524 "type_condition": {
525 "kind": "named_type",
526 "loc": {"end": 218, "start": 213},
527 "name": {
528 "kind": "name",
529 "loc": {"end": 218, "start": 213},
530 "value": "Human",
531 },
532 },
533 },
534 ],
535 },
536 }
537 ],
538 },
539 "variable_definitions": [
540 {
541 "default_value": None,
542 "description": None,
543 "directives": [],
544 "kind": "variable_definition",
545 "loc": {"end": 47, "start": 34},
546 "type": {
547 "kind": "non_null_type",
548 "loc": {"end": 47, "start": 39},
549 "type": {
550 "kind": "named_type",
551 "loc": {"end": 46, "start": 39},
552 "name": {
553 "kind": "name",
554 "loc": {"end": 46, "start": 39},
555 "value": "Episode",
556 },
557 },
558 },
559 "variable": {
560 "kind": "variable",
561 "loc": {"end": 37, "start": 34},
562 "name": {
563 "kind": "name",
564 "loc": {"end": 37, "start": 35},
565 "value": "ep",
566 },
567 },
568 }
569 ],
570 }
571 ],
572 "kind": "document",
573 "loc": {"end": 306, "start": 0},
574 }
575 keys = list(res)
576 assert keys[0] == "kind"
577 assert keys[-1] == "loc"
578 assert list(res["loc"]) == ["start", "end"]