Files
o2o-weather/backend/tests/test_sky_score.py
T
minaChoi 3f8f7daeb1 Merge origin/main into main-2: Open-Meteo 구름 20·100km·모델 2개 구조에 노을 하늘 상황 판정 맞춤
- 하늘 상황 판정·테스트를 구름 지점 2곳(20·100km), 모델 2개(JMA·ICON) 기준으로
- Open-Meteo 보조값 호출(fetch_weather)도 장애 대처(8초 끊기·2분 건너뛰기) 적용
- worker: 행사 수집 스케줄과 Open-Meteo 보조값 스케줄 둘 다 유지
- 지수 seed: main 의 한계 설명 + main-2 의 점수 구간 설명
- 옛 data-pipeline 문서는 삭제 유지, 내용은 openmeteo-data.md·데이터_흐름.html 에 반영
2026-10-07 14:04:18 +09:00

88 lines
3.5 KiB
Python

from datetime import date, datetime, timedelta, timezone
from common.enums import Confidence, SkyGrade, SkyType
from common.utils.geo import destination, haversine_km
from services.calc.sky_score import GRADE_LABEL, Layers, best_times, confidence, grade, reason, score, situation, sunset_timeline, verdict
from services.calc.sun import sun_times
CLEAR = Layers(0, 0, 0)
def test_high_clouds_overhead_and_clear_horizon_is_best():
canvas = Layers(low=0, mid=20, high=50)
assert score(canvas, CLEAR) == 100
def test_clear_sky_is_only_soft():
assert grade(score(CLEAR, CLEAR)) is SkyGrade.SOFT
def test_low_clouds_toward_sun_block_the_light():
canvas = Layers(low=0, mid=20, high=50)
assert grade(score(canvas, Layers(low=90, mid=50, high=0))) is SkyGrade.GRAY
def test_overcast_low_clouds_is_gray():
gray = Layers(low=100, mid=100, high=100)
assert grade(score(gray, gray)) is SkyGrade.GRAY
def test_confidence_from_model_spread():
assert confidence([70, 72, 68, 71]) is Confidence.HIGH
assert confidence([40, 80, 60, 90]) is Confidence.LOW
assert confidence([70]) is Confidence.LOW
def test_destination_point_is_at_given_distance():
lat, lon = destination(37.56, 126.91, 266, 50)
assert abs(haversine_km(37.56, 126.91, lat, lon) - 50) < 0.1
assert lon < 126.91
def test_sunset_best_times_order():
sunset = datetime(2026, 10, 2, 9, 15, tzinfo=timezone.utc)
dusk = datetime(2026, 10, 2, 9, 41, tzinfo=timezone.utc)
labels = [t["label"] for t in best_times(SkyType.SUNSET, sunset, dusk, 50)]
assert labels == ["해 져요", "가장 붉을 때", "블루아워"]
def test_reason_names_what_decided_the_score():
canvas = Layers(low=0, mid=20, high=50)
assert "알맞게" in reason(canvas, CLEAR)
assert "먼 하늘" in reason(canvas, Layers(low=90, mid=50, high=0))
assert "낮은 구름" in reason(Layers(100, 0, 0), CLEAR)
assert "거의 없어" in reason(CLEAR, CLEAR)
def test_verdict_bands_and_gray_label_is_not_weather_word():
assert [verdict(v) for v in (60, 59, 40, 39)] == ["go", "maybe", "maybe", "rest"]
assert GRADE_LABEL[SkyGrade.GRAY] != "흐림"
def test_sunset_timeline_runs_golden_to_blue():
sun = sun_times(date(2026, 10, 6), 37.56, 126.91)
rows = sunset_timeline(sun.golden_start_at, sun.sunset_at, sun.sunset_at + timedelta(minutes=15), sun.dusk_end_at)
assert [r["kind"] for r in rows] == ["golden", "sun", "peak", "blue"]
ats = [datetime.fromisoformat(r["at"]) for r in rows]
assert ats == sorted(ats) and 15 <= (ats[1] - ats[0]).total_seconds() / 60 <= 60
def test_situation_follows_reason_order():
from common.enums import SkySituation
clear, glow = Layers(0, 0, 0), Layers(0, 40, 50)
assert situation(clear, clear) is SkySituation.FEW
assert situation(glow, clear) is SkySituation.GLOW
assert situation(Layers(0, 90, 95), clear) is SkySituation.THICK
assert situation(Layers(80, 0, 0), clear) is SkySituation.LOW_CLOUD
assert situation(glow, Layers(70, 0, 0)) is SkySituation.COVERED
def test_ranking_summary_only_when_most_spots_share_a_situation():
from types import SimpleNamespace
from common.enums import SkySituation
from services.sky_service import ranking_summary
item = lambda s: SimpleNamespace(score=SimpleNamespace(situation=s.value)) # noqa: E731
assert ranking_summary([item(SkySituation.FEW)] * 8 + [item(SkySituation.GLOW)] * 2) == "오늘은 전국이 맑아 대부분 짧게 끝나요"
assert ranking_summary([item(SkySituation.FEW)] * 5 + [item(SkySituation.GLOW)] * 5) is None