import pytest from sqlalchemy import text from common.database.db_session_manager import DB_SESSION_MNG from scheduler.jobs import cleanup_old_rows REGION = "1114000000" async def _count(table: str) -> int: _, n = await DB_SESSION_MNG.execute_lambda(lambda s: s.scalar(text(f"SELECT count(*) FROM {table}"))) return n @pytest.mark.anyio async def test_cleanup_removes_scores_older_than_seven_days(): today = "(now() AT TIME ZONE 'Asia/Seoul')::date" await DB_SESSION_MNG.execute_lambda_run([ lambda s, d=d: s.execute(text( f"INSERT INTO index_score (region_code, score_date, index_id, score, summary, temp_score, rain_score, wind_score, humidity_score, air_score, time_score, notes, computed_at) " f"VALUES ('{REGION}', {today} - {d}, (SELECT min(index_id) FROM index_definition), 50, '', 50, 50, 50, 50, 50, 50, '{{}}', now())" )) for d in (8, 0) ] + [ lambda s, d=d: s.execute(text( f"INSERT INTO sky_score (region_code, score_date, sky_type, score, grade, confidence, best_times, computed_at) " f"VALUES ('{REGION}', {today} - {d}, 1, 50, 1, 1, '[]', now())" )) for d in (8, 0) ]) await cleanup_old_rows() assert await _count("index_score") == 1 assert await _count("sky_score") == 1