85ecbc22ac
Hourly background task deletes events older than configured retention period (default 90 days). Reads event_retention_days from settings table. Includes 2 tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
"""Tests for event retention cleanup."""
|
|
|
|
import sqlite3
|
|
|
|
from database import init_db, set_db_path
|
|
|
|
|
|
def test_cleanup_deletes_old_events(db_path):
|
|
"""Old events beyond retention should be deleted."""
|
|
set_db_path(db_path)
|
|
init_db()
|
|
conn = sqlite3.connect(db_path)
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
# Insert an old event (100 days ago)
|
|
conn.execute(
|
|
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status, created_at)
|
|
VALUES ('old-1', 'cam', 'person', 0.9, 'unknown', datetime('now', '-100 days'))"""
|
|
)
|
|
# Insert a recent event
|
|
conn.execute(
|
|
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status)
|
|
VALUES ('new-1', 'cam', 'person', 0.9, 'unknown')"""
|
|
)
|
|
conn.commit()
|
|
|
|
# Simulate cleanup with 90-day retention
|
|
result = conn.execute(
|
|
"DELETE FROM events WHERE created_at < datetime('now', '-90 days')"
|
|
)
|
|
conn.commit()
|
|
assert result.rowcount == 1
|
|
|
|
remaining = conn.execute("SELECT COUNT(*) as c FROM events").fetchone()["c"]
|
|
assert remaining == 1
|
|
conn.close()
|
|
|
|
|
|
def test_cleanup_preserves_recent_events(db_path):
|
|
"""Events within retention period should not be deleted."""
|
|
set_db_path(db_path)
|
|
init_db()
|
|
conn = sqlite3.connect(db_path)
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
# Insert events from 30 and 60 days ago
|
|
conn.execute(
|
|
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status, created_at)
|
|
VALUES ('mid-1', 'cam', 'person', 0.9, 'unknown', datetime('now', '-30 days'))"""
|
|
)
|
|
conn.execute(
|
|
"""INSERT INTO events (frigate_event_id, camera, label, score, match_status, created_at)
|
|
VALUES ('mid-2', 'cam', 'car', 0.8, 'known', datetime('now', '-60 days'))"""
|
|
)
|
|
conn.commit()
|
|
|
|
result = conn.execute(
|
|
"DELETE FROM events WHERE created_at < datetime('now', '-90 days')"
|
|
)
|
|
conn.commit()
|
|
assert result.rowcount == 0
|
|
|
|
remaining = conn.execute("SELECT COUNT(*) as c FROM events").fetchone()["c"]
|
|
assert remaining == 2
|
|
conn.close()
|