From 1711192ac933db9c9ba9b71203eb33fbc1a5a4e8 Mon Sep 17 00:00:00 2001 From: Luna Mendes Date: Mon, 12 Nov 2018 17:39:30 -0300 Subject: [PATCH] tests.test_main: fix testing --- run.py | 6 ++++++ tests/test_main.py | 33 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/run.py b/run.py index 6ad4c0a..d11531c 100644 --- a/run.py +++ b/run.py @@ -63,6 +63,9 @@ def make_app(): def set_blueprints(app_): + """Set the blueprints for a given app instance""" + log.debug('blueprint setup') + bps = { gateway: None, auth: '/auth', @@ -91,6 +94,8 @@ def set_blueprints(app_): suffix = suffix or '' app_.register_blueprint(bp, url_prefix=f'/api/v6{suffix}') + log.debug('blueprint setup: OK') + app = make_app() set_blueprints(app) @@ -148,6 +153,7 @@ async def app_set_ratelimit_headers(resp): async def init_app_db(app): """Connect to databases""" + log.info('db connect') app.db = await asyncpg.create_pool(**app.config['POSTGRES']) diff --git a/tests/test_main.py b/tests/test_main.py index d374c27..567e161 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -4,37 +4,48 @@ import os import pytest +# this is very hacky. sys.path.append(os.getcwd()) -from run import make_app, init_app_db, init_app_managers, set_blueprints +from run import app as main_app, set_blueprints @pytest.fixture(name='app') -def _test_app(): - app = make_app() - set_blueprints(app) +def _test_app(unused_tcp_port): + set_blueprints(main_app) + loop = asyncio.get_event_loop() - loop.run_until_complete(init_app_db(app)) - init_app_managers(app) - return app + + # reassign an unused tcp port for websockets + # since the config might give a used one. + main_app.config['WS_PORT'] = unused_tcp_port + + # make sure we're calling the before_serving hooks + loop.run_until_complete(main_app.startup()) + + return main_app @pytest.fixture(name='test_cli') def _test_cli(app): + """Give a test client.""" return app.test_client() @pytest.mark.asyncio async def test_index(test_cli): + """Test if the main index page works.""" resp = await test_cli.get('/') assert resp.status_code == 200 + assert (await resp.get_data()).decode() == 'hewwo' @pytest.mark.asyncio async def test_gw(test_cli): - resp = await test_cli.get('/api/v6/gateway/') + """Test if the gateway route is sane.""" + resp = await test_cli.get('/api/v6/gateway') assert resp.status_code == 200 - rjson = await resp.json() + rjson = await resp.json assert isinstance(rjson, dict) - assert 'gateway' in rjson - assert isinstance(rjson['gateway'], str) + assert 'url' in rjson + assert isinstance(rjson['url'], str)