Spaces:
Running
Running
Download tests/api/test_composition.py from Arrechenash/stocks: direct link, hf CLI and curl.
- Browser
- Download file 3.42 kB
-
https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/tests/api/test_composition.py
- Command line
-
hf download hf://spaces/Arrechenash/stocks/tests/api/test_composition.py
-
curl -L -o test_composition.py https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/tests/api/test_composition.py
3.42 kB
| """The app composition table (plan ARCH-A1). | |
| app/composition.py is the one place that knows the full set of routers. Two | |
| things can break silently: a router dropped from the table (its URLs 404 with | |
| no other error anywhere) and a router mounted twice (FastAPI answers from the | |
| first match, so a duplicate is invisible rather than an error). Both are | |
| checked here against the routes the app actually ends up serving. | |
| """ | |
| from fastapi import APIRouter, FastAPI | |
| from app.composition import API_ROUTERS, PAGE_ROUTERS, include_routers | |
| def test_api_routers_are_not_empty() -> None: | |
| assert len(API_ROUTERS) >= 20, "the composition table lost routers" | |
| def test_page_routers_are_not_empty() -> None: | |
| assert len(PAGE_ROUTERS) >= 12 | |
| def test_no_router_is_mounted_twice() -> None: | |
| api_ids = [id(r) for r in API_ROUTERS] | |
| page_ids = [id(r) for r in PAGE_ROUTERS] | |
| assert len(api_ids) == len(set(api_ids)), "duplicate entry in API_ROUTERS" | |
| assert len(page_ids) == len(set(page_ids)), "duplicate entry in PAGE_ROUTERS" | |
| assert not set(api_ids) & set(page_ids), "a router is in both tables" | |
| def test_every_entry_is_a_router() -> None: | |
| for router in (*API_ROUTERS, *PAGE_ROUTERS): | |
| assert isinstance(router, APIRouter) | |
| def test_api_routers_are_all_prefixed() -> None: | |
| """An unprefixed API router would expose its paths at the site root and | |
| shadow a page route.""" | |
| for router in API_ROUTERS: | |
| assert router.prefix.startswith("/api"), f"{router.prefix} is not under /api" | |
| def _probe_router() -> APIRouter: | |
| router = APIRouter(prefix="/api/legacy-probe") | |
| def probe() -> dict[str, bool]: | |
| return {"ok": True} | |
| return router | |
| def test_include_routers_mounts_the_legacy_router() -> None: | |
| """The legacy in-core /api surface is handed in and mounted, so its paths | |
| stay reachable.""" | |
| app = FastAPI() | |
| include_routers(app, _probe_router()) | |
| assert "/api/legacy-probe/x" in app.openapi()["paths"] | |
| def test_include_routers_mounts_module_and_page_routers() -> None: | |
| """A sample from each table must survive the mount -- a router dropped from | |
| the table would otherwise only show up as a 404 in the browser. Page routers | |
| are the redirects that keep the bookmarkable URLs alive. One mount and one | |
| OpenAPI build cover both tables.""" | |
| app = FastAPI() | |
| include_routers(app, _probe_router()) | |
| paths = app.openapi()["paths"] | |
| for expected in ("/api/watchlist", "/api/screener", "/api/dashboard/widgets", "/trades", "/dashboard", "/at-risk"): | |
| assert expected in paths, f"{expected} is no longer served" | |
| def test_the_real_app_has_no_duplicate_operation_ids() -> None: | |
| """Two routes answering to one operation id means one of them is | |
| unreachable through generated clients. The composition table is | |
| hand-maintained, so this is the check that catches a router being | |
| mounted twice.""" | |
| from app.main import app | |
| seen: set[str] = set() | |
| duplicates: list[str] = [] | |
| for path, operations in app.openapi()["paths"].items(): | |
| for method, op in operations.items(): | |
| op_id = op.get("operationId") | |
| if not op_id: | |
| continue | |
| if op_id in seen: | |
| duplicates.append(f"{method.upper()} {path} -> {op_id}") | |
| seen.add(op_id) | |
| assert not duplicates, f"duplicate operation ids: {duplicates}" | |