Skip to content

Commit 328bb36

Browse files
mpaoliniasvetlov
authored andcommitted
Demo app fixes after #981 refactor (#983)
Also added end-to-end testing with tox
1 parent a5b75e0 commit 328bb36

File tree

9 files changed

+96
-15
lines changed

9 files changed

+96
-15
lines changed

demos/polls/README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Open browser::
2929
:align: center
3030

3131

32+
Run integration tests::
33+
34+
pip install tox
35+
tox
36+
37+
3238
Requirements
3339
============
3440
* aiohttp_

demos/polls/aiohttpdemo_polls/main.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
from aiohttpdemo_polls.middlewares import setup_middlewares
1010
from aiohttpdemo_polls.routes import setup_routes
11-
from aiohttpdemo_polls.utils import init_postgres, load_config
12-
from aiohttpdemo_polls.views import SiteHandler
11+
from aiohttpdemo_polls.utils import load_config
12+
from aiohttpdemo_polls.db import init_postgres
1313

14-
15-
PROJ_ROOT = pathlib.Path(__file__).parent.parent
14+
PROJ_ROOT = pathlib.Path(__file__).parent
1615

1716

1817
async def close_pg(app):
@@ -25,18 +24,16 @@ async def init(loop):
2524
app = web.Application(loop=loop)
2625
aiohttp_jinja2.setup(
2726
app, loader=jinja2.PackageLoader('aiohttpdemo_polls', 'templates'))
28-
# load config from yaml file
29-
conf = load_config(str(PROJ_ROOT / 'config' / 'polls.yaml'))
27+
# load config from yaml file in current dir
28+
conf = load_config(str(pathlib.Path('.') / 'config' / 'polls.yaml'))
3029

3130
# create connection to the database
3231
db = await init_postgres(conf['postgres'], loop)
3332
app['db'] = db
3433

3534
app.on_cleanup.append(close_pg)
36-
3735
# setup views and routes
38-
handler = SiteHandler(db)
39-
setup_routes(app, handler, PROJ_ROOT)
36+
setup_routes(app, PROJ_ROOT)
4037
setup_middlewares(app)
4138

4239
host, port = conf['host'], conf['port']
File renamed without changes.

demos/polls/aiohttpdemo_polls/views.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66
@aiohttp_jinja2.template('index.html')
77
async def index(request):
8-
async with request['db'].acquire() as conn:
8+
async with request.app['db'].acquire() as conn:
99
cursor = await conn.execute(db.question.select())
1010
records = await cursor.fetchall()
11-
1211
questions = [dict(q) for q in records]
1312
return {'questions': questions}
1413

1514

1615
@aiohttp_jinja2.template('detail.html')
1716
async def poll(request):
18-
async with request['db'].acquire() as conn:
17+
async with request.app['db'].acquire() as conn:
1918
question_id = request.match_info['question_id']
2019
try:
2120
question, choices = await db.get_question(conn,
@@ -30,7 +29,7 @@ async def poll(request):
3029

3130
@aiohttp_jinja2.template('results.html')
3231
async def results(request):
33-
async with request['db'].acquire() as conn:
32+
async with request.app['db'].acquire() as conn:
3433
question_id = request.match_info['question_id']
3534

3635
try:
@@ -46,7 +45,7 @@ async def results(request):
4645

4746

4847
async def vote(request):
49-
async with request['db'].acquire() as conn:
48+
async with request.app['db'].acquire() as conn:
5049
question_id = int(request.match_info['question_id'])
5150
data = await request.post()
5251
try:

demos/polls/setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def read_version():
2929
description='Polls project example from aiohttp',
3030
platforms=['POSIX'],
3131
packages=find_packages(),
32+
package_data={
33+
'': ['templates/*.html', 'static/*.*']
34+
},
3235
include_package_data=True,
3336
install_requires=install_requires,
3437
zip_safe=False)

demos/polls/sql/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
sudo -u postgres psql -c "DROP DATABASE IF EXISTS aiohttpdemo_polls"
12
sudo -u postgres psql -c "DROP ROLE IF EXISTS aiohttpdemo_user"
23
sudo -u postgres psql -c "CREATE USER aiohttpdemo_user WITH PASSWORD 'aiohttpdemo_user';"
3-
sudo -u postgres psql -c "DROP DATABASE IF EXISTS aiohttpdemo_polls"
44
sudo -u postgres psql -c "CREATE DATABASE aiohttpdemo_polls ENCODING 'UTF8';"
55
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE aiohttpdemo_polls TO aiohttpdemo_user;"
66

demos/polls/tests/conftest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pathlib
2+
import subprocess
3+
4+
import aiohttp
5+
import pytest
6+
7+
8+
@pytest.yield_fixture
9+
def create_app(event_loop, unused_tcp_port):
10+
app = handler = srv = client_session = None
11+
12+
async def create():
13+
nonlocal app, handler, srv, client_session
14+
import aiohttpdemo_polls.main
15+
app, host, port = await aiohttpdemo_polls.main.init(event_loop)
16+
handler = app.make_handler(debug=True, keep_alive_on=False)
17+
srv = await event_loop.create_server(handler, '127.0.0.1', port)
18+
url = "http://127.0.0.1:{}".format(port)
19+
client_session = aiohttp.ClientSession()
20+
return app, url, client_session
21+
22+
yield create
23+
24+
async def finish():
25+
await handler.finish_connections()
26+
await app.finish()
27+
await client_session.close()
28+
srv.close()
29+
await srv.wait_closed()
30+
31+
event_loop.run_until_complete(finish())
32+
33+
34+
BASE_DIR = pathlib.Path(__file__).parent.parent
35+
36+
37+
@pytest.fixture
38+
def app_db():
39+
subprocess.call(
40+
[(BASE_DIR / 'sql' / 'install.sh').as_posix()],
41+
shell=True,
42+
cwd=BASE_DIR.as_posix()
43+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Integration tests. They need a running database.
3+
4+
Beware, they destroy your db using sudo.
5+
"""
6+
7+
async def _test_index(create_app):
8+
app, url, client_session = await create_app()
9+
async with client_session.get('{}/'.format(url)) as response:
10+
assert response.status == 200, await response.text()
11+
12+
13+
def test_index(create_app, event_loop, app_db):
14+
event_loop.run_until_complete(_test_index(create_app))
15+
16+
17+
async def _test_results(create_app):
18+
app, url, client_session = await create_app()
19+
async with client_session.get('{}/results'.format(url)) as response:
20+
assert response.status == 200, await response.text()
21+
22+
23+
def test_results(create_app, event_loop, app_db):
24+
event_loop.run_until_complete(_test_results(create_app))

demos/polls/tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tox]
2+
envlist = py35
3+
4+
[testenv]
5+
deps =
6+
pytest
7+
pytest-asyncio==0.3.0
8+
usedevelop = True
9+
commands=py.test tests -s

0 commit comments

Comments
 (0)