from datetime import timedelta import pytest from httpx import ASGITransport, AsyncClient import services.air_service as air_service import services.warning_service as warning_service import services.weather_service as weather_service from common.utils.kma_time import kst_midnight, now_kst from router.router import app REGION = "1141000000" async def _async(v): return v def _vilage(*_): start = kst_midnight(now_kst().date()) items = [] for h in range(24 * 4): at = start + timedelta(hours=h) for c, v in [("TMP", "21"), ("SKY", "1"), ("PTY", "0"), ("POP", "0"), ("PCP", "강수없음"), ("REH", "50"), ("WSD", "1.5")]: items.append({"category": c, "fcstDate": at.strftime("%Y%m%d"), "fcstTime": at.strftime("%H%M"), "fcstValue": v}) return items @pytest.fixture def fakes(monkeypatch): now = now_kst() monkeypatch.setattr(weather_service, "fetch_vilage", lambda *a: _async(_vilage())) monkeypatch.setattr(air_service, "fetch_stations", lambda *a: _async([{"stationName": "신촌동", "dmX": "37.565", "dmY": "126.937"}])) monkeypatch.setattr(air_service, "fetch_realtime", lambda *a: _async([{ "stationName": "신촌동", "pm25Value": "10", "pm10Value": "20", "pm25Grade": "1", "pm10Grade": "1", "dataTime": now.replace(minute=0).strftime("%Y-%m-%d %H:%M"), }])) monkeypatch.setattr(warning_service, "fetch_warning_events", lambda *a: _async([])) @pytest.mark.anyio async def test_index_list_ranks_all_23_with_3_day_trend(fakes): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/index/list", params={"region": REGION})).json() assert body["result"]["success"] is True assert len(body["items"]) == 23 and len(body["top3"]) == 3 scores = [i["score"] for i in body["items"]] assert scores == sorted(scores, reverse=True) assert all(len(i["trend"]) == 3 for i in body["items"]) assert 0 <= body["weather_score"] <= 100 @pytest.mark.anyio async def test_index_list_filters_core(fakes): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/index/list", params={"region": REGION, "core": True})).json() assert {i["code"] for i in body["items"]} == {"picnic", "hiking", "camping", "stars"} @pytest.mark.anyio async def test_index_detail_has_six_factors_and_rules(fakes): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/index/1", params={"region": REGION})).json() assert body["index"]["code"] == "yajang" assert [f["label"] for f in body["factors"]] == ["기온", "비", "바람", "습도", "공기", "시간대"] assert sum(f["weight"] for f in body["factors"]) == 100 assert body["zero_rules"] == "비 확률 80% 이상 또는 바람 14m/s 이상이면 바로 0점" assert body["limit"].startswith("모기")