Update tests/test_violation_log.py
Browse files- tests/test_violation_log.py +42 -38
tests/test_violation_log.py
CHANGED
|
@@ -1,47 +1,51 @@
|
|
| 1 |
import pytest
|
| 2 |
import os
|
| 3 |
-
import tempfile
|
| 4 |
import json
|
| 5 |
-
from
|
| 6 |
-
import sys
|
| 7 |
import numpy as np
|
| 8 |
import cv2
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def test_log_violation():
|
| 14 |
"""Test violation logging functionality"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Verify content
|
| 44 |
-
with open(log_file_path, 'r') as f:
|
| 45 |
-
logs = json.load(f)
|
| 46 |
-
assert len(logs) == 1
|
| 47 |
-
assert logs[0]['violation_type'] == "No Helmet"
|
|
|
|
| 1 |
import pytest
|
| 2 |
import os
|
|
|
|
| 3 |
import json
|
| 4 |
+
from app import save_snapshot, log_violation
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
+
def test_save_snapshot():
|
| 10 |
+
"""Test snapshot saving functionality"""
|
| 11 |
+
# Create a test frame
|
| 12 |
+
frame = np.zeros((640, 640, 3), dtype=np.uint8)
|
| 13 |
+
snapshot_path = save_snapshot(frame)
|
| 14 |
+
|
| 15 |
+
assert os.path.exists(snapshot_path), "Snapshot file should exist"
|
| 16 |
+
assert snapshot_path.endswith('.jpg'), "Snapshot should be a JPG file"
|
| 17 |
+
|
| 18 |
+
# Clean up
|
| 19 |
+
if os.path.exists(snapshot_path):
|
| 20 |
+
os.remove(snapshot_path)
|
| 21 |
|
| 22 |
def test_log_violation():
|
| 23 |
"""Test violation logging functionality"""
|
| 24 |
+
frame = np.zeros((640, 640, 3), dtype=np.uint8)
|
| 25 |
+
snapshot_path = save_snapshot(frame)
|
| 26 |
+
|
| 27 |
+
violation_data = {
|
| 28 |
+
'site_id': "Site1",
|
| 29 |
+
'camera_id': "Camera1",
|
| 30 |
+
'violation_type': "No Helmet",
|
| 31 |
+
'timestamp': "2025-06-07T09:23:00Z",
|
| 32 |
+
'snapshot_url': snapshot_path,
|
| 33 |
+
'severity': "Critical"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
log_violation(violation_data)
|
| 37 |
+
|
| 38 |
+
# Check if log file exists
|
| 39 |
+
log_file = "/tmp/snapshots/violation_logs.json"
|
| 40 |
+
assert os.path.exists(log_file), "Violation logs file should exist"
|
| 41 |
+
|
| 42 |
+
# Check if violation was logged
|
| 43 |
+
with open(log_file, 'r') as f:
|
| 44 |
+
logs = json.load(f)
|
| 45 |
+
|
| 46 |
+
assert len(logs) > 0, "At least one violation should be logged"
|
| 47 |
+
assert logs[-1]['violation_type'] == "No Helmet", "Latest violation should match"
|
| 48 |
+
|
| 49 |
+
# Clean up
|
| 50 |
+
if os.path.exists(snapshot_path):
|
| 51 |
+
os.remove(snapshot_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|