95 lines
4.0 KiB
Python
95 lines
4.0 KiB
Python
from datetime import datetime
|
|
|
|
from common.database.model.models import WeatherHourly
|
|
from common.enums import RainType, Sky
|
|
from common.utils.kma_time import KST
|
|
from services.external.airkorea import parse_realtime
|
|
from services.external.kma import active_warnings, parse_ultra
|
|
from services.now_service import _tomorrow
|
|
from services.warning_service import warning_applies
|
|
|
|
|
|
def _kst(d, h):
|
|
return datetime(2026, 10, d, h, tzinfo=KST)
|
|
|
|
|
|
def test_parse_ultra_reads_rain_and_temp():
|
|
items = [
|
|
{"category": c, "fcstDate": "20261002", "fcstTime": "1000", "fcstValue": v}
|
|
for c, v in [("PTY", "1"), ("RN1", "1mm 미만"), ("T1H", "17")]
|
|
]
|
|
assert parse_ultra(items) == [{"at": _kst(2, 10), "rain_type": RainType.RAIN, "rain_mm": 0.5, "temp_c": 17.0, "lightning": False}]
|
|
|
|
|
|
def _event(area, var, cmd, tm, seq=1, stress=0):
|
|
return {"areaCode": area, "warnVar": var, "warnStress": stress, "command": cmd, "cancel": "0", "tmFc": tm, "tmSeq": seq}
|
|
|
|
|
|
def test_active_warnings_uses_latest_command_per_zone_and_type():
|
|
events = [
|
|
_event("L1100400", 1, 1, 202610010900),
|
|
_event("L1100400", 1, 2, 202610011800),
|
|
_event("L1090000", 7, 1, 202610020300, stress=1),
|
|
_event("S1323400", 6, 1, 202610020900),
|
|
]
|
|
assert active_warnings(events) == [{"warning_zone_code": "L1090000", "warning_type": 7, "level": 2}]
|
|
|
|
|
|
def test_warning_applies_to_parent_and_child_zones():
|
|
assert warning_applies("L1100400", "L1100400")
|
|
assert warning_applies("L1100400", "L1100000")
|
|
assert warning_applies("L1120000", "L1120100")
|
|
assert not warning_applies("L1100400", "L1100100")
|
|
assert not warning_applies("L1100400", "L1090000")
|
|
|
|
|
|
def test_parse_realtime_handles_24h_and_missing_values():
|
|
rows = parse_realtime([
|
|
{"stationName": "마포구", "pm25Value": "11", "pm10Value": "24", "pm25Grade": "1", "pm10Grade": "2", "dataTime": "2026-10-01 24:00"},
|
|
{"stationName": "점검중", "pm25Value": "-", "pm10Value": "-", "pm25Grade": None, "pm10Grade": None, "dataTime": "2026-10-02 08:00"},
|
|
])
|
|
assert rows[0] == {"station_name": "마포구", "pm25": 11, "pm10": 24, "grade": 2, "measured_at": _kst(2, 0)}
|
|
assert rows[1]["pm25"] is None and rows[1]["grade"] is None
|
|
|
|
|
|
def _row(h, rain=RainType.NONE, mm=0.0, sky=Sky.CLEAR, temp=16.0):
|
|
return WeatherHourly(forecast_at=_kst(3, h), rain_type=rain.value, rain_mm=mm, sky=sky.value, temp_c=temp)
|
|
|
|
|
|
def test_tomorrow_text_and_commute_rows():
|
|
rain_from, text, commute = _tomorrow([_row(8), _row(15, RainType.RAIN, 0.5), _row(19, RainType.RAIN, 2.0, temp=18.0)])
|
|
assert rain_from == _kst(3, 15)
|
|
assert text == "내일은 오후 3시쯤부터 부슬부슬 와요"
|
|
assert [(c.label, c.text) for c in commute] == [("내일 출근 8시", "맑음 16°"), ("내일 퇴근 7시", "비 18°")]
|
|
|
|
|
|
def test_tomorrow_without_rain():
|
|
assert _tomorrow([_row(8)])[1] == "내일은 비 소식이 없어요"
|
|
|
|
|
|
def test_ieyo_particle():
|
|
from common.utils.text import ieyo
|
|
assert ieyo("환기") == "환기예요"
|
|
assert ieyo("야장각") == "야장각이에요"
|
|
assert ieyo("아아→뜨아") == "아아→뜨아예요"
|
|
|
|
|
|
def test_parse_ultra_reads_drizzle_codes():
|
|
"""초단기예보에만 있는 5 빗방울 · 6 빗방울눈날림 · 7 눈날림."""
|
|
items = [
|
|
{"fcstDate": "20261013", "fcstTime": f"{h:02d}00", "category": c, "fcstValue": v}
|
|
for h, pty in ((10, "5"), (11, "6"), (12, "7"))
|
|
for c, v in [("PTY", pty), ("RN1", "1mm 미만"), ("T1H", "17")]
|
|
]
|
|
assert [p["rain_type"] for p in parse_ultra(items)] == [RainType.RAIN, RainType.SLEET, RainType.SNOW]
|
|
|
|
|
|
def test_parse_ultra_reads_lightning():
|
|
"""LGT 는 낙뢰(kA). 0 이면 없음, 칸이 없어도 없음."""
|
|
items = [
|
|
{"fcstDate": "20261013", "fcstTime": f"{h:02d}00", "category": c, "fcstValue": v}
|
|
for h, lgt in ((10, "0"), (11, "3"), (12, None))
|
|
for c, v in [("PTY", "1"), ("RN1", "5.0mm"), ("T1H", "17"), ("LGT", lgt)] if v is not None
|
|
]
|
|
assert [p["lightning"] for p in parse_ultra(items)] == [False, True, False]
|