12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import pytest
16
- from unittest .mock import AsyncMock , MagicMock
17
15
from inspect import Parameter , Signature
18
- from typing import Any , Optional , Callable
16
+ from typing import Any , Callable , Optional
17
+ from unittest .mock import AsyncMock , MagicMock
18
+
19
+ import pytest
19
20
20
21
from toolbox_core .tool import ToolboxTool
21
22
23
+
22
24
class TestToolboxTool :
23
25
@pytest .fixture
24
26
def mock_session (self ) -> MagicMock : # Added self
@@ -32,7 +34,12 @@ def tool_details(self) -> dict:
32
34
tool_name = "test_tool"
33
35
params = [
34
36
Parameter ("arg1" , Parameter .POSITIONAL_OR_KEYWORD , annotation = str ),
35
- Parameter ("opt_arg" , Parameter .POSITIONAL_OR_KEYWORD , default = 123 , annotation = Optional [int ]),
37
+ Parameter (
38
+ "opt_arg" ,
39
+ Parameter .POSITIONAL_OR_KEYWORD ,
40
+ default = 123 ,
41
+ annotation = Optional [int ],
42
+ ),
36
43
]
37
44
return {
38
45
"base_url" : base_url ,
@@ -63,10 +70,13 @@ def _configure(json_data: Any, status: int = 200):
63
70
mock_resp .__aenter__ .return_value = mock_resp
64
71
mock_resp .__aexit__ .return_value = None
65
72
mock_session .post .return_value = mock_resp
73
+
66
74
return _configure
67
75
68
76
@pytest .mark .asyncio
69
- async def test_initialization_and_introspection (self , tool : ToolboxTool , tool_details : dict ):
77
+ async def test_initialization_and_introspection (
78
+ self , tool : ToolboxTool , tool_details : dict
79
+ ):
70
80
"""Verify attributes are set correctly during initialization."""
71
81
assert tool .__name__ == tool_details ["name" ]
72
82
assert tool .__doc__ == tool_details ["desc" ]
@@ -82,7 +92,7 @@ async def test_call_success(
82
92
tool : ToolboxTool ,
83
93
mock_session : MagicMock ,
84
94
tool_details : dict ,
85
- configure_mock_response : Callable
95
+ configure_mock_response : Callable ,
86
96
):
87
97
expected_result = "Operation successful!"
88
98
configure_mock_response ({"result" : expected_result })
@@ -104,7 +114,7 @@ async def test_call_success_with_defaults(
104
114
tool : ToolboxTool ,
105
115
mock_session : MagicMock ,
106
116
tool_details : dict ,
107
- configure_mock_response : Callable
117
+ configure_mock_response : Callable ,
108
118
):
109
119
expected_result = "Default success!"
110
120
configure_mock_response ({"result" : expected_result })
@@ -126,7 +136,7 @@ async def test_call_api_error(
126
136
tool : ToolboxTool ,
127
137
mock_session : MagicMock ,
128
138
tool_details : dict ,
129
- configure_mock_response : Callable
139
+ configure_mock_response : Callable ,
130
140
):
131
141
error_message = "Tool execution failed on server"
132
142
configure_mock_response ({"error" : error_message })
@@ -148,7 +158,7 @@ async def test_call_missing_result_key(
148
158
tool : ToolboxTool ,
149
159
mock_session : MagicMock ,
150
160
tool_details : dict ,
151
- configure_mock_response : Callable
161
+ configure_mock_response : Callable ,
152
162
):
153
163
fallback_response = {"status" : "completed" , "details" : "some info" }
154
164
configure_mock_response (fallback_response )
@@ -165,9 +175,7 @@ async def test_call_missing_result_key(
165
175
166
176
@pytest .mark .asyncio
167
177
async def test_call_invalid_arguments_type_error (
168
- self ,
169
- tool : ToolboxTool ,
170
- mock_session : MagicMock
178
+ self , tool : ToolboxTool , mock_session : MagicMock
171
179
):
172
180
with pytest .raises (TypeError ):
173
181
await tool ("val1" , 2 , 3 )
@@ -178,4 +186,4 @@ async def test_call_invalid_arguments_type_error(
178
186
with pytest .raises (TypeError ):
179
187
await tool (opt_arg = 500 )
180
188
181
- mock_session .post .assert_not_called ()
189
+ mock_session .post .assert_not_called ()
0 commit comments