Spaces:
Running
Running
File size: 13,319 Bytes
eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 6a28f91 eadbc29 |
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
"""Integration tests for simulation engine.
Tests multi-day simulation, case progression, ripeness tracking, and outcome validation.
"""
from datetime import date
import pytest
from src.data.case_generator import CaseGenerator
from src.simulation.engine import CourtSim, CourtSimConfig
@pytest.mark.integration
@pytest.mark.simulation
class TestSimulationBasics:
"""Test basic simulation execution."""
def test_single_day_simulation(self, small_case_set, temp_output_dir):
"""Test running a 1-day simulation."""
config = CourtSimConfig(
start=date(2024, 1, 15), # Monday
days=1,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, small_case_set)
result = sim.run()
assert result is not None
assert result.hearings_total >= 0
assert result.end_date == config.start
def test_week_simulation(self, sample_cases, temp_output_dir):
"""Test running a 1-week (5 working days) simulation."""
config = CourtSimConfig(
start=date(2024, 1, 15), # Monday
days=7,
seed=42,
courtrooms=3,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, sample_cases)
result = sim.run()
assert result.hearings_total > 0
# Should have had some disposals
assert result.disposals >= 0
@pytest.mark.slow
def test_month_simulation(self, sample_cases, temp_output_dir):
"""Test running a 30-day simulation."""
config = CourtSimConfig(
start=date(2024, 1, 1),
days=30,
seed=42,
courtrooms=5,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, sample_cases)
result = sim.run()
assert result.hearings_total > 0
assert (
result.hearings_heard + result.hearings_adjourned == result.hearings_total
)
# Check disposal rate is reasonable
if result.hearings_total > 0:
disposal_rate = result.disposals / len(sample_cases)
assert 0.0 <= disposal_rate <= 1.0
@pytest.mark.integration
@pytest.mark.simulation
class TestOutcomeTracking:
"""Test tracking of simulation outcomes."""
def test_disposal_counting(self, small_case_set, temp_output_dir):
"""Test that disposals are counted correctly."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=30,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, small_case_set)
result = sim.run()
# Count disposed cases
disposed_count = sum(1 for case in small_case_set if case.is_disposed())
# Should match result
assert result.disposals == disposed_count
def test_adjournment_rate(self, sample_cases, temp_output_dir):
"""Test that adjournment rate is realistic."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=30,
seed=42,
courtrooms=5,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, sample_cases)
result = sim.run()
if result.hearings_total > 0:
adj_rate = result.hearings_adjourned / result.hearings_total
# Realistic adjournment rate: 20-60%
assert 0.0 <= adj_rate <= 1.0
def test_utilization_calculation(self, sample_cases, temp_output_dir):
"""Test courtroom utilization calculation."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=20,
seed=42,
courtrooms=3,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, sample_cases)
result = sim.run()
# Utilization should be 0-100%
assert 0.0 <= result.utilization <= 100.0
@pytest.mark.integration
@pytest.mark.simulation
class TestStageProgression:
"""Test case stage progression during simulation."""
def test_cases_progress_stages(self, sample_cases, temp_output_dir):
"""Test that cases progress through stages."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=90,
seed=42,
courtrooms=5,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
# Record initial stages
initial_stages = {case.case_id: case.current_stage for case in sample_cases}
sim = CourtSim(config, sample_cases)
sim.run()
# Check if any cases progressed
progressed = sum(
1
for case in sample_cases
if case.current_stage != initial_stages.get(case.case_id)
)
# At least some cases should progress
assert progressed >= 0
def test_terminal_stage_handling(self, sample_cases, temp_output_dir):
"""Test that cases in terminal stages are handled correctly."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=60,
seed=42,
courtrooms=5,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, sample_cases)
sim.run()
# Check disposed cases are in terminal stages
from src.data.config import TERMINAL_STAGES
for case in sample_cases:
if case.is_disposed():
assert case.current_stage in TERMINAL_STAGES
@pytest.mark.integration
@pytest.mark.simulation
class TestRipenessIntegration:
"""Test ripeness classification integration."""
def test_ripeness_reevaluation(self, sample_cases, temp_output_dir):
"""Test that ripeness is re-evaluated during simulation."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=30,
seed=42,
courtrooms=5,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, sample_cases)
result = sim.run()
# Check ripeness transitions tracked
assert result.ripeness_transitions >= 0
def test_unripe_filtering(self, temp_output_dir):
"""Test that unripe cases are filtered from scheduling."""
# Create mix of ripe and unripe cases
generator = CaseGenerator(
start=date(2024, 1, 1), end=date(2024, 1, 10), seed=42
)
cases = generator.generate(50)
# Mark some as unripe
for i, case in enumerate(cases):
if i % 3 == 0:
case.service_status = "PENDING"
case.purpose_of_hearing = "FOR SUMMONS"
config = CourtSimConfig(
start=date(2024, 2, 1),
days=10,
seed=42,
courtrooms=3,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, cases)
result = sim.run()
# Should have filtered some unripe cases
assert result.unripe_filtered >= 0
@pytest.mark.integration
@pytest.mark.edge_case
class TestSimulationEdgeCases:
"""Test simulation edge cases."""
def test_zero_initial_cases(self, temp_output_dir):
"""Test simulation with no initial cases."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=10,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, [])
result = sim.run()
# Should complete without errors
assert result.hearings_total == 0
assert result.disposals == 0
def test_all_cases_disposed_early(self, temp_output_dir):
"""Test when all cases dispose before simulation end."""
# Create very simple cases that dispose quickly
generator = CaseGenerator(start=date(2024, 1, 1), end=date(2024, 1, 5), seed=42)
cases = generator.generate(5)
# Set all to near-disposal stage
for case in cases:
case.current_stage = "ORDERS"
case.service_status = "SERVED"
config = CourtSimConfig(
start=date(2024, 2, 1),
days=90,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, cases)
result = sim.run()
# Should handle gracefully
assert result.disposals <= len(cases)
@pytest.mark.failure
def test_invalid_start_date(self, small_case_set, temp_output_dir):
"""Test simulation with invalid start date."""
with pytest.raises(ValueError):
CourtSimConfig(
start="invalid-date", # Should be date object
days=10,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
@pytest.mark.failure
def test_negative_days(self, small_case_set, temp_output_dir):
"""Test simulation with negative days."""
with pytest.raises(ValueError):
CourtSimConfig(
start=date(2024, 1, 15),
days=-10,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
@pytest.mark.integration
@pytest.mark.simulation
class TestEventLogging:
"""Test event logging functionality."""
def test_events_written(self, small_case_set, temp_output_dir):
"""Test that events are written to CSV."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=5,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, small_case_set)
sim.run()
# Check if events file exists
events_file = temp_output_dir / "events.csv"
if events_file.exists():
# Verify it's readable
import pandas as pd
df = pd.read_csv(events_file)
assert len(df) >= 0
def test_event_count_matches_hearings(self, small_case_set, temp_output_dir):
"""Test that event count matches total hearings."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=10,
seed=42,
courtrooms=2,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir,
)
sim = CourtSim(config, small_case_set)
sim.run()
# Events should correspond to hearings
events_file = temp_output_dir / "events.csv"
if events_file.exists():
import pandas as pd
pd.read_csv(events_file)
# Event count should match or be close to hearings_total
# (may have additional events for filings, etc.)
@pytest.mark.integration
@pytest.mark.simulation
class TestPolicyComparison:
"""Test different scheduling policies."""
def test_fifo_policy(self, sample_cases, temp_output_dir):
"""Test simulation with FIFO policy."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=20,
seed=42,
courtrooms=3,
daily_capacity=50,
policy="fifo",
log_dir=temp_output_dir / "fifo",
)
sim = CourtSim(config, sample_cases.copy())
result = sim.run()
assert result.hearings_total > 0
def test_age_policy(self, sample_cases, temp_output_dir):
"""Test simulation with age-based policy."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=20,
seed=42,
courtrooms=3,
daily_capacity=50,
policy="age",
log_dir=temp_output_dir / "age",
)
sim = CourtSim(config, sample_cases.copy())
result = sim.run()
assert result.hearings_total > 0
def test_readiness_policy(self, sample_cases, temp_output_dir):
"""Test simulation with readiness policy."""
config = CourtSimConfig(
start=date(2024, 1, 15),
days=20,
seed=42,
courtrooms=3,
daily_capacity=50,
policy="readiness",
log_dir=temp_output_dir / "readiness",
)
sim = CourtSim(config, sample_cases.copy())
result = sim.run()
assert result.hearings_total > 0
|