File size: 7,647 Bytes
1009dbf e9c00d1 7b2b891 1009dbf e9c00d1 1009dbf e9c00d1 7b2b891 1009dbf 14c79c4 1009dbf 7b2b891 1009dbf 7b2b891 1009dbf ebb16fa 1009dbf 14c79c4 1009dbf | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | """Integration tests for simulation API endpoints."""
from __future__ import annotations
import pytest
from rest_framework import status
from rest_framework.test import APIClient
from apps.profiles.models import DailyProgress, Profile, WallSection
@pytest.mark.django_db
class TestSimulationAPI:
"""Test cases for simulation API endpoints."""
def test_simulate_endpoint_creates_profiles_and_sections(
self,
api_client: APIClient,
) -> None:
"""Test that simulation endpoint creates profiles and sections."""
config_data = {
"config": "21 25 28\n17\n17 22 17 19 17",
"num_teams": 4,
"start_date": "2025-10-20",
}
response = api_client.post("/api/profiles/simulate/", config_data, format="json")
assert response.status_code == status.HTTP_201_CREATED
assert Profile.objects.count() == 3
assert WallSection.objects.count() == 9
def test_simulate_endpoint_runs_simulation(
self,
api_client: APIClient,
) -> None:
"""Test that simulation endpoint creates daily progress records."""
config_data = {
"config": "28 29",
"num_teams": 2,
"start_date": "2025-10-20",
}
response = api_client.post("/api/profiles/simulate/", config_data, format="json")
assert response.status_code == status.HTTP_201_CREATED
assert DailyProgress.objects.count() > 0
assert response.data["total_sections"] == 2
assert int(response.data["total_days"]) > 0
def test_simulate_endpoint_validates_config(
self,
api_client: APIClient,
) -> None:
"""Test that simulation endpoint validates config."""
config_data = {
"config": "21 abc 28",
"num_teams": 2,
}
response = api_client.post("/api/profiles/simulate/", config_data, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "config" in response.data
assert "Invalid number format" in str(response.data["config"])
def test_simulate_endpoint_requires_config(
self,
api_client: APIClient,
) -> None:
"""Test that simulation endpoint requires config."""
config_data = {
"num_teams": 2,
}
response = api_client.post("/api/profiles/simulate/", config_data, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "config" in response.data
assert "config parameter is required" in str(response.data["config"])
def test_simulate_endpoint_validates_num_teams(
self,
api_client: APIClient,
) -> None:
"""Test that simulation endpoint validates num_teams."""
config_data = {
"config": "21 25 28",
"num_teams": -1,
}
response = api_client.post("/api/profiles/simulate/", config_data, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert "num_teams" in response.data
assert "must be at least" in str(response.data["num_teams"])
def test_days_endpoint_returns_ice_usage(
self,
api_client: APIClient,
) -> None:
"""Test GET /profiles/<id>/days/<day>/ endpoint."""
config_data = {
"config": "28 29",
"num_teams": 2,
"start_date": "2025-10-20",
}
api_client.post("/api/profiles/simulate/", config_data, format="json")
profile = Profile.objects.first()
assert profile is not None
response = api_client.get(f"/api/profiles/{profile.id}/days/1/")
assert response.status_code == status.HTTP_200_OK
assert "day" in response.data
assert "total_feet_built" in response.data
assert "total_ice_cubic_yards" in response.data
assert "sections" in response.data
assert response.data["day"] == 1
assert isinstance(response.data["sections"], list)
assert len(response.data["sections"]) == 2
def test_days_endpoint_returns_404_for_no_data(
self,
api_client: APIClient,
) -> None:
"""Test days endpoint returns 404 when no simulation data."""
profile = Profile.objects.create(name="Test", team_lead="Lead")
response = api_client.get(f"/api/profiles/{profile.id}/days/1/")
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_overview_by_day_endpoint(
self,
api_client: APIClient,
) -> None:
"""Test GET /profiles/<id>/overview/<day>/ endpoint."""
config_data = {
"config": "28 29",
"num_teams": 2,
"start_date": "2025-10-20",
}
api_client.post("/api/profiles/simulate/", config_data, format="json")
profile = Profile.objects.first()
assert profile is not None
response = api_client.get(f"/api/profiles/{profile.id}/overview/1/")
assert response.status_code == status.HTTP_200_OK
assert "day" in response.data
assert "cost" in response.data
assert response.data["day"] == "1"
def test_overview_all_by_day_endpoint(
self,
api_client: APIClient,
) -> None:
"""Test GET /profiles/overview/<day>/ endpoint."""
config_data = {
"config": "28\n29",
"num_teams": 2,
"start_date": "2025-10-20",
}
api_client.post("/api/profiles/simulate/", config_data, format="json")
response = api_client.get("/api/profiles/overview/1/")
assert response.status_code == status.HTTP_200_OK
assert "day" in response.data
assert "cost" in response.data
assert response.data["day"] == "1"
def test_overview_total_endpoint(
self,
api_client: APIClient,
) -> None:
"""Test GET /profiles/overview/ endpoint returns total days.
The endpoint now calculates and returns the actual number of
construction days from DailyProgress records instead of None.
"""
config_data = {
"config": "28 29",
"num_teams": 2,
"start_date": "2025-10-20",
}
api_client.post("/api/profiles/simulate/", config_data, format="json")
response = api_client.get("/api/profiles/overview/")
assert response.status_code == status.HTTP_200_OK
assert "day" in response.data
assert "cost" in response.data
# Verify day is now calculated from simulation data
assert response.data["day"] > 0
assert isinstance(response.data["day"], int)
def test_spec_example_simulation(
self,
api_client: APIClient,
) -> None:
"""Test simulation with spec example data."""
config_data = {
"config": "21 25 28\n17\n17 22 17 19 17",
"num_teams": 20,
"start_date": "2025-10-20",
}
response = api_client.post("/api/profiles/simulate/", config_data, format="json")
assert response.status_code == status.HTTP_201_CREATED
assert response.data["total_sections"] == 9
profile_1 = Profile.objects.get(name="Profile 1")
day_1_response = api_client.get(f"/api/profiles/{profile_1.id}/days/1/")
assert day_1_response.status_code == status.HTTP_200_OK
assert day_1_response.data["total_ice_cubic_yards"] == "585"
overview_response = api_client.get("/api/profiles/overview/")
assert overview_response.status_code == status.HTTP_200_OK
|