Merge branch 'main' into feat/open-meteo
# Conflicts: # backend/router/v1/index/index.py # backend/services/calc/index_calc.py # backend/services/index_service.py # backend/tests/conftest.py # backend/tests/test_index_api.py # backend/tests/test_index_calc.py # backend/tests/test_sky_api.py # backend/worker_main.py
This commit is contained in:
commit
1a1df4bb87
111 files changed
+5787
-679
No files matched your search
@@ -1,8 +1,12 @@
|
||||
from dataclasses import replace
|
||||
from datetime import date, datetime, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
from common.enums import RainType, Sky
|
||||
from services.calc.index_calc import Context, Hour, evaluate, rain_score, sun_score, temp_score, uv_score, view_score, zero_rules_text
|
||||
from services.calc.index_calc import (
|
||||
OUTING_HOURS, Context, DayForecast, Hour, axis_label, estimate, evaluate, hour_temp, outing, outing_estimate, rain_score, sun_score, temp_score,
|
||||
uv_score, view_score, zero_rules_text,
|
||||
)
|
||||
from services.calc.moon import illumination
|
||||
|
||||
CTX = Context(day=date(2026, 10, 2), is_holiday=False, pm25=10, air_label="좋음", moon=0.5, sunset_score=80, wind_warning=False, lookahead_rain_prob=0)
|
||||
@@ -44,7 +48,7 @@ def test_temp_peaks_at_best_and_loses_8_per_degree_outside():
|
||||
assert temp_score({"type": "range", "lo": 18, "hi": 24, "best": 22}, 22)[0] == 100
|
||||
assert temp_score({"type": "range", "lo": 18, "hi": 24}, 15)[0] == 61
|
||||
assert temp_score({"type": "lower", "zero": 25, "full": 5}, 15)[0] == 50
|
||||
assert temp_score({"type": "range", "lo": 10, "hi": 20, "offset": -6}, 21)[1].startswith("정상 15°")
|
||||
assert temp_score({"type": "range", "lo": 10, "hi": 20, "offset": -6}, 21)[1].startswith("산 정상 기준 15°")
|
||||
|
||||
|
||||
def test_temp_away_rises_outside_comfort():
|
||||
@@ -63,7 +67,28 @@ def test_rain_dry_and_wet():
|
||||
def test_zero_rule_forces_zero_and_cap_limits():
|
||||
assert evaluate(_defn(), [[_h(19, prob=80)]], CTX).score == 0
|
||||
capped = _defn(zero_rules=[{"field": "rain_prob", "op": ">=", "value": 70, "cap": 20}])
|
||||
assert evaluate(capped, [[_h(19, prob=70)]], CTX).score <= 20
|
||||
r = evaluate(capped, [[_h(19, prob=70)]], CTX)
|
||||
assert r.score <= 20 and r.zero_reason == "비 확률 70% 이상이라 최대 20점"
|
||||
assert evaluate(_defn(), [[_h(19, prob=80)]], CTX).zero_reason == "비 확률 80% 이상이라 0점"
|
||||
|
||||
|
||||
def test_lookahead_rain_is_named_in_note():
|
||||
defn = _defn(windows=[{"from": 9, "to": 18, "lookahead_days": 2}])
|
||||
r = evaluate(defn, [[_h(10, prob=0)]], replace(CTX, lookahead_rain_prob=60))
|
||||
assert r.notes["rain"] == "모레까지 비 확률 60%"
|
||||
assert evaluate(defn, [[_h(10, prob=0)]], CTX).notes["rain"] == "비 확률 0%"
|
||||
|
||||
|
||||
def test_air_note_has_unit():
|
||||
assert evaluate(_defn(), [[_h(19)]], CTX).notes["air"] == "초미세먼지 10㎍/㎥, 좋음"
|
||||
|
||||
|
||||
def test_axis_label_reads_outward_as_good():
|
||||
rules = _defn().factor_rules
|
||||
assert [axis_label(a, rules) for a in ("temp", "rain", "wind", "humidity", "air", "time")] == \
|
||||
["기온 알맞음", "비 안 옴", "바람 잔잔", "습도 알맞음", "공기 맑음", "요일 알맞음"]
|
||||
wet = {"temp": {"type": "lower"}, "rain": {"dir": "wet"}, "wind": {"type": "strong"}, "humidity": {"type": "higher"}, "time": {"type": "moon"}}
|
||||
assert [axis_label(a, wet) for a in ("temp", "rain", "wind", "humidity", "time")] == ["추움", "비 옴", "바람 셈", "습도 높음", "달 어두움"]
|
||||
|
||||
|
||||
def test_best_window_is_chosen():
|
||||
@@ -73,7 +98,11 @@ def test_best_window_is_chosen():
|
||||
|
||||
def test_zero_rules_text():
|
||||
assert zero_rules_text([{"field": "rain_prob", "op": ">=", "value": 80}, {"field": "wind_ms", "op": ">=", "value": 14}]) == \
|
||||
"비 확률 80% 이상 또는 바람 14m/s 이상이면 바로 0점"
|
||||
"비 확률 80% 이상이거나 바람 14m/s 이상이면 0점"
|
||||
assert zero_rules_text([{"field": "rain_prob", "op": ">=", "value": 70}, {"field": "warning", "op": "==", "value": "wind"}]) == \
|
||||
"비 확률 70% 이상이거나 강풍 특보가 있으면 0점"
|
||||
assert zero_rules_text([{"field": "rain_prob", "op": ">=", "value": 70, "cap": 20}]) == "비 확률 70% 이상이면 최대 20점"
|
||||
assert zero_rules_text([{"field": "pm25", "op": ">=", "value": 75}]) == "초미세먼지 75㎍/㎥ 이상이면 0점"
|
||||
assert zero_rules_text([]) == "없음 (재미로 보는 지수)"
|
||||
|
||||
|
||||
@@ -118,5 +147,75 @@ def test_gust_and_ground_temperature_zero_rules():
|
||||
assert evaluate(gust, [[_h(19, wind=1.0, gust_ms=10.0)]], CTX).score == 100
|
||||
assert evaluate(gust, [[_h(19, wind=1.0)]], CTX).score == 100 # 값이 없으면 걸리지 않는다
|
||||
ground = [{"field": "ground_c", "op": ">=", "value": 45}]
|
||||
assert zero_rules_text(ground) == "지표면 온도 45° 이상이면 바로 0점"
|
||||
assert evaluate(_defn(zero_rules=ground), [[_h(19, wind=1.0, ground_c=47.0)]], CTX).zero_reason == "지표면 온도 45° 이상이면"
|
||||
assert zero_rules_text(ground) == "지표면 온도 45° 이상이면 0점"
|
||||
assert evaluate(_defn(zero_rules=ground), [[_h(19, wind=1.0, ground_c=47.0)]], CTX).zero_reason == "지표면 온도 45° 이상이라 0점"
|
||||
|
||||
|
||||
def test_estimate_and_labels_cover_openmeteo_axes():
|
||||
r = estimate(_uv_defn(), _fc(), [[19, 20]], CTX)
|
||||
assert r.factors["uv"] == 70 and r.factors["sun"] == 0 and r.factors["view"] == 0
|
||||
assert [axis_label(a, {}) for a in ("uv", "sun", "view")] == ["자외선", "햇빛", "가시거리"]
|
||||
|
||||
|
||||
def _fc(lo=15.0, hi=23.0, am=0, pm=0, sky=Sky.CLEAR):
|
||||
return DayForecast(temp_min_c=lo, temp_max_c=hi, rain_prob_am=am, rain_prob_pm=pm, sky=sky)
|
||||
|
||||
|
||||
def test_hour_temp_follows_min_at_dawn_and_max_mid_afternoon():
|
||||
assert hour_temp(10, 20, 6) == 10 and hour_temp(10, 20, 15) == 20
|
||||
assert 10 < hour_temp(10, 20, 21) < 20 and hour_temp(10, 20, 30) == 10
|
||||
|
||||
|
||||
def test_estimate_is_flagged_and_unknown_factors_are_70():
|
||||
r = estimate(_defn(), _fc(), [[19, 20]], CTX)
|
||||
assert r.estimated is True
|
||||
assert r.factors["wind"] == 70 and r.factors["humidity"] == 70 and r.factors["air"] == 70
|
||||
assert "예상" in r.summary
|
||||
|
||||
|
||||
def test_estimate_uses_rain_prob_of_the_window_half_day():
|
||||
morning, evening = estimate(_defn(), _fc(am=60, pm=0), [[9, 10]], CTX), estimate(_defn(), _fc(am=60, pm=0), [[19, 20]], CTX)
|
||||
assert morning.factors["rain"] == 40 and evening.factors["rain"] == 100
|
||||
|
||||
|
||||
def test_estimate_applies_rain_zero_rule_only():
|
||||
r = estimate(_defn(zero_rules=[{"field": "rain_prob", "op": ">=", "value": 80}, {"field": "wind_ms", "op": ">=", "value": 1}]), _fc(pm=80), [[19]], CTX)
|
||||
assert r.score == 0 and r.zero_reason.startswith("비 확률 80% 이상")
|
||||
|
||||
|
||||
def test_estimate_without_temps_gives_none():
|
||||
assert estimate(_defn(), _fc(lo=None, hi=None), [[19]], CTX) is None
|
||||
|
||||
|
||||
def _day(**kw):
|
||||
return [_h(h, **kw) for h in range(*OUTING_HOURS)]
|
||||
|
||||
|
||||
def test_outing_perfect_day_is_100_and_reason_names_rain_temp_sky():
|
||||
r = outing(_day(wind=1.0))
|
||||
assert r.score == 100 and r.estimated is False
|
||||
assert r.reason == "비 0% · 체감 21° · 맑음"
|
||||
|
||||
|
||||
def test_outing_cloudy_and_hot_days_lose_points():
|
||||
clear, cloudy, hot = outing(_day()), outing(_day(sky=Sky.CLOUDY)), outing(_day(feels=30.0))
|
||||
assert cloudy.score < clear.score and hot.score < clear.score
|
||||
assert "흐림" in cloudy.reason
|
||||
|
||||
|
||||
def test_outing_rain_caps_score():
|
||||
r = outing(_day(prob=70))
|
||||
assert r.score <= 30 and r.capped
|
||||
assert outing([_h(10, prob=30, rain=RainType.RAIN)] + _day()[1:]).score <= 30
|
||||
|
||||
|
||||
def test_outing_needs_all_hours():
|
||||
assert outing(_day()[:3]) is None
|
||||
|
||||
|
||||
def test_outing_estimate_from_mid_forecast():
|
||||
r = outing_estimate(_fc(lo=12, hi=22, am=10, pm=20))
|
||||
assert r.estimated is True and r.factors["wind"] == 70
|
||||
assert r.reason.startswith("비 20% · 낮 ")
|
||||
assert outing_estimate(_fc(pm=60)).score <= 30
|
||||
assert outing_estimate(_fc(lo=None, hi=None)) is None
|
||||
Reference in new issue
Block a user