Skip to content

Add graphiql options and missing flask context #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Provide same context tests to other integration
  • Loading branch information
KingDarBoja committed Jul 12, 2020
commit aa6c19a3ae03aea095905f98ec6045f0f30697b2
13 changes: 11 additions & 2 deletions tests/aiohttp/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ def resolve_raises(*_):
resolve=lambda obj, info, *args: info.context["request"].query.get("q"),
),
"context": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info, *args: info.context["request"],
GraphQLObjectType(
name="context",
fields={
"session": GraphQLField(GraphQLString),
"request": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info: info.context["request"],
),
},
),
resolve=lambda obj, info: info.context,
),
"test": GraphQLField(
type_=GraphQLString,
Expand Down
172 changes: 93 additions & 79 deletions tests/aiohttp/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ async def test_handles_unsupported_http_methods(client):
}


@pytest.mark.parametrize("app", [create_app()])
@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app()])
async def test_passes_request_into_request_context(app, client):
response = await client.get(url_string(query="{request}", q="testing"))

Expand All @@ -532,27 +532,42 @@ async def test_passes_request_into_request_context(app, client):
}


class TestCustomContext:
@pytest.mark.parametrize(
"app", [create_app(context="CUSTOM CONTEXT")],
)
@pytest.mark.asyncio
async def test_context_remapped(self, app, client):
response = await client.get(url_string(query="{context}"))
@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(context={"session": "CUSTOM CONTEXT"})])
async def test_passes_custom_context_into_context(app, client):
response = await client.get(url_string(query="{context { session request }}"))

_json = await response.json()
assert response.status == 200
assert "data" in _json
assert "session" in _json["data"]["context"]
assert "request" in _json["data"]["context"]
assert "CUSTOM CONTEXT" in _json["data"]["context"]["session"]
assert "Request" in _json["data"]["context"]["request"]

_json = await response.json()
assert response.status == 200
assert "Request" in _json["data"]["context"]
assert "CUSTOM CONTEXT" not in _json["data"]["context"]

@pytest.mark.parametrize("app", [create_app(context={"request": "test"})])
@pytest.mark.asyncio
async def test_request_not_replaced(self, app, client):
response = await client.get(url_string(query="{context}"))
@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(context="CUSTOM CONTEXT")])
async def test_context_remapped_if_not_mapping(app, client):
response = await client.get(url_string(query="{context { session request }}"))

_json = await response.json()
assert response.status == 200
assert _json["data"]["context"] == "test"
_json = await response.json()
assert response.status == 200
assert "data" in _json
assert "session" in _json["data"]["context"]
assert "request" in _json["data"]["context"]
assert "CUSTOM CONTEXT" not in _json["data"]["context"]["request"]
assert "Request" in _json["data"]["context"]["request"]


@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(context={"request": "test"})])
async def test_request_not_replaced(app, client):
response = await client.get(url_string(query="{context { request }}"))

_json = await response.json()
assert response.status == 200
assert _json["data"]["context"]["request"] == "test"


@pytest.mark.asyncio
Expand Down Expand Up @@ -583,69 +598,68 @@ async def test_post_multipart_data(client):
assert await response.json() == {"data": {u"writeTest": {u"test": u"Hello World"}}}


class TestBatchExecutor:
@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(batch=True)])
async def test_batch_allows_post_with_json_encoding(self, app, client):
response = await client.post(
"/graphql",
data=json.dumps([dict(id=1, query="{test}")]),
headers={"content-type": "application/json"},
)
@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(batch=True)])
async def test_batch_allows_post_with_json_encoding(app, client):
response = await client.post(
"/graphql",
data=json.dumps([dict(id=1, query="{test}")]),
headers={"content-type": "application/json"},
)

assert response.status == 200
assert await response.json() == [{"data": {"test": "Hello World"}}]

@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(batch=True)])
async def test_batch_supports_post_json_query_with_json_variables(
self, app, client
):
response = await client.post(
"/graphql",
data=json.dumps(
[
dict(
id=1,
query="query helloWho($who: String){ test(who: $who) }",
variables={"who": "Dolly"},
)
]
),
headers={"content-type": "application/json"},
)
assert response.status == 200
assert await response.json() == [{"data": {"test": "Hello World"}}]

