- 초단기실황(getUltraSrtNcst)을 받아 지금 정시 한 시각의 비·기온·습도·바람을 예보 대신 관측값으로 덮어씀 (체감·불쾌지수는 다시 계산). 지역별 1시간 캐시, 실패하면 초단기예보·단기예보로 되돌아감, 새 시각 실황이 나오면 각 점수를 다시 계산. 가이드대로 ±900 이상은 결측 처리. 테스트가 실제 기상청을 부르지 않게 실황 호출을 기본으로 막음 - 시간대 알맞음 요소를 31개 각에서 빼고 비중을 5% 단위로 나눔. 요소가 3개 미만이 된 뜨아각·파전각·감성노래각·감사각·선크림각에 요소를 더함. 러닝각의 활동 시간대 밖 점수 상한도 없앰. 요소가 하나뿐인 각은 가장 낮은 요소로 깎지 않음 - 우산각은 비 한 가지: 남은 하루(지금~자정) 비 확률 평균이 점수, 지금 비가 오면 70점에서 시작해 평균 × 30점을 더함. 내일·토·일 칸은 그날 6~24시 평균 × 100. 우산분실각도 비 한 가지 - 각 계산 기준 문서는 생성기를 새 규칙에 맞게 고쳐 다시 생성, 점수 계산식·서비스 동작 안내 문서 갱신
96 lines
3.6 KiB
Python
96 lines
3.6 KiB
Python
from datetime import datetime, timedelta
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
import services.weather_service as weather_service
|
|
from common.database.model.models import WeatherHourly
|
|
from common.enums import RainType, Sky
|
|
from common.utils.kma_time import KST
|
|
from services.calc.weather_calc import discomfort_index, feels_like_c
|
|
from services.external.data_go_kr import DataGoKrRequestFailed
|
|
from services.weather_service import Observed, observed_now, with_observed
|
|
|
|
REGION = SimpleNamespace(code="1", nx=60, ny=127)
|
|
|
|
|
|
def _at(h: int, mi: int = 0) -> datetime:
|
|
return datetime(2026, 10, 8, 0, 0, tzinfo=KST) + timedelta(hours=h, minutes=mi)
|
|
|
|
|
|
def _hourly(h: int) -> WeatherHourly:
|
|
return WeatherHourly(
|
|
region_code="1", forecast_at=_at(h), temp_c=21, feels_c=21, discomfort_index=60, sky=Sky.CLOUDY.value, rain_type=RainType.NONE.value,
|
|
rain_prob=30, rain_mm=0, humidity=60, wind_ms=1.0, issued_at=_at(h),
|
|
)
|
|
|
|
|
|
_OBS = Observed(at=_at(14), rain_type=RainType.RAIN, rain_mm=0.3, temp_c=9.8, humidity=78, wind_ms=4.5)
|
|
|
|
|
|
def test_observed_replaces_the_observed_hour_with_what_is_measured():
|
|
rows = {r.forecast_at: r for r in (_hourly(13), _hourly(14), _hourly(15))}
|
|
r = with_observed(rows, _OBS)[_at(14)]
|
|
assert (r.rain_type, float(r.rain_mm), float(r.temp_c), r.humidity, float(r.wind_ms)) == (RainType.RAIN.value, 0.3, 9.8, 78, 4.5)
|
|
assert float(r.feels_c) == feels_like_c(9.8, 78, 4.5, 10) and r.discomfort_index == discomfort_index(9.8, 78)
|
|
|
|
|
|
def test_observed_keeps_forecast_only_values_and_other_hours():
|
|
rows = {r.forecast_at: r for r in (_hourly(13), _hourly(14), _hourly(15))}
|
|
out = with_observed(rows, _OBS)
|
|
assert (out[_at(14)].rain_prob, out[_at(14)].sky) == (30, Sky.CLOUDY.value)
|
|
assert out[_at(13)] is rows[_at(13)] and out[_at(15)] is rows[_at(15)]
|
|
assert rows[_at(14)].rain_type == RainType.NONE.value # 원본은 그대로
|
|
|
|
|
|
def test_observed_does_nothing_without_an_observation_or_the_hours_row():
|
|
rows = {_at(13): _hourly(13)}
|
|
assert with_observed(rows, None) == rows
|
|
assert with_observed(rows, _OBS) == rows
|
|
|
|
|
|
_ITEMS = [{"category": c, "obsrValue": v} for c, v in {"PTY": "1", "REH": "78", "RN1": "0.3", "T1H": "9.8", "WSD": "4.5"}.items()]
|
|
|
|
|
|
@pytest.fixture
|
|
def ncst_calls(monkeypatch):
|
|
calls = []
|
|
|
|
async def fetch(client, nx, ny, base):
|
|
calls.append(base)
|
|
return _ITEMS
|
|
|
|
monkeypatch.setattr(weather_service, "fetch_ncst", fetch)
|
|
return calls
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_observed_now_reads_the_latest_hour_and_keeps_it_for_that_hour(ncst_calls):
|
|
first = await observed_now(REGION, _at(14, 20))
|
|
again = await observed_now(REGION, _at(14, 50))
|
|
assert first == again == _OBS
|
|
assert ncst_calls == [_at(14)]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_observed_now_asks_again_when_a_new_hour_is_out(ncst_calls):
|
|
await observed_now(REGION, _at(14, 20))
|
|
await observed_now(REGION, _at(15, 5)) # 15시 실황은 15:10부터라 아직 14시 것
|
|
assert ncst_calls == [_at(14)]
|
|
await observed_now(REGION, _at(15, 10))
|
|
assert ncst_calls == [_at(14), _at(15)]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_observed_now_is_nothing_when_the_api_fails_or_answers_with_gaps(monkeypatch):
|
|
async def down(*_a):
|
|
raise DataGoKrRequestFailed("down")
|
|
|
|
async def gaps(*_a):
|
|
return [i for i in _ITEMS if i["category"] != "T1H"]
|
|
|
|
monkeypatch.setattr(weather_service, "fetch_ncst", down)
|
|
assert await observed_now(REGION, _at(14, 20)) is None
|
|
monkeypatch.setattr(weather_service, "fetch_ncst", gaps)
|
|
assert await observed_now(REGION, _at(14, 20)) is None
|