Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarZanzouri committed Feb 16, 2024
1 parent 7fde442 commit ce66749
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion robot-server/robot_server/errors/error_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions robot-server/robot_server/protocols/protocol_analyzer.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
],
Expand Down
20 changes: 16 additions & 4 deletions robot-server/tests/protocols/test_protocol_analyzer.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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!!",
)
Expand All @@ -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",
Expand Down

0 comments on commit ce66749

Please sign in to comment.