18
18
import pytest
19
19
from aiohttp import ClientSession
20
20
from toolbox_core .client import ToolboxClient as ToolboxCoreClient
21
- from toolbox_core .protocol import ManifestSchema
22
21
from toolbox_core .protocol import ParameterSchema as CoreParameterSchema
23
22
from toolbox_core .tool import ToolboxTool as ToolboxCoreTool
24
- from toolbox_core .utils import params_to_pydantic_model
25
23
26
24
from toolbox_langchain .async_client import AsyncToolboxClient
27
25
from toolbox_langchain .async_tools import AsyncToolboxTool
56
54
57
55
@pytest .mark .asyncio
58
56
class TestAsyncToolboxClient :
59
- @pytest .fixture ()
60
- def manifest_schema (self ):
61
- return ManifestSchema (** MANIFEST_JSON )
62
-
63
57
@pytest .fixture ()
64
58
def mock_session (self ):
65
59
return AsyncMock (spec = ClientSession )
66
60
67
61
@pytest .fixture
68
- def mock_core_client_instance (self , manifest_schema , mock_session ):
62
+ def mock_core_client_instance (self , mock_session ):
69
63
mock = AsyncMock (spec = ToolboxCoreClient )
70
64
71
65
async def mock_load_tool_impl (name , auth_token_getters , bound_params ):
@@ -80,7 +74,8 @@ async def mock_load_tool_impl(name, auth_token_getters, bound_params):
80
74
core_tool_mock = AsyncMock (spec = ToolboxCoreTool )
81
75
core_tool_mock .__name__ = name
82
76
core_tool_mock .__doc__ = tool_schema_dict ["description" ]
83
- core_tool_mock ._pydantic_model = params_to_pydantic_model (name , core_params )
77
+ core_tool_mock ._name = name
78
+ core_tool_mock ._params = core_params
84
79
# Add other necessary attributes or method mocks if AsyncToolboxTool uses them
85
80
return core_tool_mock
86
81
@@ -97,9 +92,8 @@ async def mock_load_toolset_impl(
97
92
core_tool_mock = AsyncMock (spec = ToolboxCoreTool )
98
93
core_tool_mock .__name__ = tool_name_iter
99
94
core_tool_mock .__doc__ = tool_schema_dict ["description" ]
100
- core_tool_mock ._pydantic_model = params_to_pydantic_model (
101
- tool_name_iter , core_params
102
- )
95
+ core_tool_mock ._name = tool_name_iter
96
+ core_tool_mock ._params = core_params
103
97
core_tools_list .append (core_tool_mock )
104
98
return core_tools_list
105
99
@@ -130,86 +124,122 @@ async def test_create_with_existing_session(self, mock_client, mock_session):
130
124
async def test_aload_tool (
131
125
self ,
132
126
mock_client ,
133
- manifest_schema , # mock_session removed as it's part of mock_core_client_instance
134
127
):
135
128
tool_name = "test_tool_1"
136
- # manifest_schema is used by mock_core_client_instance fixture to provide tool details
129
+ test_bound_params = { "bp1" : "value1" }
137
130
138
- tool = await mock_client .aload_tool (tool_name )
131
+ tool = await mock_client .aload_tool (tool_name , bound_params = test_bound_params )
139
132
140
133
# Assert that the core client's load_tool was called correctly
141
134
mock_client ._AsyncToolboxClient__core_client .load_tool .assert_called_once_with (
142
- name = tool_name , auth_token_getters = {}, bound_params = {}
135
+ name = tool_name , auth_token_getters = {}, bound_params = test_bound_params
143
136
)
144
137
assert isinstance (tool , AsyncToolboxTool )
145
138
assert (
146
139
tool .name == tool_name
147
140
) # AsyncToolboxTool gets its name from the core_tool
148
141
149
- async def test_aload_tool_auth_headers_deprecated (
150
- self , mock_client , manifest_schema
151
- ):
142
+ async def test_aload_tool_auth_headers_deprecated (self , mock_client ):
152
143
tool_name = "test_tool_1"
153
- auth_lambda = lambda : "Bearer token" # Define lambda once
144
+ auth_lambda = lambda : "Bearer token"
154
145
with catch_warnings (record = True ) as w :
155
146
simplefilter ("always" )
156
147
await mock_client .aload_tool (
157
148
tool_name ,
158
- auth_headers = {"Authorization" : auth_lambda }, # Use the defined lambda
149
+ auth_headers = {"Authorization" : auth_lambda },
159
150
)
160
151
assert len (w ) == 1
161
152
assert issubclass (w [- 1 ].category , DeprecationWarning )
162
153
assert "auth_headers" in str (w [- 1 ].message )
154
+ assert "Use `auth_token_getters` instead" in str (w [- 1 ].message )
163
155
164
156
mock_client ._AsyncToolboxClient__core_client .load_tool .assert_called_once_with (
165
157
name = tool_name ,
166
158
auth_token_getters = {"Authorization" : auth_lambda },
167
159
bound_params = {},
168
160
)
169
161
170
- async def test_aload_tool_auth_headers_and_tokens (
171
- self , mock_client , manifest_schema
172
- ):
162
+ async def test_aload_tool_auth_headers_and_getters_precedence (self , mock_client ):
173
163
tool_name = "test_tool_1"
174
- auth_getters = {"test " : lambda : "token " }
175
- auth_headers_lambda = lambda : "Bearer token" # Define lambda once
164
+ auth_getters = {"test_source " : lambda : "id_token_from_getters " }
165
+ auth_headers_lambda = lambda : "Bearer token_from_headers"
176
166
177
167
with catch_warnings (record = True ) as w :
178
168
simplefilter ("always" )
179
169
await mock_client .aload_tool (
180
170
tool_name ,
181
- auth_headers = {
182
- "Authorization" : auth_headers_lambda
183
- }, # Use defined lambda
171
+ auth_headers = {"Authorization" : auth_headers_lambda },
184
172
auth_token_getters = auth_getters ,
185
173
)
186
- assert (
187
- len (w ) == 1
188
- ) # Only one warning because auth_token_getters takes precedence
174
+ assert len (w ) == 1
189
175
assert issubclass (w [- 1 ].category , DeprecationWarning )
190
- assert "auth_headers" in str (w [- 1 ].message ) # Warning for auth_headers
176
+ assert "auth_headers" in str (w [- 1 ].message )
177
+ assert "`auth_token_getters` will be used" in str (w [- 1 ].message )
191
178
192
179
mock_client ._AsyncToolboxClient__core_client .load_tool .assert_called_once_with (
193
180
name = tool_name , auth_token_getters = auth_getters , bound_params = {}
194
181
)
195
182
196
- async def test_aload_toolset (
197
- self , mock_client , manifest_schema # mock_session removed
198
- ):
199
- tools = await mock_client .aload_toolset ()
183
+ async def test_aload_tool_auth_tokens_deprecated (self , mock_client ):
184
+ tool_name = "test_tool_1"
185
+ token_lambda = lambda : "id_token"
186
+ with catch_warnings (record = True ) as w :
187
+ simplefilter ("always" )
188
+ await mock_client .aload_tool (
189
+ tool_name ,
190
+ auth_tokens = {"some_token_key" : token_lambda },
191
+ )
192
+ assert len (w ) == 1
193
+ assert issubclass (w [- 1 ].category , DeprecationWarning )
194
+ assert "auth_tokens" in str (w [- 1 ].message )
195
+ assert "Use `auth_token_getters` instead" in str (w [- 1 ].message )
196
+
197
+ mock_client ._AsyncToolboxClient__core_client .load_tool .assert_called_once_with (
198
+ name = tool_name ,
199
+ auth_token_getters = {"some_token_key" : token_lambda },
200
+ bound_params = {},
201
+ )
202
+
203
+ async def test_aload_tool_auth_tokens_and_getters_precedence (self , mock_client ):
204
+ tool_name = "test_tool_1"
205
+ auth_getters = {"real_source" : lambda : "token_from_getters" }
206
+ token_lambda = lambda : "token_from_auth_tokens"
207
+
208
+ with catch_warnings (record = True ) as w :
209
+ simplefilter ("always" )
210
+ await mock_client .aload_tool (
211
+ tool_name ,
212
+ auth_tokens = {"deprecated_source" : token_lambda },
213
+ auth_token_getters = auth_getters ,
214
+ )
215
+ assert len (w ) == 1
216
+ assert issubclass (w [- 1 ].category , DeprecationWarning )
217
+ assert "auth_tokens" in str (w [- 1 ].message )
218
+ assert "`auth_token_getters` will be used" in str (w [- 1 ].message )
219
+
220
+ mock_client ._AsyncToolboxClient__core_client .load_tool .assert_called_once_with (
221
+ name = tool_name , auth_token_getters = auth_getters , bound_params = {}
222
+ )
223
+
224
+ async def test_aload_toolset (self , mock_client ):
225
+ test_bound_params = {"bp_set" : "value_set" }
226
+ tools = await mock_client .aload_toolset (
227
+ bound_params = test_bound_params , strict = True
228
+ )
200
229
201
230
mock_client ._AsyncToolboxClient__core_client .load_toolset .assert_called_once_with (
202
- name = None , auth_token_getters = {}, bound_params = {}, strict = False
231
+ name = None ,
232
+ auth_token_getters = {},
233
+ bound_params = test_bound_params ,
234
+ strict = True ,
203
235
)
204
- assert len (tools ) == 2 # Based on MANIFEST_JSON
236
+ assert len (tools ) == 2
205
237
for tool in tools :
206
238
assert isinstance (tool , AsyncToolboxTool )
207
239
assert tool .name in ["test_tool_1" , "test_tool_2" ]
208
240
209
- async def test_aload_toolset_with_toolset_name (
210
- self , mock_client , manifest_schema # mock_session removed
211
- ):
212
- toolset_name = "test_toolset_1" # This name isn't in MANIFEST_JSON, but load_toolset mock doesn't filter by it
241
+ async def test_aload_toolset_with_toolset_name (self , mock_client ):
242
+ toolset_name = "test_toolset_1"
213
243
tools = await mock_client .aload_toolset (toolset_name = toolset_name )
214
244
215
245
mock_client ._AsyncToolboxClient__core_client .load_toolset .assert_called_once_with (
@@ -218,43 +248,80 @@ async def test_aload_toolset_with_toolset_name(
218
248
assert len (tools ) == 2
219
249
for tool in tools :
220
250
assert isinstance (tool , AsyncToolboxTool )
221
- assert tool .name in ["test_tool_1" , "test_tool_2" ]
222
251
223
- async def test_aload_toolset_auth_headers_deprecated (
224
- self , mock_client , manifest_schema
225
- ):
226
- auth_lambda = lambda : "Bearer token" # Define lambda once
252
+ async def test_aload_toolset_auth_headers_deprecated (self , mock_client ):
253
+ auth_lambda = lambda : "Bearer token"
227
254
with catch_warnings (record = True ) as w :
228
255
simplefilter ("always" )
229
- await mock_client .aload_toolset (
230
- auth_headers = {"Authorization" : auth_lambda } # Use defined lambda
231
- )
256
+ await mock_client .aload_toolset (auth_headers = {"Authorization" : auth_lambda })
232
257
assert len (w ) == 1
233
258
assert issubclass (w [- 1 ].category , DeprecationWarning )
234
259
assert "auth_headers" in str (w [- 1 ].message )
260
+ assert "Use `auth_token_getters` instead" in str (w [- 1 ].message )
261
+
235
262
mock_client ._AsyncToolboxClient__core_client .load_toolset .assert_called_once_with (
236
263
name = None ,
237
264
auth_token_getters = {"Authorization" : auth_lambda },
238
265
bound_params = {},
239
266
strict = False ,
240
267
)
241
268
242
- async def test_aload_toolset_auth_headers_and_tokens (
243
- self , mock_client , manifest_schema
269
+ async def test_aload_toolset_auth_headers_and_getters_precedence ( # Renamed for clarity
270
+ self , mock_client
244
271
):
245
- auth_getters = {"test " : lambda : "token " }
246
- auth_headers_lambda = lambda : "Bearer token" # Define lambda once
272
+ auth_getters = {"test_source " : lambda : "id_token_from_getters " }
273
+ auth_headers_lambda = lambda : "Bearer token_from_headers"
247
274
with catch_warnings (record = True ) as w :
248
275
simplefilter ("always" )
249
276
await mock_client .aload_toolset (
250
- auth_headers = {
251
- "Authorization" : auth_headers_lambda
252
- }, # Use defined lambda
277
+ auth_headers = {"Authorization" : auth_headers_lambda },
253
278
auth_token_getters = auth_getters ,
254
279
)
255
280
assert len (w ) == 1
256
281
assert issubclass (w [- 1 ].category , DeprecationWarning )
257
282
assert "auth_headers" in str (w [- 1 ].message )
283
+ assert "`auth_token_getters` will be used" in str (w [- 1 ].message )
284
+
285
+ mock_client ._AsyncToolboxClient__core_client .load_toolset .assert_called_once_with (
286
+ name = None ,
287
+ auth_token_getters = auth_getters ,
288
+ bound_params = {},
289
+ strict = False , # auth_getters takes precedence
290
+ )
291
+
292
+ async def test_aload_toolset_auth_tokens_deprecated (self , mock_client ):
293
+ token_lambda = lambda : "id_token"
294
+ with catch_warnings (record = True ) as w :
295
+ simplefilter ("always" )
296
+ await mock_client .aload_toolset (
297
+ auth_tokens = {"some_token_key" : token_lambda }
298
+ )
299
+ assert len (w ) == 1
300
+ assert issubclass (w [- 1 ].category , DeprecationWarning )
301
+ assert "auth_tokens" in str (w [- 1 ].message )
302
+ assert "Use `auth_token_getters` instead" in str (w [- 1 ].message )
303
+
304
+ mock_client ._AsyncToolboxClient__core_client .load_toolset .assert_called_once_with (
305
+ name = None ,
306
+ auth_token_getters = {"some_token_key" : token_lambda },
307
+ bound_params = {},
308
+ strict = False ,
309
+ )
310
+
311
+ async def test_aload_toolset_auth_tokens_and_getters_precedence (self , mock_client ):
312
+ auth_getters = {"real_source" : lambda : "token_from_getters" }
313
+ token_lambda = lambda : "token_from_auth_tokens"
314
+ with catch_warnings (record = True ) as w :
315
+ simplefilter ("always" )
316
+ await mock_client .aload_toolset (
317
+ auth_tokens = {"deprecated_source" : token_lambda },
318
+ auth_token_getters = auth_getters ,
319
+ )
320
+ assert len (w ) == 1
321
+ assert issubclass (w [- 1 ].category , DeprecationWarning )
322
+ assert "auth_tokens" in str (w [- 1 ].message )
323
+ assert "`auth_token_getters` will be used" in str (w [- 1 ].message )
324
+
258
325
mock_client ._AsyncToolboxClient__core_client .load_toolset .assert_called_once_with (
259
326
name = None , auth_token_getters = auth_getters , bound_params = {}, strict = False
260
327
)
0 commit comments