from datetime import date import httpx import pytest from common.enums import EventKind from config.settings import settings from services.external import culture_api from services.external.culture_api import CONCERT, fetch_exhibitions, parse_exhibitions from services.external.data_go_kr import DataGoKrRequestFailed _ITEM = """공연/전시{seq}{title}{start}{end} 테스트 미술관전시서울종로구{thumb} 126.9768937.57592""" def _page(items: list[str], total: int) -> str: return (f"
00NORMAL SERVICE.
" f"{''.join(items)}21{total}
") @pytest.fixture def key(monkeypatch): monkeypatch.setattr(settings, "data_go_kr_service_key", "fake-key") monkeypatch.setattr(culture_api, "_PAGE_SIZE", 2) monkeypatch.setattr(culture_api, "_BACKOFF_SEC", 0) monkeypatch.setattr(culture_api, "_PAGE_GAP_SEC", 0) def _client(handler) -> httpx.AsyncClient: return httpx.AsyncClient(transport=httpx.MockTransport(handler)) @pytest.mark.anyio async def test_fetch_exhibitions_pages_through_xml(key): seen = [] def handler(req: httpx.Request) -> httpx.Response: seen.append(dict(req.url.params)) page = int(req.url.params["PageNo"]) items = [_ITEM.format(seq=page * 10 + i, title=f"전시{page}-{i}", start="20261001", end="20261130", thumb="") for i in range(2 if page == 1 else 1)] return httpx.Response(200, text=_page(items, 3), headers={"content-type": "application/xml"}) async with _client(handler) as client: items = await fetch_exhibitions(client, "11", date(2026, 10, 6), 30) assert [it["title"] for it in items] == ["전시1-0", "전시1-1", "전시2-0"] assert seen[0]["realmCode"] == "D000" and seen[0]["sido"] == "서울" and seen[0]["from"] == "20261006" and seen[0]["to"] == "20261105" @pytest.mark.anyio async def test_fetch_exhibitions_asks_concert_realm(key): seen = [] def handler(req: httpx.Request) -> httpx.Response: seen.append(dict(req.url.params)) return httpx.Response(200, text=_page([], 0)) async with _client(handler) as client: assert await fetch_exhibitions(client, "26", date(2026, 10, 6), 30, CONCERT) == [] assert seen[0]["realmCode"] == "B000" and seen[0]["sido"] == "부산" @pytest.mark.anyio async def test_fetch_exhibitions_reports_unregistered_key(key): body = ("SERVICE_KEY_IS_NOT_REGISTERED_ERROR" "등록되지 않은 서비스키30") async with _client(lambda req: httpx.Response(403, text=body)) as client: with pytest.raises(DataGoKrRequestFailed, match="30 SERVICE_KEY_IS_NOT_REGISTERED_ERROR"): await fetch_exhibitions(client, "11", date(2026, 10, 6), 30) @pytest.mark.anyio async def test_fetch_exhibitions_skips_unknown_sido(key): async with _client(lambda req: pytest.fail("호출하면 안 된다")) as client: assert await fetch_exhibitions(client, "99", date(2026, 10, 6), 30) == [] def test_parse_exhibitions_maps_fields_and_drops_ended_or_duplicate(): items = [ {"seq": "501", "title": "가을 특별전", "place": "테스트 미술관", "startDate": "20261001", "endDate": "2026-11-30", "thumbnail": "http://www.culture.go.kr/upload/a.jpg", "gpsX": "126.97689", "gpsY": "37.57592"}, {"seq": "501", "title": "가을 특별전", "place": "테스트 미술관", "startDate": "20261001", "endDate": "20261130", "thumbnail": "", "gpsX": "", "gpsY": ""}, {"seq": "502", "title": "끝난 전시", "place": "테스트 박물관", "startDate": "20260101", "endDate": "20261005"}, {"seq": "503", "title": "날짜 없음", "place": "테스트 박물관", "startDate": "", "endDate": ""}, {"seq": "504", "title": "좌표 뒤바뀜", "place": "테스트 박물관", "startDate": "20261001", "endDate": "20261010", "gpsX": "37.5", "gpsY": "127.0"}, ] rows = parse_exhibitions(items, "11", date(2026, 10, 6)) assert [r["source_id"] for r in rows] == ["kcis:501", "kcis:504"] assert rows[0]["end_date"] == date(2026, 11, 30) assert rows[1]["lat"] == 37.5 and rows[1]["lon"] == 127.0 assert rows[0]["image_url"] == "https://www.culture.go.kr/upload/a.jpg" assert rows[0]["lat"] == 37.57592 and rows[0]["lon"] == 126.97689 assert rows[0]["sido_code"] == "11" and rows[0]["place_name"] == "테스트 미술관" assert rows[0]["kind"] == EventKind.EXHIBITION assert all(len(r["source_id"]) <= 20 for r in rows) def test_parse_exhibitions_unescapes_title_and_place(): items = [{"seq": "601", "title": "테스트 콘서트 <가을밤>", "place": "테스트 홀 & 라운지", "startDate": "20261010", "endDate": "20261010"}] rows = parse_exhibitions(items, "11", date(2026, 10, 6), CONCERT) assert rows[0]["name"] == "테스트 콘서트 <가을밤>" and rows[0]["place_name"] == "테스트 홀 & 라운지" assert rows[0]["kind"] == EventKind.CONCERT and rows[0]["icon"] == "concert"