Debian Codegraph @

tests/utilities/test_introspection_from_schema.py

raw
1 import pickle
2 import sys
3 from copy import deepcopy
4
5 from pytest import mark
6
7 from graphql.language import DirectiveLocation
8 from graphql.type import (
9 GraphQLDirective,
10 GraphQLField,
11 GraphQLObjectType,
12 GraphQLSchema,
13 GraphQLString,
14 )
15 from graphql.utilities import (
16 build_client_schema,
17 build_schema,
18 print_schema,
19 introspection_from_schema,
20 IntrospectionQuery,
21 )
22
23 from ..fixtures import big_schema_introspection_result, big_schema_sdl # noqa: F401
24 from ..utils import dedent, timeout_factor
25
26
27 def introspection_to_sdl(introspection: IntrospectionQuery) -> str:
28 return print_schema(build_client_schema(introspection))
29
30
31 def describe_introspection_from_schema():
32
33 schema = GraphQLSchema(
34 GraphQLObjectType(
35 "Simple",
36 {
37 "string": GraphQLField(
38 GraphQLString, description="This is a string field"
39 )
40 },
41 description="This is a simple type",
42 ),
43 description="This is a simple schema",
44 )
45
46 def converts_a_simple_schema():
47 introspection = introspection_from_schema(schema)
48
49 assert introspection_to_sdl(introspection) == dedent('''
50 """This is a simple schema"""
51 schema {
52 query: Simple
53 }
54
55 """This is a simple type"""
56 type Simple {
57 """This is a string field"""
58 string: String
59 }
60 ''')
61
62 def converts_a_simple_schema_without_description():
63 introspection = introspection_from_schema(schema, descriptions=False)
64
65 assert introspection_to_sdl(introspection) == dedent("""
66 schema {
67 query: Simple
68 }
69
70 type Simple {
71 string: String
72 }
73 """)
74
75 def includes_deprecated_directives():
76 schema_with_deprecated_directive = GraphQLSchema(
77 query=GraphQLObjectType(
78 name="Query",
79 fields={"string": GraphQLField(GraphQLString)},
80 ),
81 directives=[
82 GraphQLDirective(
83 name="deprecatedDirective",
84 locations=[DirectiveLocation.QUERY],
85 deprecation_reason="Use another directive",
86 ),
87 ],
88 )
89 introspection = introspection_from_schema(schema_with_deprecated_directive)
90 deprecated_directive = next(
91 directive
92 for directive in introspection["__schema"]["directives"]
93 if directive["name"] == "deprecatedDirective"
94 )
95
96 assert deprecated_directive["name"] == "deprecatedDirective"
97 assert deprecated_directive["isDeprecated"] is True
98 assert deprecated_directive["deprecationReason"] == "Use another directive"
99
100 def describe_deepcopy_and_pickle(): # pragma: no cover
101 # introspect the schema
102 introspected_schema = introspection_from_schema(schema)
103 introspection_size = len(str(introspected_schema))
104
105 def can_deep_copy_schema():
106 # create a deepcopy of the schema
107 copied = deepcopy(schema)
108 # check that introspecting the copied schema gives the same result
109 assert introspection_from_schema(copied) == introspected_schema
110
111 def can_pickle_and_unpickle_schema():
112 # check that the schema can be pickled
113 # (particularly, there should be no recursion error,
114 # or errors because of trying to pickle lambdas or local functions)
115 dumped = pickle.dumps(schema)
116
117 # check that the pickle size is reasonable
118 assert len(dumped) < 5 * introspection_size
119 loaded = pickle.loads(dumped)
120
121 # check that introspecting the unpickled schema gives the same result
122 assert introspection_from_schema(loaded) == introspected_schema
123
124 # check that pickling again creates the same result
125 dumped = pickle.dumps(schema)
126 assert len(dumped) < 5 * introspection_size
127 loaded = pickle.loads(dumped)
128 assert introspection_from_schema(loaded) == introspected_schema
129
130 def can_deep_copy_pickled_schema():
131 # pickle and unpickle the schema
132 loaded = pickle.loads(pickle.dumps(schema))
133 # create a deepcopy of the unpickled schema
134 copied = deepcopy(loaded)
135 # check that introspecting the copied schema gives the same result
136 assert introspection_from_schema(copied) == introspected_schema
137
138 @mark.slow
139 def describe_deepcopy_and_pickle_big(): # pragma: no cover
140 @mark.timeout(20 * timeout_factor)
141 def can_deep_copy_big_schema(big_schema_sdl): # noqa: F811
142 # introspect the original big schema
143 big_schema = build_schema(big_schema_sdl)
144 expected_introspection = introspection_from_schema(big_schema)
145
146 # create a deepcopy of the schema
147 copied = deepcopy(big_schema)
148 # check that introspecting the copied schema gives the same result
149 assert introspection_from_schema(copied) == expected_introspection
150
151 @mark.timeout(60 * timeout_factor)
152 def can_pickle_and_unpickle_big_schema(big_schema_sdl): # noqa: F811
153 # introspect the original big schema
154 big_schema = build_schema(big_schema_sdl)
155 expected_introspection = introspection_from_schema(big_schema)
156 size_introspection = len(str(expected_introspection))
157
158 limit = sys.getrecursionlimit()
159 sys.setrecursionlimit(max(limit, 4000)) # needed for pickle
160
161 try:
162 # check that the schema can be pickled
163 # (particularly, there should be no recursion error,
164 # or errors because of trying to pickle lambdas or local functions)
165 dumped = pickle.dumps(big_schema)
166
167 # check that the pickle size is reasonable
168 assert len(dumped) < 5 * size_introspection
169 loaded = pickle.loads(dumped)
170
171 # check that introspecting the pickled schema gives the same result
172 assert introspection_from_schema(loaded) == expected_introspection
173
174 # check that pickling again creates the same result
175 dumped = pickle.dumps(loaded)
176 assert len(dumped) < 5 * size_introspection
177 loaded = pickle.loads(dumped)
178
179 # check that introspecting the re-pickled schema gives the same result
180 assert introspection_from_schema(loaded) == expected_introspection
181
182 finally:
183 sys.setrecursionlimit(limit)
184
185 @mark.timeout(60 * timeout_factor)
186 def can_deep_copy_pickled_big_schema(big_schema_sdl): # noqa: F811
187 # introspect the original big schema
188 big_schema = build_schema(big_schema_sdl)
189 expected_introspection = introspection_from_schema(big_schema)
190
191 limit = sys.getrecursionlimit()
192 sys.setrecursionlimit(max(limit, 4000)) # needed for pickle
193
194 try:
195 # pickle and unpickle the schema
196 loaded = pickle.loads(pickle.dumps(big_schema))
197 # create a deepcopy of the unpickled schema
198 copied = deepcopy(loaded)
199
200 # check that introspecting the copied schema gives the same result
201 assert introspection_from_schema(copied) == expected_introspection
202
203 finally:
204 sys.setrecursionlimit(limit)