Debian Codegraph @

tests/execution/test_sync.py

raw
1 from gc import collect
2 from inspect import isawaitable
3
4 from pytest import mark, raises
5
6 from graphql import graphql_sync
7 from graphql.execution import execute, execute_sync
8 from graphql.language import parse
9 from graphql.type import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLString
10 from graphql.validation import validate
11
12
13 def describe_execute_synchronously_when_possible():
14 def _resolve_sync(root_value, _info):
15 return root_value
16
17 async def _resolve_async(root_value, _info):
18 return root_value
19
20 schema = GraphQLSchema(
21 GraphQLObjectType(
22 "Query",
23 {
24 "syncField": GraphQLField(GraphQLString, resolve=_resolve_sync),
25 "asyncField": GraphQLField(GraphQLString, resolve=_resolve_async),
26 },
27 ),
28 GraphQLObjectType(
29 "Mutation",
30 {"syncMutationField": GraphQLField(GraphQLString, resolve=_resolve_sync)},
31 ),
32 )
33
34 def does_not_return_an_awaitable_for_initial_errors():
35 doc = "fragment Example on Query { syncField }"
36 assert execute(schema, parse(doc), "rootValue") == (
37 None,
38 [{"message": "Must provide an operation."}],
39 )
40
41 def does_not_return_an_awaitable_if_fields_are_all_synchronous():
42 doc = "query Example { syncField }"
43 assert execute(schema, parse(doc), "rootValue") == (
44 {"syncField": "rootValue"},
45 None,
46 )
47
48 def does_not_return_an_awaitable_if_mutation_fields_are_all_synchronous():
49 doc = "mutation Example { syncMutationField }"
50 assert execute(schema, parse(doc), "rootValue") == (
51 {"syncMutationField": "rootValue"},
52 None,
53 )
54
55 @mark.asyncio
56 async def returns_an_awaitable_if_any_field_is_asynchronous():
57 doc = "query Example { syncField, asyncField }"
58 result = execute(schema, parse(doc), "rootValue")
59 assert isawaitable(result)
60 assert await result == (
61 {"syncField": "rootValue", "asyncField": "rootValue"},
62 None,
63 )
64
65 def describe_execute_sync():
66 def does_not_return_an_awaitable_for_sync_execution():
67 doc = "query Example { syncField }"
68 result = execute_sync(schema, document=parse(doc), root_value="rootValue")
69 assert result == (
70 {"syncField": "rootValue"},
71 None,
72 )
73
74 def does_not_throw_if_not_encountering_async_execution_with_check_sync():
75 doc = "query Example { syncField }"
76 result = execute_sync(
77 schema, document=parse(doc), root_value="rootValue", check_sync=True
78 )
79 assert result == (
80 {"syncField": "rootValue"},
81 None,
82 )
83
84 @mark.asyncio
85 @mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
86 async def throws_if_encountering_async_execution_with_check_sync():
87 doc = "query Example { syncField, asyncField }"
88 with raises(RuntimeError) as exc_info:
89 execute_sync(
90 schema, document=parse(doc), root_value="rootValue", check_sync=True
91 )
92 msg = str(exc_info.value)
93 assert msg == "GraphQL execution failed to complete synchronously."
94
95 @mark.asyncio
96 @mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
97 async def throws_if_encountering_async_operation_without_check_sync():
98 doc = "query Example { syncField, asyncField }"
99 result = execute_sync(schema, document=parse(doc), root_value="rootValue")
100 assert result == (
101 {"syncField": "rootValue", "asyncField": None},
102 [
103 {
104 "message": "String cannot represent value:"
105 " <coroutine _resolve_async>",
106 "locations": [(1, 28)],
107 "path": ["asyncField"],
108 }
109 ],
110 )
111 # garbage collect coroutine in order to not postpone the warning
112 del result
113 collect()
114
115 def describe_graphql_sync():
116 def reports_errors_raised_during_schema_validation():
117 bad_schema = GraphQLSchema()
118 result = graphql_sync(schema=bad_schema, source="{ __typename }")
119 assert result == (None, [{"message": "Query root type must be provided."}])
120
121 def does_not_return_an_awaitable_for_syntax_errors():
122 doc = "fragment Example on Query { { { syncField }"
123 assert graphql_sync(schema, doc) == (
124 None,
125 [
126 {
127 "message": "Syntax Error: Expected Name, found '{'.",
128 "locations": [(1, 29)],
129 }
130 ],
131 )
132
133 def does_not_return_an_awaitable_for_validation_errors():
134 doc = "fragment Example on Query { unknownField }"
135 validation_errors = validate(schema, parse(doc))
136 result = graphql_sync(schema, doc)
137 assert result == (None, validation_errors)
138
139 def does_not_return_an_awaitable_for_sync_execution():
140 doc = "query Example { syncField }"
141 assert graphql_sync(schema, doc, "rootValue") == (
142 {"syncField": "rootValue"},
143 None,
144 )
145
146 def does_not_throw_if_not_encountering_async_operation_with_check_sync():
147 doc = "query Example { syncField }"
148 assert graphql_sync(schema, doc, "rootValue") == (
149 {"syncField": "rootValue"},
150 None,
151 )
152
153 @mark.asyncio
154 @mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
155 async def throws_if_encountering_async_operation_with_check_sync():
156 doc = "query Example { syncField, asyncField }"
157 with raises(RuntimeError) as exc_info:
158 graphql_sync(schema, doc, "rootValue", check_sync=True)
159 msg = str(exc_info.value)
160 assert msg == "GraphQL execution failed to complete synchronously."
161
162 @mark.asyncio
163 @mark.filterwarnings("ignore:.* was never awaited:RuntimeWarning")
164 async def throws_if_encountering_async_operation_without_check_sync():
165 doc = "query Example { syncField, asyncField }"
166 result = graphql_sync(schema, doc, "rootValue")
167 assert result == (
168 {"syncField": "rootValue", "asyncField": None},
169 [
170 {
171 "message": "String cannot represent value:"
172 " <coroutine _resolve_async>",
173 "locations": [(1, 28)],
174 "path": ["asyncField"],
175 }
176 ],
177 )
178 # garbage collect coroutine in order to not postpone the warning
179 del result
180 collect()