Skip to content

Commit 98f6dd4

Browse files
committed
fix: Fix unittest failing due to mock attaching wrong spec signature while asserting
1 parent 4aa3ca2 commit 98f6dd4

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

packages/toolbox-core/tests/test_sync_tool.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from inspect import Parameter, Signature
1818
from threading import Thread
1919
from typing import Any, Callable, Mapping, Union
20-
from unittest.mock import MagicMock, Mock, patch
20+
from unittest.mock import MagicMock, Mock, create_autospec, patch
2121

2222
import pytest
2323

@@ -27,8 +27,8 @@
2727

2828
@pytest.fixture
2929
def mock_async_tool() -> MagicMock:
30-
"""Fixture for a MagicMock simulating a ToolboxTool instance."""
31-
tool = MagicMock(spec=ToolboxTool)
30+
"""Fixture for an auto-specced MagicMock simulating a ToolboxTool instance."""
31+
tool = create_autospec(ToolboxTool, instance=True)
3232
tool.__name__ = "mock_async_tool_name"
3333
tool.__doc__ = "Mock async tool documentation."
3434

@@ -41,16 +41,10 @@ def mock_async_tool() -> MagicMock:
4141

4242
tool.__annotations__ = {"a": str, "b": int, "return": str}
4343

44-
# Mock the __call__ method to return a coroutine (MagicMock that can be awaited)
45-
# We'll make the internal call return a simple value for testing the sync wrapper
46-
async def mock_async_call(*args, **kwargs):
47-
return f"async_called_with_{args}_{kwargs}"
48-
49-
tool.__call__ = MagicMock(side_effect=lambda *a, **k: mock_async_call(*a, **k))
50-
51-
# Mock methods that return a new async_tool
52-
tool.add_auth_token_getters = MagicMock(return_value=MagicMock(spec=ToolboxTool))
53-
tool.bind_params = MagicMock(return_value=MagicMock(spec=ToolboxTool))
44+
tool.add_auth_token_getters.return_value = create_autospec(
45+
ToolboxTool, instance=True
46+
)
47+
tool.bind_params.return_value = create_autospec(ToolboxTool, instance=True)
5448

5549
return tool
5650

@@ -150,8 +144,8 @@ def test_toolbox_sync_tool_call(
150144
kwargs_dict = {"kwarg1": "value1"}
151145

152146
# Create a mock coroutine to be returned by async_tool.__call__
153-
mock_coro = MagicMock() # Represents the coroutine object
154-
mock_async_tool.return_value = mock_coro # async_tool() returns mock_coro
147+
mock_coro = MagicMock(name="mock_coro_returned_by_async_tool")
148+
mock_async_tool.return_value = mock_coro
155149

156150
result = toolbox_sync_tool(*args_tuple, **kwargs_dict)
157151

@@ -170,9 +164,8 @@ def test_toolbox_sync_tool_add_auth_token_getters(
170164
"""Tests the add_auth_token_getters method."""
171165
auth_getters: Mapping[str, Callable[[], str]] = {"service1": lambda: "token1"}
172166

173-
# The mock_async_tool.add_auth_token_getters is already set up to return a new MagicMock
174167
new_mock_async_tool = mock_async_tool.add_auth_token_getters.return_value
175-
new_mock_async_tool.__name__ = "new_async_tool_with_auth" # for __qualname__
168+
new_mock_async_tool.__name__ = "new_async_tool_with_auth"
176169

177170
new_sync_tool = toolbox_sync_tool.add_auth_token_getters(auth_getters)
178171

@@ -232,8 +225,6 @@ def test_toolbox_sync_tool_bind_param(
232225
new_mock_async_tool = mock_async_tool.bind_params.return_value
233226
new_mock_async_tool.__name__ = "new_async_tool_with_single_bound_param"
234227

235-
# Since bind_param calls self.bind_params, which in turn calls async_tool.bind_params,
236-
# we check that async_tool.bind_params is called correctly.
237228
new_sync_tool = toolbox_sync_tool.bind_param(param_name, param_value)
238229

239230
mock_async_tool.bind_params.assert_called_once_with({param_name: param_value})

0 commit comments

Comments
 (0)