Skip to content

Commit

Permalink
More test boilerplate.
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxColoring committed Mar 19, 2024
1 parent 6770571 commit e0e1259
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from opentrons.hardware_control import HardwareControlAPI, OT2HardwareControlAPI

from opentrons.protocol_engine import errors
from opentrons.protocol_engine.error_recovery_policy import (
ErrorRecoveryPolicy,
ErrorRecoveryType,
)
from opentrons.protocol_engine.errors.exceptions import (
EStopActivatedError as PE_EStopActivatedError,
)
Expand Down Expand Up @@ -132,6 +136,12 @@ def command_note_tracker_provider(decoy: Decoy) -> CommandNoteTrackerProvider:
return decoy.mock(cls=CommandNoteTrackerProvider)


@pytest.fixture
def error_recovery_policy(decoy: Decoy) -> ErrorRecoveryPolicy:
"""Get a mock error recovery policy."""
return decoy.mock(cls=ErrorRecoveryPolicy)


def get_next_tracker(
decoy: Decoy, provider: CommandNoteTrackerProvider
) -> CommandNoteTracker:
Expand Down Expand Up @@ -169,6 +179,7 @@ def subject(
status_bar: StatusBarHandler,
model_utils: ModelUtils,
command_note_tracker_provider: CommandNoteTrackerProvider,
error_recovery_policy: ErrorRecoveryPolicy,
) -> CommandExecutor:
"""Get a CommandExecutor test subject with its dependencies mocked out."""
return CommandExecutor(
Expand All @@ -186,6 +197,7 @@ def subject(
rail_lights=rail_lights,
status_bar=status_bar,
command_note_tracker_provider=command_note_tracker_provider,
error_recovery_policy=error_recovery_policy,
)


Expand Down Expand Up @@ -357,6 +369,7 @@ async def test_execute_raises_protocol_engine_error(
model_utils: ModelUtils,
subject: CommandExecutor,
command_note_tracker: CommandNoteTracker,
error_recovery_policy: ErrorRecoveryPolicy,
command_error: Exception,
expected_error: Any,
unexpected_error: bool,
Expand Down Expand Up @@ -430,6 +443,10 @@ def _ImplementationCls(self) -> Type[_TestCommandImpl]:
datetime(year=2023, month=3, day=3),
)

decoy.when(error_recovery_policy(matchers.Anything(), expected_error)).then_return(
ErrorRecoveryType.WAIT_FOR_RECOVERY
)

await subject.execute("command-id")

decoy.verify(
Expand All @@ -442,6 +459,7 @@ def _ImplementationCls(self) -> Type[_TestCommandImpl]:
error_id="error-id",
failed_at=datetime(year=2023, month=3, day=3),
error=expected_error,
type=ErrorRecoveryType.WAIT_FOR_RECOVERY,
)
),
)
Expand Down Expand Up @@ -588,6 +606,7 @@ async def test_executor_forwards_notes_on_command_failure(
model_utils: ModelUtils,
subject: CommandExecutor,
command_note_tracker: CommandNoteTracker,
error_recovery_policy: ErrorRecoveryPolicy,
) -> None:
"""It should handle an error occuring during execution."""
TestCommandImplCls = decoy.mock(func=_TestCommandImpl)
Expand Down Expand Up @@ -668,6 +687,9 @@ def _ImplementationCls(self) -> Type[_TestCommandImpl]:
datetime(year=2022, month=2, day=2),
datetime(year=2023, month=3, day=3),
)
decoy.when(
error_recovery_policy(matchers.Anything(), matchers.Anything())
).then_return(ErrorRecoveryType.WAIT_FOR_RECOVERY)
decoy.when(command_note_tracker.get_notes()).then_return(command_notes)

await subject.execute("command-id")
Expand All @@ -685,6 +707,7 @@ def _ImplementationCls(self) -> Type[_TestCommandImpl]:
error_id="error-id",
failed_at=datetime(year=2023, month=3, day=3),
error=matchers.ErrorMatching(PythonException, match="oh no"),
type=ErrorRecoveryType.WAIT_FOR_RECOVERY,
)
),
)
6 changes: 5 additions & 1 deletion api/tests/opentrons/protocol_engine/test_protocol_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from opentrons_shared_data.robot.dev_types import RobotType
from opentrons.ordered_set import OrderedSet
from opentrons.protocol_engine.actions.actions import ResumeFromRecoveryAction
from opentrons.protocol_engine.error_recovery_policy import ErrorRecoveryType

from opentrons.types import DeckSlotName
from opentrons.hardware_control import HardwareControlAPI, OT2HardwareControlAPI
Expand Down Expand Up @@ -695,7 +696,8 @@ async def test_wait_until_complete(
decoy.verify(
await state_store.wait_for(
condition=state_store.commands.get_all_commands_final
)
),
state_store.commands.raise_fatal_command_error(),
)


Expand Down Expand Up @@ -778,12 +780,14 @@ async def test_estop_during_command(
error_id=error_id,
failed_at=timestamp,
error=EStopActivatedError(message="Estop Activated"),
type=ErrorRecoveryType.FAIL_RUN,
)
expected_action_2 = FailCommandAction(
command_id=fake_command_set.head(),
error_id=error_id,
failed_at=timestamp,
error=EStopActivatedError(message="Estop Activated"),
type=ErrorRecoveryType.FAIL_RUN,
)

subject.estop(maintenance_run=maintenance_run)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
commands as pe_commands,
actions as pe_actions,
)
from opentrons.protocol_engine.error_recovery_policy import ErrorRecoveryType
from opentrons.protocol_engine.resources import (
ModuleDataProvider,
pipette_data_provider,
Expand Down Expand Up @@ -158,6 +159,7 @@ def test_map_after_with_error_command() -> None:
LegacyContextCommandError,
match="oh no",
),
type=ErrorRecoveryType.FAIL_RUN,
)
]

Expand Down Expand Up @@ -251,6 +253,7 @@ def test_command_stack() -> None:
error_id=matchers.IsA(str),
failed_at=matchers.IsA(datetime),
error=matchers.ErrorMatching(LegacyContextCommandError, "oh no"),
type=ErrorRecoveryType.FAIL_RUN,
),
]

Expand Down

0 comments on commit e0e1259

Please sign in to comment.