From 59db1c067bc60d2ff1904af6f0d7e130b9684c1a Mon Sep 17 00:00:00 2001 From: wakonig_k Date: Wed, 2 Oct 2024 18:11:51 +0200 Subject: [PATCH] test: fixed npoint tests after axis refactoring --- tests/tests_devices/test_npoint_piezo.py | 42 +++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/tests_devices/test_npoint_piezo.py b/tests/tests_devices/test_npoint_piezo.py index e03e3e2..b37c2fc 100644 --- a/tests/tests_devices/test_npoint_piezo.py +++ b/tests/tests_devices/test_npoint_piezo.py @@ -19,9 +19,16 @@ def controller(): @pytest.fixture -def npointx(controller): - npointx = NPointAxis(controller, 0, "nx") +def npointx(): + controller = mock.MagicMock() + npointx = NPointAxis( + axis_Id="A", name="npointx", host="localhost", port=1234, socket_cls=controller + ) + npointx.controller.on() + npointx.controller.sock.reset_mock() + npointx.controller.sock.receive.reset_mock() yield npointx + npointx.controller.off() @pytest.mark.parametrize( @@ -33,10 +40,27 @@ def npointx(controller): ], ) def test_axis_put(npointx, pos, msg): - npointx.set(pos) + npointx.controller._set_target_pos(npointx.axis_Id_numeric, pos) npointx.controller.sock.put.assert_called_with(msg) +def test_npoint_axis_move(npointx): + """ + Test that the move method sends the correct messages to the controller. + It should send the set target position, followed by 2 get current position messages. + """ + npointx.controller.sock.receive.side_effect = [ + b"\xa0\x34\x13\x83\x11\x00\x00\x00\x00U", # pos 0 + b"\xa0\x34\x13\x83\x11\xcd\xcc\x00\x00U", # pos 5 + ] + npointx.move(5) + assert ( + mock.call(b"\xa2\x18\x12\x83\x11\xcd\xcc\x00\x00U") + in npointx.controller.sock.put.mock_calls + ) + assert len(npointx.controller.sock.put.mock_calls) == 3 + + @pytest.mark.parametrize( "pos, msg_in, msg_out", [ @@ -46,8 +70,8 @@ def test_axis_put(npointx, pos, msg): ], ) def test_axis_get_out(npointx, pos, msg_in, msg_out): - npointx.controller.sock.receive.return_value = msg_out - assert pytest.approx(npointx.get(), rel=0.01) == pos + npointx.controller.sock.receive.side_effect = [msg_out] + assert pytest.approx(npointx.readback.get(), rel=0.01) == pos @pytest.mark.parametrize( @@ -59,14 +83,16 @@ def test_axis_get_out(npointx, pos, msg_in, msg_out): ], ) def test_axis_get_in(npointx, axis, msg_in, msg_out): - npointx.controller.sock.receive.return_value = msg_out + 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): with pytest.raises(ValueError): - npointx = NPointAxis(controller, 3, "nx") + npointx = NPointAxis( + axis_Id="G", name="npointx", host="localhost", port=1234, socket_cls=mock.MagicMock() + ) def test_get_axis_out_of_range(controller): @@ -105,7 +131,7 @@ def test_hex_list_to_int(in_buffer, byteorder, signed, val): ], ) def test_get_range(npointx, axis, msg_in, msg_out): - npointx.controller.sock.receive.return_value = msg_out + npointx.controller.sock.receive.side_effect = [msg_out] val = npointx.controller._get_range(axis) npointx.controller.sock.put.assert_called_once_with(msg_in) assert val == 100