from datetime import date import httpx import pytest import services.week_service as week_service from services.external.seoul_culture import LIST_URL, fetch_seoul_festivals, parse_seoul_festivals from services.week_service import _seoul ITEM = { "cultcode": 159216, "title": "[중랑문화재단] 2026 중랑용마폭포축제", "strtdate": "2026-10-10", "endDate": "2026-10-10", "atchFileId": "abc", "xCoord": "37.5118239", "yCoord": "127.0591590", "facName": "용마폭포공원", "addr": "서울특별시 중랑구", } def test_parse_reads_lat_from_x_and_builds_image_and_link(): [row] = parse_seoul_festivals([ITEM, {**ITEM, "cultcode": None}, {**ITEM, "cultcode": 2, "strtdate": ""}]) assert row["source_id"] == "seoul:159216" and row["sido_code"] == "11" assert (row["lat"], row["lon"]) == (37.51182, 127.05916) assert row["name"] == "2026 중랑용마폭포축제" assert row["place_name"] == "용마폭포공원" and row["start_date"] == date(2026, 10, 10) assert row["image_url"].endswith("atchFileId=abc&thumb=Y") and "cultcode=159216" in row["source_url"] @pytest.mark.anyio async def test_fetch_pages_until_empty(monkeypatch): monkeypatch.setattr("services.external.seoul_culture.REQUEST_GAP_SEC", 0) pages = [] def handler(req: httpx.Request) -> httpx.Response: assert str(req.url) == LIST_URL page = int(dict(x.split("=") for x in req.content.decode().split("&"))["pageIndex"]) pages.append(page) return httpx.Response(200, json={"resultList": [ITEM] * 10 if page < 3 else []}) async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client: items = await fetch_seoul_festivals(client, date(2026, 10, 8), 30) assert len(items) == 20 and pages == [1, 2, 3] @pytest.mark.anyio async def test_seoul_skips_festivals_tour_api_already_has_and_other_sidos(monkeypatch): other = {**ITEM, "cultcode": 2, "title": "2026 서울어텀페스타"} async def fake(client, since, days): return [ITEM, other] monkeypatch.setattr(week_service, "fetch_seoul_festivals", fake) known = [{"name": "제3회 중랑용마폭포축제"}] rows = await _seoul(None, "11", date(2026, 10, 8), known) assert [r["name"] for r in rows] == ["2026 서울어텀페스타"] assert await _seoul(None, "41", date(2026, 10, 8), known) == [] @pytest.mark.anyio async def test_seoul_failure_keeps_other_sources(monkeypatch): async def broken(client, since, days): raise httpx.ConnectError("down") monkeypatch.setattr(week_service, "fetch_seoul_festivals", broken) assert await _seoul(None, "11", date(2026, 10, 8), []) == []