- FastAPI + PostgreSQL + Redis, web·worker 분리, compose 에 backend·worker·postgres·redis 추가 - 기상청 단기·초단기·중기·특보, 에어코리아, Open-Meteo, TourAPI, 특일정보 연동 (첫 조회 때 받고 캐시) - 매일 03:00 worker 가 전국 공통 피드 칸을 정하고 지난 데이터를 정리 - 시드: 시군구 256곳, 지수 23개, 노을 대표 지점·명소, 데이트 명소, 읽을거리 - pytest 66개 (테스트 전용 DB·Redis 사용) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
2.7 KiB
Python
65 lines
2.7 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, temp_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):
|
|
return Hour(hour=hour, feels_c=feels, rain_type=rain, rain_prob=prob, wind_ms=wind, humidity=hum, sky=sky)
|
|
|
|
|
|
def test_perfect_evening_scores_100():
|
|
r = evaluate(_defn(), [[_h(19), _h(20)]], CTX)
|
|
assert r.score == 100 and r.zero_reason is None
|
|
assert r.notes["temp"] == "체감 21°, 편한 범위 18~24° 안"
|
|
|
|
|
|
def test_temp_outside_range_loses_8_per_degree():
|
|
assert temp_score({"type": "range", "lo": 18, "hi": 24}, 15)[0] == 76
|
|
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_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
|