Skip to content

Commit 63c393c

Browse files
committed
test with AsyncioExecutor
1 parent 427cccb commit 63c393c

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed

tests/test_asyncio.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# flake8: noqa
2+
3+
import pytest
4+
5+
asyncio = pytest.importorskip("asyncio")
6+
7+
from graphql.execution.executors.asyncio import AsyncioExecutor
8+
from graphql.type.definition import (
9+
GraphQLField,
10+
GraphQLNonNull,
11+
GraphQLObjectType,
12+
)
13+
from graphql.type.scalars import GraphQLString
14+
from graphql.type.schema import GraphQLSchema
15+
from graphql_server import RequestParams, run_http_query
16+
from promise import Promise
17+
18+
from .utils import as_dicts
19+
20+
21+
def resolve_error_sync(_obj, _info):
22+
raise ValueError("error sync")
23+
24+
25+
@asyncio.coroutine
26+
def resolve_error_async(_obj, _info):
27+
yield from asyncio.sleep(0.001)
28+
raise ValueError("error async")
29+
30+
31+
def resolve_field_sync(_obj, _info):
32+
return "sync"
33+
34+
35+
@asyncio.coroutine
36+
def resolve_field_async(_obj, info):
37+
yield from asyncio.sleep(0.001)
38+
return "async"
39+
40+
41+
NonNullString = GraphQLNonNull(GraphQLString)
42+
43+
QueryRootType = GraphQLObjectType(
44+
name="QueryRoot",
45+
fields={
46+
"errorSync": GraphQLField(NonNullString, resolver=resolve_error_sync),
47+
"errorAsync": GraphQLField(NonNullString, resolver=resolve_error_async),
48+
"fieldSync": GraphQLField(NonNullString, resolver=resolve_field_sync),
49+
"fieldAsync": GraphQLField(NonNullString, resolver=resolve_field_async),
50+
},
51+
)
52+
53+
schema = GraphQLSchema(QueryRootType)
54+
55+
56+
def test_get_reponses_using_asyncioexecutor():
57+
class TestExecutor(AsyncioExecutor):
58+
called = False
59+
waited = False
60+
cleaned = False
61+
62+
def wait_until_finished(self):
63+
TestExecutor.waited = True
64+
super().wait_until_finished()
65+
66+
def clean(self):
67+
TestExecutor.cleaned = True
68+
super().clean()
69+
70+
def execute(self, fn, *args, **kwargs):
71+
TestExecutor.called = True
72+
return super().execute(fn, *args, **kwargs)
73+
74+
query = "{fieldSync fieldAsync}"
75+
76+
loop = asyncio.get_event_loop()
77+
78+
@asyncio.coroutine
79+
def get_results():
80+
result_promises, params = run_http_query(
81+
schema,
82+
"get",
83+
{},
84+
dict(query=query),
85+
executor=TestExecutor(loop=loop),
86+
return_promise=True,
87+
)
88+
results = yield from Promise.all(result_promises)
89+
return results, params
90+
91+
results, params = loop.run_until_complete(get_results())
92+
93+
expected_results = [{"data": {"fieldSync": "sync", "fieldAsync": "async"}}]
94+
95+
assert as_dicts(results) == expected_results
96+
assert params == [RequestParams(query=query, variables=None, operation_name=None)]
97+
assert TestExecutor.called
98+
assert not TestExecutor.waited
99+
assert TestExecutor.cleaned

tests/test_query.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
from pytest import raises
1616

1717
from .schema import schema
18-
19-
20-
def as_dicts(results):
21-
"""Convert execution results to a list of tuples of dicts for better comparison."""
22-
return [result.to_dict(dict_class=dict) for result in results]
18+
from .utils import as_dicts
2319

2420

2521
def test_request_params():

tests/utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def as_dicts(results):
2+
"""Convert execution results to a list of tuples of dicts for better comparison."""
3+
return [result.to_dict(dict_class=dict) for result in results]

tox.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ deps =
1010
graphql-core>=2.1,<3
1111
pytest-cov>=2.7
1212
commands =
13-
py{py,27,33,34,35,36,37}: py.test tests {posargs}
13+
py{py,27}: py.test tests {posargs} --ignore=tests/test_asyncio.py
14+
py{py3,33,34,35,36,37,38}: py.test tests {posargs}
1415

1516
[testenv:black]
1617
basepython=python3.7

0 commit comments

Comments
 (0)