Files
o2o-weather/backend/tests/test_index_slots.py
T

74 lines
2.8 KiB
Python

from datetime import date, timedelta
from types import SimpleNamespace
from common.enums import RainType, Sky
from common.utils.kma_time import kst_midnight
from services.calc.index_calc import Context, DayForecast
from services.index_service import day_result, slot_days, slot_label
REGION = SimpleNamespace(lat=37.56, lon=126.97)
SAT = date(2026, 10, 10)
CTX = Context(day=SAT, is_holiday=False, pm25=10, air_label="좋음", moon=0.5, sunset_score=None, wind_warning=False, lookahead_rain_prob=0)
DEFN = SimpleNamespace(
weight_temp=40, weight_rain=40, weight_wind=20, weight_humidity=0, weight_air=0, weight_time=0,
factor_rules={
"temp": {"type": "range", "lo": 18, "hi": 24}, "rain": {"dir": "dry"}, "wind": {"type": "calm", "ok": 4, "limit": 12},
"humidity": {"type": "ignore"}, "time": {"type": "fixed", "value": 100},
},
zero_rules=[], windows=[{"from": 11, "to": 16}],
)
FC = {SAT: DayForecast(temp_min_c=12, temp_max_c=22, rain_prob_am=10, rain_prob_pm=20, sky=Sky.CLEAR)}
def _hourly(day: date) -> dict:
start = kst_midnight(day)
return {
start + timedelta(hours=h): SimpleNamespace(
forecast_at=start + timedelta(hours=h), feels_c=21, rain_type=RainType.NONE.value, rain_prob=0, wind_ms=1.0, humidity=50, sky=Sky.CLEAR.value,
)
for h in range(24)
}
def _d(day: int) -> date:
return date(2026, 10, day)
def test_slots_on_weekdays_are_today_tomorrow_sat_sun():
for today in (5, 6, 7, 8):
assert slot_days(_d(today)) == [_d(today), _d(today + 1), _d(10), _d(11)]
def test_slots_on_friday_keep_this_sunday_then_next_saturday():
assert slot_days(_d(9)) == [_d(9), _d(10), _d(11), _d(17)]
def test_slots_on_saturday_and_sunday_move_to_next_weekend():
assert slot_days(_d(10)) == [_d(10), _d(11), _d(17), _d(18)]
assert slot_days(_d(11)) == [_d(11), _d(12), _d(17), _d(18)]
def test_slot_labels():
today = date(2026, 10, 6)
assert [slot_label(d, today) for d in slot_days(today)] == ["오늘", "내일", "토 10.10", "일 10.11"]
assert [slot_label(d, _d(9)) for d in slot_days(_d(9))] == ["오늘", "내일", "일 10.11", "토 10.17"]
def test_weekend_in_short_range_uses_hourly_score():
r = day_result(DEFN, SAT, REGION, _hourly(SAT), FC, CTX, short=False)
assert r.estimated is False and r.factors["wind"] == 100
def test_weekend_beyond_short_range_uses_mid_forecast_estimate():
r = day_result(DEFN, SAT, REGION, {}, FC, CTX, short=False)
assert r.estimated is True and r.factors["wind"] == 70 and r.factors["rain"] == 95
def test_partial_hourly_falls_back_to_estimate():
partial = {k: v for k, v in _hourly(SAT).items() if k.hour < 13}
assert day_result(DEFN, SAT, REGION, partial, FC, CTX, short=False).estimated is True
def test_no_mid_forecast_gives_no_score():
assert day_result(DEFN, SAT, REGION, {}, {}, CTX, short=False) is None