Coverage for tests/playground.py: 100%
24 statements
« prev ^ index » next coverage.py v7.15.3, created at 2026-08-03 00:00 +0000
« prev ^ index » next coverage.py v7.15.3, created at 2026-08-03 00:00 +0000
1import pathlib
2import types
3import typing
5import fastapi
6from starlette.responses import HTMLResponse as StarletteHtmlResponse
8from archdocs.integrations.fastapi import add_architecture_doc_routes
9from archdocs.main import SettingsForArchdocs
12TESTS_ROOT: typing.Final = pathlib.Path(__file__).parent
13KUBERNETES_DIR: typing.Final = TESTS_ROOT / "kubernetes_fixtures" / "chart"
14PLAYGROUND_EXAMPLES: typing.Final = types.MappingProxyType(
15 {
16 "fastapi": SettingsForArchdocs(
17 root_dir=TESTS_ROOT / "fastapi",
18 service_name="fastapi-example",
19 kubernetes_dir=KUBERNETES_DIR,
20 ),
21 "litestar": SettingsForArchdocs(
22 root_dir=TESTS_ROOT / "litestar",
23 service_name="litestar-example",
24 kubernetes_dir=KUBERNETES_DIR,
25 ),
26 "showcase": SettingsForArchdocs(
27 root_dir=TESTS_ROOT / "showcase",
28 service_name="showcase-service",
29 kubernetes_dir=KUBERNETES_DIR,
30 ),
31 },
32)
33INDEX_PATH: typing.Final = "/"
34_INDEX_TEMPLATE: typing.Final = (
35 "<!doctype html><html><head><meta charset='utf-8'><title>archdocs playground</title></head>"
36 "<body><h1>archdocs playground</h1><ul>{example_links}</ul></body></html>"
37)
40def render_example_path(example_name: str, /) -> str:
41 return f"/{example_name}/"
44async def _handle_index_route() -> StarletteHtmlResponse:
45 example_links: typing.Final = "".join(
46 f'<li><a href="{render_example_path(one_example_name)}">{one_example_settings.service_name}</a></li>'
47 for one_example_name, one_example_settings in PLAYGROUND_EXAMPLES.items()
48 )
49 return StarletteHtmlResponse(_INDEX_TEMPLATE.format(example_links=example_links))
52def create_playground_app() -> fastapi.FastAPI:
53 playground_app: typing.Final = fastapi.FastAPI(title="archdocs playground")
54 playground_app.add_api_route(INDEX_PATH, _handle_index_route, response_class=StarletteHtmlResponse)
55 for one_example_name, one_example_settings in PLAYGROUND_EXAMPLES.items():
56 add_architecture_doc_routes(
57 playground_app,
58 route_path=render_example_path(one_example_name),
59 arch_settings=one_example_settings,
60 )
61 return playground_app
64playground_app: typing.Final = create_playground_app()