67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import ast
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import unittest
|
|
|
|
|
|
SCRIPT = (
|
|
Path(__file__).resolve().parents[1]
|
|
/ "tools"
|
|
/ "inventory"
|
|
/ "platform-interface-inventory.py"
|
|
)
|
|
SPEC = importlib.util.spec_from_file_location("platform_interface_inventory", SCRIPT)
|
|
assert SPEC is not None and SPEC.loader is not None
|
|
inventory = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(inventory)
|
|
|
|
|
|
class PlatformInterfaceInventoryTests(unittest.TestCase):
|
|
def test_canonical_api_path_normalizes_versions_and_parameters(self) -> None:
|
|
self.assertEqual(
|
|
inventory.canonical_api_path(
|
|
"http://localhost/api/v1/campaigns/${campaignId}?limit=10"
|
|
),
|
|
"/campaigns/{}",
|
|
)
|
|
self.assertEqual(
|
|
inventory.canonical_api_path("/api/v2/campaigns/{campaign_id}"),
|
|
"/campaigns/{}",
|
|
)
|
|
|
|
def test_fastapi_route_scanner_includes_router_prefix(self) -> None:
|
|
tree = ast.parse(
|
|
"""
|
|
from fastapi import APIRouter
|
|
router = APIRouter(prefix="/api/v1/items")
|
|
|
|
@router.get("/{item_id}")
|
|
def read_item(item_id: str):
|
|
return item_id
|
|
"""
|
|
)
|
|
prefixes = inventory._router_prefixes(tree)
|
|
function = next(
|
|
node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)
|
|
)
|
|
|
|
route = inventory._endpoint_from_decorator(
|
|
function.decorator_list[0],
|
|
prefixes=prefixes,
|
|
)
|
|
|
|
self.assertEqual(
|
|
route,
|
|
{
|
|
"method": "GET",
|
|
"path": "/api/v1/items/{item_id}",
|
|
"router": "router",
|
|
},
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|