assert response.status == 200
assert await response.json() == [{"data": {"test": "Hello Dolly"}}]

@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(batch=True)])
async def test_batch_allows_post_with_operation_name(self, app, client):
response = await client.post(
"/graphql",
data=json.dumps(
[
dict(
id=1,
query="""
query helloYou { test(who: "You"), ...shared }
query helloWorld { test(who: "World"), ...shared }
query helloDolly { test(who: "Dolly"), ...shared }
fragment shared on QueryRoot {
shared: test(who: "Everyone")
}
""",
operationName="helloWorld",
)
]
),
headers={"content-type": "application/json"},
)

assert response.status == 200
assert await response.json() == [
{"data": {"test": "Hello World", "shared": "Hello Everyone"}}
]
@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(batch=True)])
async def test_batch_supports_post_json_query_with_json_variables(app, client):
response = await client.post(
"/graphql",
data=json.dumps(
[
dict(
id=1,
query="query helloWho($who: String){ test(who: $who) }",
variables={"who": "Dolly"},
)
]
),
headers={"content-type": "application/json"},
)

assert response.status == 200
assert await response.json() == [{"data": {"test": "Hello Dolly"}}]


@pytest.mark.asyncio
@pytest.mark.parametrize("app", [create_app(batch=True)])
async def test_batch_allows_post_with_operation_name(app, client):
response = await client.post(
"/graphql",
data=json.dumps(
[
dict(
id=1,
query="""
query helloYou { test(who: "You"), ...shared }
query helloWorld { test(who: "World"), ...shared }
query helloDolly { test(who: "Dolly"), ...shared }
fragment shared on QueryRoot {
shared: test(who: "Everyone")
}
""",
operationName="helloWorld",
)
]
),
headers={"content-type": "application/json"},
)

assert response.status == 200
assert await response.json() == [
{"data": {"test": "Hello World", "shared": "Hello Everyone"}}
]


@pytest.mark.asyncio
Expand Down
35 changes: 14 additions & 21 deletions tests/flask/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,35 +491,28 @@ def test_passes_request_into_request_context(app, client):

@pytest.mark.parametrize("app", [create_app(context={"session": "CUSTOM CONTEXT"})])
def test_passes_custom_context_into_context(app, client):
response = client.get(url_string(app, query="{context { session }}"))
response = client.get(url_string(app, query="{context { session request }}"))

print(response_json(response))
assert response.status_code == 200
assert "data" in response_json(response)
assert "session" in response_json(response)["data"]["context"]
assert "CUSTOM CONTEXT" in response_json(response)["data"]["context"]["session"]


@pytest.mark.parametrize("app", [create_app(context={"session": "CUSTOM CONTEXT"})])
def test_context_remapped_if_mapping(app, client):
response = client.get(url_string(app, query="{context { request }}"))

print(response_json(response))
assert response.status_code == 200
assert "data" in response_json(response)
assert "request" in response_json(response)["data"]["context"]
assert "Request" in response_json(response)["data"]["context"]["request"]
res = response_json(response)
assert "data" in res
assert "session" in res["data"]["context"]
assert "request" in res["data"]["context"]
assert "CUSTOM CONTEXT" in res["data"]["context"]["session"]
assert "Request" in res["data"]["context"]["request"]


@pytest.mark.parametrize("app", [create_app(context="CUSTOM CONTEXT")])
def test_context_remapped_if_not_mapping(app, client):
response = client.get(url_string(app, query="{context { request }}"))
response = client.get(url_string(app, query="{context { session request }}"))

print(response_json(response))
assert response.status_code == 200
assert "data" in response_json(response)
assert "request" in response_json(response)["data"]["context"]
assert "CUSTOM CONTEXT" not in response_json(response)["data"]["context"]["request"]
res = response_json(response)
assert "data" in res
assert "session" in res["data"]["context"]
assert "request" in res["data"]["context"]
assert "CUSTOM CONTEXT" not in res["data"]["context"]["request"]
assert "Request" in res["data"]["context"]["request"]


