구름 - 노을·일출 점수를 구 중심 20·100km 두 지점으로 계산 (50km 제외), 모델은 JMA·ICON 두 개만 사용 - cloud_hourly를 (구 코드, 방향, 거리, 시각, 모델) 키로 바꾸고 점수에 쓰는 시각만 저장 보조값 - openmeteo_hourly 신규: 자외선·가시거리·돌풍·기압·지표면 온도(끝 시각 값), 일조(3시간 합), 일사(평균·최대) - worker가 03·09·15·21시 10분에 구 256곳을 요청 11번으로 일괄 수집, 7일 지난 행 정리 - 새 응답에서 값이 비어 오면 이전 값을 유지(COALESCE) 지수 - 새 축 uv·sun·view 추가 (index_definition/index_score 칼럼, 비중 있는 지수에만 상세에 표시) - 빨래·멍뭉산책·피크닉·등산·러닝·가드닝·별보기·사진산책에 비중 배분, 자전거·캠핑·멍뭉산책에 돌풍·지표면 온도 0점 조건 - 자외선·가시거리 등 순간값은 가장 가까운 3시간 구간, 일조는 덮는 구간을 계산 창에 연결 - 보조값이 새로 오면 지수를 다시 계산하도록 재계산 기준에 fetched_at 추가 테스트 102개 통과 (PGSSLMODE=disable, JWT는 테스트용 환경변수).
123 lines
6.0 KiB
Python
123 lines
6.0 KiB
Python
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.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)
|
|
|
|
|
|
def _defn(**kw):
|
|
base = dict(
|
|
weight_temp=30, weight_rain=25, weight_wind=15, weight_humidity=10, weight_air=10, weight_time=10,
|
|
factor_rules={
|
|
"temp": {"type": "range", "lo": 18, "hi": 24}, "rain": {"dir": "dry"}, "wind": {"type": "calm", "ok": 2, "limit": 10},
|
|
"humidity": {"type": "range", "lo": 40, "hi": 60}, "time": {"type": "weekday", "fri": 100, "default": 78},
|
|
},
|
|
zero_rules=[{"field": "rain_prob", "op": ">=", "value": 80}],
|
|
)
|
|
return SimpleNamespace(**{**base, **kw})
|
|
|
|
|
|
def _h(hour, feels=21.0, prob=0, rain=RainType.NONE, wind=1.5, hum=50, sky=Sky.CLEAR, **extra):
|
|
return Hour(hour=hour, feels_c=feels, rain_type=rain, rain_prob=prob, wind_ms=wind, humidity=hum, sky=sky, **extra)
|
|
|
|
|
|
def test_perfect_evening_scores_100():
|
|
r = evaluate(_defn(), [[_h(19, wind=1.0), _h(20, wind=1.0)]], CTX)
|
|
assert r.score == 100 and r.zero_reason is None
|
|
assert r.notes["temp"] == "체감 21°, 편한 범위 18~24° 안"
|
|
|
|
|
|
def test_good_but_not_perfect_stays_below_100():
|
|
assert evaluate(_defn(), [[_h(19, feels=23, hum=58, wind=1.8)]], CTX).score < 95
|
|
|
|
|
|
def test_one_bad_factor_pulls_total_down():
|
|
assert evaluate(_defn(), [[_h(19, wind=1.0, prob=60)]], CTX).score < 80
|
|
|
|
|
|
def test_temp_peaks_at_best_and_loses_8_per_degree_outside():
|
|
assert temp_score({"type": "range", "lo": 18, "hi": 24}, 21)[0] == 100
|
|
assert temp_score({"type": "range", "lo": 18, "hi": 24}, 24)[0] == 85
|
|
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°")
|
|
|
|
|
|
def test_temp_away_rises_outside_comfort():
|
|
rule = {"type": "away", "lo": 15, "hi": 24}
|
|
assert temp_score(rule, 20)[0] == 30
|
|
assert temp_score(rule, 34)[0] == 100
|
|
assert temp_score(rule, 5)[0] == 100
|
|
|
|
|
|
def test_rain_dry_and_wet():
|
|
assert rain_score({"dir": "dry"}, 30, False)[0] == 70
|
|
assert rain_score({"dir": "dry"}, 30, True)[0] == 0
|
|
assert rain_score({"dir": "wet"}, 30, True)[0] == 100
|
|
|
|
|
|
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
|
|
|
|
|
|
def test_best_window_is_chosen():
|
|
r = evaluate(_defn(), [[_h(7, feels=5)], [_h(20, feels=21)]], CTX)
|
|
assert r.window == (20, 20)
|
|
|
|
|
|
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점"
|
|
assert zero_rules_text([]) == "없음 (재미로 보는 지수)"
|
|
|
|
|
|
def test_moon_full_and_new():
|
|
assert illumination(datetime(2000, 1, 6, 18, 14, tzinfo=timezone.utc)) < 0.01
|
|
assert illumination(datetime(2000, 1, 21, 12, tzinfo=timezone.utc)) > 0.95
|
|
|
|
|
|
def test_uv_sun_view_scores_are_linear_and_unknown_is_70():
|
|
assert uv_score({"zero": 8, "full": 2}, 1)[0] == 100 and uv_score({"zero": 8, "full": 2}, 5)[0] == 50 and uv_score({"zero": 8, "full": 2}, 9)[0] == 0
|
|
assert sun_score({"zero": 0.2, "full": 0.8}, 0.5)[0] == 50 and sun_score({"zero": 0.2, "full": 0.8}, 1.0)[0] == 100
|
|
assert view_score({"zero": 5, "full": 20}, 12.5)[0] == 50 and view_score({"zero": 5, "full": 20}, 3)[0] == 0
|
|
assert (uv_score({"zero": 8, "full": 2}, None)[0], sun_score({"zero": 0.2, "full": 0.8}, None)[0], view_score({"zero": 5, "full": 20}, None)[0]) == (70, 70, 70)
|
|
assert uv_score({"zero": 8, "full": 2}, 5)[1] == "자외선 5.0"
|
|
assert view_score({"zero": 5, "full": 20}, 12.5)[1] == "가시거리 12.5km" and view_score({"zero": 5, "full": 20}, None)[1] == "가시거리 정보 없음"
|
|
|
|
|
|
def _uv_defn():
|
|
rules = _defn().factor_rules | {"uv": {"zero": 8, "full": 2}}
|
|
return _defn(weight_temp=25, weight_humidity=5, weight_uv=10, factor_rules=rules)
|
|
|
|
|
|
def test_weighted_uv_axis_lowers_score_when_uv_is_high_and_is_unknown_when_missing():
|
|
low = evaluate(_uv_defn(), [[_h(19, wind=1.0, uv=1.0)]], CTX)
|
|
high = evaluate(_uv_defn(), [[_h(19, wind=1.0, uv=9.0)]], CTX)
|
|
missing = evaluate(_uv_defn(), [[_h(19, wind=1.0)]], CTX)
|
|
assert low.score == 100 and low.factors["uv"] == 100
|
|
assert high.factors["uv"] == 0 and high.score < 80
|
|
assert missing.factors["uv"] == 70 and missing.notes["uv"] == "자외선 정보 없음"
|
|
|
|
|
|
def test_unweighted_new_axes_do_not_affect_score_and_use_max_uv_in_window():
|
|
plain = evaluate(_defn(), [[_h(19, wind=1.0, uv=9.0)]], CTX)
|
|
assert plain.score == 100 and plain.factors["uv"] == 0
|
|
window = evaluate(_uv_defn(), [[_h(19, wind=1.0, uv=1.0), _h(20, wind=1.0, uv=9.0)]], CTX)
|
|
assert window.factors["uv"] == 0
|
|
|
|
|
|
def test_gust_and_ground_temperature_zero_rules():
|
|
gust = _defn(zero_rules=[{"field": "gust_ms", "op": ">=", "value": 15}])
|
|
assert evaluate(gust, [[_h(19, wind=1.0, gust_ms=16.0)]], CTX).score == 0
|
|
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° 이상이면"
|