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