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