87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
"""Module to test camera base integration class for Debye."""
|
|
|
|
import threading
|
|
from unittest import mock
|
|
|
|
import ophyd
|
|
import pytest
|
|
from ophyd_devices.tests.utils import MockPV, patch_dual_pvs
|
|
|
|
from debye_bec.devices.cameras.debye_base_cam import DebyeBaseCamera
|
|
|
|
# pylint: disable=protected-access
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def mock_cam():
|
|
"""Fixture to mock the camera device."""
|
|
name = "cam"
|
|
prefix = "test:"
|
|
with mock.patch.object(ophyd, "cl") as mock_cl:
|
|
mock_cl.get_pv = MockPV
|
|
mock_cl.thread_class = threading.Thread
|
|
dev = DebyeBaseCamera(name=name, prefix=prefix)
|
|
patch_dual_pvs(dev)
|
|
yield dev
|
|
|
|
|
|
def test_init(mock_cam):
|
|
"""Test the initialization of the camera device."""
|
|
assert mock_cam.name == "cam"
|
|
assert mock_cam.prefix == "test:"
|
|
assert mock_cam._update_frequency == 1
|
|
assert mock_cam._live_mode is False
|
|
assert mock_cam._live_mode_event is None
|
|
assert mock_cam._task_status is None
|
|
assert mock_cam.preview.ndim == 2
|
|
assert mock_cam.preview.num_rotation_90 == -1
|
|
|
|
|
|
def test_start_live_mode(mock_cam):
|
|
"""Test starting live mode."""
|
|
|
|
def mock_emit_to_bec(*args, **kwargs):
|
|
"""Mock emit_to_bec method."""
|
|
while not mock_cam._live_mode_event.wait(1 / mock_cam._update_frequency):
|
|
pass
|
|
|
|
with mock.patch.object(mock_cam, "emit_to_bec", side_effect=mock_emit_to_bec):
|
|
mock_cam._start_live_mode()
|
|
assert mock_cam._live_mode_event is not None
|
|
assert mock_cam._task_status is not None
|
|
assert mock_cam._task_status.state == "running"
|
|
mock_cam._live_mode_event.set()
|
|
# Wait for the task to resolve
|
|
mock_cam._task_status.wait(timeout=5)
|
|
assert mock_cam._task_status.done is True
|
|
|
|
|
|
def test_stop_live_mode(mock_cam):
|
|
"""Test stopping live mode."""
|
|
with mock.patch.object(mock_cam, "_live_mode_event") as mock_live_mode_event:
|
|
mock_cam._stop_live_mode()
|
|
assert mock_live_mode_event.set.called
|
|
assert mock_cam._live_mode_event is None
|
|
|
|
|
|
def test_live_mode_property(mock_cam):
|
|
"""Test the live_mode property."""
|
|
assert mock_cam.live_mode is False
|
|
with mock.patch.object(mock_cam, "_start_live_mode") as mock_start_live_mode:
|
|
with mock.patch.object(mock_cam, "_stop_live_mode") as mock_stop_live_mode:
|
|
# Set to true
|
|
mock_cam.live_mode = True
|
|
assert mock_start_live_mode.called
|
|
assert mock_cam._live_mode is True
|
|
assert mock_start_live_mode.call_count == 1
|
|
# Second call should call _start_live_mode
|
|
mock_cam.live_mode = True
|
|
assert mock_start_live_mode.call_count == 1
|
|
|
|
# Set to false
|
|
mock_cam.live_mode = False
|
|
assert mock_stop_live_mode.called
|
|
assert mock_cam._live_mode is False
|
|
assert mock_stop_live_mode.call_count == 1
|