Spaces:
Sleeping
Sleeping
Download src/test_ml_endpoints.py from InduwaraR/Interest_Predictions: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/spaces/InduwaraR/Interest_Predictions/resolve/main/src/test_ml_endpoints.py
- Command line
-
hf download hf://spaces/InduwaraR/Interest_Predictions/src/test_ml_endpoints.py
-
curl -L -o test_ml_endpoints.py https://huggingface.co/spaces/InduwaraR/Interest_Predictions/resolve/main/src/test_ml_endpoints.py
3.13 kB
| 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 |