Spaces:
Sleeping
Sleeping
ibraheem15
commited on
Commit
·
3fa282f
1
Parent(s):
e7268a9
Add test cases for API endpoints including health check and spam prediction
Browse files- tests/test_api.py +36 -0
tests/test_api.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tests/test_api.py
|
| 2 |
+
import pytest
|
| 3 |
+
from fastapi.testclient import TestClient
|
| 4 |
+
from main import app
|
| 5 |
+
|
| 6 |
+
# --- FIX: Create a fixture that manages the startup/shutdown ---
|
| 7 |
+
@pytest.fixture(scope="module")
|
| 8 |
+
def client():
|
| 9 |
+
# The 'with' statement triggers the lifespan (startup) event
|
| 10 |
+
with TestClient(app) as c:
|
| 11 |
+
yield c
|
| 12 |
+
|
| 13 |
+
def test_health_check(client):
|
| 14 |
+
"""Test the root endpoint."""
|
| 15 |
+
response = client.get("/")
|
| 16 |
+
assert response.status_code == 200
|
| 17 |
+
assert response.json()["status"] == "ok"
|
| 18 |
+
assert response.json()["model_loaded"] is True
|
| 19 |
+
|
| 20 |
+
def test_predict_spam(client):
|
| 21 |
+
"""Test the prediction endpoint with a spam message."""
|
| 22 |
+
payload = {"text": "Congratulations! You've won a $1000 gift card. Click here."}
|
| 23 |
+
response = client.post("/predict", json=payload)
|
| 24 |
+
|
| 25 |
+
assert response.status_code == 200
|
| 26 |
+
data = response.json()
|
| 27 |
+
assert data["prediction"] in ["spam", "ham"]
|
| 28 |
+
|
| 29 |
+
def test_predict_ham(client):
|
| 30 |
+
"""Test the prediction endpoint with a normal message."""
|
| 31 |
+
payload = {"text": "Hey, are we still meeting for lunch tomorrow?"}
|
| 32 |
+
response = client.post("/predict", json=payload)
|
| 33 |
+
|
| 34 |
+
assert response.status_code == 200
|
| 35 |
+
data = response.json()
|
| 36 |
+
assert data["prediction"] == "ham" # Expecting 'ham' for this text
|