From 8da08935d457048782b84b0f1a254db57e95c84b Mon Sep 17 00:00:00 2001 From: Remi Cadene Date: Thu, 26 Sep 2024 16:45:04 +0200 Subject: [PATCH] move mock_motor in test_motors.py --- tests/test_motors.py | 39 +++++++++++++++++++++++++++++++- tests/utils.py | 54 ++------------------------------------------ 2 files changed, 40 insertions(+), 53 deletions(-) diff --git a/tests/test_motors.py b/tests/test_motors.py index 262a7b64..fcf33a3e 100644 --- a/tests/test_motors.py +++ b/tests/test_motors.py @@ -26,13 +26,14 @@ pytest -sx 'tests/test_motors.py::test_motors_bus[dynamixel-True]' # TODO(rcadene): add compatibility with other motors bus import time +import traceback import numpy as np import pytest from lerobot import available_motors from lerobot.common.robot_devices.utils import RobotDeviceAlreadyConnectedError, RobotDeviceNotConnectedError -from tests.utils import make_motors_bus, mock_builtins_input, mock_motor, require_motor +from tests.utils import make_motors_bus, mock_builtins_input, require_motor def _test_configure_motors_all_ids_1(motor_type): @@ -152,3 +153,39 @@ def test_motors_bus_mock(monkeypatch, motor_type): @require_motor def test_motors_bus(request, motor_type): _test_motors_bus(motor_type) + + +def mock_motor(monkeypatch, motor_type): + if motor_type not in available_motors: + raise ValueError( + f"The motor type '{motor_type}' is not valid. Expected one of these '{available_motors}" + ) + + if motor_type == "dynamixel": + try: + import dynamixel_sdk + + from tests.mock_dynamixel import ( + MockGroupSyncRead, + MockGroupSyncWrite, + MockPacketHandler, + MockPortHandler, + mock_convert_to_bytes, + ) + + monkeypatch.setattr(dynamixel_sdk, "GroupSyncRead", MockGroupSyncRead) + monkeypatch.setattr(dynamixel_sdk, "GroupSyncWrite", MockGroupSyncWrite) + monkeypatch.setattr(dynamixel_sdk, "PacketHandler", MockPacketHandler) + monkeypatch.setattr(dynamixel_sdk, "PortHandler", MockPortHandler) + + # Import dynamixel AFTER mocking dynamixel_sdk to use mocked classes + from lerobot.common.robot_devices.motors import dynamixel + + # TODO(rcadene): remove need to mock `convert_to_bytes` by implemented the inverse transform + # `convert_bytes_to_value` + monkeypatch.setattr(dynamixel, "convert_to_bytes", mock_convert_to_bytes) + except ImportError: + traceback.print_exc() + pytest.skip("To avoid skipping tests mocking dynamixel motors, run `pip install dynamixel-sdk`.") + else: + raise NotImplementedError("Implement mocking logic for new motor.") diff --git a/tests/utils.py b/tests/utils.py index a21891bf..4bd47413 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -314,22 +314,8 @@ def require_mock_camera(func): return wrapper -def require_mock_motor(func): - @wraps(func) - def wrapper(*args, **kwargs): - # Access the pytest request context to get the mockeypatch fixture - monkeypatch = kwargs.get("monkeypatch") - motor_type = kwargs.get("motor_type") - - if monkeypatch is None: - raise ValueError("The 'monkeypatch' fixture must be an argument of the test function.") - if motor_type is None: - raise ValueError("The 'motor_type' must be an argument of the test function.") - - mock_motor(monkeypatch, motor_type) - return func(*args, **kwargs) - - return wrapper +def mock_motor(**kwargs): + pass def mock_robot(monkeypatch, robot_type): @@ -394,42 +380,6 @@ def mock_camera(monkeypatch, camera_type): raise NotImplementedError("Implement mocking logic for new camera.") -def mock_motor(monkeypatch, motor_type): - if motor_type not in available_motors: - raise ValueError( - f"The motor type '{motor_type}' is not valid. Expected one of these '{available_motors}" - ) - - if motor_type == "dynamixel": - try: - import dynamixel_sdk - - from tests.mock_dynamixel import ( - MockGroupSyncRead, - MockGroupSyncWrite, - MockPacketHandler, - MockPortHandler, - mock_convert_to_bytes, - ) - - monkeypatch.setattr(dynamixel_sdk, "GroupSyncRead", MockGroupSyncRead) - monkeypatch.setattr(dynamixel_sdk, "GroupSyncWrite", MockGroupSyncWrite) - monkeypatch.setattr(dynamixel_sdk, "PacketHandler", MockPacketHandler) - monkeypatch.setattr(dynamixel_sdk, "PortHandler", MockPortHandler) - - # Import dynamixel AFTER mocking dynamixel_sdk to use mocked classes - from lerobot.common.robot_devices.motors import dynamixel - - # TODO(rcadene): remove need to mock `convert_to_bytes` by implemented the inverse transform - # `convert_bytes_to_value` - monkeypatch.setattr(dynamixel, "convert_to_bytes", mock_convert_to_bytes) - except ImportError: - traceback.print_exc() - pytest.skip("To avoid skipping tests mocking dynamixel motors, run `pip install dynamixel-sdk`.") - else: - raise NotImplementedError("Implement mocking logic for new motor.") - - def mock_builtins_input(monkeypatch): def print_text(text=None): if text is not None: