[tests] add dummy_project_router tests for path-based routers (without ROUTES variable)
This commit is contained in:
parent
eb68d06ac0
commit
78c75cd60e
|
@ -228,3 +228,42 @@ def dummy_debug_app():
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def test_client(dummy_app):
|
def test_client(dummy_app):
|
||||||
return TestClient(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')
|
||||||
|
|
|
@ -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))
|
Loading…
Reference in New Issue