Files
romm/backend/tests/tasks/test_cleanup_upload_tmp.py
Georges-Antoine Assi 787bbba2b4 refactor(tasks): give every enqueue one home
Scheduling moved onto RQ, which left the task catalog with two identity
schemes: the registry key a job payload now carries, and the dotted func
path each PeriodicTask still declared. Nothing read the latter, so it is
gone, along with the test that guarded paths no payload names.

Four sites built the same enqueue by hand and had already drifted: the
startup backfills passed the global timeout and no result_ttl, so an
auto-enqueued conversion fell out of the task list after RQ's default
500s while the same task triggered by an admin lived for TASK_RESULT_TTL.
They now go through enqueue_task, which derives timeout, ttl and meta
from the task itself.

Scan discovery paid a Redis round trip per queued job to re-read a status
the fetch already carried, and the stale sweep read every delayed job to
find the few past due. Both now ask Redis once.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-02 21:03:30 -04:00

83 lines
2.5 KiB
Python

import time
import pytest
from tasks.scheduled.cleanup_upload_tmp import CleanupUploadTmpTask
class TestCleanupUploadTmpTask:
@pytest.fixture
def task(self):
return CleanupUploadTmpTask()
def test_init(self, task):
"""Test task initialization"""
assert task.enabled is True
assert task.cron_string == "0 * * * *"
async def test_run_no_tmp_dir(self, task, monkeypatch, tmp_path):
"""Task is a no-op when the uploads tmp directory does not exist."""
import tasks.scheduled.cleanup_upload_tmp as mod
monkeypatch.setattr(mod, "ROM_UPLOAD_TMP_BASE", tmp_path / "nonexistent")
# Should complete without error
await task.run()
async def test_run_removes_expired_dirs(self, task, monkeypatch, tmp_path):
"""Directories whose mtime is older than ROM_UPLOAD_TTL should be removed."""
import tasks.scheduled.cleanup_upload_tmp as mod
uploads = tmp_path / "uploads"
uploads.mkdir()
monkeypatch.setattr(mod, "ROM_UPLOAD_TMP_BASE", uploads)
monkeypatch.setattr(mod, "ROM_UPLOAD_TTL", 3600)
old_dir = uploads / "old-upload"
old_dir.mkdir()
# Backdate mtime to 2 hours ago
old_ts = time.time() - 7200
import os
os.utime(old_dir, (old_ts, old_ts))
await task.run()
assert not old_dir.exists()
async def test_run_keeps_recent_dirs(self, task, monkeypatch, tmp_path):
"""Directories whose mtime is within ROM_UPLOAD_TTL should be kept."""
import tasks.scheduled.cleanup_upload_tmp as mod
uploads = tmp_path / "uploads"
uploads.mkdir()
monkeypatch.setattr(mod, "ROM_UPLOAD_TMP_BASE", uploads)
monkeypatch.setattr(mod, "ROM_UPLOAD_TTL", 3600)
recent_dir = uploads / "recent-upload"
recent_dir.mkdir()
# mtime is now (within TTL)
await task.run()
assert recent_dir.exists()
async def test_run_skips_files(self, task, monkeypatch, tmp_path):
"""Files (not directories) in the uploads root are left untouched."""
import tasks.scheduled.cleanup_upload_tmp as mod
uploads = tmp_path / "uploads"
uploads.mkdir()
monkeypatch.setattr(mod, "ROM_UPLOAD_TMP_BASE", uploads)
monkeypatch.setattr(mod, "ROM_UPLOAD_TTL", 3600)
stale_file = uploads / "stale.tmp"
stale_file.write_bytes(b"data")
old_ts = time.time() - 7200
import os
os.utime(stale_file, (old_ts, old_ts))
await task.run()
assert stale_file.exists()