Spaces:
Running
Running
File size: 2,427 Bytes
15d6ad0 c136a98 15d6ad0 c136a98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | """
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()
|