from datetime import timedelta import pytest from httpx import ASGITransport, AsyncClient from sqlalchemy import text import services.season_service as season_service from common.database.db_session_manager import DB_SESSION_MNG from common.utils.kma_time import now_kst from router.router import app async def _async(v): return v @pytest.mark.anyio async def test_season_week_composes_headline_and_term_progress(monkeypatch): today = now_kst().date() past, nxt = today - timedelta(days=5), today + timedelta(days=10) async def fake_terms(client, year, month): rows = [{"term_date": past, "name": "추분"}, {"term_date": nxt, "name": "한로"}] return [r for r in rows if r["term_date"].year == year and r["term_date"].month == month] monkeypatch.setattr(season_service, "fetch_solar_terms", fake_terms) async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/season/week")).json() assert body["result"]["success"] is True assert body["term_now"]["name"] == "추분" and body["term_next"]["name"] == "한로" assert body["headline"].startswith("추분 지나 5일째") assert body["progress"] == round(5 / 15, 2) @pytest.mark.anyio async def test_article_detail_and_not_found(): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: listed = (await client.get("/v1/season/article/list")).json()["articles"] first = (await client.get(f"/v1/season/article/{listed[0]['article_id']}")).json() missing = (await client.get("/v1/season/article/9999")).json() assert first["body"] and first["article"]["title"] == listed[0]["title"] assert missing["result"]["desc"] == "NOT_FOUND" @pytest.mark.anyio async def test_article_photo_only_when_seeded(): async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: listed = {a["code"]: a for a in (await client.get("/v1/season/article/list")).json()["articles"]} umbrella = (await client.get(f"/v1/season/article/{listed['umbrella']['article_id']}")).json()["article"] _, maple_id = await DB_SESSION_MNG.execute_lambda(lambda s: s.scalar(text("SELECT article_id FROM article WHERE code = 'maple'"))) maple = (await client.get(f"/v1/season/article/{maple_id}")).json()["article"] assert umbrella["image"] == "/season/umbrella.jpg" and "image_credit" not in umbrella assert maple["image"] == "/season/maple.jpg" and maple["image_credit"] == "ⓒJoycekim77 · CC BY-SA 3.0" assert "image" not in listed["howgak"] and "image" not in listed["di"]