feat: sincronizar scaffold completo del shape web-backend/python
Sincroniza el scaffold canónico desde templates/web-backend-python/ del repo IDP. Reemplaza el placeholder mínimo previo (.gitea/workflows/ci.yaml, README.md, .gitignore, catalog-info.yaml) por el scaffold completo documentado en ADR-053 §3: - code/ — FastAPI app con /health (db check), /metrics, /demo estático, middleware correlation_id, settings Pydantic, db.py con SQLAlchemy - code/alembic/ — migración inicial vacía + env.py usando MIGRATION_DATABASE_URL - code/tests/ — pytest + httpx TestClient (test_health, test_demo) - helm/APP_NAME/ — chart con Deployment, Service, IngressRoute Traefik, NetworkPolicy, ServiceMonitor, Job alembic-upgrade como hook pre-upgrade,pre-install (backoffLimit: 0) - coralware-shape.yaml — manifiesto v1alpha1 del contrato del shape - claim-mariadb.yaml — AddonClaim declarativo (uso futuro ADR-052) - catalog-info.yaml — entidad Backstage actualizada - .gitea/workflows/APP_NAME-build.yaml — invoca reusable workflow ADR-038 + job helm-deploy con KUBECONFIG_TENANT_B64 Placeholders __APP_NAME__, __TIER__, __TENANT_ID__, __TENANT_ID_NODASHES__ serán sustituidos server-side por repo-provisioner cuando materialice el template para un tenant (pendiente — ADR-053 §11.5). El openspec/ del golden path NO se sincroniza al repo Gitea (gobernanza interna de Coralware). Refs: ADR-052, ADR-053, poc-nexobms.md (PoC NexoBMS Demetrio).
This commit is contained in:
0
code/tests/__init__.py
Normal file
0
code/tests/__init__.py
Normal file
34
code/tests/conftest.py
Normal file
34
code/tests/conftest.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""
|
||||
Fixtures para los tests del shape.
|
||||
|
||||
Levanta una MariaDB efímera con testcontainers para que los tests corran
|
||||
contra una BD real (no mocks). El reusable workflow ADR-038 garantiza que
|
||||
Docker está disponible en el runner.
|
||||
"""
|
||||
|
||||
import os
|
||||
from collections.abc import Iterator
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from testcontainers.mariadb import MariaDbContainer
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def mariadb_url() -> Iterator[str]:
|
||||
"""MariaDB efímera por sesión de tests; URL exportada como DATABASE_URL."""
|
||||
with MariaDbContainer("mariadb:11") as mdb:
|
||||
url = mdb.get_connection_url().replace("mysql://", "mysql+pymysql://", 1)
|
||||
os.environ["DATABASE_URL"] = url
|
||||
os.environ["MIGRATION_DATABASE_URL"] = url
|
||||
yield url
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(mariadb_url: str) -> Iterator[TestClient]:
|
||||
"""TestClient con la app cargada contra la BD efímera."""
|
||||
from app.main import create_app
|
||||
|
||||
app = create_app()
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
22
code/tests/test_demo.py
Normal file
22
code/tests/test_demo.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""Cubre el endpoint /demo: sirve el index estático."""
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
def test_demo_serves_index_html(client: TestClient) -> None:
|
||||
response = client.get("/demo/")
|
||||
assert response.status_code == 200
|
||||
assert "text/html" in response.headers.get("content-type", "")
|
||||
assert "__APP_NAME__" in response.text or "demo" in response.text.lower()
|
||||
|
||||
|
||||
def test_metrics_endpoint_responds(client: TestClient) -> None:
|
||||
response = client.get("/metrics")
|
||||
assert response.status_code == 200
|
||||
assert "text/plain" in response.headers.get("content-type", "")
|
||||
|
||||
|
||||
def test_openapi_accessible_when_auth_disabled(client: TestClient) -> None:
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200
|
||||
spec = response.json()
|
||||
assert spec["info"]["title"] == "__APP_NAME__"
|
||||
30
code/tests/test_health.py
Normal file
30
code/tests/test_health.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Cubre el endpoint /health: respuesta 200 con la BD viva, 503 cuando cae."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
def test_health_db_up_returns_200(client: TestClient) -> None:
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"status": "ok", "db": "up"}
|
||||
|
||||
|
||||
def test_health_db_down_returns_503(client: TestClient) -> None:
|
||||
with patch("app.main.db_alive", return_value=False):
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 503
|
||||
body = response.json()
|
||||
assert body["status"] == "degraded"
|
||||
assert body["db"] == "down"
|
||||
|
||||
|
||||
def test_health_propagates_correlation_id(client: TestClient) -> None:
|
||||
cid = "test-cid-12345"
|
||||
response = client.get("/health", headers={"X-Correlation-ID": cid})
|
||||
assert response.headers.get("X-Correlation-ID") == cid
|
||||
|
||||
|
||||
def test_health_generates_correlation_id_when_absent(client: TestClient) -> None:
|
||||
response = client.get("/health")
|
||||
assert response.headers.get("X-Correlation-ID")
|
||||
Reference in New Issue
Block a user