|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from linkedapi.core.operation import Operation |
| 9 | +from linkedapi.errors import LINKED_API_ERROR_TYPES |
| 10 | +from linkedapi.types import ( |
| 11 | + WorkflowInProgressResponse, |
| 12 | + WorkflowResponse, |
| 13 | + WorkflowStartedResponse, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class _StubMapper: |
| 18 | + def map_request(self, params: Any) -> Any: |
| 19 | + return params |
| 20 | + |
| 21 | + def map_response(self, completion: Any) -> Any: |
| 22 | + return completion |
| 23 | + |
| 24 | + |
| 25 | +class _StubOperation(Operation): # type: ignore[type-arg] |
| 26 | + operation_name = "fetchPerson" |
| 27 | + |
| 28 | + def __init__(self, http_client: Any) -> None: |
| 29 | + super().__init__(http_client) |
| 30 | + self.mapper = _StubMapper() # type: ignore[assignment] |
| 31 | + |
| 32 | + |
| 33 | +def _operation(workflow_result: dict[str, Any]) -> _StubOperation: |
| 34 | + http_client = Mock() |
| 35 | + http_client.get.return_value = Mock( |
| 36 | + error=None, |
| 37 | + result=WorkflowResponse.model_validate(workflow_result), |
| 38 | + ) |
| 39 | + return _StubOperation(http_client) |
| 40 | + |
| 41 | + |
| 42 | +class TestPendingReasonModels: |
| 43 | + def test_in_progress_response_accepts_a_pending_reason(self) -> None: |
| 44 | + response = WorkflowInProgressResponse( |
| 45 | + workflowId="wf-1", |
| 46 | + workflowStatus="pending", |
| 47 | + pendingReason="outsideWorkingHours", |
| 48 | + ) |
| 49 | + |
| 50 | + assert response.pending_reason == "outsideWorkingHours" |
| 51 | + |
| 52 | + def test_started_response_accepts_a_pending_reason(self) -> None: |
| 53 | + response = WorkflowStartedResponse( |
| 54 | + workflowId="wf-1", |
| 55 | + workflowStatus="pending", |
| 56 | + pendingReason="queued", |
| 57 | + ) |
| 58 | + |
| 59 | + assert response.pending_reason == "queued" |
| 60 | + |
| 61 | + @pytest.mark.parametrize( |
| 62 | + "model", |
| 63 | + [WorkflowInProgressResponse, WorkflowStartedResponse, WorkflowResponse], |
| 64 | + ) |
| 65 | + def test_field_is_optional_for_back_compat(self, model: Any) -> None: |
| 66 | + response = model(workflowId="wf-1", workflowStatus="running") |
| 67 | + |
| 68 | + assert response.pending_reason is None |
| 69 | + |
| 70 | + |
| 71 | +class TestStatusCarriesPendingReason: |
| 72 | + def test_carries_the_outside_working_hours_reason(self) -> None: |
| 73 | + operation = _operation( |
| 74 | + { |
| 75 | + "workflowId": "wf-1", |
| 76 | + "workflowStatus": "pending", |
| 77 | + "pendingReason": "outsideWorkingHours", |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + result = operation.status("wf-1") |
| 82 | + |
| 83 | + assert isinstance(result, WorkflowInProgressResponse) |
| 84 | + assert result.pending_reason == "outsideWorkingHours" |
| 85 | + |
| 86 | + def test_carries_the_queued_reason(self) -> None: |
| 87 | + operation = _operation( |
| 88 | + {"workflowId": "wf-1", "workflowStatus": "pending", "pendingReason": "queued"} |
| 89 | + ) |
| 90 | + |
| 91 | + result = operation.status("wf-1") |
| 92 | + |
| 93 | + assert isinstance(result, WorkflowInProgressResponse) |
| 94 | + assert result.pending_reason == "queued" |
| 95 | + |
| 96 | + def test_keeps_none_when_the_api_sends_nothing(self) -> None: |
| 97 | + operation = _operation({"workflowId": "wf-1", "workflowStatus": "running"}) |
| 98 | + |
| 99 | + result = operation.status("wf-1") |
| 100 | + |
| 101 | + assert isinstance(result, WorkflowInProgressResponse) |
| 102 | + assert result.pending_reason is None |
| 103 | + |
| 104 | + |
| 105 | +class TestWorkingHoursErrorTypes: |
| 106 | + @pytest.mark.parametrize("error_type", ["outsideWorkingHours", "workingHoursWaitExpired"]) |
| 107 | + def test_error_type_is_known(self, error_type: str) -> None: |
| 108 | + assert error_type in LINKED_API_ERROR_TYPES |
0 commit comments