Files

93 lines
2.6 KiB
Python

from unittest.mock import MagicMock, patch
import pytest
from epics import PV, Motor
from aare.devices.mx_lib import clean_filename, is_epics_type, pv_wait, wait_for_movement_to_finish
def test_clean_filename():
assert clean_filename("my file!.txt") == "my_file_.txt"
assert clean_filename(" /path/to/somewhere/file.txt ") == "path_to_somewhere_file.txt"
assert clean_filename("abcABC123") == "abcABC123"
assert clean_filename("file._-") == "file"
with pytest.raises(ValueError):
clean_filename("!!!")
def test_is_epics_type():
mock_pv = MagicMock(spec=PV)
mock_pv.type = "double"
assert is_epics_type(mock_pv, "double") is True
assert is_epics_type(mock_pv, "enum") is False
class FakeType:
pass
assert is_epics_type(mock_pv, FakeType) is False
@patch("aare.devices.mx_lib.poll")
def test_wait_for_movement_to_finish(mock_poll):
mock_motor = MagicMock(spec=Motor)
mock_motor.readback = 10.0
mock_motor.slew_speed = 1.0
mock_motor.done_moving = True
wait_for_movement_to_finish(mock_motor)
assert mock_poll.called
@patch("aare.devices.mx_lib.poll")
def test_wait_for_movement_to_finish_timeout(mock_poll):
mock_motor = MagicMock(spec=Motor)
mock_motor.readback = 10.0
mock_motor.slew_speed = 1.0
mock_motor.done_moving = False
mock_motor.drive = 11.0
mock_motor.units = "mm"
mock_motor._prefix = "MOT1:"
with patch("time.time", side_effect=[0, 0, 100, 101]), pytest.raises(TimeoutError):
wait_for_movement_to_finish(mock_motor)
@patch("aare.devices.mx_lib.wait_motor_position")
def test_pv_wait_motor(mock_wait_motor):
mock_motor = MagicMock(spec=Motor)
pv_wait(mock_motor, 10.0)
mock_wait_motor.assert_called_once()
@patch("aare.devices.mx_lib.wait_float_condition")
def test_pv_wait_double(mock_wait_float):
mock_pv = MagicMock(spec=PV)
mock_pv.type = "double"
pv_wait(mock_pv, 10.0)
mock_wait_float.assert_called_once()
@patch("aare.devices.mx_lib.wait_enum_condition")
def test_pv_wait_enum(mock_wait_enum):
mock_pv = MagicMock(spec=PV)
mock_pv.type = "enum"
pv_wait(mock_pv, "READY")
mock_wait_enum.assert_called_once()
@patch("aare.devices.mx_lib.wait_string_condition")
def test_pv_wait_string(mock_wait_string):
mock_pv = MagicMock(spec=PV)
mock_pv.type = "string"
pv_wait(mock_pv, "hello")
mock_wait_string.assert_called_once()
def test_pv_wait_unknown_type():
mock_pv = MagicMock(spec=PV)
mock_pv.type = "unknown"
mock_pv.pvname = "TEST:PV"
with pytest.raises(ValueError):
pv_wait(mock_pv, 1.0)