Spaces:
Sleeping
Sleeping
File size: 3,132 Bytes
523415f | 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import pytest
from fastapi.testclient import TestClient
from .main import app
client = TestClient(app)
def test_read_root():
"""Test the root endpoint."""
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Travellers Interest Predictions is running!"}
def test_health_check():
"""Test the health check endpoint."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert "status" in data
assert "model_loaded" in data
assert "message" in data
assert data["status"] in ["healthy", "degraded"]
def test_get_questions():
"""Test the questions endpoint."""
response = client.get("/questions")
assert response.status_code == 200
data = response.json()
assert "questions" in data
assert "total_questions" in data
assert data["total_questions"] == 10
assert len(data["questions"]) == 10
def test_get_sample_prediction():
"""Test the sample prediction endpoint."""
response = client.get("/predict/sample")
assert response.status_code == 200
data = response.json()
assert "example_request" in data
assert "description" in data
assert "questions_endpoint" in data
def test_predict_valid_request():
"""Test prediction with valid input."""
valid_answers = [3, 4, 2, 5, 3, 2, 4, 1, 3, 4]
response = client.post("/predict", json={"answers": valid_answers})
# The response should be 200 regardless of whether the model is loaded
assert response.status_code == 200 or response.status_code == 503
if response.status_code == 200:
data = response.json()
assert "success" in data
assert "prediction" in data
assert "message" in data
def test_predict_invalid_length():
"""Test prediction with invalid answer length."""
invalid_answers = [3, 4, 2, 5, 3] # Only 5 answers instead of 10
response = client.post("/predict", json={"answers": invalid_answers})
assert response.status_code == 422 # Validation error
def test_predict_invalid_values():
"""Test prediction with invalid answer values."""
invalid_answers = [3, 4, 6, 5, 3, 2, 4, 1, 3, 4] # 6 is out of range
response = client.post("/predict", json={"answers": invalid_answers})
assert response.status_code == 422 # Validation error
def test_predict_invalid_type():
"""Test prediction with invalid answer types."""
invalid_answers = [3, 4, "hello", 5, 3, 2, 4, 1, 3, 4] # String instead of int
response = client.post("/predict", json={"answers": invalid_answers})
assert response.status_code == 422 # Validation error
def test_predict_empty_request():
"""Test prediction with empty request."""
response = client.post("/predict", json={})
assert response.status_code == 422 # Validation error
def test_cors_headers():
"""Test that CORS headers are present."""
response = client.options("/")
# FastAPI automatically handles OPTIONS requests for CORS
assert response.status_code in [200, 405] # 405 if OPTIONS not explicitly defined |