Skip to content

Commit 559d4ae

Browse files
committed
chore: add tests for sanic and aiohttp jinja graphiql render
1 parent 3e8685f commit 559d4ae

File tree

8 files changed

+89
-158
lines changed

8 files changed

+89
-158
lines changed

tests/aiohttp/app.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from aiohttp import web
44

55
from graphql_server.aiohttp import GraphQLView
6-
from tests.aiohttp.schema import Schema
6+
7+
from .schema import Schema
78

89

910
def create_app(schema=Schema, **kwargs):
@@ -13,10 +14,5 @@ def create_app(schema=Schema, **kwargs):
1314
return app
1415

1516

16-
def url_string(**url_params):
17-
base_url = "/graphql"
18-
19-
if url_params:
20-
return f"{base_url}?{urlencode(url_params)}"
21-
22-
return base_url
17+
def url_string(url="/graphql", **url_params):
18+
return f"{url}?{urlencode(url_params)}" if url_params else url

tests/aiohttp/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
import pytest_asyncio
3+
from aiohttp.test_utils import TestClient, TestServer
4+
5+
from .app import create_app
6+
7+
8+
@pytest.fixture
9+
def app():
10+
return create_app()
11+
12+
13+
@pytest_asyncio.fixture
14+
async def client(app):
15+
client = TestClient(TestServer(app))
16+
await client.start_server()
17+
yield client
18+
await client.close()

tests/aiohttp/test_graphiqlview.py

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,35 @@
11
import pytest
2-
import pytest_asyncio
3-
from aiohttp.test_utils import TestClient, TestServer
42
from jinja2 import Environment
53

6-
from tests.aiohttp.app import create_app, url_string
7-
from tests.aiohttp.schema import AsyncSchema, Schema, SyncSchema
8-
9-
10-
@pytest.fixture
11-
def app():
12-
app = create_app()
13-
return app
14-
15-
16-
@pytest_asyncio.fixture
17-
async def client(app):
18-
client = TestClient(TestServer(app))
19-
await client.start_server()
20-
yield client
21-
await client.close()
22-
23-
24-
@pytest.fixture
25-
def view_kwargs():
26-
return {
27-
"schema": Schema,
28-
"graphiql": True,
29-
}
30-
31-
32-
@pytest.fixture
33-
def pretty_response():
34-
return (
35-
"{\n"
36-
' "data": {\n'
37-
' "test": "Hello World"\n'
38-
" }\n"
39-
"}".replace('"', '\\"').replace("\n", "\\n")
40-
)
4+
from .app import create_app, url_string
5+
from .schema import AsyncSchema, SyncSchema
416

427

438
@pytest.mark.asyncio
44-
@pytest.mark.parametrize("app", [create_app(graphiql=True)])
9+
@pytest.mark.parametrize(
10+
"app",
11+
[
12+
create_app(graphiql=True),
13+
create_app(graphiql=True, jinja_env=Environment()),
14+
create_app(graphiql=True, jinja_env=Environment(enable_async=True)),
15+
],
16+
)
4517
async def test_graphiql_is_enabled(app, client):
46-
response = await client.get(
47-
url_string(query="{test}"), headers={"Accept": "text/html"}
48-
)
49-
assert response.status == 200
50-
51-
52-
@pytest.mark.asyncio
53-
@pytest.mark.parametrize("app", [create_app(graphiql=True)])
54-
async def test_graphiql_simple_renderer(app, client, pretty_response):
5518
response = await client.get(
5619
url_string(query="{test}"),
5720
headers={"Accept": "text/html"},
5821
)
5922
assert response.status == 200
60-
assert pretty_response in await response.text()
61-
6223

