Update tests/test_rtsp.py
Browse files- tests/test_rtsp.py +24 -14
tests/test_rtsp.py
CHANGED
|
@@ -1,27 +1,37 @@
|
|
| 1 |
import pytest
|
|
|
|
| 2 |
from unittest.mock import patch, MagicMock
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
# Add the parent directory to the path to import app
|
| 7 |
-
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
|
| 9 |
@patch('cv2.VideoCapture')
|
| 10 |
def test_rtsp_connection(mock_video_capture):
|
| 11 |
-
"""Test RTSP connection
|
| 12 |
# Mock VideoCapture to simulate a successful connection
|
| 13 |
mock_instance = mock_video_capture.return_value
|
| 14 |
mock_instance.isOpened.return_value = True
|
| 15 |
-
mock_instance.read.side_effect = [(True, None), (False, None)] # Simulate one frame then stop
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if mock_instance.isOpened():
|
| 22 |
-
yield None, "2025-01-01T12:00:00+05:30" # Simulate one frame
|
| 23 |
|
| 24 |
-
frames = list(mock_capture_rtsp_frames(rtsp_url, max_frames=1))
|
| 25 |
assert len(frames) == 1, "Expected one frame from mocked RTSP stream"
|
|
|
|
|
|
|
|
|
|
| 26 |
mock_video_capture.assert_called_with(rtsp_url)
|
| 27 |
-
mock_instance.release.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pytest
|
| 2 |
+
from app import capture_rtsp_frames
|
| 3 |
from unittest.mock import patch, MagicMock
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
@patch('cv2.VideoCapture')
|
| 8 |
def test_rtsp_connection(mock_video_capture):
|
| 9 |
+
"""Test RTSP connection with mocked VideoCapture"""
|
| 10 |
# Mock VideoCapture to simulate a successful connection
|
| 11 |
mock_instance = mock_video_capture.return_value
|
| 12 |
mock_instance.isOpened.return_value = True
|
|
|
|
| 13 |
|
| 14 |
+
# Create a dummy frame
|
| 15 |
+
dummy_frame = np.zeros((480, 640, 3), dtype=np.uint8)
|
| 16 |
+
mock_instance.read.side_effect = [(True, dummy_frame), (False, None)] # Simulate one frame then stop
|
| 17 |
|
| 18 |
+
rtsp_url = "rtsp://test:8554/stream"
|
| 19 |
+
frames = list(capture_rtsp_frames(rtsp_url, max_frames=1)) # Consume generator
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
assert len(frames) == 1, "Expected one frame from mocked RTSP stream"
|
| 22 |
+
assert frames[0][0] is not None, "Frame should not be None"
|
| 23 |
+
assert frames[0][1] is not None, "Timestamp should not be None"
|
| 24 |
+
|
| 25 |
mock_video_capture.assert_called_with(rtsp_url)
|
| 26 |
+
mock_instance.release.assert_called_once()
|
| 27 |
+
|
| 28 |
+
def test_rtsp_connection_failure():
|
| 29 |
+
"""Test RTSP connection failure handling"""
|
| 30 |
+
with patch('cv2.VideoCapture') as mock_video_capture:
|
| 31 |
+
mock_instance = mock_video_capture.return_value
|
| 32 |
+
mock_instance.isOpened.return_value = False
|
| 33 |
+
|
| 34 |
+
rtsp_url = "rtsp://invalid:8554/stream"
|
| 35 |
+
|
| 36 |
+
with pytest.raises(ValueError, match="RTSP stream not accessible"):
|
| 37 |
+
list(capture_rtsp_frames(rtsp_url, max_frames=1))
|