|
| 1 | +from datetime import datetime |
| 2 | +from typing import Any |
| 3 | +from unittest.mock import create_autospec |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import temporalio.converter |
| 8 | +from temporalio._workflow_context import ( |
| 9 | + _current_workflow_flags, |
| 10 | + _workflow_flag_scope, |
| 11 | + _WorkflowLogicFlag, |
| 12 | +) |
| 13 | +from temporalio.worker._workflow_instance import _WorkflowInstanceImpl |
| 14 | +from temporalio.worker.workflow_sandbox._importer import Importer |
| 15 | +from temporalio.worker.workflow_sandbox._restrictions import ( |
| 16 | + RestrictionContext, |
| 17 | + SandboxRestrictions, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class FlagRuntime: |
| 22 | + def __init__(self, enabled: bool) -> None: |
| 23 | + self.enabled = enabled |
| 24 | + self.checked: list[_WorkflowLogicFlag] = [] |
| 25 | + instance = create_autospec(_WorkflowInstanceImpl, instance=True) |
| 26 | + instance._workflow_logic_flag_enabled.side_effect = ( |
| 27 | + self._workflow_logic_flag_enabled |
| 28 | + ) |
| 29 | + self.instance: _WorkflowInstanceImpl = instance |
| 30 | + |
| 31 | + def _workflow_logic_flag_enabled(self, flag: _WorkflowLogicFlag) -> bool: |
| 32 | + self.checked.append(flag) |
| 33 | + return self.enabled |
| 34 | + |
| 35 | + |
| 36 | +def datetime_hint() -> type: |
| 37 | + with Importer(SandboxRestrictions.default, RestrictionContext()).applied(): |
| 38 | + from datetime import datetime as sandboxed_datetime |
| 39 | + |
| 40 | + return sandboxed_datetime |
| 41 | + |
| 42 | + |
| 43 | +def test_workflow_flag_scope_restores_context_after_exception() -> None: |
| 44 | + outer, inner = FlagRuntime(True), FlagRuntime(False) |
| 45 | + assert _current_workflow_flags.get() is None |
| 46 | + with _workflow_flag_scope(runtime=outer.instance): |
| 47 | + context = _current_workflow_flags.get() |
| 48 | + assert context is not None |
| 49 | + assert context.is_flag_enabled(_WorkflowLogicFlag.DECODE_DATETIME_PROXIES) |
| 50 | + with ( |
| 51 | + pytest.raises(ValueError), |
| 52 | + _workflow_flag_scope(runtime=inner.instance), |
| 53 | + ): |
| 54 | + nested = _current_workflow_flags.get() |
| 55 | + assert nested is not None |
| 56 | + assert not nested.is_flag_enabled( |
| 57 | + _WorkflowLogicFlag.DECODE_DATETIME_PROXIES |
| 58 | + ) |
| 59 | + raise ValueError("test cleanup") |
| 60 | + assert _current_workflow_flags.get() is context |
| 61 | + assert _current_workflow_flags.get() is None |
| 62 | + |
| 63 | + |
| 64 | +def test_datetime_proxy_conversion_policy() -> None: |
| 65 | + hint = datetime_hint() |
| 66 | + assert hint is not datetime |
| 67 | + value = "2021-01-02T00:00:00+00:00" |
| 68 | + with pytest.raises(TypeError): |
| 69 | + temporalio.converter.value_to_type(hint, value) |
| 70 | + |
| 71 | + disabled = FlagRuntime(False) |
| 72 | + with _workflow_flag_scope(runtime=disabled.instance): |
| 73 | + with pytest.raises(TypeError): |
| 74 | + temporalio.converter.value_to_type(hint, value) |
| 75 | + assert disabled.checked == [_WorkflowLogicFlag.DECODE_DATETIME_PROXIES] |
| 76 | + |
| 77 | + enabled = FlagRuntime(True) |
| 78 | + with _workflow_flag_scope(runtime=enabled.instance): |
| 79 | + assert temporalio.converter.value_to_type( |
| 80 | + hint, value |
| 81 | + ) == datetime.fromisoformat(value) |
| 82 | + assert enabled.checked == [_WorkflowLogicFlag.DECODE_DATETIME_PROXIES] |
| 83 | + |
| 84 | + |
| 85 | +def test_datetime_proxy_custom_converter_precedence() -> None: |
| 86 | + expected_hint = datetime_hint() |
| 87 | + |
| 88 | + class CustomConverter(temporalio.converter.JSONTypeConverter): |
| 89 | + def to_typed_value(self, hint: type, value: Any) -> Any: |
| 90 | + assert hint is expected_hint |
| 91 | + return "custom" |
| 92 | + |
| 93 | + runtime = FlagRuntime(True) |
| 94 | + with _workflow_flag_scope(runtime=runtime.instance): |
| 95 | + assert ( |
| 96 | + temporalio.converter.value_to_type( |
| 97 | + expected_hint, "input", [CustomConverter()] |
| 98 | + ) |
| 99 | + == "custom" |
| 100 | + ) |
| 101 | + assert not runtime.checked |
0 commit comments