feat: 날씨각 백엔드 API (날씨·노을·지수·주말·계절·피드·로그인·반응)
- FastAPI + PostgreSQL + Redis, web·worker 분리, compose 에 backend·worker·postgres·redis 추가 - 기상청 단기·초단기·중기·특보, 에어코리아, Open-Meteo, TourAPI, 특일정보 연동 (첫 조회 때 받고 캐시) - 매일 03:00 worker 가 전국 공통 피드 칸을 정하고 지난 데이터를 정리 - 시드: 시군구 256곳, 지수 23개, 노을 대표 지점·명소, 데이트 명소, 읽을거리 - pytest 66개 (테스트 전용 DB·Redis 사용) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9cdc0d8fea
commit
e134295ce5
135 files changed
+9123
No files matched your search
@@ -0,0 +1,113 @@
|
||||
from datetime import date, datetime, 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
|
||||
import services.week_service as week_service
|
||||
from common.database.model.models import Event
|
||||
from common.enums import RainType, Sky
|
||||
from common.utils.kma_time import KST, kst_midnight, now_kst
|
||||
from router.router import app
|
||||
from services.external.kma import latest_mid_base, mid_word, parse_mid
|
||||
from services.week_service import _status
|
||||
|
||||
REGION = "1144000000"
|
||||
|
||||
|
||||
def test_mid_word_maps_text():
|
||||
assert mid_word("맑음") == (Sky.CLEAR, RainType.NONE)
|
||||
assert mid_word("구름많고 소나기") == (Sky.PARTLY, RainType.SHOWER)
|
||||
assert mid_word("흐리고 비/눈") == (Sky.CLOUDY, RainType.SLEET)
|
||||
|
||||
|
||||
def test_parse_mid_days_from_base():
|
||||
base = datetime(2026, 10, 2, 6, tzinfo=KST)
|
||||
land = {"wf4Am": "맑음", "wf4Pm": "흐리고 비", "rnSt4Am": 10, "rnSt4Pm": 60}
|
||||
ta = {"taMin4": 10, "taMax4": 22}
|
||||
day = parse_mid(land, ta, base)[date(2026, 10, 6)]
|
||||
assert day == {"temp_min_c": 10.0, "temp_max_c": 22.0, "sky": Sky.CLOUDY, "rain_type": RainType.RAIN, "rain_prob": 60}
|
||||
|
||||
|
||||
def test_latest_mid_base():
|
||||
assert latest_mid_base(datetime(2026, 10, 2, 6, 20, tzinfo=KST)) == datetime(2026, 10, 1, 18, tzinfo=KST)
|
||||
assert latest_mid_base(datetime(2026, 10, 2, 6, 30, tzinfo=KST)) == datetime(2026, 10, 2, 6, tzinfo=KST)
|
||||
|
||||
|
||||
def test_event_status_labels():
|
||||
today = date(2026, 10, 2)
|
||||
ev = lambda s, e: Event(start_date=s, end_date=e) # noqa: E731
|
||||
assert _status(ev(date(2026, 10, 1), date(2026, 10, 5)), today) == ("live", "진행 중")
|
||||
assert _status(ev(date(2026, 10, 3), date(2026, 10, 3)), today) == ("soon", "이번 토요일")
|
||||
assert _status(ev(date(2026, 10, 20), date(2026, 10, 21)), today) == ("soon", "D-18")
|
||||
|
||||
|
||||
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)
|
||||
rainy = h == 24 * 2 + 15
|
||||
for c, v in [("TMP", "18"), ("SKY", "1"), ("PTY", "1" if rainy else "0"), ("POP", "70" if rainy else "0"),
|
||||
("PCP", "1.0mm" if rainy else "강수없음"), ("REH", "50"), ("WSD", "1.5")]:
|
||||
items.append({"category": c, "fcstDate": at.strftime("%Y%m%d"), "fcstTime": at.strftime("%H%M"), "fcstValue": v})
|
||||
return items
|
||||
|
||||
|
||||
def _mid(*_):
|
||||
land = {f"wf{n}{p}": "맑음" for n in range(4, 8) for p in ("Am", "Pm")} | {f"rnSt{n}{p}": 10 for n in range(4, 8) for p in ("Am", "Pm")}
|
||||
ta = {f"taMin{n}": 11 for n in range(4, 8)} | {f"taMax{n}": 23 for n in range(4, 8)}
|
||||
return land, ta
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def fakes(monkeypatch):
|
||||
today = now_kst().date()
|
||||
monkeypatch.setattr(weather_service, "fetch_vilage", lambda *a: _async(_vilage()))
|
||||
monkeypatch.setattr(week_service, "fetch_mid", lambda *a: _async(_mid()))
|
||||
monkeypatch.setattr(week_service, "fetch_holidays", lambda c, y, m: _async(
|
||||
[{"holiday_date": today + timedelta(days=1), "name": "테스트공휴일"}] if m == (today + timedelta(days=1)).month and y == today.year else []
|
||||
))
|
||||
monkeypatch.setattr(week_service, "fetch_festivals", lambda *a: _async([
|
||||
{"contentid": "1", "title": "가을 축제", "addr1": "서울 마포구", "eventstartdate": (today + timedelta(days=3)).strftime("%Y%m%d"),
|
||||
"eventenddate": (today + timedelta(days=4)).strftime("%Y%m%d")},
|
||||
{"contentid": "2", "title": "상설 전시", "addr1": "서울", "eventstartdate": "20250101", "eventenddate": "20261231"},
|
||||
]))
|
||||
monkeypatch.setattr(air_service, "fetch_stations", lambda *a: _async([]))
|
||||
monkeypatch.setattr(air_service, "fetch_realtime", lambda *a: _async([]))
|
||||
monkeypatch.setattr(warning_service, "fetch_warning_events", lambda *a: _async([]))
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_week_has_seven_days_with_holiday_and_rain(fakes):
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
|
||||
body = (await client.get("/v1/week", params={"region": REGION})).json()
|
||||
days = body["days"]
|
||||
assert len(days) == 7
|
||||
assert days[1]["holiday"] == "테스트공휴일"
|
||||
assert days[2]["is_rainy"] is True and days[2]["rain_hours"] == [15, 16]
|
||||
assert days[6]["temp_max_c"] == 23
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_events_exclude_long_running_and_add_weather_fit(fakes):
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
|
||||
body = (await client.get("/v1/week/event/list", params={"region": REGION})).json()
|
||||
assert [e["name"] for e in body["events"]] == ["가을 축제"]
|
||||
assert body["events"][0]["fit"]
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_places_put_indoor_first_on_rainy_day(fakes):
|
||||
rainy_day = (now_kst().date() + timedelta(days=2)).isoformat()
|
||||
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
|
||||
await client.get("/v1/week", params={"region": REGION})
|
||||
body = (await client.get("/v1/week/place/list", params={"region": REGION, "day": rainy_day})).json()
|
||||
assert body["rainy"] is True
|
||||
assert body["places"][0]["is_outdoor"] is False and body["places"][0]["is_good"] is True
|
||||
Reference in new issue
Block a user