File size: 1,771 Bytes
8b96826 | 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 | """Risk formula: deterministic, auditable, edge-case safe."""
from app.risk import clamp, compute_risk
def test_no_factors_is_low_risk():
result = compute_risk()
assert result["score"] == 0
assert result["level"] == "LOW"
def test_heavy_everything_caps_at_100():
result = compute_risk(
traffic_delay_pct=2.0, # 200% slower
rain_mm=50,
event_impact=2,
festival_impact=2,
advisory_count=10,
)
assert result["score"] == 100
assert result["level"] == "HIGH"
def test_traffic_only_moderate_delay():
# 25% slower than free flow -> 20 traffic points -> LOW
result = compute_risk(traffic_delay_pct=0.25)
assert result["breakdown"]["traffic"] == 20.0
assert result["level"] == "LOW"
def test_festival_pushes_to_medium():
# City-wide festival (Bada Mangal) + modest traffic = MEDIUM
result = compute_risk(traffic_delay_pct=0.25, festival_impact=2)
assert result["score"] == 40
assert result["level"] == "MEDIUM"
def test_rain_plus_festival_plus_traffic_is_high():
result = compute_risk(traffic_delay_pct=0.4, rain_mm=5, festival_impact=2)
# 32 + 15 + 20 = 67
assert result["score"] == 67
assert result["level"] == "HIGH"
def test_rain_points_capped_at_20():
result = compute_risk(rain_mm=100)
assert result["breakdown"]["rain"] == 20
def test_level_boundaries():
assert compute_risk(traffic_delay_pct=0.36)["level"] == "LOW" # 28.8 -> 29
assert compute_risk(traffic_delay_pct=0.38)["level"] == "MEDIUM" # 30.4 -> 30
assert compute_risk(traffic_delay_pct=0.5, festival_impact=2)["level"] == "HIGH" # 60
def test_clamp():
assert clamp(5, 0, 10) == 5
assert clamp(-1, 0, 10) == 0
assert clamp(99, 0, 10) == 10
|