def test_post_multipart_data(app, client):
Expand Down
13 changes: 11 additions & 2 deletions tests/sanic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ def resolve_raises(*_):
resolve=lambda obj, info: info.context["request"].args.get("q"),
),
"context": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info: info.context["request"],
GraphQLObjectType(
name="context",
fields={
"session": GraphQLField(GraphQLString),
"request": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info: info.context["request"],
),
},
),
resolve=lambda obj, info: info.context,
),
"test": GraphQLField(
type_=GraphQLString,
Expand Down
29 changes: 23 additions & 6 deletions tests/sanic/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,30 @@ def test_passes_request_into_request_context(app):
assert response_json(response) == {"data": {"request": "testing"}}


@pytest.mark.parametrize("app", [create_app(context="CUSTOM CONTEXT")])
def test_supports_pretty_printing_on_custom_context_response(app):
_, response = app.client.get(uri=url_string(query="{context}"))
@pytest.mark.parametrize("app", [create_app(context={"session": "CUSTOM CONTEXT"})])
def test_passes_custom_context_into_context(app):
_, response = app.client.get(uri=url_string(query="{context { session request }}"))

assert response.status == 200
assert "data" in response_json(response)
assert response_json(response)["data"]["context"] == "<Request: GET /graphql>"
assert response.status_code == 200
res = response_json(response)
assert "data" in res
assert "session" in res["data"]["context"]
assert "request" in res["data"]["context"]
assert "CUSTOM CONTEXT" in res["data"]["context"]["session"]
assert "Request" in res["data"]["context"]["request"]


@pytest.mark.parametrize("app", [create_app(context="CUSTOM CONTEXT")])
def test_context_remapped_if_not_mapping(app):
_, response = app.client.get(uri=url_string(query="{context { session request }}"))

assert response.status_code == 200
res = response_json(response)
assert "data" in res
assert "session" in res["data"]["context"]
assert "request" in res["data"]["context"]
assert "CUSTOM CONTEXT" not in res["data"]["context"]["request"]
assert "Request" in res["data"]["context"]["request"]


@pytest.mark.parametrize("app", [create_app()])
Expand Down
13 changes: 11 additions & 2 deletions tests/webob/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ def resolve_raises(*_):
resolve=lambda obj, info: info.context["request"].params.get("q"),
),
"context": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info: info.context["request"],
GraphQLObjectType(
name="context",
fields={
"session": GraphQLField(GraphQLString),
"request": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda obj, info: info.context["request"],
),
},
),
resolve=lambda obj, info: info.context,
),
"test": GraphQLField(
type_=GraphQLString,
Expand Down
28 changes: 21 additions & 7 deletions tests/webob/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,30 @@ def test_passes_request_into_request_context(client):
assert response_json(response) == {"data": {"request": "testing"}}


@pytest.mark.parametrize("settings", [dict(context={"session": "CUSTOM CONTEXT"})])
def test_passes_custom_context_into_context(client, settings):
response = client.get(url_string(query="{context { session request }}"))

assert response.status_code == 200
res = response_json(response)
assert "data" in res
assert "session" in res["data"]["context"]
assert "request" in res["data"]["context"]
assert "CUSTOM CONTEXT" in res["data"]["context"]["session"]
assert "request" in res["data"]["context"]["request"]


@pytest.mark.parametrize("settings", [dict(context="CUSTOM CONTEXT")])
def test_supports_custom_context(client, settings):
response = client.get(url_string(query="{context}"))
def test_context_remapped_if_not_mapping(client, settings):
response = client.get(url_string(query="{context { session request }}"))

assert response.status_code == 200
assert "data" in response_json(response)
assert (
response_json(response)["data"]["context"]
== "GET /graphql?query=%7Bcontext%7D HTTP/1.0\r\nHost: localhost:80"
)
res = response_json(response)
assert "data" in res
assert "session" in res["data"]["context"]
assert "request" in res["data"]["context"]
assert "CUSTOM CONTEXT" not in res["data"]["context"]["request"]
assert "request" in res["data"]["context"]["request"]


def test_post_multipart_data(client):
Expand Down