from datetime import datetime from common.enums import RainType, Sky from common.utils.kma_time import KST, latest_ncst_base, latest_ultra_base, latest_vilage_base from services.external.kma import parse_amount, parse_ncst, parse_vilage def _kst(y, mo, d, h, mi=0): return datetime(y, mo, d, h, mi, tzinfo=KST) def test_latest_vilage_base_waits_10_minutes_after_issue(): assert latest_vilage_base(_kst(2026, 10, 1, 17, 9)) == _kst(2026, 10, 1, 14) assert latest_vilage_base(_kst(2026, 10, 1, 17, 10)) == _kst(2026, 10, 1, 17) def test_latest_vilage_base_before_first_issue_uses_yesterday_23(): assert latest_vilage_base(_kst(2026, 10, 1, 2, 5)) == _kst(2026, 9, 30, 23) def test_latest_ultra_base_available_from_45_minutes(): assert latest_ultra_base(_kst(2026, 10, 1, 10, 44)) == _kst(2026, 10, 1, 9, 30) assert latest_ultra_base(_kst(2026, 10, 1, 10, 45)) == _kst(2026, 10, 1, 10, 30) def test_latest_ncst_base_available_from_10_minutes(): assert latest_ncst_base(_kst(2026, 10, 1, 10, 9)) == _kst(2026, 10, 1, 9) assert latest_ncst_base(_kst(2026, 10, 1, 10, 10)) == _kst(2026, 10, 1, 10) assert latest_ncst_base(_kst(2026, 10, 1, 0, 5)) == _kst(2026, 9, 30, 23) # 2026-11-23 14시 서울 실황 응답 그대로 _NCST = {"PTY": "1", "REH": "78", "RN1": "0.3", "T1H": "9.8", "UUU": "1.1", "VEC": "194", "VVV": "4.4", "WSD": "4.5"} def _ncst(**override): return [{"baseDate": "20261123", "baseTime": "1400", "category": c, "nx": 60, "ny": 127, "obsrValue": v} for c, v in (_NCST | override).items()] def test_parse_ncst_reads_what_is_observed_now(): assert parse_ncst(_ncst()) == {"rain_type": RainType.RAIN, "rain_mm": 0.3, "temp_c": 9.8, "humidity": 78, "wind_ms": 4.5} def test_parse_ncst_reads_no_rain_and_drizzle_codes(): assert parse_ncst(_ncst(PTY="0", RN1="0"))["rain_type"] is RainType.NONE assert [parse_ncst(_ncst(PTY=c))["rain_type"] for c in ("5", "6", "7")] == [RainType.RAIN, RainType.SLEET, RainType.SNOW] def test_parse_ncst_without_every_value_is_nothing(): assert parse_ncst([]) is None assert parse_ncst([i for i in _ncst() if i["category"] != "T1H"]) is None def test_parse_ncst_treats_900_or_more_as_missing(): """가이드: +900 이상, -900 이하 값은 결측.""" for category, value in (("T1H", "-999"), ("WSD", "900"), ("REH", "-999"), ("RN1", "-999")): assert parse_ncst(_ncst(**{category: value})) is None, category def test_parse_amount_text(): assert parse_amount("강수없음") == 0 assert parse_amount("1mm 미만") == 0.5 assert parse_amount("1.0mm") == 1.0 assert parse_amount("30.0~50.0mm") == 30.0 assert parse_amount("50.0mm 이상") == 50.0 def _item(d, t, cat, val): return {"baseDate": "20261001", "baseTime": "1400", "category": cat, "fcstDate": d, "fcstTime": t, "fcstValue": val, "nx": 59, "ny": 127} def test_parse_vilage_groups_rows_by_hour_and_maps_codes(): items = [ _item("20261001", "1500", c, v) for c, v in [("TMP", "24"), ("SKY", "3"), ("PTY", "1"), ("POP", "60"), ("PCP", "1mm 미만"), ("REH", "55"), ("WSD", "2.1")] ] + [_item("20261002", "0600", "TMN", "13.0"), _item("20261002", "1500", "TMX", "23.0")] hourly, daily = parse_vilage(items) v = hourly[_kst(2026, 10, 1, 15)] assert (v["temp_c"], v["sky"], v["rain_type"], v["rain_prob"], v["rain_mm"], v["wind_ms"]) == (24.0, Sky.PARTLY, RainType.RAIN, 60, 0.5, 2.1) assert daily[datetime(2026, 10, 2).date()] == {"temp_min_c": 13.0, "temp_max_c": 23.0} def test_service_key_is_decoded_when_given_encoded(): from config.settings import Settings assert Settings(data_go_kr_service_key="a%2Bb%3D%3D").data_go_kr_service_key == "a+b==" assert Settings(data_go_kr_service_key="a+b==").data_go_kr_service_key == "a+b=="