From 246c9224e313e4ac3fe22050f3a6bee073d0a777 Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Fri, 25 Sep 2020 01:06:21 +0200 Subject: [PATCH] [tests] all tests updated to fit new fixture --- tests/conftest.py | 11 +++++++--- tests/test_cli.py | 10 +++------ tests/test_cli_proj.py | 35 +++++++++--------------------- tests/test_debug_routes.py | 16 +++++++++----- tests/test_jwt_middleware.py | 6 ++--- tests/test_jwt_middleware_debug.py | 2 +- tests/test_lib_domain.py | 15 ++++++++++--- 7 files changed, 48 insertions(+), 47 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index e755c9d..4a62481 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,9 @@ PROJNAME = os.environ.get('PROJ','tmp_api') def runner(): return CliRunner() +def halfapicli(runner): + return lambda *args: runner.invoke(Cli, args) + @pytest.fixture def dropdb(): @@ -103,9 +106,9 @@ def pytest_runtest_setup(item): pytest.xfail("previous test failed ({})".format(test_name)) @pytest.fixture -def project_runner(runner, dropdb, createdb, halform_conf_dir, halfapi_conf_dir): +def project_runner(runner, halfapi_conf_dir): + global Cli env = { - 'HALFORM_CONF_DIR': halform_conf_dir, 'HALFAPI_CONF_DIR': halfapi_conf_dir } with runner.isolated_filesystem(): @@ -125,4 +128,6 @@ def project_runner(runner, dropdb, createdb, halform_conf_dir, halfapi_conf_dir) format_halfapi_etc(PROJNAME, os.getcwd())) f.write(PROJ_CONFIG) - yield lambda args: runner.invoke(Cli, args, env=env) + importlib.reload(cli) + Cli = cli.cli + yield halfapicli(runner) diff --git a/tests/test_cli.py b/tests/test_cli.py index f646eab..2a9f312 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -61,11 +61,12 @@ class TestCli(): r = runner.invoke(Cli, ['init', testproject]) assert r.exit_code == 1 - def test_init_project(self, runner, dropdb, createdb, halform_conf_dir, halfapi_conf_dir): + def test_init_project(self, runner, halfapi_conf_dir): + """ + """ cp = ConfigParser() with runner.isolated_filesystem(): env = { - 'HALFORM_CONF_DIR': halform_conf_dir, 'HALFAPI_CONF_DIR': halfapi_conf_dir } @@ -82,10 +83,6 @@ class TestCli(): assert cp.has_option('project', 'name') assert cp.get('project', 'name') == PROJNAME assert cp.get('project', 'halfapi_version') == __version__ - - # .halfapi/domains check - assert os.path.isfile(os.path.join(PROJNAME, '.halfapi', 'domains')) - cp.read(os.path.join(PROJNAME, '.halfapi', 'domains')) assert cp.has_section('domains') except AssertionError as e: subprocess.run(['tree', '-a', os.getcwd()]) @@ -93,4 +90,3 @@ class TestCli(): assert res.exit_code == 0 assert res.exception is None - diff --git a/tests/test_cli_proj.py b/tests/test_cli_proj.py index a725ccc..a9f6b5e 100644 --- a/tests/test_cli_proj.py +++ b/tests/test_cli_proj.py @@ -11,34 +11,19 @@ from configparser import ConfigParser PROJNAME = os.environ.get('PROJ','tmp_api') -@pytest.fixture -def subproc(project_runner): - def caller(cmd): - proc = subprocess.Popen(cmd.split(' ')) - return proc.wait() - return caller @pytest.mark.incremental class TestCliProj(): - def test_cmds(self, subproc): - assert subproc('halfapi run --help') == 0 - assert subproc('halfapi domain --help') == 0 + def test_cmds(self, project_runner): + print(os.getcwd()) + assert project_runner('--help').exit_code == 0 + #assert project_runner('run', '--help').exit_code == 0 + #assert project_runner('domain', '--help').exit_code == 0 + + + def test_config_commands(self, project_runner): + assert project_runner('config') == 0 - def test_config_commands(self, subproc): - res = subproc('halfapi config pr00t') - assert res == 2 - res = subproc('halfapi config --help') - assert res == 0 - res = subproc('halfapi config') - assert res == 0 def test_domain_commands(self, subproc): - res = subproc('halfapi domain foobar') - assert res == 2 - res = subproc('halfapi domain --help') - assert res == 0 - - def test_domain_create(self, subproc): - DOMNAME='tmp_domain' - res = subproc(f'halfapi domain --create') - assert res == 0 + assert project_runner('domain') == 0 diff --git a/tests/test_debug_routes.py b/tests/test_debug_routes.py index 936554a..267fba0 100644 --- a/tests/test_debug_routes.py +++ b/tests/test_debug_routes.py @@ -2,14 +2,20 @@ import pytest from starlette.authentication import UnauthenticatedUser from starlette.testclient import TestClient -from halfapi.app import app +from halfapi.app import application +import json def test_itworks(): - c = TestClient(app) - r = c.get('/') - assert r.text == 'It Works!' + c = TestClient(application) + r = json.loads(c.get('/').text) + assert r == 'It Works!' def test_user(): - c = TestClient(app) + c = TestClient(application) r = c.get('/user') assert r.status_code == 200 + +def test_user(): + c = TestClient(application) + r = c.get('/schema') + assert r.status_code == 200 diff --git a/tests/test_jwt_middleware.py b/tests/test_jwt_middleware.py index b5f81ee..0314b9f 100644 --- a/tests/test_jwt_middleware.py +++ b/tests/test_jwt_middleware.py @@ -17,7 +17,7 @@ from starlette.authentication import ( #from halfapi.app import app -os.environ['HALFAPI_PROD'] = 'True' +#os.environ['HALFAPI_PROD'] = 'True' os.environ['HALFAPI_SECRET'] = 'randomsecret' from halfapi.lib.jwt_middleware import (PRODUCTION, SECRET, @@ -25,8 +25,8 @@ from halfapi.lib.jwt_middleware import (PRODUCTION, SECRET, JWTWebSocketAuthenticationBackend) def test_constants(): - assert PRODUCTION == bool(os.environ['HALFAPI_PROD']) - assert SECRET == os.environ['HALFAPI_SECRET'] + assert isinstance(PRODUCTION, bool) + assert isinstance(SECRET, str) @pytest.fixture def token(): diff --git a/tests/test_jwt_middleware_debug.py b/tests/test_jwt_middleware_debug.py index bc4beb7..9379652 100644 --- a/tests/test_jwt_middleware_debug.py +++ b/tests/test_jwt_middleware_debug.py @@ -26,7 +26,7 @@ from halfapi.lib.jwt_middleware import (PRODUCTION, SECRET, def test_constants(): assert PRODUCTION == bool(os.environ['HALFAPI_PROD']) - assert SECRET == os.environ['HALFAPI_SECRET'] + #assert SECRET == os.environ['HALFAPI_SECRET'] @pytest.fixture diff --git a/tests/test_lib_domain.py b/tests/test_lib_domain.py index da79b7f..1b5308a 100644 --- a/tests/test_lib_domain.py +++ b/tests/test_lib_domain.py @@ -1,12 +1,21 @@ #!/usr/bin/env python3 -from halfapi.lib.domain import VERBS, router_scanner +from starlette.routing import Route +from halfapi.lib.domain import VERBS, gen_router_routes -def test_route_scanner(): +from halfapi.lib.routes import gen_starlette_routes + +def test_gen_router_routes(): from .dummy_domain import routers - for route in router_scanner(routers): + for route in gen_router_routes(routers): print(f'[{route["verb"]}] {route["path"]} {route["fct"]}') assert route['verb'] in VERBS assert isinstance(route['path'], str) assert len(route['params']) > 0 assert hasattr(route['fct'], '__call__') + +def test_gen_starlette_routes(): + from . import dummy_domain + for route in gen_starlette_routes(dummy_domain): + assert isinstance(route, Route) +