|
| 1 | +"""Regression tests: @mockable must not collide with user args named `func`/`params`.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from uipath.eval.mocks import mockable |
| 9 | +from uipath.eval.mocks._mock_runtime import ( |
| 10 | + clear_execution_context, |
| 11 | + set_execution_context, |
| 12 | +) |
| 13 | +from uipath.eval.mocks._types import MockingContext |
| 14 | +from uipath.eval.models.evaluation_set import EvaluationItem |
| 15 | + |
| 16 | +_mock_span_collector = MagicMock() |
| 17 | + |
| 18 | + |
| 19 | +def _build_evaluation( |
| 20 | + function_name: str, kwargs: dict[str, Any], value: Any |
| 21 | +) -> EvaluationItem: |
| 22 | + evaluation_item: dict[str, Any] = { |
| 23 | + "id": "evaluation-id", |
| 24 | + "name": "Test evaluation", |
| 25 | + "inputs": {}, |
| 26 | + "evaluationCriterias": {"ExactMatchEvaluator": None}, |
| 27 | + "mockingStrategy": { |
| 28 | + "type": "mockito", |
| 29 | + "behaviors": [ |
| 30 | + { |
| 31 | + "function": function_name, |
| 32 | + "arguments": {"args": [], "kwargs": kwargs}, |
| 33 | + "then": [{"type": "return", "value": value}], |
| 34 | + } |
| 35 | + ], |
| 36 | + }, |
| 37 | + } |
| 38 | + return EvaluationItem(**evaluation_item) |
| 39 | + |
| 40 | + |
| 41 | +class TestMockableArgCollision: |
| 42 | + """Ensure `@mockable` works when the wrapped function has args named `func` or `params`.""" |
| 43 | + |
| 44 | + def test_sync_function_with_func_and_params_args(self): |
| 45 | + """A sync mockable function that takes `func` and `params` kwargs should not raise.""" |
| 46 | + |
| 47 | + @mockable() |
| 48 | + def test_function(func: str, params: dict[str, Any]) -> str: |
| 49 | + raise NotImplementedError() |
| 50 | + |
| 51 | + evaluation = _build_evaluation( |
| 52 | + "test_function", |
| 53 | + kwargs={"func": "some_func", "params": {"k": "v"}}, |
| 54 | + value="mocked_result", |
| 55 | + ) |
| 56 | + |
| 57 | + set_execution_context( |
| 58 | + MockingContext( |
| 59 | + strategy=evaluation.mocking_strategy, |
| 60 | + name=evaluation.name, |
| 61 | + inputs=evaluation.inputs, |
| 62 | + ), |
| 63 | + _mock_span_collector, |
| 64 | + "test-execution-id", |
| 65 | + ) |
| 66 | + |
| 67 | + try: |
| 68 | + with patch("uipath.eval.mocks.mockable.UiPathSpanUtils"): |
| 69 | + with patch("uipath.eval.mocks.mockable.trace"): |
| 70 | + result = test_function(func="some_func", params={"k": "v"}) |
| 71 | + |
| 72 | + assert result == "mocked_result" |
| 73 | + finally: |
| 74 | + clear_execution_context() |
| 75 | + |
| 76 | + @pytest.mark.asyncio |
| 77 | + async def test_async_function_with_func_and_params_args(self): |
| 78 | + """An async mockable function that takes `func` and `params` kwargs should not raise.""" |
| 79 | + |
| 80 | + @mockable() |
| 81 | + async def test_function(func: str, params: dict[str, Any]) -> str: |
| 82 | + raise NotImplementedError() |
| 83 | + |
| 84 | + evaluation = _build_evaluation( |
| 85 | + "test_function", |
| 86 | + kwargs={"func": "some_func", "params": {"k": "v"}}, |
| 87 | + value="mocked_result", |
| 88 | + ) |
| 89 | + |
| 90 | + set_execution_context( |
| 91 | + MockingContext( |
| 92 | + strategy=evaluation.mocking_strategy, |
| 93 | + name=evaluation.name, |
| 94 | + inputs=evaluation.inputs, |
| 95 | + ), |
| 96 | + _mock_span_collector, |
| 97 | + "test-execution-id", |
| 98 | + ) |
| 99 | + |
| 100 | + try: |
| 101 | + with patch("uipath.eval.mocks.mockable.UiPathSpanUtils"): |
| 102 | + with patch("uipath.eval.mocks.mockable.trace"): |
| 103 | + result = await test_function(func="some_func", params={"k": "v"}) |
| 104 | + |
| 105 | + assert result == "mocked_result" |
| 106 | + finally: |
| 107 | + clear_execution_context() |
0 commit comments