|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
| 14 | + |
| 15 | +from inspect import Parameter, signature |
| 16 | +from typing import Optional |
| 17 | + |
14 | 18 | import pytest
|
15 | 19 | import pytest_asyncio
|
16 | 20 | from pydantic import ValidationError
|
@@ -64,14 +68,15 @@ async def test_load_toolset_specific(
|
64 | 68 | async def test_load_toolset_default(self, toolbox: ToolboxClient):
|
65 | 69 | """Load the default toolset, i.e. all tools."""
|
66 | 70 | toolset = await toolbox.load_toolset()
|
67 |
| - assert len(toolset) == 5 |
| 71 | + assert len(toolset) == 6 |
68 | 72 | tool_names = {tool.__name__ for tool in toolset}
|
69 | 73 | expected_tools = [
|
70 | 74 | "get-row-by-content-auth",
|
71 | 75 | "get-row-by-email-auth",
|
72 | 76 | "get-row-by-id-auth",
|
73 | 77 | "get-row-by-id",
|
74 | 78 | "get-n-rows",
|
| 79 | + "search-rows", |
75 | 80 | ]
|
76 | 81 | assert tool_names == set(expected_tools)
|
77 | 82 |
|
@@ -217,3 +222,160 @@ async def test_run_tool_param_auth_no_field(
|
217 | 222 | match="no field named row_data in claims",
|
218 | 223 | ):
|
219 | 224 | await tool()
|
| 225 | + |
| 226 | + |
| 227 | +@pytest.mark.asyncio |
| 228 | +@pytest.mark.usefixtures("toolbox_server") |
| 229 | +class TestOptionalParams: |
| 230 | + """ |
| 231 | + End-to-end tests for tools with optional parameters. |
| 232 | + """ |
| 233 | + |
| 234 | + async def test_tool_signature_is_correct(self, toolbox: ToolboxClient): |
| 235 | + """Verify the client correctly constructs the signature for a tool with optional params.""" |
| 236 | + tool = await toolbox.load_tool("search-rows") |
| 237 | + sig = signature(tool) |
| 238 | + |
| 239 | + assert "email" in sig.parameters |
| 240 | + assert "data" in sig.parameters |
| 241 | + assert "id" in sig.parameters |
| 242 | + |
| 243 | + # The required parameter should have no default |
| 244 | + assert sig.parameters["email"].default is Parameter.empty |
| 245 | + assert sig.parameters["email"].annotation is str |
| 246 | + |
| 247 | + # The optional parameter should have a default of None |
| 248 | + assert sig.parameters["data"].default is None |
| 249 | + assert sig.parameters["data"].annotation is Optional[str] |
| 250 | + |
| 251 | + # The optional parameter should have a default of None |
| 252 | + assert sig.parameters["id"].default is None |
| 253 | + assert sig.parameters["id"].annotation is Optional[int] |
| 254 | + |
| 255 | + async def test_run_tool_with_optional_params_omitted(self, toolbox: ToolboxClient): |
| 256 | + """Invoke a tool providing only the required parameter.""" |
| 257 | + tool = await toolbox.load_tool("search-rows") |
| 258 | + |
| 259 | + response = await tool( email="[email protected]") |
| 260 | + assert isinstance(response, str) |
| 261 | + assert '"email":"[email protected]"' in response |
| 262 | + assert "row1" not in response |
| 263 | + assert "row2" in response |
| 264 | + assert "row3" not in response |
| 265 | + assert "row4" not in response |
| 266 | + assert "row5" not in response |
| 267 | + assert "row6" not in response |
| 268 | + |
| 269 | + async def test_run_tool_with_optional_data_provided(self, toolbox: ToolboxClient): |
| 270 | + """Invoke a tool providing both required and optional parameters.""" |
| 271 | + tool = await toolbox.load_tool("search-rows") |
| 272 | + |
| 273 | + response = await tool( email="[email protected]", data="row3") |
| 274 | + assert isinstance(response, str) |
| 275 | + assert '"email":"[email protected]"' in response |
| 276 | + assert "row1" not in response |
| 277 | + assert "row2" not in response |
| 278 | + assert "row3" in response |
| 279 | + assert "row4" not in response |
| 280 | + assert "row5" not in response |
| 281 | + assert "row6" not in response |
| 282 | + |
| 283 | + async def test_run_tool_with_optional_data_null(self, toolbox: ToolboxClient): |
| 284 | + """Invoke a tool providing both required and optional parameters.""" |
| 285 | + tool = await toolbox.load_tool("search-rows") |
| 286 | + |
| 287 | + response = await tool( email="[email protected]", data=None) |
| 288 | + assert isinstance(response, str) |
| 289 | + assert '"email":"[email protected]"' in response |
| 290 | + assert "row1" not in response |
| 291 | + assert "row2" in response |
| 292 | + assert "row3" not in response |
| 293 | + assert "row4" not in response |
| 294 | + assert "row5" not in response |
| 295 | + assert "row6" not in response |
| 296 | + |
| 297 | + async def test_run_tool_with_optional_id_provided(self, toolbox: ToolboxClient): |
| 298 | + """Invoke a tool providing both required and optional parameters.""" |
| 299 | + tool = await toolbox.load_tool("search-rows") |
| 300 | + |
| 301 | + response = await tool( email="[email protected]", id=1) |
| 302 | + assert isinstance(response, str) |
| 303 | + assert response == "null" |
| 304 | + |
| 305 | + async def test_run_tool_with_optional_id_null(self, toolbox: ToolboxClient): |
| 306 | + """Invoke a tool providing both required and optional parameters.""" |
| 307 | + tool = await toolbox.load_tool("search-rows") |
| 308 | + |
| 309 | + response = await tool( email="[email protected]", id=None) |
| 310 | + assert isinstance(response, str) |
| 311 | + assert '"email":"[email protected]"' in response |
| 312 | + assert "row1" not in response |
| 313 | + assert "row2" in response |
| 314 | + assert "row3" not in response |
| 315 | + assert "row4" not in response |
| 316 | + assert "row5" not in response |
| 317 | + assert "row6" not in response |
| 318 | + |
| 319 | + async def test_run_tool_with_missing_required_param(self, toolbox: ToolboxClient): |
| 320 | + """Invoke a tool without its required parameter.""" |
| 321 | + tool = await toolbox.load_tool("search-rows") |
| 322 | + with pytest.raises(TypeError, match="missing a required argument: 'email'"): |
| 323 | + await tool(id=5, data="row5") |
| 324 | + |
| 325 | + async def test_run_tool_with_required_param_null(self, toolbox: ToolboxClient): |
| 326 | + """Invoke a tool without its required parameter.""" |
| 327 | + tool = await toolbox.load_tool("search-rows") |
| 328 | + with pytest.raises(ValidationError, match="email"): |
| 329 | + await tool(email=None, id=5, data="row5") |
| 330 | + |
| 331 | + async def test_run_tool_with_all_default_params(self, toolbox: ToolboxClient): |
| 332 | + """Invoke a tool providing all parameters.""" |
| 333 | + tool = await toolbox.load_tool("search-rows") |
| 334 | + |
| 335 | + response = await tool( email="[email protected]", id=0, data="row2") |
| 336 | + assert isinstance(response, str) |
| 337 | + assert '"email":"[email protected]"' in response |
| 338 | + assert "row1" not in response |
| 339 | + assert "row2" in response |
| 340 | + assert "row3" not in response |
| 341 | + assert "row4" not in response |
| 342 | + assert "row5" not in response |
| 343 | + assert "row6" not in response |
| 344 | + |
| 345 | + async def test_run_tool_with_all_valid_params(self, toolbox: ToolboxClient): |
| 346 | + """Invoke a tool providing all parameters.""" |
| 347 | + tool = await toolbox.load_tool("search-rows") |
| 348 | + |
| 349 | + response = await tool( email="[email protected]", id=3, data="row3") |
| 350 | + assert isinstance(response, str) |
| 351 | + assert '"email":"[email protected]"' in response |
| 352 | + assert "row1" not in response |
| 353 | + assert "row2" not in response |
| 354 | + assert "row3" in response |
| 355 | + assert "row4" not in response |
| 356 | + assert "row5" not in response |
| 357 | + assert "row6" not in response |
| 358 | + |
| 359 | + async def test_run_tool_with_different_email(self, toolbox: ToolboxClient): |
| 360 | + """Invoke a tool providing all parameters but with a different email.""" |
| 361 | + tool = await toolbox.load_tool("search-rows") |
| 362 | + |
| 363 | + response = await tool( email="[email protected]", id=3, data="row3") |
| 364 | + assert isinstance(response, str) |
| 365 | + assert response == "null" |
| 366 | + |
| 367 | + async def test_run_tool_with_different_data(self, toolbox: ToolboxClient): |
| 368 | + """Invoke a tool providing all parameters but with a different data.""" |
| 369 | + tool = await toolbox.load_tool("search-rows") |
| 370 | + |
| 371 | + response = await tool( email="[email protected]", id=3, data="row4") |
| 372 | + assert isinstance(response, str) |
| 373 | + assert response == "null" |
| 374 | + |
| 375 | + async def test_run_tool_with_different_id(self, toolbox: ToolboxClient): |
| 376 | + """Invoke a tool providing all parameters but with a different data.""" |
| 377 | + tool = await toolbox.load_tool("search-rows") |
| 378 | + |
| 379 | + response = await tool( email="[email protected]", id=4, data="row3") |
| 380 | + assert isinstance(response, str) |
| 381 | + assert response == "null" |
0 commit comments