Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 12, 2016
1 parent a8b1468 commit 81d2e9a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 187 deletions.
4 changes: 4 additions & 0 deletions requirements-dev.txt
Expand Up @@ -4,3 +4,7 @@ pytest==3.0.3
pytest-cov==2.4.0
pep8==1.7.0
pyflakes==1.3.0
aiohttp==1.1.3
pytest-aiohttp==0.1.3
pytest-sugar==0.7.1
-e .
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -19,7 +19,7 @@ def read(f):
tests_require = install_requires + ['nose']


setup(name='aiohttp_mako',
setup(name='aiohttp-mako',
version=version,
description=("mako template renderer for aiohttp.web "
"(http server for asyncio)"),
Expand All @@ -34,7 +34,7 @@ def read(f):
'Topic :: Internet :: WWW/HTTP'],
author='Nikolay Novik',
author_email='nickolainovik@gmail.com',
url='https://github.com/aio-libs/aiohttp_mako/',
url='https://github.com/aio-libs/aiohttp-mako/',
license='Apache 2',
packages=find_packages(),
install_requires=install_requires,
Expand Down
62 changes: 9 additions & 53 deletions tests/conftest.py
@@ -1,5 +1,3 @@
import asyncio
import socket
import sys

import pytest
Expand All @@ -15,54 +13,12 @@ def pytest_ignore_collect(path, config):


@pytest.fixture
def unused_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port


@pytest.fixture
def loop(request):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)

def fin():
loop.close()

request.addfinalizer(fin)
return loop


@pytest.yield_fixture
def create_server(loop, unused_port):
app = app_handler = srv = None

@asyncio.coroutine
def create(*, debug=False, **kw):
nonlocal app, app_handler, srv
app = web.Application(loop=loop)
lookup = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

tplt = "<html><body><h1>${head}</h1>${text}</body></html>"
lookup.put_string('tplt.html', tplt)

app_handler = app.make_handler(debug=debug, keep_alive_on=False)
port = unused_port
srv = yield from loop.create_server(app_handler, '127.0.0.1', port)
url = "http://127.0.0.1:{}/".format(port)
return app, url

yield create

@asyncio.coroutine
def finish():
yield from app_handler.finish_connections()
yield from app.finish()
srv.close()
yield from srv.wait_closed()

loop.run_until_complete(finish())
def app(loop):
app = web.Application(loop=loop)
lookup = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

tplt = "<html><body><h1>${head}</h1>${text}</body></html>"
lookup.put_string('tplt.html', tplt)
return app
18 changes: 8 additions & 10 deletions tests/pep492/test_await.py
@@ -1,19 +1,17 @@
import aiohttp
import aiohttp_mako


def test_func(loop, create_server):
async def test_func(app, test_client):

@aiohttp_mako.template('tplt.html')
async def func(request):
return {'head': 'HEAD', 'text': 'text'}

async def go():
app, url = await create_server()
app.router.add_route('GET', '/', func)
app.router.add_route('GET', '/', func)

resp = await aiohttp.request('GET', url, loop=loop)
assert 200 == resp.status
txt = await resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
loop.run_until_complete(go())
client = await test_client(app)

resp = await client.get('/')
assert 200 == resp.status
txt = await resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
196 changes: 74 additions & 122 deletions tests/test_simple_renderer.py
@@ -1,53 +1,32 @@
import asyncio
import pytest
from distutils.version import StrictVersion
from unittest import mock

import aiohttp
from aiohttp import web, CIMultiDict
from aiohttp import web
from aiohttp.test_utils import make_mocked_request
from mako.lookup import TemplateLookup

import aiohttp_mako


def make_request(app, method, path):
headers = CIMultiDict()
if StrictVersion(aiohttp.__version__) < StrictVersion('0.20.0'):
message = aiohttp.RawRequestMessage(method, path,
aiohttp.HttpVersion(1, 1),
headers, False, False)
else:
message = aiohttp.RawRequestMessage(method, path,
aiohttp.HttpVersion(1, 1),
headers, headers, False, False)
payload = mock.Mock()
transport = mock.Mock()
writer = mock.Mock()
req = web.Request(app, message, payload, transport,
writer, 15)
return req


def test_func(loop, create_server):
@asyncio.coroutine
def test_func(app, test_client):

@aiohttp_mako.template('tplt.html')
@asyncio.coroutine
def func(request):
return {'head': 'HEAD', 'text': 'text'}

@asyncio.coroutine
def go():
app, url = yield from create_server()
app.router.add_route('GET', '/', func)
app.router.add_route('GET', '/', func)

resp = yield from aiohttp.request('GET', url, loop=loop)
assert 200 == resp.status
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
loop.run_until_complete(go())
client = yield from test_client(app)
resp = yield from client.get('/')
assert 200 == resp.status
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt


def test_meth(loop, create_server):
@asyncio.coroutine
def test_meth(app, test_client):

class Handler:

Expand All @@ -56,147 +35,120 @@ class Handler:
def meth(self, request):
return {'head': 'HEAD', 'text': 'text'}

@asyncio.coroutine
def go():
app, url = yield from create_server()
handler = Handler()
app.router.add_route('GET', '/', handler.meth)
handler = Handler()
app.router.add_route('GET', '/', handler.meth)

