- 초단기실황(getUltraSrtNcst)을 받아 지금 정시 한 시각의 비·기온·습도·바람을 예보 대신 관측값으로 덮어씀 (체감·불쾌지수는 다시 계산). 지역별 1시간 캐시, 실패하면 초단기예보·단기예보로 되돌아감, 새 시각 실황이 나오면 각 점수를 다시 계산. 가이드대로 ±900 이상은 결측 처리. 테스트가 실제 기상청을 부르지 않게 실황 호출을 기본으로 막음 - 시간대 알맞음 요소를 31개 각에서 빼고 비중을 5% 단위로 나눔. 요소가 3개 미만이 된 뜨아각·파전각·감성노래각·감사각·선크림각에 요소를 더함. 러닝각의 활동 시간대 밖 점수 상한도 없앰. 요소가 하나뿐인 각은 가장 낮은 요소로 깎지 않음 - 우산각은 비 한 가지: 남은 하루(지금~자정) 비 확률 평균이 점수, 지금 비가 오면 70점에서 시작해 평균 × 30점을 더함. 내일·토·일 칸은 그날 6~24시 평균 × 100. 우산분실각도 비 한 가지 - 각 계산 기준 문서는 생성기를 새 규칙에 맞게 고쳐 다시 생성, 점수 계산식·서비스 동작 안내 문서 갱신
87 lines
3.7 KiB
Python
87 lines
3.7 KiB
Python
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=="
|