class TestCameraStreamReader(object):

    SAMPLE_CAMERA_ID = 0
    SAMPLE_IMAGE_SIZE = Vector2(10, 10)

    @classmethod
    def setup_class(cls):
        cls._sample_image = Image.from_attributes(cls.SAMPLE_IMAGE_SIZE.width, cls.SAMPLE_IMAGE_SIZE.height, ColorMode.BGR)
    
    @mock.patch('services.video_streaming.stream_readers.CameraStreamReader.VideoCapture', autospec = False)
    def setup(self, video_capture_mock):
        self._video_capture_mock_instance = video_capture_mock.return_value
        self._video_capture_mock_instance.open_camera.return_value = True
        self._camera_stream_reader = CameraStreamReader(self.SAMPLE_CAMERA_ID)

    @raises(InvalidOperationError)
    def test_given_camera_not_opened_when_reading_next_frame_then_error_is_raised(self):
        self._camera_stream_reader.close()
        self._camera_stream_reader.read_next()

    @raises(IOError)
    def test_given_unsuccessful_video_capture_read_when_reading_next_frame_then_error_is_raised(self):
        self._video_capture_mock_instance.read.return_value = False, None
        self._camera_stream_reader.read_next()

    def test_given_successful_video_capture_read_when_reading_next_frame_then_image_is_returned(self):
        self._video_capture_mock_instance.read.return_value = True, self._sample_image.cv_image
        image = self._camera_stream_reader.read_next()
        assert_is(image.cv_image, self._sample_image.cv_image)

    def test_given_opened_camera_when_camera_stream_reader_is_closed_then_video_capture_is_released(self):
        self._camera_stream_reader.close()
        self._video_capture_mock_instance.release.assert_called_with()
 def setup(self, video_capture_mock):
     self._video_capture_mock_instance = video_capture_mock.return_value
     self._video_capture_mock_instance.open_camera.return_value = True
     self._camera_stream_reader = CameraStreamReader(self.SAMPLE_CAMERA_ID)
 def set_camera_source(self, camera_id, frame_rotation_angle = 0):
     camera_stream_reader = CameraStreamReader(camera_id)
     camera_stream_reader.frame_size = self.TARGET_CAMERA_FRAME_SIZE
     camera_stream_reader.frame_rotation_angle = frame_rotation_angle
     self._change_source(camera_stream_reader)