Skip to content

Repair outdated tests, add build on Travis CI #13

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: python
os:
- linux
python:
- 3.6
- 3.7
- 3.8-dev
- pypy3
cache:
directories:
- $HOME/.cache/pip
install:
- pip install -e .[test]
- pip install pytest-cov codecov
script:
- python -m pytest --cov-branch --cov=aiohttp_graphql --cov-report=term-missing
after_success:
- codecov
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Adds [GraphQL] support to your [aiohttp] application.

Based on [flask-graphql] by [Syrus Akbary] and [sanic-graphql] by [Sergey Porivaev].

[![TravisCI Build Status](https://github.com/graphql-python/aiohttp-graphql.svg?branch=master)](https://github.com/graphql-python/aiohttp-graphql)
[![codecov](https://github.com/graphql-python/aiohttp-graphql/branch/master/graph/badge.svg)](https://github.com/graphql-python/aiohttp-graphql)

## Usage
Just use the `GraphQLView` view from `aiohttp_graphql`

Expand Down
19 changes: 12 additions & 7 deletions aiohttp_graphql/graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ async def __call__(self, request):
)

except HttpQueryError as err:
if err.headers and 'Allow' in err.headers:
# bug in graphql_server.execute_graphql_request
# https://github.com/graphql-python/graphql-server-core/pull/4
if isinstance(err.headers['Allow'], list):
err.headers['Allow'] = ', '.join(err.headers['Allow'])

return web.Response(
text=self.encoder({
'errors': [self.error_formatter(err)]
Expand Down Expand Up @@ -202,4 +196,15 @@ def process_preflight(self, request):
def attach(cls, app, *, route_path='/graphql', route_name='graphql',
**kwargs):
view = cls(**kwargs)
app.router.add_route('*', route_path, view, name=route_name)
app.router.add_route('*', route_path, _asyncify(view), name=route_name)


def _asyncify(handler):
"""
This is mainly here because ``aiohttp`` can't infer the async definition of
:py:meth:`.GraphQLView.__call__` and raises a :py:class:`DeprecationWarning`
in tests. Wrapping it into an async function avoids the noisy warning.
"""
async def _dispatch(request):
return await handler(request)
return _dispatch
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ norecursedirs =
.git
.tox
testpaths = tests/
filterwarnings =
ignore:(context|root)_value has been deprecated.*:DeprecationWarning:graphql.backend.core:32
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def view_kwargs():

# aiohttp Fixtures
@pytest.fixture
def app(event_loop, executor, view_kwargs):
app = web.Application(loop=event_loop)
def app(executor, view_kwargs):
app = web.Application()
GraphQLView.attach(app, executor=executor, **view_kwargs)
return app


@pytest.fixture
async def client(event_loop, app):
client = aiohttp.test_utils.TestClient(app, loop=event_loop)
client = aiohttp.test_utils.TestClient(aiohttp.test_utils.TestServer(app), loop=event_loop)
await client.start_server()
yield client
await client.close()
Expand Down
28 changes: 23 additions & 5 deletions tests/test_graphqlview.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ async def test_handles_field_errors_caught_by_graphql(client, url_builder):
assert await response.json() == {
'data': None,
'errors': [
{'locations': [{'column': 2, 'line': 1}], 'message': 'Throws!'},
{'locations': [{'column': 2, 'line': 1}], 'message': 'Throws!', 'path': ['thrower']},
],
}

Expand All @@ -447,8 +447,7 @@ async def test_handles_syntax_errors_caught_by_graphql(client, url_builder):
{
'locations': [{'column': 1, 'line': 1}],
'message': (
'Syntax Error GraphQL request (1:1) '
'Unexpected Name "syntaxerror"\n\n1: syntaxerror\n ^\n'
'Syntax Error GraphQL (1:1) Unexpected Name "syntaxerror"\n\n1: syntaxerror\n ^\n'
),
},
],
Expand Down Expand Up @@ -545,12 +544,18 @@ async def test_passes_request_into_request_context(client, url_builder):

class TestCustomContext:
@pytest.fixture
def view_kwargs(self, view_kwargs):
def view_kwargs(self, request, view_kwargs):
# pylint: disable=no-self-use
# pylint: disable=redefined-outer-name
view_kwargs.update(context='CUSTOM CONTEXT')
view_kwargs.update(context=request.param)
return view_kwargs

@pytest.mark.parametrize(
'view_kwargs',
['CUSTOM CONTEXT', {'CUSTOM_CONTEXT': 'test'}],
indirect=True,
ids=repr
)
@pytest.mark.asyncio
async def test_context_remapped(self, client, url_builder):
response = await client.get(url_builder(query='{context}'))
Expand All @@ -561,6 +566,19 @@ async def test_context_remapped(self, client, url_builder):
assert 'CUSTOM CONTEXT' not in _json['data']['context']


@pytest.mark.parametrize(
'view_kwargs', [{'request': 'test'}], indirect=True, ids=repr
)
@pytest.mark.asyncio
async def test_request_not_replaced(self, client, url_builder):
response = await client.get(url_builder(query='{context}'))

_json = await response.json()
assert response.status == 200
assert 'request' in _json['data']['context']
assert _json['data']['context'] == str({'request': 'test'})


@pytest.mark.asyncio
async def test_post_multipart_data(client, base_url):
# pylint: disable=line-too-long
Expand Down