import json from datetime import date import httpx import pytest from common.enums import EventKind from services.external import popga from services.external.popga import fetch_popup_index, parse_popup, sido_of def _page(popup_id: int, kind: str, start: str, end: str, address: str = "서울 성동구 테스트로 1") -> str: ld = { "@context": "https://schema.org", "@type": "Event", "name": "테스트 팝업 & 굿즈", "startDate": start, "endDate": end, "location": {"@type": "Place", "name": "테스트 빌딩 1F", "address": {"@type": "PostalAddress", "streetAddress": address}, "geo": {"@type": "GeoCoordinates", "latitude": 37.5446123, "longitude": 127.0557456}}, } payload = f'self.__next_f.push([1,"{{\\"data\\":{{\\"id\\":{popup_id},\\"type\\":\\"{kind}\\",\\"source\\":null}}}}"])' return f'' def test_parse_popup_maps_store(): row = parse_popup(_page(501, "STORE", "2026-10-02", "2026-10-11"), 501, date(2026, 10, 1)) assert row == { "source_id": "popga:501", "sido_code": "11", "name": "테스트 팝업 & 굿즈", "place_name": "테스트 빌딩 1F", "start_date": date(2026, 10, 2), "end_date": date(2026, 10, 11), "is_confirmed": True, "source_url": "https://popga.co.kr/popup/501", "image_url": None, "lat": 37.54461, "lon": 127.05575, "kind": EventKind.POPUP, "icon": "popup", } def test_parse_popup_maps_festival(): row = parse_popup(_page(504, "FESTIVAL", "2026-10-02", "2026-10-11", "경기 수원시 팔달구 테스트로 1"), 504, date(2026, 10, 1)) assert (row["kind"], row["icon"], row["sido_code"]) == (EventKind.FESTIVAL, "fireworks", "41") @pytest.mark.parametrize("kind,end,address", [ ("PERFORMANCE", "2026-10-11", "서울 성동구 테스트로 1"), ("STORE", "2026-09-30", "서울 성동구 테스트로 1"), ("STORE", "2026-10-11", "알 수 없는 주소"), ]) def test_parse_popup_skips_non_store_ended_or_unknown_sido(kind, end, address): assert parse_popup(_page(502, kind, "2026-09-01", end, address), 502, date(2026, 10, 1)) is None def test_parse_popup_reads_type_of_its_own_id(): page = _page(503, "STORE", "2026-10-02", "2026-10-11").replace('\\"id\\":503,\\"type\\":\\"STORE\\"', '\\"id\\":999,\\"type\\":\\"STORE\\"') assert parse_popup(page, 503, date(2026, 10, 1)) is None @pytest.mark.parametrize("address,code", [ ("서울 마포구 테스트로 1", "11"), ("서울특별시 마포구", "11"), ("경기 수원시", "41"), ("경상남도 진주시", "48"), ("강원특별자치도 춘천시", "51"), ("전북특별자치도 전주시", "52"), ("", None), ]) def test_sido_of(address, code): assert sido_of(address) == code @pytest.mark.anyio async def test_fetch_popup_index_reads_popup_pages_only(monkeypatch): monkeypatch.setattr(popga, "REQUEST_GAP_SEC", 0) pages = { "/sitemap.xml": "https://popga.co.kr/sitemap/2.xml" "https://popga.co.kr/sitemap/5.xml", "/sitemap/2.xml": "https://popga.co.kr/popup/122026-10-06T00:00:00Z" "https://popga.co.kr/popup/11", "/sitemap/5.xml": "https://popga.co.kr/popup/12/review/32026-10-06", } async with httpx.AsyncClient(transport=httpx.MockTransport(lambda req: httpx.Response(200, text=pages[req.url.path]))) as client: assert await fetch_popup_index(client) == {12: "2026-10-06T00:00:00Z", 11: ""}