Files
o2o-weather/backend/tests/test_weather_score_table.py
skkim 0edb7059c8 feat: 주말 나들이 점수를 평균 60 정규분포로 보정, 지금 날씨·나들이 점수 100점은 완벽한 날만
- 나들이 점수(주말 전국 순위·당일치기)에 지금 날씨 점수와 같은 방식의 바꾸기 표를 적용: 원래 점수를 1년 치 분포의 순위로 보고 평균 60·표준편차 15 정규분포 점수로 바꿈 (weekend_rank.py, outing_score_table.json, scripts/gen_outing_score_table.py)
- 나들이 점수 만점 조건을 엄격하게: 체감온도 최적 21°±0.5°, 바람 초속 1m 이하일 때만 100점 (같은 예보를 받는 여러 지역이 한꺼번에 100점이 되던 것을 줄임)
- 표 맨 위가 100으로 잘려 비슷한 원래 점수(96·97·99)가 모두 100점이 되던 것을 막기 위해 straighten_top 추가: 표시 점수 83 이하인 마지막 원래 점수에서 100점까지 직선으로 이어, 100점은 구간에서 나올 수 있는 가장 높은 원래 점수(밤은 85)일 때만 나옴. 지금 날씨 점수 표도 다시 만듦 (90점 이상 약 2.4% → 1.5%)
- 당일치기 최소 점수는 원래 50점과 같은 날씨 수준(표시 62점)으로 옮김
- 주말 순위 캐시 키를 weekend_rank:v2:<날짜>로 올려 옛 점수 캐시를 쓰지 않게 함
- 테스트 추가: test_outing_score_table, test_weather_score_table(straighten_top·100점 조건)
- docs/점수_계산식.md의 지금 날씨 점수·나들이 점수 항목을 새 기준과 숫자로 갱신
2026-10-23 15:02:09 +09:00

75 lines
3.9 KiB
Python

from dataclasses import replace
from datetime import date
from services.calc.index_calc import Context, evaluate
from scripts.gen_weather_score_table import straighten_top
from services.index_service import EARLY, WEATHER_SCORE_DEF, WEATHER_SCORE_TABLE, calibrated_weather, weather_slot_scores
from tests.test_index_calc import _defn, _h
PERFECT = Context(day=date(2026, 10, 2), is_holiday=False, pm25=8, air_label="좋음", moon=0, sunset_score=None, wind_warning=False, lookahead_rain_prob=0)
SLOTS = ("morning", "day", "evening", "night")
def test_weather_score_has_no_worst_factor_pull():
"""해 난 비율만 0점(밤)이어도 나머지가 만점이면 비중대로 85점. 가장 낮은 요소로 25% 더 깎지 않는다."""
hours = [_h(h, feels=21.0, prob=0, wind=0.8, hum=50, sunshine_ratio=0.0) for h in range(19, 23)]
r = evaluate(WEATHER_SCORE_DEF, [hours], PERFECT)
assert r.factors["sun"] == 0
assert r.score == 85
def test_gak_scores_keep_worst_factor_pull():
"""각 점수는 그대로: 한 요소가 0점이면 섞은 점수에서 25% 깎인다."""
defn = _defn(zero_rules=[])
r = evaluate(defn, [[_h(19, wind=12.0)]], PERFECT)
assert r.factors["wind"] == 0
plain = round(sum(r.factors[a] * getattr(defn, f"weight_{a}", 0) for a in r.factors) / 100)
assert r.score == round(plain * 0.75)
def test_table_covers_every_slot_and_never_goes_down():
for slot in SLOTS:
table = WEATHER_SCORE_TABLE[slot]
assert len(table) == 101
assert all(0 <= v <= 100 for v in table)
assert all(a <= b for a, b in zip(table, table[1:]))
assert table[100] == 100
def test_table_makes_90_rare_and_centers_60():
"""구간마다 원래 점수 중앙값 근처가 60점 근처로, 90점 이상은 원래 점수 상위 몇 칸뿐."""
for slot in SLOTS:
table = WEATHER_SCORE_TABLE[slot]
assert any(55 <= v <= 65 for v in table)
assert sum(v >= 90 for v in table) < sum(v < 90 for v in table)
def test_calibrated_uses_slot_table_and_last_night_for_early():
assert calibrated_weather("day", 70) == WEATHER_SCORE_TABLE["day"][70]
assert calibrated_weather(EARLY, 70) == WEATHER_SCORE_TABLE["night"][70]
def test_weather_slot_scores_keeps_results_with_calibrated_scores():
"""점수 근거(factors)는 그대로 두고 점수만 표시 점수로 바꾼 결과를 돌려준다."""
results = {k: replace(evaluate(WEATHER_SCORE_DEF, [[_h(12)]], PERFECT), score=s) for k, s in {"morning": 40, "night": 60, EARLY: 60}.items()}
shown = weather_slot_scores(results)
assert {k: r.score for k, r in shown.items()} == {"morning": WEATHER_SCORE_TABLE["morning"][40], "night": WEATHER_SCORE_TABLE["night"][60], EARLY: WEATHER_SCORE_TABLE["night"][60]}
assert all(shown[k].factors == results[k].factors for k in results)
assert results["morning"].score == 40 # 원래 결과는 바뀌지 않는다
def test_straighten_top_makes_100_only_at_the_top_and_keeps_the_rest():
clamped = list(range(0, 80)) + [85, 90, 95] + [100] * 18 # 맨 위가 100으로 잘린 표 (원래 80~82 → 85·90·95, 83 이상은 모두 100)
fixed = straighten_top(clamped, top=100)
assert fixed[:80] == clamped[:80] # 아래쪽은 그대로
assert fixed[100] == 100 and max(fixed[:100]) < 100
above = fixed[80:]
assert all(a < b for a, b in zip(above, above[1:])) or above[-1] == 100 # 위쪽은 구별되는 점수로 오른다
assert straighten_top(clamped, top=90)[90] == 100 # 완벽한 점수가 100보다 낮은 구간(밤)은 거기서 100
def test_weather_table_gives_100_only_at_the_best_reachable_original_score():
assert WEATHER_SCORE_TABLE["day"][99] < 100 == WEATHER_SCORE_TABLE["day"][100]
night_top = 100 - WEATHER_SCORE_DEF.weight_sun # 밤은 해 난 비율이 늘 0점이라 원래 점수가 85까지만 나온다
assert WEATHER_SCORE_TABLE["night"][night_top - 1] < 100 == WEATHER_SCORE_TABLE["night"][night_top]