import pytest from httpx import ASGITransport, AsyncClient from sqlalchemy import text import services.sky_service as sky_service from common.cache.redis_client import REDIS from common.database.db_session_manager import DB_SESSION_MNG from router.router import app def _fake_response(points, days): from datetime import datetime, timedelta start = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=1) times = [(start + timedelta(hours=h)).strftime("%Y-%m-%dT%H:00") for h in range(24 * (days + 1))] hourly = {"time": times} for m in ("ecmwf_ifs025", "gfs_seamless", "jma_seamless", "icon_seamless"): hourly[f"cloud_cover_low_{m}"] = [0] * len(times) hourly[f"cloud_cover_mid_{m}"] = [20] * len(times) hourly[f"cloud_cover_high_{m}"] = [50] * len(times) return [{"hourly": hourly} for _ in points] @pytest.fixture async def clean(): async def wipe(): await DB_SESSION_MNG.execute_lambda_run([ lambda s: s.execute(text("DELETE FROM sky_score")), lambda s: s.execute(text("DELETE FROM cloud_hourly")), ]) keys = [k async for k in REDIS.conn.scan_iter("sky:*")] if keys: await REDIS.conn.delete(*keys) await wipe() yield await wipe() @pytest.mark.anyio async def test_sky_view_scores_three_days_with_spots(monkeypatch, clean): async def fake_fetch(client, points, days=3): return _fake_response(points, days) monkeypatch.setattr(sky_service, "fetch_clouds", fake_fetch) async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/sky", params={"region": "1144000000"})).json() assert body["result"]["success"] is True assert body["sunset"]["grade"] == 4 and body["sunset"]["grade_label"] == "붉음" assert body["sunset"]["confidence"] == 1 assert len(body["days"]) == 3 assert [s["name"] for s in body["spots"]] == ["망원한강공원", "하늘공원", "선유도공원"] assert 255 <= body["sunset_azimuth"] <= 280 @pytest.mark.anyio async def test_sky_ranking_has_30_spots_sorted(monkeypatch, clean): async def fake_fetch(client, points, days=3): return _fake_response(points, days) monkeypatch.setattr(sky_service, "fetch_clouds", fake_fetch) async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client: body = (await client.get("/v1/sky/ranking")).json() scores = [i["score"] for i in body["items"]] assert len(scores) == 30 and scores == sorted(scores, reverse=True) assert body["items"][0]["rank"] == 1 and body["items"][0]["note"]