PrashanthB461 commited on
Commit
d4c52fd
·
verified ·
1 Parent(s): 7a1e317

Update tests/test_violation_log.py

Browse files
Files changed (1) hide show
  1. 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 unittest.mock import patch
6
- import sys
7
  import numpy as np
8
  import cv2
 
9
 
10
- # Add the parent directory to the path to import app
11
- sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
 
 
 
 
 
 
 
 
 
 
12
 
13
  def test_log_violation():
14
  """Test violation logging functionality"""
15
- # Create a temporary directory for testing
16
- with tempfile.TemporaryDirectory() as temp_dir:
17
- # Mock the log file path
18
- log_file_path = os.path.join(temp_dir, "violation_logs.json")
19
-
20
- # Create a test frame
21
- frame = np.zeros((640, 640, 3), dtype=np.uint8)
22
-
23
- # Create test violation data
24
- violation_data = {
25
- 'site_id': "SITE001",
26
- 'camera_id': "CAM001",
27
- 'violation_type': "No Helmet",
28
- 'timestamp': "2025-01-01T12:00:00+05:30",
29
- 'snapshot_url': "/tmp/snapshots/test_snapshot.jpg",
30
- 'severity': "Critical"
31
- }
32
-
33
- # Test logging
34
- with patch('pathlib.Path') as mock_path:
35
- mock_path.return_value.exists.return_value = False
36
- # Since we can't easily import and test the actual function,
37
- # we'll test the concept by writing directly
38
- with open(log_file_path, 'w') as f:
39
- json.dump([violation_data], f)
40
-
41
- assert os.path.exists(log_file_path), "Violation logs file not found"
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)