From 78c75cd60ead023e7a2d7fa2e5d1059fada0044f Mon Sep 17 00:00:00 2001 From: "Maxime Alves LIRMM@home" Date: Mon, 14 Jun 2021 16:34:58 +0200 Subject: [PATCH] [tests] add dummy_project_router tests for path-based routers (without ROUTES variable) --- tests/conftest.py | 39 ++++++++++++++++++++++ tests/test_dummy_project_router.py | 52 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 tests/test_dummy_project_router.py diff --git a/tests/conftest.py b/tests/conftest.py index 0429703..9cfd68c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -228,3 +228,42 @@ def dummy_debug_app(): @pytest.fixture def test_client(dummy_app): return TestClient(dummy_app) + +@pytest.fixture +def create_route(): + def wrapped(domain_path, method, path): + stack = [domain_path, *path.split('/')[1:]] + for i in range(len(stack)): + if len(stack[i]) == 0: + continue + + path = os.path.join(*stack[0:i+1]) + if os.path.isdir(os.path.join(path)): + continue + os.mkdir(path) + init_path = os.path.join(*stack, '__init__.py') + with open(init_path, 'a+') as f: + f.write(f'\ndef {method}():\n raise NotImplementedError') + + return wrapped + + + +@pytest.fixture +def dummy_project(): + halfapi_dirname = tempfile.mkdtemp(prefix='halfapi_') + domain_dirname = os.path.join(halfapi_dirname, 'test_domain') + halfapi_path = os.path.join(halfapi_dirname, '.halfapi') + os.mkdir(halfapi_path) + os.mkdir(os.path.join(domain_dirname)) + os.mkdir(os.path.join(domain_dirname, 'test_router')) + + with open(os.path.join(halfapi_path, 'config'), 'w') as f: + f.writelines([ + '[domains]', + f'test_domain = test_router' + ]) + with open(os.path.join(halfapi_dirname, 'test_domain', '__init__.py'), 'w') as f: + f.write('') + + return (halfapi_dirname, 'test_domain') diff --git a/tests/test_dummy_project_router.py b/tests/test_dummy_project_router.py new file mode 100644 index 0000000..c1fe2c9 --- /dev/null +++ b/tests/test_dummy_project_router.py @@ -0,0 +1,52 @@ +import os +import sys +import importlib +import subprocess +import time +import pytest +from starlette.routing import Route + +from halfapi.lib.routes import gen_starlette_routes + + +def test_create_route(dummy_project, create_route): + + create_route(os.path.join(dummy_project[0], dummy_project[1]), + 'get', '/test') + create_route(os.path.join(dummy_project[0], dummy_project[1]), + 'post', '/test') + create_route(os.path.join(dummy_project[0], dummy_project[1]), + 'put', '/test') + + os.chdir(dummy_project[0]) + + sys.path.insert(0, '.') + router_path = os.path.join('.', dummy_project[1], 'test') + os.path.isdir(router_path) + try: + mod = importlib.import_module('.'.join((dummy_project[1], 'test'))) + except ModuleNotFoundError as exc: + print('.'.join((dummy_project[1], 'test'))) + print(os.listdir('.')) + raise exc + + assert hasattr(mod, 'get') + assert hasattr(mod, 'post') + assert hasattr(mod, 'put') + +def test_create_route(dummy_project, create_route): + + create_route(os.path.join(dummy_project[0], dummy_project[1]), + 'get', '/test') + + os.chdir(dummy_project[0]) + sys.path.insert(0, '.') + try: + mod = importlib.import_module(dummy_project[1], 'test') + except ModuleNotFoundError as exc: + print('.'.join((dummy_project[1], 'test'))) + print(os.listdir('.')) + raise exc + + for elt in gen_starlette_routes({dummy_project[1]: mod}): + assert(isinstance(elt, Route))