Add autoclosing fixture
This commit is contained in:
parent
7d558d058e
commit
fedac994c3
|
@ -424,7 +424,6 @@ class MockMotors(MockSerial):
|
|||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.open()
|
||||
|
||||
@property
|
||||
def stubs(self) -> dict[str, WaitableStub]:
|
||||
|
|
|
@ -283,7 +283,6 @@ class MockMotors(MockSerial):
|
|||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.open()
|
||||
|
||||
@property
|
||||
def stubs(self) -> dict[str, WaitableStub]:
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import sys
|
||||
from typing import Generator
|
||||
from unittest.mock import patch
|
||||
|
||||
import dynamixel_sdk as dxl
|
||||
|
@ -18,6 +19,14 @@ def patch_port_handler():
|
|||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_motors() -> Generator[MockMotors, None, None]:
|
||||
motors = MockMotors()
|
||||
motors.open()
|
||||
yield motors
|
||||
motors.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_motors() -> dict[str, Motor]:
|
||||
return {
|
||||
|
@ -91,8 +100,7 @@ def test_abc_implementation(dummy_motors):
|
|||
[3, 1120],
|
||||
],
|
||||
)
|
||||
def test_ping(idx, model_nb, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_ping(idx, model_nb, mock_motors, dummy_motors):
|
||||
mock_motors.build_ping_stub(idx, model_nb)
|
||||
motors_bus = DynamixelMotorsBus(
|
||||
port=mock_motors.port,
|
||||
|
@ -105,13 +113,12 @@ def test_ping(idx, model_nb, dummy_motors):
|
|||
assert ping_model_nb == model_nb
|
||||
|
||||
|
||||
def test_broadcast_ping(dummy_motors):
|
||||
def test_broadcast_ping(mock_motors, dummy_motors):
|
||||
expected_pings = {
|
||||
1: [1060, 50],
|
||||
2: [1120, 30],
|
||||
3: [1190, 10],
|
||||
}
|
||||
mock_motors = MockMotors()
|
||||
mock_motors.build_broadcast_ping_stub(expected_pings)
|
||||
motors_bus = DynamixelMotorsBus(
|
||||
port=mock_motors.port,
|
||||
|
@ -134,8 +141,7 @@ def test_broadcast_ping(dummy_motors):
|
|||
],
|
||||
ids=["None", "by ids", "by names", "mixed"],
|
||||
)
|
||||
def test_read_all_motors(motors, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_all_motors(motors, mock_motors, dummy_motors):
|
||||
expected_positions = {
|
||||
1: 1337,
|
||||
2: 42,
|
||||
|
@ -163,8 +169,7 @@ def test_read_all_motors(motors, dummy_motors):
|
|||
[3, 4016],
|
||||
],
|
||||
)
|
||||
def test_read_single_motor_by_name(idx, pos, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_single_motor_by_name(idx, pos, mock_motors, dummy_motors):
|
||||
expected_position = {idx: pos}
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_position)
|
||||
motors_bus = DynamixelMotorsBus(
|
||||
|
@ -187,8 +192,7 @@ def test_read_single_motor_by_name(idx, pos, dummy_motors):
|
|||
[3, 4016],
|
||||
],
|
||||
)
|
||||
def test_read_single_motor_by_id(idx, pos, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_single_motor_by_id(idx, pos, mock_motors, dummy_motors):
|
||||
expected_position = {idx: pos}
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_position)
|
||||
motors_bus = DynamixelMotorsBus(
|
||||
|
@ -212,8 +216,7 @@ def test_read_single_motor_by_id(idx, pos, dummy_motors):
|
|||
[2, 1, 999],
|
||||
],
|
||||
)
|
||||
def test_read_num_retry(num_retry, num_invalid_try, pos, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_num_retry(num_retry, num_invalid_try, pos, mock_motors, dummy_motors):
|
||||
expected_position = {1: pos}
|
||||
stub_name = mock_motors.build_sync_read_stub(
|
||||
"Present_Position", expected_position, num_invalid_try=num_invalid_try
|
||||
|
@ -244,8 +247,7 @@ def test_read_num_retry(num_retry, num_invalid_try, pos, dummy_motors):
|
|||
],
|
||||
ids=["by ids", "by names", "mixed"],
|
||||
)
|
||||
def test_write_all_motors(motors, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_write_all_motors(motors, mock_motors, dummy_motors):
|
||||
goal_positions = {
|
||||
1: 1337,
|
||||
2: 42,
|
||||
|
@ -271,8 +273,7 @@ def test_write_all_motors(motors, dummy_motors):
|
|||
["Torque_Enable", 1],
|
||||
],
|
||||
)
|
||||
def test_write_all_motors_single_value(data_name, value, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_write_all_motors_single_value(data_name, value, mock_motors, dummy_motors):
|
||||
values = {m.id: value for m in dummy_motors.values()}
|
||||
stub_name = mock_motors.build_sync_write_stub(data_name, values)
|
||||
motors_bus = DynamixelMotorsBus(
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import sys
|
||||
from typing import Generator
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
@ -18,6 +19,14 @@ def patch_port_handler():
|
|||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_motors() -> Generator[MockMotors, None, None]:
|
||||
motors = MockMotors()
|
||||
motors.open()
|
||||
yield motors
|
||||
motors.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dummy_motors() -> dict[str, Motor]:
|
||||
return {
|
||||
|
@ -92,8 +101,7 @@ def test_abc_implementation(dummy_motors):
|
|||
[3, 1120],
|
||||
],
|
||||
)
|
||||
def test_ping(idx, model_nb, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_ping(idx, model_nb, mock_motors, dummy_motors):
|
||||
mock_motors.build_ping_stub(idx, model_nb)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
|
@ -107,13 +115,12 @@ def test_ping(idx, model_nb, dummy_motors):
|
|||
|
||||
|
||||
@pytest.mark.skip("TODO")
|
||||
def test_broadcast_ping(dummy_motors):
|
||||
def test_broadcast_ping(mock_motors, dummy_motors):
|
||||
expected_pings = {
|
||||
1: [1060, 50],
|
||||
2: [1120, 30],
|
||||
3: [1190, 10],
|
||||
}
|
||||
mock_motors = MockMotors()
|
||||
mock_motors.build_broadcast_ping_stub(expected_pings)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
port=mock_motors.port,
|
||||
|
@ -136,8 +143,7 @@ def test_broadcast_ping(dummy_motors):
|
|||
],
|
||||
ids=["None", "by ids", "by names", "mixed"],
|
||||
)
|
||||
def test_read_all_motors(motors, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_all_motors(motors, mock_motors, dummy_motors):
|
||||
expected_positions = {
|
||||
1: 1337,
|
||||
2: 42,
|
||||
|
@ -165,8 +171,7 @@ def test_read_all_motors(motors, dummy_motors):
|
|||
[3, 4016],
|
||||
],
|
||||
)
|
||||
def test_read_single_motor_by_name(idx, pos, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_single_motor_by_name(idx, pos, mock_motors, dummy_motors):
|
||||
expected_position = {idx: pos}
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_position)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
|
@ -189,8 +194,7 @@ def test_read_single_motor_by_name(idx, pos, dummy_motors):
|
|||
[3, 4016],
|
||||
],
|
||||
)
|
||||
def test_read_single_motor_by_id(idx, pos, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_single_motor_by_id(idx, pos, mock_motors, dummy_motors):
|
||||
expected_position = {idx: pos}
|
||||
stub_name = mock_motors.build_sync_read_stub("Present_Position", expected_position)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
|
@ -214,8 +218,7 @@ def test_read_single_motor_by_id(idx, pos, dummy_motors):
|
|||
[2, 1, 999],
|
||||
],
|
||||
)
|
||||
def test_read_num_retry(num_retry, num_invalid_try, pos, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_read_num_retry(num_retry, num_invalid_try, pos, mock_motors, dummy_motors):
|
||||
expected_position = {1: pos}
|
||||
stub_name = mock_motors.build_sync_read_stub(
|
||||
"Present_Position", expected_position, num_invalid_try=num_invalid_try
|
||||
|
@ -246,8 +249,7 @@ def test_read_num_retry(num_retry, num_invalid_try, pos, dummy_motors):
|
|||
],
|
||||
ids=["by ids", "by names", "mixed"],
|
||||
)
|
||||
def test_write_all_motors(motors, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_write_all_motors(motors, mock_motors, dummy_motors):
|
||||
goal_positions = {
|
||||
1: 1337,
|
||||
2: 42,
|
||||
|
@ -273,8 +275,7 @@ def test_write_all_motors(motors, dummy_motors):
|
|||
["Torque_Enable", 1],
|
||||
],
|
||||
)
|
||||
def test_write_all_motors_single_value(data_name, value, dummy_motors):
|
||||
mock_motors = MockMotors()
|
||||
def test_write_all_motors_single_value(data_name, value, mock_motors, dummy_motors):
|
||||
values = {m.id: value for m in dummy_motors.values()}
|
||||
stub_name = mock_motors.build_sync_write_stub(data_name, values)
|
||||
motors_bus = FeetechMotorsBus(
|
||||
|
|
Loading…
Reference in New Issue