-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathtest_docs.py
375 lines (332 loc) · 13.4 KB
/
test_docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Test all code snippets in the documentation"""
from __future__ import annotations
from pathlib import Path
from typing import Any, Dict
from .utils import dedent
try:
from typing import TypeAlias
except ImportError: # Python < 3.10
from typing_extensions import TypeAlias
Scope: TypeAlias = Dict[str, Any]
def get_snippets(source, indent=4):
"""Get all code snippets from a given documentation source file."""
if not source.endswith(".rst"): # pragma: no cover
source += ".rst"
source_path = Path(__file__).parents[1] / "docs" / source
with source_path.open() as source_file:
lines = source_file.readlines()
snippets: list[str] = []
snippet: list[str] = []
snippet_start = " " * indent
for line in lines:
if not line.rstrip() and snippet:
snippet.append(line)
elif line.startswith(snippet_start):
snippet.append(line[indent:])
elif snippet:
snippets.append("".join(snippet).rstrip() + "\n")
snippet = []
if snippet:
snippets.append("".join(snippet).rstrip() + "\n")
return snippets
def expected_result(snippets):
"""Get and normalize expected result from snippet."""
out = snippets.pop(0)
assert out.startswith("ExecutionResult(")
return " ".join(out.split()).replace("( ", "(") + "\n"
def expected_errors(snippets):
"""Get and normalize expected errors from snippet."""
out = snippets.pop(0)
assert out.startswith("[GraphQLError(")
return " ".join(out.split()).replace("( ", "(").replace('" "', "")
def describe_introduction():
def getting_started(capsys):
intro = get_snippets("intro")
pip_install = intro.pop(0)
assert "pip install" in pip_install
assert "graphql-core" in pip_install
poetry_install = intro.pop(0)
assert "poetry install" in poetry_install
create_schema = intro.pop(0)
assert "schema = GraphQLSchema(" in create_schema
scope: Scope = {}
exec(create_schema, scope)
schema = scope.get("schema")
schema_class = scope.get("GraphQLSchema")
assert schema
assert schema_class
assert isinstance(schema, schema_class)
query = intro.pop(0)
assert "graphql_sync" in query
exec(query, scope)
out, err = capsys.readouterr()
assert out.startswith("ExecutionResult")
assert not err
expected_out = intro.pop(0)
assert out == expected_out
def describe_usage():
sdl = get_snippets("usage/schema")[0]
resolvers = get_snippets("usage/resolvers")[0]
def building_a_type_schema():
schema = get_snippets("usage/schema")
assert schema.pop(0) == sdl
assert "enum Episode { NEWHOPE, EMPIRE, JEDI }" in sdl
import_blocks = schema.pop(0)
assert "from graphql import" in import_blocks
assert "GraphQLObjectType" in import_blocks
scope: Scope = {}
exec(import_blocks, scope)
assert "GraphQLObjectType" in scope
build_enum = schema.pop(0)
assert "episode_enum = " in build_enum
exec(build_enum, scope)
assert scope["episode_enum"].values["EMPIRE"].value == 5
scope2 = scope.copy()
build_enum2 = schema.pop(0)
assert "episode_enum = " in build_enum2
exec(build_enum2, scope2)
assert scope["episode_enum"].values["EMPIRE"].value == 5
scope3 = scope.copy()
build_enum3 = schema.pop(0)
assert "episode_enum = " in build_enum3
exec(build_enum3, scope3)
assert scope["episode_enum"].values["EMPIRE"].value == 5
build_character = schema.pop(0)
assert "character_interface = " in build_character
exec(resolvers, scope)
exec(build_character, scope)
assert "character_interface" in scope
build_human_and_droid = schema.pop(0)
assert "human_type = " in build_human_and_droid
assert "droid_type = " in build_human_and_droid
exec(build_human_and_droid, scope)
assert "human_type" in scope
assert "droid_type" in scope
build_query_type = schema.pop(0)
assert "query_type = " in build_query_type
exec(build_query_type, scope)
assert "query_type" in scope
define_schema = schema.pop(0)
assert "schema = " in define_schema
exec(define_schema, scope)
def implementing_resolvers():
assert "luke = dict(" in resolvers
assert "def get_human(" in resolvers
scope: Scope = {}
exec(resolvers, scope)
get_human = scope["get_human"]
human = get_human(None, None, "1000")
assert human["name"] == "Luke Skywalker"
def executing_queries(capsys):
scope: Scope = {}
exec(resolvers, scope)
schema = "\n".join(get_snippets("usage/schema")[1:])
exec(schema, scope)
queries = get_snippets("usage/queries")
async_query = queries.pop(0)
assert "asyncio" in async_query
assert "graphql_sync" not in async_query
assert "asyncio.run" in async_query
from asyncio import run # noqa: F401
exec(async_query, scope)
out, err = capsys.readouterr()
assert not err
assert "R2-D2" in out
assert out == expected_result(queries)
sync_query = queries.pop(0)
assert "graphql_sync" in sync_query
assert "asyncio" not in sync_query
exec(sync_query, scope)
out, err = capsys.readouterr()
assert not err
assert "Luke" in out
assert out == expected_result(queries)
bad_query = queries.pop(0)
assert "homePlace" in bad_query
exec(bad_query, scope)
out, err = capsys.readouterr()
assert not err
assert "Cannot query" in out
assert out == expected_result(queries)
typename_query = queries.pop(0)
assert "__typename" in typename_query
exec(typename_query, scope)
out, err = capsys.readouterr()
assert not err
assert "__typename" in out
assert "Human" in out
assert out == expected_result(queries)
backstory_query = queries.pop(0)
assert "secretBackstory" in backstory_query
exec(backstory_query, scope)
out, err = capsys.readouterr()
assert not err
assert "errors" in out
assert "secretBackstory" in out
assert out == expected_result(queries)
def using_the_sdl(capsys):
use_sdl = get_snippets("usage/sdl")
build_schema = use_sdl.pop(0)
build_schema_sdl = dedent(
build_schema.partition('build_schema("""\n')[2].partition('""")')[0]
)
assert build_schema_sdl == sdl.rstrip()
scope: Scope = {}
exec(build_schema, scope)
schema = scope["schema"]
assert list(schema.query_type.fields) == ["hero", "human", "droid"]
exec(resolvers, scope)
assert schema.query_type.fields["hero"].resolve is None
attach_functions = use_sdl.pop(0)
exec(attach_functions, scope)
assert schema.query_type.fields["hero"].resolve is scope["get_hero"]
define_enum_values = use_sdl.pop(0)
define_episode_enum = get_snippets("usage/schema")[3]
define_episode_enum = define_episode_enum.partition("episode_enum =")[0]
assert "class EpisodeEnum" in define_episode_enum
exec(define_episode_enum, scope)
exec(define_enum_values, scope)
assert schema.get_type("Episode").values["EMPIRE"].value == 5
query = use_sdl.pop(0)
assert "graphql_sync" in query
assert "print(result)" in query
exec(query, scope)
out, err = capsys.readouterr()
assert not err
assert "Luke" in out
assert "appearsIn" in out
assert "EMPIRE" in out
assert out == expected_result(use_sdl)
def using_resolver_methods(capsys):
scope: Scope = {}
exec(resolvers, scope)
build_schema = get_snippets("usage/sdl")[0]
exec(build_schema, scope)
methods = get_snippets("usage/methods")
root_class = methods.pop(0)
assert root_class.startswith("class Root:")
assert "def human(self, info, id):" in root_class
exec(root_class, scope)
assert "Root" in scope
query = methods.pop(0)
assert "graphql_sync" in query
assert "Root()" in query
exec(query, scope)
out, err = capsys.readouterr()
assert not err
assert "R2-D2" in out
assert "primaryFunction" in out
assert "Astromech" in out
assert out == expected_result(methods)
def using_introspection(capsys):
introspect = get_snippets("usage/introspection")
get_query = introspect.pop(0)
assert "import get_introspection_query" in get_query
assert "descriptions=True" in get_query
scope: Scope = {}
exec(get_query, scope)
query = scope["query"]
assert query.lstrip().startswith("query IntrospectionQuery")
assert "description" in query
get_query = introspect.pop(0)
assert "descriptions=False" in get_query
scope2 = scope.copy()
exec(get_query, scope2)
query = scope2["query"]
assert query.lstrip().startswith("query IntrospectionQuery")
assert "description" not in query
exec(resolvers, scope)
create_schema = "\n".join(get_snippets("usage/schema")[1:])
exec(create_schema, scope)
get_result = introspect.pop(0)
assert "result = graphql_sync(" in get_result
exec(get_result, scope)
query_result = scope["introspection_query_result"]
assert query_result.errors is None
result = str(query_result.data)
result = "".join(result.split())
expected_result = introspect.pop(0)
result = "".join(result.split())
expected_result = "\n".join(expected_result.splitlines()[:7])
expected_result = "".join(expected_result.split())
assert result.startswith(expected_result)
build_schema = introspect.pop(0)
assert "schema = build_client_schema(" in build_schema
scope = {"introspection_query_result": query_result}
exec(build_schema, scope)
schema = scope["client_schema"]
assert list(schema.query_type.fields) == ["hero", "human", "droid"]
print_schema = introspect.pop(0)
scope = {"client_schema": schema}
assert "print_schema(" in print_schema
exec(print_schema, scope)
out, err = capsys.readouterr()
assert not err
assert "enum Episode {" in out
assert "id: String!" in out
assert "interface Character {" in out
assert "type Droid implements Character {" in out
assert "type Human implements Character {" in out
assert '"""A character in the Star Wars Trilogy"""' in out
assert '"""A humanoid creature in the Star Wars universe."""' in out
def parsing_graphql():
parser = get_snippets("usage/parser")
parse_document = parser.pop(0)
assert "document = parse(" in parse_document
scope: Scope = {}
exec(parse_document, scope)
document = scope["document"]
name = document.definitions[0].fields[0].name
assert name.value == "me"
assert str(name.loc) == "24:26"
parse_document2 = parser.pop(0)
assert "document = parse(" in parse_document2
assert "..., no_location=True" in parse_document2
parse_document = parse_document.replace('""")', '""", no_location=True)')
scope.clear()
exec(parse_document, scope)
document = scope["document"]
name = document.definitions[0].fields[0].name
assert name.value == "me"
assert name.loc is None
create_document = parser.pop(0)
assert "document = DocumentNode(" in create_document
assert "FieldDefinitionNode(" in create_document
assert "name=NameNode(value='me')," in create_document
scope = {}
exec(create_document, scope)
assert scope["document"] == document
def extending_a_schema(capsys):
scope: Scope = {}
exec(resolvers, scope)
create_schema = "\n".join(get_snippets("usage/schema")[1:])
exec(create_schema, scope)
extension = get_snippets("usage/extension")
extend_schema = extension.pop(0)
assert "extend_schema(" in extend_schema
exec(extend_schema, scope)
schema = scope["schema"]
human_type = schema.get_type("Human")
assert "lastName" in human_type.fields
attach_resolver = extension.pop(0)
exec(attach_resolver, scope)
assert human_type.fields["lastName"].resolve is scope["get_last_name"]
query = extension.pop(0)
assert "graphql_sync(" in query
exec(query, scope)
out, err = capsys.readouterr()
assert not err
assert "lastName" in out
assert "Skywalker" in out
assert out == expected_result(extension)
def validating_queries():
scope: Scope = {}
exec(resolvers, scope)
create_schema = "\n".join(get_snippets("usage/schema")[1:])
exec(create_schema, scope)
validator = get_snippets("usage/validator")
validate = validator.pop(0)
assert "errors = validate(" in validate
exec(validate, scope)
errors = str(scope["errors"])
assert errors == expected_errors(validator)