Skip to content

Commit 3fb7a4e

Browse files
committed
test with AsyncioExecutor
1 parent 427cccb commit 3fb7a4e

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

tests/test_asyncio.py

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

0 commit comments

Comments
 (0)