| """ |
| Tests for database operations. |
| |
| These use the real SQLite backend (a test DB would be cleaner, but for an |
| FYP the real DB is acceptable as long as tests are read-heavy and skip |
| destructive operations unless explicitly run). |
| """ |
|
|
| from __future__ import annotations |
|
|
| import sys |
| from pathlib import Path |
|
|
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
|
|
| from database import ( |
| init_db, |
| log_scan, |
| get_scan_history, |
| count_scans, |
| delete_scan, |
| get_stats, |
| get_disk_usage, |
| get_cache_stats, |
| ) |
|
|
|
|
| class TestDatabase: |
| def test_init_db(self): |
| """init_db should not raise.""" |
| init_db() |
|
|
| def test_log_and_retrieve(self): |
| """Log a scan and verify it appears in history.""" |
| |
| import uuid |
| tag = str(uuid.uuid4())[:8] |
| fname = f"test_{tag}.jpg" |
| log_scan(fname, "Fake", 85.5, 1) |
| history = get_scan_history(search=tag) |
| assert any(fname in row[1] for row in history) |
|
|
| def test_count_scans(self): |
| count = count_scans() |
| assert isinstance(count, int) |
| assert count >= 0 |
|
|
| def test_get_stats_shape(self): |
| stats = get_stats() |
| for key in ("total_scans", "fake", "real", "no_face", "videos", "avg_confidence", "today", "model_version"): |
| assert key in stats, f"Missing key: {key}" |
| assert isinstance(stats["model_version"], str) |
|
|
| def test_disk_usage(self): |
| du = get_disk_usage() |
| for key in ("scans_dir", "database", "scans_dir_bytes", "database_bytes"): |
| assert key in du |
|
|
| def test_cache_stats(self): |
| cs = get_cache_stats() |
| assert "cached_images" in cs |
| assert "total_cache_hits" in cs |
|
|