feat(fc-cleanup): audit lifecycle service functions (start/apply/cancel) + tests — Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -74,3 +74,99 @@ def test_delete_min_dimension_violations_unlinks_and_cascades(db_sync, tmp_path)
|
||||
).scalars().all()
|
||||
assert remaining_ids == [rec_big.id]
|
||||
assert not small_path.exists() # file unlinked
|
||||
|
||||
|
||||
# --- Audit lifecycle tests (Task 5) ---
|
||||
|
||||
import backend.app.tasks.library_audit # noqa: F401, E402 — celery registration
|
||||
|
||||
|
||||
def test_start_audit_run_creates_row_and_dispatches(db_sync, monkeypatch):
|
||||
dispatched = []
|
||||
from backend.app.tasks import library_audit as la_mod
|
||||
monkeypatch.setattr(
|
||||
la_mod.scan_library_for_rule, "delay",
|
||||
lambda audit_id: dispatched.append(audit_id),
|
||||
)
|
||||
audit_id = cleanup_service.start_audit_run(
|
||||
db_sync, rule="transparency", params={"threshold": 0.9},
|
||||
)
|
||||
db_sync.commit()
|
||||
row_status = db_sync.execute(
|
||||
select(LibraryAuditRun.status).where(LibraryAuditRun.id == audit_id)
|
||||
).scalar_one()
|
||||
assert row_status == "running"
|
||||
assert dispatched == [audit_id]
|
||||
|
||||
|
||||
def test_start_audit_run_rejects_when_another_is_running(db_sync, monkeypatch):
|
||||
from backend.app.tasks import library_audit as la_mod
|
||||
monkeypatch.setattr(
|
||||
la_mod.scan_library_for_rule, "delay", lambda audit_id: None,
|
||||
)
|
||||
cleanup_service.start_audit_run(
|
||||
db_sync, rule="transparency", params={"threshold": 0.9},
|
||||
)
|
||||
db_sync.commit()
|
||||
with pytest.raises(cleanup_service.AuditAlreadyRunning):
|
||||
cleanup_service.start_audit_run(
|
||||
db_sync, rule="single_color",
|
||||
params={"threshold": 0.95, "tolerance": 30},
|
||||
)
|
||||
|
||||
|
||||
def test_apply_audit_run_with_correct_token_deletes_matched(db_sync, tmp_path):
|
||||
rec = _make_image_record(
|
||||
db_sync, tmp_path, width=100, height=100, color=(60, 0, 0),
|
||||
)
|
||||
db_sync.flush()
|
||||
audit = LibraryAuditRun(
|
||||
rule="transparency", params={"threshold": 0.9},
|
||||
status="ready", scanned_count=1, matched_count=1,
|
||||
matched_ids=[rec.id],
|
||||
)
|
||||
db_sync.add(audit)
|
||||
db_sync.commit()
|
||||
|
||||
deleted = cleanup_service.apply_audit_run(
|
||||
db_sync, audit_id=audit.id,
|
||||
confirm_token=f"apply-audit-{audit.id}",
|
||||
images_root=tmp_path,
|
||||
)
|
||||
assert deleted == 1
|
||||
remaining_count = db_sync.execute(
|
||||
select(func.count()).select_from(ImageRecord)
|
||||
).scalar_one()
|
||||
assert remaining_count == 0
|
||||
new_status = db_sync.execute(
|
||||
select(LibraryAuditRun.status).where(LibraryAuditRun.id == audit.id)
|
||||
).scalar_one()
|
||||
assert new_status == "applied"
|
||||
|
||||
|
||||
def test_apply_audit_run_with_wrong_token_raises(db_sync, tmp_path):
|
||||
audit = LibraryAuditRun(
|
||||
rule="transparency", params={"threshold": 0.9},
|
||||
status="ready", matched_ids=[],
|
||||
)
|
||||
db_sync.add(audit)
|
||||
db_sync.commit()
|
||||
with pytest.raises(cleanup_service.ConfirmTokenMismatch):
|
||||
cleanup_service.apply_audit_run(
|
||||
db_sync, audit_id=audit.id,
|
||||
confirm_token="wrong-token", images_root=tmp_path,
|
||||
)
|
||||
|
||||
|
||||
def test_cancel_audit_run_flips_status(db_sync):
|
||||
audit = LibraryAuditRun(
|
||||
rule="transparency", params={"threshold": 0.9},
|
||||
status="running", matched_ids=[],
|
||||
)
|
||||
db_sync.add(audit)
|
||||
db_sync.commit()
|
||||
cleanup_service.cancel_audit_run(db_sync, audit_id=audit.id)
|
||||
new_status = db_sync.execute(
|
||||
select(LibraryAuditRun.status).where(LibraryAuditRun.id == audit.id)
|
||||
).scalar_one()
|
||||
assert new_status == "cancelled"
|
||||
|
||||
Reference in New Issue
Block a user