Skip to content

Commit 7992355

Browse files
authored
Adopt tests to aiohttp 3 (aio-libs#254)
* Setup pytest warning mode * Convert test_client -> aiohttp_client * Fix deprecation warnings * Tune dependencies
1 parent 8497304 commit 7992355

12 files changed

+122
-110
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ docs/_build/
5454
target/
5555

5656
coverage
57-
venv
57+
venv
58+
59+
.pytest_cache

pytest.ini

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
filterwarnings=
3+
error

requirements-dev.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
attrs==17.4.0
12
flake8==3.5.0
2-
coverage==4.5
3-
sphinx==1.6.7
3+
sphinx==1.7.0
44
pep257==0.7.0
55
-e .
66
aioredis==1.0.0
@@ -11,7 +11,8 @@ pytest==3.4.0
1111
pytest-aiohttp==0.3.0
1212
pytest-cov==2.5.1
1313
pytest-mock==1.6.3
14-
aiohttp==2.3.10
14+
pytest-sugar==0.9.1
15+
aiohttp==3.0.1
1516
multidict==4.1.0
1617
chardet==3.0.4
1718
yarl==1.1.0

tests/test_abstract_storage.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def make_cookie(client, data):
1717
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': value})
1818

1919

20-
def create_app(loop, handler):
21-
app = web.Application(loop=loop)
20+
def create_app(handler):
21+
app = web.Application()
2222
setup_middleware(app, SimpleCookieStorage(max_age=10))
2323
app.router.add_route('GET', '/', handler)
2424
return app
2525

2626

27-
async def test_max_age_also_returns_expires(test_client):
27+
async def test_max_age_also_returns_expires(aiohttp_client):
2828

2929
async def handler(request):
3030
time.time.return_value = 0.0
@@ -35,7 +35,7 @@ async def handler(request):
3535
with mock.patch('time.time') as m_clock:
3636
m_clock.return_value = 0.0
3737

38-
client = await test_client(create_app, handler)
38+
client = await aiohttp_client(create_app(handler))
3939
make_cookie(client, {'a': 1, 'b': 2})
4040
resp = await client.get('/')
4141
assert resp.status == 200

tests/test_cookie_storage.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ def make_cookie(client, data):
1616
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': value})
1717

1818

19-
def create_app(loop, handler):
19+
def create_app(handler):
2020
middleware = session_middleware(SimpleCookieStorage())
21-
app = web.Application(middlewares=[middleware], loop=loop)
21+
app = web.Application(middlewares=[middleware])
2222
app.router.add_route('GET', '/', handler)
2323
return app
2424

2525

26-
async def test_create_new_session(test_client):
26+
async def test_create_new_session(aiohttp_client):
2727

2828
async def handler(request):
2929
session = await get_session(request)
@@ -33,12 +33,12 @@ async def handler(request):
3333
assert {} == session
3434
return web.Response(body=b'OK')
3535

36-
client = await test_client(create_app, handler)
36+
client = await aiohttp_client(create_app(handler))
3737
resp = await client.get('/')
3838
assert resp.status == 200
3939

4040

41-
async def test_load_existing_session(test_client):
41+
async def test_load_existing_session(aiohttp_client):
4242

4343
async def handler(request):
4444
session = await get_session(request)
@@ -49,20 +49,20 @@ async def handler(request):
4949
assert {'a': 1, 'b': 2} == session
5050
return web.Response(body=b'OK')
5151

52-
client = await test_client(create_app, handler)
52+
client = await aiohttp_client(create_app(handler))
5353
make_cookie(client, {'a': 1, 'b': 2})
5454
resp = await client.get('/')
5555
assert resp.status == 200
5656

5757

58-
async def test_change_session(test_client):
58+
async def test_change_session(aiohttp_client):
5959

6060
async def handler(request):
6161
session = await get_session(request)
6262
session['c'] = 3
6363
return web.Response(body=b'OK')
6464

65-
client = await test_client(create_app, handler)
65+
client = await aiohttp_client(create_app(handler))
6666
make_cookie(client, {'a': 1, 'b': 2})
6767
resp = await client.get('/')
6868
assert resp.status == 200
@@ -81,14 +81,14 @@ async def handler(request):
8181
assert '/' == morsel['path']
8282

8383

84-
async def test_clear_cookie_on_session_invalidation(test_client):
84+
async def test_clear_cookie_on_session_invalidation(aiohttp_client):
8585

8686
async def handler(request):
8787
session = await get_session(request)
8888
session.invalidate()
8989
return web.Response(body=b'OK')
9090

91-
client = await test_client(create_app, handler)
91+
client = await aiohttp_client(create_app(handler))
9292
make_cookie(client, {'a': 1, 'b': 2})
9393
resp = await client.get('/')
9494
assert resp.status == 200
@@ -97,12 +97,12 @@ async def handler(request):
9797
resp.cookies['AIOHTTP_SESSION'].output().upper()
9898

9999

100-
async def test_dont_save_not_requested_session(test_client):
100+
async def test_dont_save_not_requested_session(aiohttp_client):
101101

102102
async def handler(request):
103103
return web.Response(body=b'OK')
104104

105-
client = await test_client(create_app, handler)
105+
client = await aiohttp_client(create_app(handler))
106106
make_cookie(client, {'a': 1, 'b': 2})
107107
resp = await client.get('/')
108108
assert resp.status == 200

tests/test_encrypted_cookie_storage.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def make_cookie(client, fernet, data):
2323
client.session.cookie_jar.update_cookies({'AIOHTTP_SESSION': data})
2424

2525

26-
def create_app(loop, handler, key):
26+
def create_app(handler, key):
2727
middleware = session_middleware(EncryptedCookieStorage(key))
28-
app = web.Application(middlewares=[middleware], loop=loop)
28+
app = web.Application(middlewares=[middleware])
2929
app.router.add_route('GET', '/', handler)
3030
return app
3131

@@ -59,7 +59,8 @@ def test_invalid_key():
5959
EncryptedCookieStorage(b'123') # short key
6060

6161

62-
async def test_create_new_session_broken_by_format(test_client, fernet, key):
62+
async def test_create_new_session_broken_by_format(aiohttp_client,
63+
fernet, key):
6364

6465
async def handler(request):
6566
session = await get_session(request)
@@ -70,13 +71,13 @@ async def handler(request):
7071
return web.Response(body=b'OK')
7172

7273
new_fernet = Fernet(Fernet.generate_key())
73-
client = await test_client(create_app, handler, key)
74+
client = await aiohttp_client(create_app(handler, key))
7475
make_cookie(client, new_fernet, {'a': 1, 'b': 12})
7576
resp = await client.get('/')
7677
assert resp.status == 200
7778

7879

79-
async def test_load_existing_session(test_client, fernet, key):
80+
async def test_load_existing_session(aiohttp_client, fernet, key):
8081

8182
async def handler(request):
8283
session = await get_session(request)
@@ -86,20 +87,20 @@ async def handler(request):
8687
assert {'a': 1, 'b': 12} == session
8788
return web.Response(body=b'OK')
8889

89-
client = await test_client(create_app, handler, key)
90+
client = await aiohttp_client(create_app(handler, key))
9091
make_cookie(client, fernet, {'a': 1, 'b': 12})
9192
resp = await client.get('/')
9293
assert resp.status == 200
9394

9495

95-
async def test_change_session(test_client, fernet, key):
96+
async def test_change_session(aiohttp_client, fernet, key):
9697

9798
async def handler(request):
9899
session = await get_session(request)
99100
session['c'] = 3
100101
return web.Response(body=b'OK')
101102

102-
client = await test_client(create_app, handler, key)
103+
client = await aiohttp_client(create_app(handler, key))
103104
make_cookie(client, fernet, {'a': 1, 'b': 2})
104105
resp = await client.get('/')
105106
assert resp.status == 200
@@ -118,14 +119,15 @@ async def handler(request):
118119
assert '/' == morsel['path']
119120

120121

121-
async def test_clear_cookie_on_session_invalidation(test_client, fernet, key):
122+
async def test_clear_cookie_on_session_invalidation(aiohttp_client,
123+
fernet, key):
122124

123125
async def handler(request):
124126
session = await get_session(request)
125127
session.invalidate()
126128
return web.Response(body=b'OK')
127129

128-
client = await test_client(create_app, handler, key)
130+
client = await aiohttp_client(create_app(handler, key))
129131
make_cookie(client, fernet, {'a': 1, 'b': 2})
130132
resp = await client.get('/')
131133
assert resp.status == 200

tests/test_http_exception.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
get_session, SimpleCookieStorage)
44

55

6-
def create_app(loop, *handlers):
6+
def create_app(*handlers):
77
middleware = session_middleware(SimpleCookieStorage())
8-
app = web.Application(middlewares=[middleware], loop=loop)
8+
app = web.Application(middlewares=[middleware])
99
for url, handler in handlers:
1010
app.router.add_route('GET', url, handler)
1111
return app
1212

1313

14-
async def test_exceptions(test_client):
14+
async def test_exceptions(aiohttp_client):
1515

1616
async def save(request):
1717
session = await get_session(request)
@@ -23,7 +23,7 @@ async def show(request):
2323
message = session.get('message')
2424
return web.Response(text=str(message))
2525

26-
client = await test_client(create_app, ('/save', save), ('/show', show))
26+
client = await aiohttp_client(create_app(('/save', save), ('/show', show)))
2727

2828
resp = await client.get('/save')
2929
assert resp.status == 200

0 commit comments

Comments
 (0)