from datetime import date import pytest from httpx import ASGITransport, AsyncClient from sqlalchemy import text from common.database.db_session_manager import DB_SESSION_MNG from common.enums import PostType from crud.feed_crud import FeedCRUD from crud.season_crud import SeasonCRUD from router.router import app from services.feed_service import build_feed_day REGION = "1114000000" async def _posts(day: date): _, rows = await DB_SESSION_MNG.execute_lambda(lambda s: s.execute(text(f"SELECT post_type FROM post WHERE feed_date='{day}' ORDER BY position"))) return [r[0] for r in rows] @pytest.mark.anyio async def test_build_day_adds_weekend_post_only_from_thursday(): monday, thursday = date(2026, 10, 12), date(2026, 10, 15) await build_feed_day(monday, FeedCRUD(), SeasonCRUD()) await build_feed_day(thursday, FeedCRUD(), SeasonCRUD()) await build_feed_day(thursday, FeedCRUD(), SeasonCRUD()) assert PostType.WEEK.value not in await _posts(monday) assert (await _posts(thursday)).count(PostType.WEEK.value) == 1 assert (await _posts(thursday))[0] == PostType.WEATHER.value @pytest.mark.anyio async def test_feed_assembles_hero_stories_posts_and_bands(offline): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/feed", params={"region": REGION})).json() assert body["result"]["success"] is True assert body["hero"]["temp_c"] == 20 and len(body["hero"]["top3"]) == 3 assert len(body["stories"]) == 23 weather = next(p for p in body["posts"] if p["post_type"] == PostType.WEATHER.value) assert [s["kind"] for s in weather["slides"]] == ["hours", "rows", "rows"] assert len(weather["slides"][0]["hours"]) == 8 assert body["bands"]["events"][0]["name"] == "테스트 축제"