diff --git a/robot-server/robot_server/errors/error_mappers.py b/robot-server/robot_server/errors/error_mappers.py index dbf4fa34a2a2..70aa815bf707 100644 --- a/robot-server/robot_server/errors/error_mappers.py +++ b/robot-server/robot_server/errors/error_mappers.py @@ -2,7 +2,7 @@ from opentrons_shared_data.errors import EnumeratedError, PythonException -async def map_unexpected_error(error: BaseException) -> EnumeratedError: +def map_unexpected_error(error: BaseException) -> EnumeratedError: """Map an unhandled Exception to a known exception.""" if isinstance(error, EnumeratedError): return error diff --git a/robot-server/robot_server/protocols/protocol_analyzer.py b/robot-server/robot_server/protocols/protocol_analyzer.py index 4f9b48adb399..49457d864f9c 100644 --- a/robot-server/robot_server/protocols/protocol_analyzer.py +++ b/robot-server/robot_server/protocols/protocol_analyzer.py @@ -1,9 +1,9 @@ """Protocol analysis module.""" import logging -from datetime import datetime, timezone from opentrons import protocol_runner from opentrons.protocol_engine.errors import ErrorOccurrence +import opentrons.util.helpers as datetime_helper import robot_server.errors.error_mappers as em @@ -38,7 +38,7 @@ async def analyze( protocol_source=protocol_resource.source, deck_configuration=[] ) except BaseException as error: - internal_error = await em.map_unexpected_error(error=error) + internal_error = em.map_unexpected_error(error=error) await self._analysis_store.update( analysis_id=analysis_id, robot_type=protocol_resource.source.robot_type, @@ -51,7 +51,7 @@ async def analyze( # TODO(tz, 2-15-24): replace with a different error type # when we are able to support different errors. id="internal-error", - createdAt=datetime.now(tz=timezone.utc), + createdAt=datetime_helper.utc_now(), error=internal_error, ) ], diff --git a/robot-server/tests/protocols/test_protocol_analyzer.py b/robot-server/tests/protocols/test_protocol_analyzer.py index 451be5811314..5f53452b7a21 100644 --- a/robot-server/tests/protocols/test_protocol_analyzer.py +++ b/robot-server/tests/protocols/test_protocol_analyzer.py @@ -1,6 +1,6 @@ """Tests for the ProtocolAnalyzer.""" import pytest -from decoy import Decoy, matchers +from decoy import Decoy from datetime import datetime from pathlib import Path @@ -17,6 +17,7 @@ ) import opentrons.protocol_runner as protocol_runner from opentrons.protocol_reader import ProtocolSource, JsonProtocolConfig +import opentrons.util.helpers as datetime_helper from robot_server.protocols.analysis_store import AnalysisStore from robot_server.protocols.protocol_store import ProtocolResource @@ -35,6 +36,13 @@ def patch_mock_map_unexpected_error( monkeypatch.setattr(em, "map_unexpected_error", mock_map_unexpected_error) +@pytest.fixture(autouse=True) +def patch_mock_get_utc_datetime(decoy: Decoy, monkeypatch: pytest.MonkeyPatch) -> None: + """Replace utc_now with a mock.""" + mock_get_utc_datetime = decoy.mock(func=datetime_helper.utc_now) + monkeypatch.setattr(datetime_helper, "utc_now", mock_get_utc_datetime) + + @pytest.fixture(autouse=True) def patch_mock_create_simulating_runner( decoy: Decoy, monkeypatch: pytest.MonkeyPatch @@ -165,7 +173,7 @@ async def test_analyze_updates_pending_on_error( analysis_store: AnalysisStore, subject: ProtocolAnalyzer, ) -> None: - """It should be able to analyze a protocol.""" + """It should update pending analysis with an internal error.""" robot_type: RobotType = "OT-3 Standard" protocol_resource = ProtocolResource( @@ -187,7 +195,7 @@ async def test_analyze_updates_pending_on_error( error_occurrence = pe_errors.ErrorOccurrence.construct( id="internal-error", - createdAt=matchers.IsA(datetime), + createdAt=datetime(year=2023, month=3, day=3), errorType="EnumeratedError", detail="You got me!!", ) @@ -212,10 +220,14 @@ async def test_analyze_updates_pending_on_error( ) ).then_raise(raised_exception) - decoy.when(await em.map_unexpected_error(error=raised_exception)).then_return( + decoy.when(em.map_unexpected_error(error=raised_exception)).then_return( enumerated_error ) + decoy.when(datetime_helper.utc_now()).then_return( + datetime(year=2023, month=3, day=3) + ) + await subject.analyze( protocol_resource=protocol_resource, analysis_id="analysis-id",