File size: 2,858 Bytes
dccc3ae 9c5290d | 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 | import pytest
from fastapi.testclient import TestClient
from backend.api_server import app
import backend.api_server as api_server
import unittest.mock as mock
# Reuse the FastAPI app through TestClient
client = TestClient(app)
# Initialize a mock predictor to avoid 503 errors
api_server.predictor = mock.Mock()
def test_health_endpoint():
"""Verify that the health check returns expected status and timestamp."""
response = client.get("/health")
assert response.status_code == 200
data = response.json()
assert data["status"] in ["healthy", "degraded"]
assert "components" in data
def test_predict_endpoint_no_data():
"""Verify 404 behavior when a match_id does not exist in Redis."""
# We mock the predictor to return an error dictionary
api_server.predictor.predict_live_match.return_value = {"error": "No live data available"}
response = client.get("/predict/invalid_match_id")
assert response.status_code == 404
assert response.json()["detail"] == "No live data available"
def test_list_matches_empty():
"""Verify match listing works even when Redis is empty."""
with mock.patch("redis.Redis.keys") as mock_keys:
mock_keys.return_value = []
response = client.get("/matches")
assert response.status_code == 200
assert response.json() == []
@pytest.mark.asyncio
async def test_predict_success():
"""Verify 200 OK and JSON structure for a successful prediction flow."""
mock_result = {
"match_id": "test_match",
"win_probability": 0.65,
"confidence": 0.85,
"balls_analyzed": 10
}
api_server.predictor.predict_live_match.return_value = mock_result
response = client.get("/predict/test_match")
assert response.status_code == 200
assert response.json()["win_probability"] == 0.65
def test_upcoming_schedule():
"""Verify upcoming matches endpoint returns schedule data from the CSV."""
response = client.get("/upcoming/2026")
assert response.status_code == 200
data = response.json()
assert "matchschedule" in data
# The schedule CSV has 70 entries; at least some should be upcoming
assert isinstance(data["matchschedule"], list)
if len(data["matchschedule"]) > 0:
match = data["matchschedule"][0]
assert "teama" in match
assert "teamb" in match
assert "venue" in match
assert "matchdate" in match
def test_points_table():
"""Verify points table endpoint returns all 10 IPL teams."""
response = client.get("/points/2026")
assert response.status_code == 200
data = response.json()
assert "points" in data
assert len(data["points"]) == 10
team = data["points"][0]
assert "name" in team
assert "teamshortname" in team
assert "matchesplayed" in team
assert "points" in team
|