Spaces:
Sleeping
Sleeping
File size: 2,087 Bytes
bcf1c8c 4d96bb5 bcf1c8c |
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 |
from fastapi.testclient import TestClient
from app.api.main import app
# Creiamo un client di test (simula il browser)
client = TestClient(app)
def test_health_check():
"""Verifica che l'endpoint /health risponda status 200"""
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok", "model_loaded": True}
def test_prediction_positive():
"""Verifica che il modello riconosca un sentimento positivo"""
payload = {"text": "I love this service, it is amazing!"}
response = client.post("/predict", json=payload)
assert response.status_code == 200
json_data = response.json()
assert "sentiment" in json_data
assert "confidence" in json_data
# Ci aspettiamo che sia positivo (o almeno non negativo)
assert json_data["sentiment"] == "positive"
def test_prediction_negative():
"""Verifica che il modello riconosca un sentimento negativo"""
payload = {"text": "This is terrible, I hate it."}
response = client.post("/predict", json=payload)
assert response.status_code == 200
assert response.json()["sentiment"] == "negative"
def test_analyze_endpoint():
"""
Verifica che l'endpoint complesso /analyze risponda correttamente.
Nota: Questo test effettua una chiamata reale a Google News.
"""
payload = {"query": "Apple", "limit": 1}
response = client.post("/analyze", json=payload)
assert response.status_code == 200
data = response.json()
# Verifichiamo la struttura della risposta
assert "query" in data
assert "results" in data
assert "summary" in data
# Verifichiamo che summary abbia i conteggi
assert "positive" in data["summary"]
"""
scrivi sul terminale: pytest Serve per lanciare il test
Oppure: pytest -v per vedere tutti i dettagli
P.S: pytest agisce come un detective, quando lancio il comando scanziona la cartella corrente
e cerca file che iniziano con "test_" o finiscono con "_test.py", una volta entrato nel file lancia solo le funzioni che iniziano con "test_"
""" |