Files

71 lines
3.2 KiB
Python

from datetime import date, timedelta
import pytest
from httpx import ASGITransport, AsyncClient
import services.first_snow_service as first_snow_service
from common.utils.kma_time import now_kst
from router.router import app
from services.external.open_meteo import OpenMeteoRequestFailed, parse_snow
from services.first_snow_service import SPOTS, first_snow_day
def _daily(start: date, cms: list) -> dict:
return {"daily": {"time": [(start + timedelta(days=i)).isoformat() for i in range(len(cms))], "snowfall_sum": cms}}
def test_parse_snow_skips_days_without_value():
rows = parse_snow(_daily(date(2026, 11, 20), [0.0, None, 1.4]))
assert rows == [(date(2026, 11, 20), 0.0), (date(2026, 11, 22), 1.4)]
def test_first_snow_day_is_first_day_from_today_with_enough_snow():
today = date(2026, 11, 22)
days = [(date(2026, 11, 21), 3.0), (date(2026, 11, 22), 0.05), (date(2026, 11, 24), 0.7), (date(2026, 11, 25), 2.0)]
assert first_snow_day(days, today) == (date(2026, 11, 24), 0.7) # 어제 눈과 0에 가까운 값은 뺀다
assert first_snow_day([(date(2026, 11, 23), 0.0)], today) is None
def test_spot_data_has_normal_dates_and_unique_names():
names = [s["name"] for s in SPOTS]
assert len(names) == len(set(names)) and len(names) >= 40
for s in SPOTS:
month, day = map(int, s["normal"].split("-"))
assert month in (11, 12) and 1 <= day <= 31
assert 33 <= s["lat"] <= 39 and 125 <= s["lon"] <= 130
@pytest.mark.anyio
async def test_first_snow_api_marks_spots_with_snow_forecast(monkeypatch):
today = now_kst().date()
calls = []
async def fake_fetch(client, points, days):
calls.append(len(points))
# 첫 지점만 모레 눈, 나머지는 눈 없음
return [_daily(today, [0.0, 0.0, 1.2, 0.0] if i == 0 else [0.0] * 4) for i in range(len(points))]
monkeypatch.setattr(first_snow_service, "fetch_snow", fake_fetch)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
body = (await client.get("/v1/season/first-snow")).json()
again = (await client.get("/v1/season/first-snow")).json()
assert body["result"]["success"] is True and body["forecast"] is True
assert len(body["spots"]) == len(SPOTS)
first = body["spots"][0]
assert first["name"] == SPOTS[0]["name"] and first["normal"] == SPOTS[0]["normal"]
assert first["snow_date"] == (today + timedelta(days=2)).isoformat() and first["snow_cm"] == 1.2
assert all("snow_date" not in s for s in body["spots"][1:])
assert again == body and calls == [len(SPOTS)] # 두 번째는 Redis 에 둔 예보를 쓴다
@pytest.mark.anyio
async def test_first_snow_api_returns_normal_dates_when_forecast_fails(monkeypatch):
async def failing_fetch(client, points, days):
raise OpenMeteoRequestFailed("down")
monkeypatch.setattr(first_snow_service, "fetch_snow", failing_fetch)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
body = (await client.get("/v1/season/first-snow")).json()
assert body["result"]["success"] is True and body["forecast"] is False
assert len(body["spots"]) == len(SPOTS) and all("snow_date" not in s for s in body["spots"])