Skip to content

Commit

Permalink
converte some files to async/await syntax (aio-libs#2392)
Browse files Browse the repository at this point in the history
* convert test_web_response.py to use async/await syntax

* convert test_urldispatch.py to use async/await syntax

* convert test_web_sendfile_functional.py to use async/await syntax

* convert test_web_protocol.py to use async/await syntax

* convert test_route_def.py to use async/await syntax

* convert test_pytest_plugin.py to use async/await syntax

* convert test_resolver.py to use async/await syntax

* convert web_middlewares.py to use async/await syntax
  • Loading branch information
jairhenrique authored and asvetlov committed Oct 25, 2017
1 parent bfcf9e9 commit 9c0f73a
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 663 deletions.
13 changes: 5 additions & 8 deletions aiohttp/web_middlewares.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import re

from aiohttp.web_exceptions import HTTPMovedPermanently
Expand All @@ -11,11 +10,10 @@
)


@asyncio.coroutine
def _check_request_resolves(request, path):
async def _check_request_resolves(request, path):
alt_request = request.clone(rel_url=path)

match_info = yield from request.app.router.resolve(alt_request)
match_info = await request.app.router.resolve(alt_request)
alt_request._match_info = match_info

if not isinstance(match_info.route, SystemRoute):
Expand Down Expand Up @@ -52,9 +50,8 @@ def normalize_path_middleware(
path into one.
"""

@asyncio.coroutine
@middleware
def normalize_path_middleware(request, handler):
async def normalize_path_middleware(request, handler):
if isinstance(request.match_info.route, SystemRoute):
paths_to_check = []
if '?' in request.raw_path:
Expand All @@ -74,11 +71,11 @@ def normalize_path_middleware(request, handler):
re.sub('//+', '/', path + '/'))

for path in paths_to_check:
resolves, request = yield from _check_request_resolves(
resolves, request = await _check_request_resolves(
request, path)
if resolves:
return redirect_class(request.path + query)

return (yield from handler(request))
return (await handler(request))

return normalize_path_middleware
108 changes: 43 additions & 65 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

def test_aiohttp_plugin(testdir):
testdir.makepyfile("""\
import asyncio
import pytest
from unittest import mock
Expand All @@ -21,8 +20,7 @@ def test_aiohttp_plugin(testdir):
pytest_plugins = 'aiohttp.pytest_plugin'
@asyncio.coroutine
def hello(request):
async def hello(request):
return web.Response(body=b'Hello, world')
Expand All @@ -32,74 +30,65 @@ def create_app(loop):
return app
@asyncio.coroutine
def test_hello(test_client):
client = yield from test_client(create_app)
resp = yield from client.get('/')
async def test_hello(test_client):
client = await test_client(create_app)
resp = await client.get('/')
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert 'Hello, world' in text
@asyncio.coroutine
def test_hello_from_app(test_client, loop):
async def test_hello_from_app(test_client, loop):
app = web.Application()
app.router.add_get('/', hello)
client = yield from test_client(app)
resp = yield from client.get('/')
client = await test_client(app)
resp = await client.get('/')
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert 'Hello, world' in text
@asyncio.coroutine
def test_hello_with_loop(test_client, loop):
client = yield from test_client(create_app)
resp = yield from client.get('/')
async def test_hello_with_loop(test_client, loop):
client = await test_client(create_app)
resp = await client.get('/')
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert 'Hello, world' in text
@asyncio.coroutine
def test_hello_fails(test_client):
client = yield from test_client(create_app)
resp = yield from client.get('/')
async def test_hello_fails(test_client):
client = await test_client(create_app)
resp = await client.get('/')
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert 'Hello, wield' in text
@asyncio.coroutine
def test_hello_with_fake_loop(test_client):
async def test_hello_with_fake_loop(test_client):
with pytest.raises(AssertionError):
fake_loop = mock.Mock()
yield from test_client(web.Application(loop=fake_loop))
await test_client(web.Application(loop=fake_loop))
@asyncio.coroutine
def test_set_args(test_client, loop):
async def test_set_args(test_client, loop):
with pytest.raises(AssertionError):
app = web.Application()
yield from test_client(app, 1, 2, 3)
await test_client(app, 1, 2, 3)
@asyncio.coroutine
def test_set_keyword_args(test_client, loop):
async def test_set_keyword_args(test_client, loop):
app = web.Application()
with pytest.raises(TypeError):
yield from test_client(app, param=1)
await test_client(app, param=1)
@asyncio.coroutine
def test_noop():
async def test_noop():
pass
@asyncio.coroutine
def previous(request):
async def previous(request):
if request.method == 'POST':
request.app['value'] = (yield from request.post())['value']
request.app['value'] = (await request.post())['value']
return web.Response(body=b'thanks for the data')
else:
v = request.app.get('value', 'unknown')
Expand All @@ -117,40 +106,37 @@ def cli(loop, test_client):
return loop.run_until_complete(test_client(create_stateful_app))
@asyncio.coroutine
def test_set_value(cli):
resp = yield from cli.post('/', data={'value': 'foo'})
async def test_set_value(cli):
resp = await cli.post('/', data={'value': 'foo'})
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert text == 'thanks for the data'
assert cli.server.app['value'] == 'foo'
@asyncio.coroutine
def test_get_value(cli):
resp = yield from cli.get('/')
async def test_get_value(cli):
resp = await cli.get('/')
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert text == 'value: unknown'
cli.server.app['value'] = 'bar'
resp = yield from cli.get('/')
resp = await cli.get('/')
assert resp.status == 200
text = yield from resp.text()
text = await resp.text()
assert text == 'value: bar'
def test_noncoro():
assert True
@asyncio.coroutine
def test_client_failed_to_create(test_client):
async def test_client_failed_to_create(test_client):
def make_app(loop):
raise RuntimeError()
with pytest.raises(RuntimeError):
yield from test_client(make_app)
await test_client(make_app)
""")
testdir.runpytest('-p', 'no:sugar')
Expand All @@ -163,7 +149,6 @@ def make_app(loop):
@pytest.mark.skipif(sys.version_info < (3, 5), reason='old python')
def test_warning_checks(testdir, capsys):
testdir.makepyfile("""\
import asyncio
pytest_plugins = 'aiohttp.pytest_plugin'
Expand All @@ -186,7 +171,6 @@ async def test_bad():

def test_aiohttp_plugin_async_fixture(testdir, capsys):
testdir.makepyfile("""\
import asyncio
import pytest
from aiohttp import web
Expand All @@ -195,8 +179,7 @@ def test_aiohttp_plugin_async_fixture(testdir, capsys):
pytest_plugins = 'aiohttp.pytest_plugin'
@asyncio.coroutine
def hello(request):
async def hello(request):
return web.Response(body=b'Hello, world')
Expand All @@ -207,28 +190,24 @@ def create_app(loop):
@pytest.fixture
@asyncio.coroutine
def cli(test_client):
client = yield from test_client(create_app)
async def cli(test_client):
client = await test_client(create_app)
return client
@pytest.fixture
@asyncio.coroutine
def foo():
async def foo():
return 42
@pytest.fixture
@asyncio.coroutine
def bar(request):
async def bar(request):
# request should be accessible in async fixtures if needed
return request.function
@asyncio.coroutine
def test_hello(cli):
resp = yield from cli.get('/')
async def test_hello(cli):
resp = await cli.get('/')
assert resp.status == 200
Expand Down Expand Up @@ -256,7 +235,6 @@ def test_bar(loop, bar):
@pytest.mark.skipif(sys.version_info < (3, 6), reason='old python')
def test_aiohttp_plugin_async_gen_fixture(testdir):
testdir.makepyfile("""\
import asyncio
import pytest
from unittest import mock
Expand Down
9 changes: 3 additions & 6 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ def __init__(self, host):
self.host = host


@asyncio.coroutine
def fake_result(addresses):
async def fake_result(addresses):
return FakeResult(addresses=tuple(addresses))


@asyncio.coroutine
def fake_query_result(result):
async def fake_query_result(result):
return [FakeQueryResult(host=h)
for h in result]


def fake_addrinfo(hosts):
@asyncio.coroutine
def fake(*args, **kwargs):
async def fake(*args, **kwargs):
if not hosts:
raise socket.gaierror

Expand Down
Loading

0 comments on commit 9c0f73a

Please sign in to comment.