generated from coulomb/repo-seed
optional FastAPI service skeleton
This commit is contained in:
66
tests/test_service_api.py
Normal file
66
tests/test_service_api.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import pytest
|
||||
|
||||
from kontextual_engine import ServiceRuntime, create_app
|
||||
from kontextual_engine.adapters.memory import InMemoryAssetRegistryRepository
|
||||
|
||||
|
||||
def test_service_runtime_health_readiness_and_version_are_importable_without_fastapi() -> None:
|
||||
runtime = ServiceRuntime(repository=InMemoryAssetRegistryRepository())
|
||||
|
||||
assert runtime.health()["status"] == "ok"
|
||||
assert runtime.readiness()["ready"] is True
|
||||
assert runtime.readiness()["checks"]["asset_registry"]["repository"] == (
|
||||
"InMemoryAssetRegistryRepository"
|
||||
)
|
||||
assert runtime.version()["api_version"] == "v1"
|
||||
|
||||
|
||||
def test_create_app_reports_missing_optional_dependency_when_fastapi_is_absent() -> None:
|
||||
try:
|
||||
import fastapi # noqa: F401
|
||||
except ImportError:
|
||||
with pytest.raises(RuntimeError, match=r"kontextual-engine\[service\]"):
|
||||
create_app()
|
||||
else:
|
||||
pytest.skip("FastAPI is installed; missing-dependency path is not active")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
pytest.importorskip("fastapi")
|
||||
pytest.importorskip("httpx")
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
app = create_app(ServiceRuntime(repository=InMemoryAssetRegistryRepository()))
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
|
||||
|
||||
def test_service_health_readiness_version_and_openapi_contracts(client) -> None:
|
||||
health = client.get("/health")
|
||||
ready = client.get("/ready")
|
||||
version = client.get("/version")
|
||||
versioned_health = client.get("/api/v1/health")
|
||||
openapi = client.get("/openapi.json")
|
||||
|
||||
assert health.status_code == 200
|
||||
assert health.json()["status"] == "ok"
|
||||
assert ready.status_code == 200
|
||||
assert ready.json()["ready"] is True
|
||||
assert ready.json()["checks"]["asset_registry"]["status"] == "ok"
|
||||
assert version.status_code == 200
|
||||
assert version.json()["api_version"] == "v1"
|
||||
assert versioned_health.status_code == 200
|
||||
assert versioned_health.json()["api_version"] == "v1"
|
||||
assert openapi.status_code == 200
|
||||
paths = openapi.json()["paths"]
|
||||
assert "/health" in paths
|
||||
assert "/ready" in paths
|
||||
assert "/version" in paths
|
||||
assert "/api/v1/health" in paths
|
||||
assert "/api/v1/ready" in paths
|
||||
assert "/api/v1/version" in paths
|
||||
|
||||
|
||||
def test_create_app_attaches_runtime_to_application_state(client) -> None:
|
||||
assert client.app.state.kontextual_runtime.api_version == "v1"
|
||||
Reference in New Issue
Block a user