Debian Codegraph @

tests/utilities/test_lexicographic_sort_schema.py

raw
1 from typing import cast
2
3 from graphql.type import (
4 GraphQLArgument,
5 GraphQLField,
6 GraphQLInputField,
7 GraphQLInputObjectType,
8 GraphQLObjectType,
9 GraphQLSchema,
10 GraphQLString,
11 )
12 from graphql.utilities import build_schema, lexicographic_sort_schema, print_schema
13
14 from ..utils import dedent
15
16
17 def sort_sdl(sdl: str) -> str:
18 schema = build_schema(sdl)
19 return print_schema(lexicographic_sort_schema(schema))
20
21
22 def describe_lexicographic_sort_schema():
23 def sort_fields():
24 sorted_sdl = sort_sdl("""
25 input Bar {
26 barB: String!
27 barA: String
28 barC: [String]
29 }
30
31 interface FooInterface {
32 fooB: String!
33 fooA: String
34 fooC: [String]
35 }
36
37 type FooType implements FooInterface {
38 fooC: [String]
39 fooA: String
40 fooB: String!
41 }
42
43 type Query {
44 dummy(arg: Bar): FooType
45 }
46 """)
47
48 assert sorted_sdl == dedent("""
49 input Bar {
50 barA: String
51 barB: String!
52 barC: [String]
53 }
54
55 interface FooInterface {
56 fooA: String
57 fooB: String!
58 fooC: [String]
59 }
60
61 type FooType implements FooInterface {
62 fooA: String
63 fooB: String!
64 fooC: [String]
65 }
66
67 type Query {
68 dummy(arg: Bar): FooType
69 }
70 """)
71
72 def sort_implemented_interfaces():
73 sorted_sdl = sort_sdl("""
74 interface FooA {
75 dummy: String
76 }
77
78 interface FooB {
79 dummy: String
80 }
81
82 interface FooC implements FooB & FooA {
83 dummy: String
84 }
85
86 type Query implements FooB & FooA & FooC {
87 dummy: String
88 }
89 """)
90
91 assert sorted_sdl == dedent("""
92 interface FooA {
93 dummy: String
94 }
95
96 interface FooB {
97 dummy: String
98 }
99
100 interface FooC implements FooA & FooB {
101 dummy: String
102 }
103
104 type Query implements FooA & FooB & FooC {
105 dummy: String
106 }
107 """)
108
109 def sort_types_in_union():
110 sorted_sdl = sort_sdl("""
111 type FooA {
112 dummy: String
113 }
114
115 type FooB {
116 dummy: String
117 }
118
119 type FooC {
120 dummy: String
121 }
122
123 union FooUnion = FooB | FooA | FooC
124
125 type Query {
126 dummy: FooUnion
127 }
128 """)
129
130 assert sorted_sdl == dedent("""
131 type FooA {
132 dummy: String
133 }
134
135 type FooB {
136 dummy: String
137 }
138
139 type FooC {
140 dummy: String
141 }
142
143 union FooUnion = FooA | FooB | FooC
144
145 type Query {
146 dummy: FooUnion
147 }
148 """)
149
150 def sort_enum_types():
151 sorted_sdl = sort_sdl("""
152 enum Foo {
153 B
154 C
155 A
156 }
157
158 type Query {
159 dummy: Foo
160 }
161 """)
162
163 assert sorted_sdl == dedent("""
164 enum Foo {
165 A
166 B
167 C
168 }
169
170 type Query {
171 dummy: Foo
172 }
173 """)
174
175 def sort_field_arguments():
176 sorted_sdl = sort_sdl("""
177 type Query {
178 dummy(argB: Int!, argA: String, argC: [Float]): ID
179 }
180 """)
181
182 assert sorted_sdl == dedent("""
183 type Query {
184 dummy(argA: String, argB: Int!, argC: [Float]): ID
185 }
186 """)
187
188 def sort_types():
189 sorted_sdl = sort_sdl("""
190 type Query {
191 dummy(arg1: FooF, arg2: FooA, arg3: FooG): FooD
192 }
193
194 type FooC implements FooE {
195 dummy: String
196 }
197
198 enum FooG {
199 enumValue
200 }
201
202 scalar FooA
203
204 input FooF {
205 dummy: String
206 }
207
208 union FooD = FooC | FooB
209
210 interface FooE {
211 dummy: String
212 }
213
214 type FooB {
215 dummy: String
216 }
217 """)
218
219 assert sorted_sdl == dedent("""
220 scalar FooA
221
222 type FooB {
223 dummy: String
224 }
225
226 type FooC implements FooE {
227 dummy: String
228 }
229
230 union FooD = FooB | FooC
231
232 interface FooE {
233 dummy: String
234 }
235
236 input FooF {
237 dummy: String
238 }
239
240 enum FooG {
241 enumValue
242 }
243
244 type Query {
245 dummy(arg1: FooF, arg2: FooA, arg3: FooG): FooD
246 }
247 """)
248
249 def sort_directive_arguments():
250 sorted_sdl = sort_sdl("""
251 directive @test(argC: Float, argA: String, argB: Int) on FIELD
252
253 type Query {
254 dummy: String
255 }
256 """)
257
258 assert sorted_sdl == dedent("""
259 directive @test(argA: String, argB: Int, argC: Float) on FIELD
260
261 type Query {
262 dummy: String
263 }
264 """)
265
266 def sort_directive_locations():
267 sorted_sdl = sort_sdl("""
268 directive @test(argC: Float, argA: String, argB: Int) on UNION | FIELD | ENUM
269
270 type Query {
271 dummy: String
272 }
273 """) # noqa: E501
274
275 assert sorted_sdl == dedent("""
276 directive @test(argA: String, argB: Int, argC: Float) on ENUM | FIELD | UNION
277
278 type Query {
279 dummy: String
280 }
281 """) # noqa: E501
282
283 def sort_directives():
284 sorted_sdl = sort_sdl("""
285 directive @fooC on FIELD
286
287 directive @fooB on UNION
288
289 directive @fooA on ENUM
290
291 type Query {
292 dummy: String
293 }
294 """)
295
296 assert sorted_sdl == dedent("""
297 directive @fooA on ENUM
298
299 directive @fooB on UNION
300
301 directive @fooC on FIELD
302
303 type Query {
304 dummy: String
305 }
306 """)
307
308 def sort_recursive_types():
309 sorted_sdl = sort_sdl("""
310 interface FooC {
311 fooB: FooB
312 fooA: FooA
313 fooC: FooC
314 }
315
316 type FooB implements FooC {
317 fooB: FooB
318 fooA: FooA
319 }
320
321 type FooA implements FooC {
322 fooB: FooB
323 fooA: FooA
324 }
325
326 type Query {
327 fooC: FooC
328 fooB: FooB
329 fooA: FooA
330 }
331 """)
332
333 assert sorted_sdl == dedent("""
334 type FooA implements FooC {
335 fooA: FooA
336 fooB: FooB
337 }
338
339 type FooB implements FooC {
340 fooA: FooA
341 fooB: FooB
342 }
343
344 interface FooC {
345 fooA: FooA
346 fooB: FooB
347 fooC: FooC
348 }
349
350 type Query {
351 fooA: FooA
352 fooB: FooB
353 fooC: FooC
354 }
355 """)
356
357 def describe_input_field_properties():
358 def preserves_input_field_deprecation_reason():
359 input_type = GraphQLInputObjectType(
360 "TestInput",
361 {
362 "zField": GraphQLInputField(GraphQLString),
363 "aField": GraphQLInputField(
364 GraphQLString, deprecation_reason="Use bField instead"
365 ),
366 "mField": GraphQLInputField(
367 GraphQLString, deprecation_reason="Deprecated field"
368 ),
369 },
370 )
371 query_type = GraphQLObjectType(
372 "Query",
373 {
374 "dummy": GraphQLField(
375 GraphQLString, args={"input": GraphQLArgument(input_type)}
376 )
377 },
378 )
379 schema = GraphQLSchema(query=query_type, types=[input_type])
380 sorted_schema = lexicographic_sort_schema(schema)
381
382 sorted_input_type = cast(
383 GraphQLInputObjectType, sorted_schema.type_map["TestInput"]
384 )
385 field_names = list(sorted_input_type.fields)
386 assert field_names == ["aField", "mField", "zField"]
387
388 assert (
389 sorted_input_type.fields["aField"].deprecation_reason
390 == "Use bField instead"
391 )
392 assert (
393 sorted_input_type.fields["mField"].deprecation_reason
394 == "Deprecated field"
395 )
396 assert sorted_input_type.fields["zField"].deprecation_reason is None
397
398 def preserves_input_field_extensions():
399 input_type = GraphQLInputObjectType(
400 "TestInput",
401 {
402 "zField": GraphQLInputField(GraphQLString, extensions={"x": 1}),
403 "aField": GraphQLInputField(
404 GraphQLString, extensions={"custom": "value"}
405 ),
406 },
407 )
408 query_type = GraphQLObjectType(
409 "Query",
410 {
411 "dummy": GraphQLField(
412 GraphQLString, args={"input": GraphQLArgument(input_type)}
413 )
414 },
415 )
416 schema = GraphQLSchema(query=query_type, types=[input_type])
417 sorted_schema = lexicographic_sort_schema(schema)
418 sorted_input_type = cast(
419 GraphQLInputObjectType, sorted_schema.type_map["TestInput"]
420 )
421
422 field_names = list(sorted_input_type.fields)
423 assert field_names == ["aField", "zField"]
424
425 assert sorted_input_type.fields["aField"].extensions == {"custom": "value"}
426 assert sorted_input_type.fields["zField"].extensions == {"x": 1}
427
428 def preserves_input_field_out_name():
429 input_type = GraphQLInputObjectType(
430 "TestInput",
431 {
432 "zField": GraphQLInputField(GraphQLString, out_name="z_field"),
433 "aField": GraphQLInputField(GraphQLString, out_name="a_field"),
434 },
435 )
436 query_type = GraphQLObjectType(
437 "Query",
438 {
439 "dummy": GraphQLField(
440 GraphQLString, args={"input": GraphQLArgument(input_type)}
441 )
442 },
443 )
444 schema = GraphQLSchema(query=query_type, types=[input_type])
445 sorted_schema = lexicographic_sort_schema(schema)
446 sorted_input_type = cast(
447 GraphQLInputObjectType, sorted_schema.type_map["TestInput"]
448 )
449
450 field_names = list(sorted_input_type.fields)
451 assert field_names == ["aField", "zField"]
452
453 assert sorted_input_type.fields["aField"].out_name == "a_field"
454 assert sorted_input_type.fields["zField"].out_name == "z_field"
455
456 def describe_schema_properties():
457 def preserves_schema_description():
458 query_type = GraphQLObjectType(
459 "Query", {"dummy": GraphQLField(GraphQLString)}
460 )
461 schema = GraphQLSchema(
462 query=query_type, description="This is the schema description."
463 )
464 sorted_schema = lexicographic_sort_schema(schema)
465
466 assert sorted_schema.description == "This is the schema description."
467
468 def preserves_schema_extensions():
469 query_type = GraphQLObjectType(
470 "Query", {"dummy": GraphQLField(GraphQLString)}
471 )
472 schema = GraphQLSchema(query=query_type, extensions={"custom": "value"})
473 sorted_schema = lexicographic_sort_schema(schema)
474
475 assert sorted_schema.extensions == {"custom": "value"}