Spaces:
Running on T4
Running on T4
Download backend/tests/integration/test_cleanup.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.17 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_cleanup.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/integration/test_cleanup.py
-
curl -L -o test_cleanup.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_cleanup.py
4.17 kB
| """The scheduled cleanup job.""" | |
| import uuid | |
| from datetime import UTC, datetime, timedelta | |
| from sqlalchemy import func, select | |
| from app.core.database import get_session_factory | |
| from app.integrations.storage import get_storage | |
| from app.jobs.cleanup import run_cleanup | |
| from app.modules.idempotency.models import IdempotencyKey | |
| from app.modules.sessions.models import SwingSession | |
| from app.modules.uploads.models import Upload | |
| from tests.factories import create_session, make_account, mark_analyzed, set_session, upload | |
| async def _keys(upload_ids) -> list[str]: | |
| async with get_session_factory()() as db: | |
| return list((await db.scalars(select(Upload.object_key).where( | |
| Upload.id.in_([uuid.UUID(str(i)) for i in upload_ids])))).all()) | |
| async def _session_upload_ids(session_id) -> list: | |
| async with get_session_factory()() as db: | |
| s = await db.get(SwingSession, uuid.UUID(session_id)) | |
| return [s.video_upload_id, s.thumbnail_upload_id] | |
| async def _cleanup(now: datetime) -> dict: | |
| async with get_session_factory()() as db: | |
| return await run_cleanup(db, get_storage(), now) | |
| async def _count(model) -> int: | |
| async with get_session_factory()() as db: | |
| return await db.scalar(select(func.count()).select_from(model)) | |
| async def test_cleanup_removes_expired_rows_and_objects(client): | |
| storage = get_storage() | |
| player = await make_account() | |
| h = player["headers"] | |
| now = datetime.now(UTC) | |
| stale_upload = await upload(client, h) | |
| stale_key = (await _keys([stale_upload]))[0] | |
| abandoned = await create_session(client, h) | |
| abandoned_keys = await _keys(await _session_upload_ids(abandoned["id"])) | |
| analysed = await create_session(client, h) | |
| await mark_analyzed(analysed["id"], 60) | |
| old_deleted = await create_session(client, h) | |
| await mark_analyzed(old_deleted["id"], 50) | |
| old_deleted_keys = await _keys(await _session_upload_ids(old_deleted["id"])) | |
| recent_deleted = await create_session(client, {**h, "Idempotency-Key": "k1"}) | |
| await mark_analyzed(recent_deleted["id"], 50) | |
| later = now + timedelta(hours=25) | |
| await set_session(old_deleted["id"], deleted_at=later - timedelta(days=31)) | |
| await set_session(recent_deleted["id"], deleted_at=later - timedelta(days=29)) | |
| fresh_upload = await upload(client, h) # created "now", so not yet stale at `later` | |
| async with get_session_factory()() as db: | |
| await db.execute(Upload.__table__.update().where(Upload.id == uuid.UUID(fresh_upload)) | |
| .values(created_at=later - timedelta(hours=1))) | |
| await db.commit() | |
| counts = await _cleanup(later) | |
| assert counts == {"unattached_uploads": 1, "pending_sessions": 1, "deleted_sessions": 1, "objects": 5, | |
| "idempotency_keys": 1, "api_request_logs": 0} | |
| for key in [stale_key, *abandoned_keys, *old_deleted_keys]: | |
| assert await storage.head(key) is None, key | |
| async with get_session_factory()() as db: | |
| remaining = set((await db.scalars(select(SwingSession.id))).all()) | |
| assert remaining == {uuid.UUID(analysed["id"]), uuid.UUID(recent_deleted["id"])} | |
| assert await db.get(Upload, uuid.UUID(fresh_upload)) is not None | |
| assert await db.get(Upload, uuid.UUID(stale_upload)) is None | |
| assert await _count(Upload) == 5 # two sessions with video + thumbnail, plus the fresh upload | |
| assert await _count(IdempotencyKey) == 0 | |
| assert await _cleanup(later) == {"unattached_uploads": 0, "pending_sessions": 0, "deleted_sessions": 0, | |
| "objects": 0, "idempotency_keys": 0, "api_request_logs": 0} | |
| async def test_cleanup_keeps_recent_data(client): | |
| player = await make_account() | |
| await upload(client, player["headers"]) | |
| await create_session(client, {**player["headers"], "Idempotency-Key": "k2"}) | |
| counts = await _cleanup(datetime.now(UTC) + timedelta(hours=23)) | |
| assert counts == {"unattached_uploads": 0, "pending_sessions": 0, "deleted_sessions": 0, "objects": 0, | |
| "idempotency_keys": 0, "api_request_logs": 0} | |
| assert await _count(SwingSession) == 1 and await _count(Upload) == 3 | |