79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
import os
|
|
import sys
|
|
import importlib
|
|
import subprocess
|
|
import time
|
|
import pytest
|
|
from starlette.routing import Route
|
|
from starlette.testclient import TestClient
|
|
|
|
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_has_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/tutu')
|
|
|
|
create_route(os.path.join(dummy_project[0], dummy_project[1]),
|
|
'patch', '/test/ID')
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
def test_get_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/tutu')
|
|
|
|
create_route(os.path.join(dummy_project[0], dummy_project[1]),
|
|
'post', '/test/ID')
|
|
|
|
from halfapi.app import application
|
|
c = TestClient(application)
|
|
r = c.get(f'/{dummy_project[1]}/test')
|
|
assert r.status_code == 200
|