63-
class TestJinjaEnv:
64-
@pytest.mark.asyncio
65-
@pytest.mark.parametrize(
66-
"app", [create_app(graphiql=True, jinja_env=Environment(enable_async=True))]
24+
pretty_response = (
25+
"{\n"
26+
' "data": {\n'
27+
' "test": "Hello World"\n'
28+
" }\n"
29+
"}".replace('"', '\\"').replace("\n", "\\n")
6730
)
68-
async def test_graphiql_jinja_renderer_async(self, app, client, pretty_response):
69-
response = await client.get(
70-
url_string(query="{test}"),
71-
headers={"Accept": "text/html"},
72-
)
73-
assert response.status == 200
74-
assert pretty_response in await response.text()
31+
32+
assert pretty_response in await response.text()
7533

7634

7735
@pytest.mark.asyncio
@@ -84,7 +42,10 @@ async def test_graphiql_html_is_not_accepted(client):
8442

8543

8644
@pytest.mark.asyncio
87-
@pytest.mark.parametrize("app", [create_app(graphiql=True)])
45+
@pytest.mark.parametrize(
46+
"app",
47+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
48+
)
8849
async def test_graphiql_get_mutation(app, client):
8950
response = await client.get(
9051
url_string(query="mutation TestMutation { writeTest { test } }"),
@@ -95,7 +56,10 @@ async def test_graphiql_get_mutation(app, client):
9556

9657

9758
@pytest.mark.asyncio
98-
@pytest.mark.parametrize("app", [create_app(graphiql=True)])
59+
@pytest.mark.parametrize(
60+
"app",
61+
[create_app(graphiql=True), create_app(graphiql=True, jinja_env=Environment())],
62+
)
9963
async def test_graphiql_get_subscriptions(app, client):
10064
response = await client.get(
10165
url_string(
@@ -109,7 +73,16 @@ async def test_graphiql_get_subscriptions(app, client):
10973

11074
@pytest.mark.asyncio
11175
@pytest.mark.parametrize(
112-
"app", [create_app(schema=AsyncSchema, enable_async=True, graphiql=True)]
76+
"app",
77+
[
78+
create_app(schema=AsyncSchema, enable_async=True, graphiql=True),
79+
create_app(
80+
schema=AsyncSchema,
81+
enable_async=True,
82+
graphiql=True,
83+
jinja_env=Environment(),
84+
),
85+
],
11386
)
11487
async def test_graphiql_enabled_async_schema(app, client):
11588
response = await client.get(
@@ -136,7 +109,13 @@ async def test_graphiql_enabled_async_schema(app, client):
136109

137110
@pytest.mark.asyncio
138111
@pytest.mark.parametrize(
139-
"app", [create_app(schema=SyncSchema, enable_async=True, graphiql=True)]
112+
"app",
113+
[
114+
create_app(schema=SyncSchema, enable_async=True, graphiql=True),
115+
create_app(
116+
schema=SyncSchema, enable_async=True, graphiql=True, jinja_env=Environment()
117+
),
118+
],
140119
)
141120
async def test_graphiql_enabled_sync_schema(app, client):
142121
response = await client.get(

tests/aiohttp/test_graphqlview.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22
from urllib.parse import urlencode
33

44
import pytest
5-
import pytest_asyncio
65
from aiohttp import FormData
7-
from aiohttp.test_utils import TestClient, TestServer
86

97
from ..utils import RepeatExecutionContext
108
from .app import create_app, url_string
119
from .schema import AsyncSchema
1210

1311

14-
@pytest.fixture
15-
def app():
16-
app = create_app()
17-
return app
18-
19-
20-
@pytest_asyncio.fixture
21-
async def client(app):
22-
client = TestClient(TestServer(app))
23-
await client.start_server()
24-
yield client
25-
await client.close()
26-
27-
2812
@pytest.mark.asyncio
2913
async def test_allows_get_with_query_param(client):
3014
response = await client.get(url_string(query="{test}"))

tests/sanic/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ def create_app(path="/graphql", schema=Schema, **kwargs):
1717
return app
1818

1919

20-
def url_string(uri="/graphql", **url_params):
21-
return f"{uri}?{urlencode(url_params)}" if url_params else uri
20+
def url_string(url="/graphql", **url_params):
21+
return f"{url}?{urlencode(url_params)}" if url_params else url

tests/sanic/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
from .app import create_app
4+
5+
6+
@pytest.fixture
7+
def app():
8+
return create_app()

tests/sanic/test_graphiqlview.py

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,29 @@
55
from .schema import AsyncSchema, SyncSchema
66

77

8-
@pytest.fixture
9-
def pretty_response():
10-
return (
11-
"{\n"
12-
' "data": {\n'
13-
' "test": "Hello World"\n'
14-
" }\n"
15-
"}".replace('"', '\\"').replace("\n", "\\n")
16-
)
17-
18-
19-
@pytest.mark.parametrize("app", [create_app(graphiql=True)])
8+
@pytest.mark.parametrize(
9+
"app",
10+
[
11+
create_app(graphiql=True),
12+
create_app(graphiql=True, jinja_env=Environment()),
13+
create_app(graphiql=True, jinja_env=Environment(enable_async=True)),
14+
],
15+
)
2016
def test_graphiql_is_enabled(app):
2117
_, response = app.test_client.get(
2218
uri=url_string(query="{test}"), headers={"Accept": "text/html"}
2319
)
24-
assert response.status == 200
2520

26-
27-
@pytest.mark.parametrize("app", [create_app(graphiql=True)])
28-
def test_graphiql_simple_renderer(app, pretty_response):
29-
_, response = app.test_client.get(
30-
uri=url_string(query="{test}"), headers={"Accept": "text/html"}
31-
)
3221
assert response.status == 200
33-
assert pretty_response in response.body.decode("utf-8")
3422

35-
36-
@pytest.mark.parametrize("app", [create_app(graphiql=True, jinja_env=Environment())])
37-
def test_graphiql_jinja_renderer(app, pretty_response):
38-
_, response = app.test_client.get(
39-
uri=url_string(query="{test}"), headers={"Accept": "text/html"}
23+
pretty_response = (
24+
"{\n"
25+
' "data": {\n'
26+
' "test": "Hello World"\n'
27+
" }\n"
28+
"}".replace('"', '\\"').replace("\n", "\\n")
4029
)
41-
assert response.status == 200
42-
assert pretty_response in response.body.decode("utf-8")
43-
4430

45-
@pytest.mark.parametrize(
46-
"app", [create_app(graphiql=True, jinja_env=Environment(enable_async=True))]
47-
)
48-
def test_graphiql_jinja_async_renderer(app, pretty_response):
49-
_, response = app.test_client.get(
50-
uri=url_string(query="{test}"), headers={"Accept": "text/html"}
51-
)
52-
assert response.status == 200
5331
assert pretty_response in response.body.decode("utf-8")
5432

5533

0 commit comments

Comments
 (0)