agrovision / tests /integration /test_api.py
alexp97's picture
feat: modo mock para validar el flujo de conteo sin modelo real
c136a98
Raw
History Blame Contribute Delete
2.43 kB
"""
Archivo: test_api.py
Fecha de modificación: 03/06/2026
Autor: Equipo AgroVisión
Descripción:
Pruebas de integración HTTP del backend con `TestClient`. Verifican el
healthcheck y que el endpoint de conteo respete el modo standby (503) cuando el
modelo no está habilitado.
Ejemplo de Integración:
uv run pytest tests/integration/test_api.py
"""
from __future__ import annotations
import cv2
import numpy as np
import pytest
from fastapi.testclient import TestClient
from backend.config import get_settings
from backend.main import app
@pytest.fixture(autouse=True)
def _reset_settings_cache(monkeypatch: pytest.MonkeyPatch) -> None:
"""Garantiza el modo standby por defecto limpiando la caché de configuración."""
monkeypatch.delenv("COUNTING_ENABLED", raising=False)
get_settings.cache_clear()
yield
get_settings.cache_clear()
def test_status_reporta_standby() -> None:
"""Verifica que `/api/status` reporte el conteo en standby por defecto."""
with TestClient(app) as client:
response = client.get("/api/status")
assert response.status_code == 200
body = response.json()
assert body["model"] == "agrovision-plantcount"
assert body["counting_enabled"] is False
def test_count_en_standby_devuelve_503() -> None:
"""Verifica que `/api/count` devuelva 503 mientras el conteo esté en standby."""
fake_image = b"no-importa-en-standby"
with TestClient(app) as client:
response = client.post(
"/api/count",
files={"file": ("orto.png", fake_image, "image/png")},
data={"area_ha": "1.0"},
)
assert response.status_code == 503
def test_count_mock_devuelve_metricas(monkeypatch: pytest.MonkeyPatch) -> None:
"""Verifica el flujo completo con datos mock: 200 + conteo + overlay."""
monkeypatch.setenv("COUNTING_ENABLED", "true")
monkeypatch.setenv("MODEL_BACKEND", "mock")
get_settings.cache_clear()
image = np.full((200, 200, 3), 100, dtype=np.uint8)
_, buffer = cv2.imencode(".png", image)
with TestClient(app) as client:
response = client.post(
"/api/count",
files={"file": ("orto.png", buffer.tobytes(), "image/png")},
data={"area_ha": "1.0"},
)
assert response.status_code == 200
body = response.json()
assert body["count"] > 0
assert body["overlay_b64"]
get_settings.cache_clear()