Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiate Mock members by function #523

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/isar/services/readers/test_base_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class TestBaseReader:
"dataclass_dict, expected_dataclass",
[
(asdict(MockMissionDefinition.default_mission), Mission),
(asdict(MockStep.drive_to), Step),
(asdict(MockStep.take_image_in_coordinate_direction), Step),
(asdict(MockPose.default_pose), Pose),
(asdict(MockStep.drive_to()), Step),
(asdict(MockStep.take_image_in_coordinate_direction()), Step),
(asdict(MockPose.default_pose()), Pose),
],
)
def test_dict_to_dataclass(self, dataclass_dict: dict, expected_dataclass: Any):
Expand Down
4 changes: 2 additions & 2 deletions tests/isar/state_machine/states/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
)
def test_step_finished(monitor: Monitor, mock_status, expected_output):
step: Step = MockStep.drive_to
step: Step = MockStep.drive_to()
step.status = mock_status
step_completed: bool = monitor._step_finished(
step=step,
Expand All @@ -36,7 +36,7 @@ def test_step_finished(monitor: Monitor, mock_status, expected_output):
def test_should_only_upload_if_status_is_completed(
monitor: Monitor, mock_status, should_queue_upload
):
step: TakeImage = MockStep.take_image_in_coordinate_direction
step: TakeImage = MockStep.take_image_in_coordinate_direction()
step.status = mock_status
task: Task = Task(steps=[step])
mission: Mission = Mission(tasks=[task])
Expand Down
20 changes: 10 additions & 10 deletions tests/isar/state_machine/test_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def test_reset_state_machine(state_machine) -> None:


def test_state_machine_transitions(injector, state_machine_thread) -> None:
step_1: Step = DriveToPose(pose=MockPose.default_pose)
step_2: Step = TakeImage(target=MockPose.default_pose.position)
step_1: Step = DriveToPose(pose=MockPose.default_pose())
step_2: Step = TakeImage(target=MockPose.default_pose().position)
mission: Mission = Mission(tasks=[Task(steps=[step_1, step_2])]) # type: ignore

scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)
Expand Down Expand Up @@ -121,8 +121,8 @@ def test_state_machine_transitions_when_running_full_mission(
) -> None:
state_machine_thread.state_machine.stepwise_mission = False

step_1: Step = DriveToPose(pose=MockPose.default_pose)
step_2: Step = TakeImage(target=MockPose.default_pose.position)
step_1: Step = DriveToPose(pose=MockPose.default_pose())
step_2: Step = TakeImage(target=MockPose.default_pose().position)
mission: Mission = Mission(tasks=[Task(steps=[step_1, step_2])]) # type: ignore

scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)
Expand All @@ -147,8 +147,8 @@ def test_state_machine_transitions_when_running_full_mission(
def test_state_machine_failed_dependency(
injector, state_machine_thread, mocker
) -> None:
drive_to_step: Step = DriveToPose(pose=MockPose.default_pose)
inspection_step: Step = MockStep.take_image_in_coordinate_direction
drive_to_step: Step = DriveToPose(pose=MockPose.default_pose())
inspection_step: Step = MockStep.take_image_in_coordinate_direction()
mission: Mission = Mission(tasks=[Task(steps=[drive_to_step, inspection_step])]) # type: ignore

mocker.patch.object(MockRobot, "step_status", return_value=StepStatus.Failed)
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_state_machine_with_successful_collection(
) -> None:
storage_mock: StorageInterface = injector.get(List[StorageInterface])[0]

step: TakeImage = MockStep.take_image_in_coordinate_direction
step: TakeImage = MockStep.take_image_in_coordinate_direction()
mission: Mission = Mission(tasks=[Task(steps=[step])])
scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)

Expand Down Expand Up @@ -207,7 +207,7 @@ def test_state_machine_with_unsuccessful_collection(

mocker.patch.object(MockRobot, "get_inspections", return_value=[])

step: TakeImage = MockStep.take_image_in_coordinate_direction
step: TakeImage = MockStep.take_image_in_coordinate_direction()
mission: Mission = Mission(tasks=[Task(steps=[step])])
scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)

Expand Down Expand Up @@ -236,7 +236,7 @@ def test_state_machine_with_successful_mission_stop(
state_machine_thread: StateMachineThread,
caplog: pytest.LogCaptureFixture,
) -> None:
step: TakeImage = MockStep.take_image_in_coordinate_direction
step: TakeImage = MockStep.take_image_in_coordinate_direction()
mission: Mission = Mission(tasks=[Task(steps=[step])])

scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_state_machine_with_unsuccessful_mission_stop(
state_machine_thread: StateMachineThread,
caplog: pytest.LogCaptureFixture,
) -> None:
step: TakeImage = MockStep.take_image_in_coordinate_direction
step: TakeImage = MockStep.take_image_in_coordinate_direction()
mission: Mission = Mission(tasks=[Task(steps=[step])])

scheduling_utilities: SchedulingUtilities = injector.get(SchedulingUtilities)
Expand Down
9 changes: 6 additions & 3 deletions tests/mocks/mission_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ class MockMissionDefinition:
id="default_mission",
tasks=[
Task(
steps=[MockStep.take_image_in_coordinate_direction, MockStep.drive_to]
steps=[
MockStep.take_image_in_coordinate_direction(),
MockStep.drive_to(),
]
),
Task(
steps=[
MockStep.take_image_in_coordinate_direction,
MockStep.take_image_in_coordinate_direction,
MockStep.take_image_in_coordinate_direction(),
MockStep.take_image_in_coordinate_direction(),
]
),
],
Expand Down
12 changes: 7 additions & 5 deletions tests/mocks/pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


class MockPose:
default_pose = Pose(
position=Position(x=0, y=0, z=0, frame=Frame("robot")),
orientation=Orientation(x=0, y=0, z=0, w=1, frame=Frame("robot")),
frame=Frame("robot"),
)
@staticmethod
def default_pose():
return Pose(
position=Position(x=0, y=0, z=0, frame=Frame("robot")),
orientation=Orientation(x=0, y=0, z=0, w=1, frame=Frame("robot")),
frame=Frame("robot"),
)
11 changes: 7 additions & 4 deletions tests/mocks/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@


class MockStep:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class MockStep:
class DummyStep:

Also consider using the naming "Stub" or "Fake", both may be representative of what we try to accomplish here. Relevant article: https://jesusvalerareales.com/testing-with-test-doubles/

Also, does not need to be solved here, you can make an issue for it and suggest a renaming for all similar "Mocks" that should be called "Dummy", "Stub", or "Fake"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, and great tip for a relevant resource. I will defer this to a separate issue, and have created one.

drive_to = DriveToPose(pose=MockPose.default_pose)
take_image_in_coordinate_direction = TakeImage(
target=Position(x=1, y=1, z=1, frame=Frame("robot"))
)
@staticmethod
def drive_to() -> DriveToPose:
sondreo marked this conversation as resolved.
Show resolved Hide resolved
return DriveToPose(pose=MockPose.default_pose())

@staticmethod
def take_image_in_coordinate_direction() -> TakeImage:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def take_image_in_coordinate_direction() -> TakeImage:
def get_dummy_take_image_step() -> TakeImage:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below regarding naming. It is added as a separate issue.

return TakeImage(target=Position(x=1, y=1, z=1, frame=Frame("robot")))
Loading