resp = yield from aiohttp.request('GET', url, loop=loop)
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
assert 200 == resp.status
client = yield from test_client(app)

loop.run_until_complete(go())
resp = yield from client.get('/')
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
assert 200 == resp.status


def test_render_template(loop, create_server):
@asyncio.coroutine
def test_render_template(app, test_client):

@asyncio.coroutine
def func(request):
return aiohttp_mako.render_template('tplt.html', request,
{'head': 'HEAD',
'text': 'text'})

@asyncio.coroutine
def go():
app, url = yield from create_server()
app.router.add_route('GET', '/', func)
resp = yield from aiohttp.request('GET', url, loop=loop)
assert 200 == resp.status
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt

loop.run_until_complete(go())
app.router.add_route('GET', '/', func)
client = yield from test_client(app)
resp = yield from client.get('/')
assert 200 == resp.status
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt


def test_convert_func_to_coroutine(loop, create_server):
@asyncio.coroutine
def test_convert_func_to_coroutine(app, test_client):

@aiohttp_mako.template('tplt.html')
def func(request):
return {'head': 'HEAD', 'text': 'text'}

@asyncio.coroutine
def go():
app, url = yield from create_server()
app.router.add_route('GET', '/', func)
resp = yield from aiohttp.request('GET', url, loop=loop)
assert 200 == resp.status
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt

loop.run_until_complete(go())
app.router.add_route('GET', '/', func)
client = yield from test_client(app)
resp = yield from client.get('/')
assert 200 == resp.status
txt = yield from resp.text()
assert '<html><body><h1>HEAD</h1>text</body></html>' == txt


@asyncio.coroutine
def test_render_not_initialized(loop):

@asyncio.coroutine
def func(request):
return aiohttp_mako.render_template('template', request, {})

@asyncio.coroutine
def go():
app = web.Application(loop=loop)
app.router.add_route('GET', '/', func)
app = web.Application(loop=loop)
app.router.add_route('GET', '/', func)

req = make_request(app, 'GET', '/')
req = make_mocked_request('GET', '/', app=app)

with pytest.raises(web.HTTPInternalServerError) as ctx:
yield from func(req)
with pytest.raises(web.HTTPInternalServerError) as ctx:
yield from func(req)

assert "Template engine is not initialized, " \
"call aiohttp_mako.setup(app_key={}) first" \
"".format(aiohttp_mako.APP_KEY) == ctx.value.text

loop.run_until_complete(go())
assert "Template engine is not initialized, " \
"call aiohttp_mako.setup(app_key={}) first" \
"".format(aiohttp_mako.APP_KEY) == ctx.value.text


@asyncio.coroutine
def test_template_not_found(loop):

@asyncio.coroutine
def func(request):
return aiohttp_mako.render_template('template', request, {})

@asyncio.coroutine
def go():
app = web.Application(loop=loop)
aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

app.router.add_route('GET', '/', func)
app = web.Application(loop=loop)
aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

req = make_request(app, 'GET', '/')
req = make_mocked_request('GET', '/', app=app)

with pytest.raises(web.HTTPInternalServerError) as ctx:
yield from func(req)
with pytest.raises(web.HTTPInternalServerError) as ctx:
aiohttp_mako.render_template('template', req, {})

assert "Template 'template' not found" == ctx.value.text

loop.run_until_complete(go())
assert "Template 'template' not found" == ctx.value.text


@asyncio.coroutine
def test_template_not_mapping(loop):

@aiohttp_mako.template('tmpl.html')
@asyncio.coroutine
def func(request):
return 'data'

@asyncio.coroutine
def go():
app = web.Application(loop=loop)
lookup = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

tplt = "<html><body><h1>${head}</h1>${text}</body></html>"
lookup.put_string('tmpl.html', tplt)
app = web.Application(loop=loop)
lookup = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

app.router.add_route('GET', '/', func)
tplt = "<html><body><h1>${head}</h1>${text}</body></html>"
lookup.put_string('tmpl.html', tplt)

req = make_request(app, 'GET', '/')
app.router.add_route('GET', '/', func)

with pytest.raises(web.HTTPInternalServerError) as ctx:
yield from func(req)
req = make_mocked_request('GET', '/', app=app)

assert "context should be mapping, not" \
" <class 'str'>" == ctx.value.text
with pytest.raises(web.HTTPInternalServerError) as ctx:
yield from func(req)

loop.run_until_complete(go())
assert "context should be mapping, not" \
" <class 'str'>" == ctx.value.text


@asyncio.coroutine
def test_get_env(loop):

@asyncio.coroutine
def go():
app = web.Application(loop=loop)
lookup1 = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

lookup2 = aiohttp_mako.get_lookup(app)
assert lookup1 is lookup2
assert isinstance(lookup2, TemplateLookup)
app = web.Application(loop=loop)
lookup1 = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])

loop.run_until_complete(go())
lookup2 = aiohttp_mako.get_lookup(app)
assert lookup1 is lookup2
assert isinstance(lookup2, TemplateLookup)

0 comments on commit 81d2e9a

Please sign in to comment.