docs: improved npoint test docs

This commit is contained in:
2024-10-02 18:21:11 +02:00
parent 59db1c067b
commit 8c2d705a89
+35
View File
@@ -5,9 +5,15 @@ import pytest
from csaxs_bec.devices.npoint import NPointAxis, NPointController
# pylint: disable=protected-access
# pylint: disable=redefined-outer-name
@pytest.fixture
def controller():
"""
Fixture to create a NPointController object.
"""
with mock.patch("ophyd_devices.utils.socket.SocketIO") as socket_cls:
controller = NPointController(
socket_cls=socket_cls, socket_host="localhost", socket_port=1234
@@ -20,6 +26,9 @@ def controller():
@pytest.fixture
def npointx():
"""
Fixture to create a NPointAxis object.
"""
controller = mock.MagicMock()
npointx = NPointAxis(
axis_Id="A", name="npointx", host="localhost", port=1234, socket_cls=controller
@@ -40,6 +49,9 @@ def npointx():
],
)
def test_axis_put(npointx, pos, msg):
"""
Test that the set target position sends the correct message to the controller.
"""
npointx.controller._set_target_pos(npointx.axis_Id_numeric, pos)
npointx.controller.sock.put.assert_called_with(msg)
@@ -70,6 +82,9 @@ def test_npoint_axis_move(npointx):
],
)
def test_axis_get_out(npointx, pos, msg_in, msg_out):
"""
Test that the readback value is correctly read from the controller.
"""
npointx.controller.sock.receive.side_effect = [msg_out]
assert pytest.approx(npointx.readback.get(), rel=0.01) == pos
@@ -83,12 +98,19 @@ def test_axis_get_out(npointx, pos, msg_in, msg_out):
],
)
def test_axis_get_in(npointx, axis, msg_in, msg_out):
"""
Test that the readback value is correctly read from the controller by directly calling the
controller's method.
"""
npointx.controller.sock.receive.side_effect = [msg_out]
npointx.controller._get_current_pos(axis)
npointx.controller.sock.put.assert_called_once_with(msg_in)
def test_axis_out_of_range(controller):
"""
Test that an error is raised when trying to create an NPointAxis object with an invalid axis ID.
"""
with pytest.raises(ValueError):
npointx = NPointAxis(
axis_Id="G", name="npointx", host="localhost", port=1234, socket_cls=mock.MagicMock()
@@ -96,11 +118,17 @@ def test_axis_out_of_range(controller):
def test_get_axis_out_of_range(controller):
"""
Test that an error is raised when trying to get the current position of an invalid axis.
"""
with pytest.raises(ValueError):
controller._get_current_pos(3)
def test_set_axis_out_of_range(controller):
"""
Test that an error is raised when trying to set the target position of an invalid axis.
"""
with pytest.raises(ValueError):
controller._set_target_pos(3, 5)
@@ -114,6 +142,9 @@ def test_set_axis_out_of_range(controller):
],
)
def test_hex_list_to_int(in_buffer, byteorder, signed, val):
"""
Test that the hex list is correctly converted to an integer
"""
assert (
NPointController._hex_list_to_int(
copy.deepcopy(in_buffer), byteorder=byteorder, signed=signed
@@ -131,6 +162,10 @@ def test_hex_list_to_int(in_buffer, byteorder, signed, val):
],
)
def test_get_range(npointx, axis, msg_in, msg_out):
"""
Test that the range is correctly read from the controller by directly calling the
controller's method.
"""
npointx.controller.sock.receive.side_effect = [msg_out]
val = npointx.controller._get_range(axis)
npointx.controller.sock.put.assert_called_once_with(msg_in)