17
17
from inspect import Parameter , Signature
18
18
from threading import Thread
19
19
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
21
21
22
22
import pytest
23
23
27
27
28
28
@pytest .fixture
29
29
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 )
32
32
tool .__name__ = "mock_async_tool_name"
33
33
tool .__doc__ = "Mock async tool documentation."
34
34
@@ -41,16 +41,10 @@ def mock_async_tool() -> MagicMock:
41
41
42
42
tool .__annotations__ = {"a" : str , "b" : int , "return" : str }
43
43
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 )
54
48
55
49
return tool
56
50
@@ -150,8 +144,8 @@ def test_toolbox_sync_tool_call(
150
144
kwargs_dict = {"kwarg1" : "value1" }
151
145
152
146
# 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
155
149
156
150
result = toolbox_sync_tool (* args_tuple , ** kwargs_dict )
157
151
@@ -170,9 +164,8 @@ def test_toolbox_sync_tool_add_auth_token_getters(
170
164
"""Tests the add_auth_token_getters method."""
171
165
auth_getters : Mapping [str , Callable [[], str ]] = {"service1" : lambda : "token1" }
172
166
173
- # The mock_async_tool.add_auth_token_getters is already set up to return a new MagicMock
174
167
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"
176
169
177
170
new_sync_tool = toolbox_sync_tool .add_auth_token_getters (auth_getters )
178
171
@@ -232,8 +225,6 @@ def test_toolbox_sync_tool_bind_param(
232
225
new_mock_async_tool = mock_async_tool .bind_params .return_value
233
226
new_mock_async_tool .__name__ = "new_async_tool_with_single_bound_param"
234
227
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.
237
228
new_sync_tool = toolbox_sync_tool .bind_param (param_name , param_value )
238
229
239
230
mock_async_tool .bind_params .assert_called_once_with ({param_name : param_value })
0 commit comments