ai-redes-rubertuito / tests /test_api.py
Carlos Ojeda
initial FastAPI Robertuito space
2391f1d
Raw
History Blame Contribute Delete
2.04 kB
from fastapi.testclient import TestClient
from app.application.classification_service import ClassificationService
from app.domain.entities import ClassificationLabel, ClassificationResult
from app.main import create_app
class FakeClassifier:
def classify(self, text: str) -> ClassificationResult:
return ClassificationResult(
predicted_label=ClassificationLabel.HATER if "odio" in text else ClassificationLabel.NEUTRAL,
confidence=0.9,
hater_score=0.9,
critical_score=0.2,
neutral_score=0.1,
raw_response={"fake": True},
)
def make_client(monkeypatch):
monkeypatch.setenv("AI_API_KEY", "test-ai-key")
app = create_app()
app.state.classification_service = ClassificationService(FakeClassifier())
return TestClient(app)
def test_health(monkeypatch):
client = make_client(monkeypatch)
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_classify_requires_api_key(monkeypatch):
client = make_client(monkeypatch)
response = client.post("/classify", json={"text": "hola"})
assert response.status_code == 403
def test_classify(monkeypatch):
client = make_client(monkeypatch)
response = client.post(
"/classify",
json={"text": "te odio"},
headers={"X-API-Key": "test-ai-key"},
)
assert response.status_code == 200
assert response.json()["predicted_label"] == "hater"
def test_classify_batch(monkeypatch):
client = make_client(monkeypatch)
response = client.post(
"/classify/batch",
json={"items": [{"id": "1", "text": "te odio"}, {"id": "2", "text": "hola"}]},
headers={"X-API-Key": "test-ai-key"},
)
assert response.status_code == 200
payload = response.json()
assert payload["items"][0]["id"] == "1"
assert payload["items"][0]["predicted_label"] == "hater"
assert payload["items"][1]["predicted_label"] == "neutral"