22from fastapi .testclient import TestClient
33from app .serve import app
44
5- # Initialize test client
65client = TestClient (app )
76
8- # -----------------------------
9- # UNIT TESTS (direct model tests)
10- # -----------------------------
7+ def test_ping_endpoint ():
8+ """Health check should return 200."""
9+ response = client .get ("/ping" )
10+ assert response .status_code == 200
11+ data = response .json ()
12+ assert "status" in data
13+ assert data ["status" ] == 200
14+
15+
1116def test_model_inference_basic ():
1217 """Ensure model returns valid sentiment label for simple text."""
13- response = client .get ("/predict?text=I love this movie" )
14- data = response .json ()
15-
18+ response = client .post (
19+ "/predict" ,
20+ json = {"text" : "I love this movie" , "quantize" : False },
21+ )
1622 assert response .status_code == 200
17- assert data ['sentiment' ] in ['positive' ,'negative' ]
18- assert 0 <= float (data ["latency_ms" ])< 3000
23+ data = response .json ()
24+ assert data ["sentiment" ] in ["positive" , "negative" ]
25+ assert "latency_ms" in data
26+ assert "quantized" in data
1927
2028
21- # -----------------------------
22- # INTEGRATION TESTS (API-level)
23- # -----------------------------
2429@pytest .mark .parametrize (
2530 "text, expected_label" ,
2631 [
27- ("I love this product" ,"positive" ),
28- ("This is the worst thing ever" ,"negative" )
29- ],
32+ ("I love this product" , "positive" ),
33+ ("This is the worst thing ever" , "negative" ),
34+ ],
3035)
31-
3236def test_api_response (text , expected_label ):
33- """Test that API returns correct label direction."""
34- response = client .get (f"/predict?text={ text } " )
35- data = response .json ()
36-
37+ """Test that API returns expected label direction."""
38+ response = client .post (
39+ "/predict" ,
40+ json = {"text" : text , "quantize" : False },
41+ )
3742 assert response .status_code == 200
38- assert "sentiment" in data
39-
40- # Ensure at least polarity consistency
41- if "love" in text :
42- assert data ['sentiment' ]== 'positive'
43- elif "worst" in text :
44- assert data ['sentiment' ] == 'negative'
45-
46- def test_ping_endpoint ():
47- """Ping health endpoint"""
48- response = client .get ("/ping" )
49- assert response .status_code == 200
43+ data = response .json ()
44+ assert data ["sentiment" ] == expected_label
0 commit comments