-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): Fully stringify well names (#13764)
Co-authored-by: Jeremy Leon <[email protected]>
- Loading branch information
1 parent
e8ba506
commit 3f52995
Showing
8 changed files
with
196 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from opentrons.protocol_engine.clients.sync_client import SyncClient | ||
from opentrons.protocol_engine.types import ( | ||
DeckSlotLocation, | ||
LabwareLocation, | ||
ModuleLocation, | ||
OnLabwareLocation, | ||
) | ||
|
||
|
||
def well(engine_client: SyncClient, well_name: str, labware_id: str) -> str: | ||
"""Return a human-readable string representing a well and its location. | ||
For example: "A1 of My Cool Labware on C2". | ||
""" | ||
labware_location = OnLabwareLocation(labwareId=labware_id) | ||
return f"{well_name} of {_labware_location_string(engine_client, labware_location)}" | ||
|
||
|
||
def _labware_location_string( | ||
engine_client: SyncClient, location: LabwareLocation | ||
) -> str: | ||
if isinstance(location, DeckSlotLocation): | ||
# TODO(mm, 2023-10-11): | ||
# Ideally, we might want to use the display name specified by the deck definition? | ||
return f"slot {location.slotName.id}" | ||
|
||
elif isinstance(location, ModuleLocation): | ||
module_name = engine_client.state.modules.get_definition( | ||
module_id=location.moduleId | ||
).displayName | ||
module_on = engine_client.state.modules.get_location( | ||
module_id=location.moduleId | ||
) | ||
module_on_string = _labware_location_string(engine_client, module_on) | ||
return f"{module_name} on {module_on_string}" | ||
|
||
elif isinstance(location, OnLabwareLocation): | ||
labware_name = _labware_name(engine_client, location.labwareId) | ||
labware_on = engine_client.state.labware.get_location( | ||
labware_id=location.labwareId | ||
) | ||
labware_on_string = _labware_location_string(engine_client, labware_on) | ||
return f"{labware_name} on {labware_on_string}" | ||
|
||
elif location == "offDeck": | ||
return "[off-deck]" | ||
|
||
|
||
def _labware_name(engine_client: SyncClient, labware_id: str) -> str: | ||
"""Return the user-specified labware label, or fall back to the display name from the def.""" | ||
user_name = engine_client.state.labware.get_display_name(labware_id=labware_id) | ||
definition_name = engine_client.state.labware.get_definition( | ||
labware_id=labware_id | ||
).metadata.displayName | ||
|
||
return user_name if user_name is not None else definition_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
api/tests/opentrons/protocol_api/core/engine/test_stringify.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""Unit tests for `stringify`.""" | ||
|
||
|
||
from decoy import Decoy | ||
from opentrons_shared_data.labware.labware_definition import LabwareDefinition | ||
|
||
from opentrons.protocol_api.core.engine import stringify as subject | ||
from opentrons.protocol_engine.clients.sync_client import SyncClient | ||
from opentrons.protocol_engine.types import ( | ||
OFF_DECK_LOCATION, | ||
DeckSlotLocation, | ||
ModuleDefinition, | ||
ModuleLocation, | ||
OnLabwareLocation, | ||
) | ||
from opentrons.types import DeckSlotName | ||
|
||
|
||
def _make_dummy_labware_definition( | ||
decoy: Decoy, display_name: str | ||
) -> LabwareDefinition: | ||
mock = decoy.mock(cls=LabwareDefinition) | ||
decoy.when(mock.metadata.displayName).then_return(display_name) | ||
return mock | ||
|
||
|
||
def _make_dummy_module_definition(decoy: Decoy, display_name: str) -> ModuleDefinition: | ||
mock = decoy.mock(cls=ModuleDefinition) | ||
decoy.when(mock.displayName).then_return(display_name) | ||
return mock | ||
|
||
|
||
def test_well_on_labware_without_user_display_name(decoy: Decoy) -> None: | ||
"""Test stringifying a well on a labware that doesn't have a user-defined label.""" | ||
mock_client = decoy.mock(cls=SyncClient) | ||
decoy.when(mock_client.state.labware.get_display_name("labware-id")).then_return( | ||
None | ||
) | ||
decoy.when(mock_client.state.labware.get_definition("labware-id")).then_return( | ||
_make_dummy_labware_definition(decoy, "definition-display-name") | ||
) | ||
decoy.when(mock_client.state.labware.get_location("labware-id")).then_return( | ||
OFF_DECK_LOCATION | ||
) | ||
|
||
result = subject.well( | ||
engine_client=mock_client, well_name="well-name", labware_id="labware-id" | ||
) | ||
assert result == "well-name of definition-display-name on [off-deck]" | ||
|
||
|
||
def test_well_on_labware_with_user_display_name(decoy: Decoy) -> None: | ||
"""Test stringifying a well on a labware that does have a user-defined label.""" | ||
mock_client = decoy.mock(cls=SyncClient) | ||
decoy.when(mock_client.state.labware.get_display_name("labware-id")).then_return( | ||
"user-display-name" | ||
) | ||
decoy.when(mock_client.state.labware.get_definition("labware-id")).then_return( | ||
_make_dummy_labware_definition(decoy, "definition-display-name") | ||
) | ||
decoy.when(mock_client.state.labware.get_location("labware-id")).then_return( | ||
OFF_DECK_LOCATION | ||
) | ||
|
||
result = subject.well( | ||
engine_client=mock_client, well_name="well-name", labware_id="labware-id" | ||
) | ||
assert result == "well-name of user-display-name on [off-deck]" | ||
|
||
|
||
def test_well_on_labware_with_complicated_location(decoy: Decoy) -> None: | ||
"""Test stringifying a well on a labware with a deeply-nested location.""" | ||
mock_client = decoy.mock(cls=SyncClient) | ||
|
||
decoy.when(mock_client.state.labware.get_display_name("labware-id-1")).then_return( | ||
None | ||
) | ||
decoy.when(mock_client.state.labware.get_definition("labware-id-1")).then_return( | ||
_make_dummy_labware_definition(decoy, "lw-1-display-name") | ||
) | ||
decoy.when(mock_client.state.labware.get_location("labware-id-1")).then_return( | ||
OnLabwareLocation(labwareId="labware-id-2") | ||
) | ||
|
||
decoy.when(mock_client.state.labware.get_display_name("labware-id-2")).then_return( | ||
None | ||
) | ||
decoy.when(mock_client.state.labware.get_definition("labware-id-2")).then_return( | ||
_make_dummy_labware_definition(decoy, "lw-2-display-name") | ||
) | ||
decoy.when(mock_client.state.labware.get_location("labware-id-2")).then_return( | ||
ModuleLocation(moduleId="module-id") | ||
) | ||
|
||
decoy.when(mock_client.state.modules.get_definition("module-id")).then_return( | ||
_make_dummy_module_definition(decoy, "module-display-name") | ||
) | ||
decoy.when(mock_client.state.modules.get_location("module-id")).then_return( | ||
DeckSlotLocation(slotName=DeckSlotName.SLOT_C2) | ||
) | ||
|
||
result = subject.well( | ||
engine_client=mock_client, well_name="well-name", labware_id="labware-id-1" | ||
) | ||
assert ( | ||
result | ||
== "well-name of lw-1-display-name on lw-2-display-name on module-display-name on slot C2" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters