Files
o2o-weather/backend/tests/test_index_season.py

56 lines
2.9 KiB
Python

from datetime import date
from types import SimpleNamespace
from common.enums import RainType, Sky
from common.utils.kma_time import KST
from services.calc.index_calc import Context, DayForecast, Hour, estimate, evaluate, time_score, zero_rules_text
from services.calc.sun import sun_times
from services.index_service import in_season, season_badge, season_of, window_hours
CTX = Context(day=date(2027, 1, 9), is_holiday=False, pm25=10, air_label="좋음", moon=0.5, sunset_score=None, wind_warning=False, lookahead_rain_prob=0)
SNOWMAN = SimpleNamespace(
weight_temp=50, weight_rain=10, weight_wind=20, weight_humidity=0, weight_air=0, weight_time=20,
factor_rules={"temp": {"type": "range", "lo": -5, "hi": 2, "best": -1}, "rain": {"dir": "wet"}, "wind": {"type": "calm", "ok": 3, "limit": 10},
"humidity": {"type": "ignore"}, "time": {"type": "fixed", "value": 100}},
zero_rules=[{"field": "snow", "op": "==", "value": "no"}], windows=[{"from": 9, "to": 16}],
)
def _h(hour, rain=RainType.NONE, feels=-1.0):
return Hour(hour=hour, feels_c=feels, rain_type=rain, rain_prob=80 if rain is not RainType.NONE else 0, wind_ms=1.0, humidity=70, sky=Sky.CLOUDY)
def test_season_names_and_in_season():
assert [season_of(m) for m in (1, 3, 6, 9, 12)] == ["겨울", "봄", "여름", "가을", "겨울"]
always, winter = SimpleNamespace(months=[]), SimpleNamespace(months=[12, 1, 2])
assert in_season(always, 7) and in_season(winter, 1) and not in_season(winter, 7)
# 일 년 내내인 각엔 계절 표시가 없다
assert season_badge(always, 1) is None and season_badge(winter, 1) == "겨울" and season_badge(winter, 7) is None
assert in_season(SimpleNamespace(), 5)
def test_snowman_is_zero_without_snow_and_high_when_snowing_near_zero():
dry = evaluate(SNOWMAN, [[_h(h) for h in range(9, 16)]], CTX)
assert dry.score == 0 and dry.zero_reason == "눈 소식이 없어 0점"
snowy = evaluate(SNOWMAN, [[_h(h, RainType.SNOW) for h in range(9, 16)]], CTX)
assert snowy.score >= 80 and snowy.zero_reason is None
assert zero_rules_text(SNOWMAN.zero_rules) == "눈 소식이 없으면 0점"
def test_snowman_estimate_from_mid_forecast_is_zero():
fc = DayForecast(temp_min_c=-4, temp_max_c=1, rain_prob_am=70, rain_prob_pm=70, sky=Sky.CLOUDY)
assert estimate(SNOWMAN, fc, [list(range(9, 16))], CTX).score == 0
def test_sunrise_score_time_rule():
assert time_score({"type": "sunrise_score"}, Context(**{**CTX.__dict__, "sunrise_score": 72})) == (72, "일출 하늘 점수 72")
assert time_score({"type": "sunrise_score"}, CTX)[0] == 70
def test_window_around_sunrise_covers_sunrise_hour():
region = SimpleNamespace(lat=37.56, lon=126.97)
day = date(2027, 1, 1)
h = sun_times(day, region.lat, region.lon).sunrise_at.astimezone(KST).hour
assert window_hours(SimpleNamespace(windows=[{"around": "sunrise", "hours": 1}]), day, region) == [[h - 1, h, h + 1]]