|
14 | 14 |
|
15 | 15 |
|
16 | 16 | import inspect
|
| 17 | +from types import MappingProxyType |
17 | 18 | from typing import AsyncGenerator, Callable, Mapping
|
18 | 19 | from unittest.mock import AsyncMock, Mock
|
19 | 20 | from warnings import catch_warnings, simplefilter
|
@@ -102,6 +103,27 @@ def unused_auth_getters() -> dict[str, Callable[[], str]]:
|
102 | 103 | return {"unused-auth-service": lambda: "unused-token-value"}
|
103 | 104 |
|
104 | 105 |
|
| 106 | +@pytest.fixture |
| 107 | +def toolbox_tool( |
| 108 | + http_session: ClientSession, |
| 109 | + sample_tool_params: list[ParameterSchema], |
| 110 | + sample_tool_description: str, |
| 111 | +) -> ToolboxTool: |
| 112 | + """Fixture for a ToolboxTool instance with common test setup.""" |
| 113 | + return ToolboxTool( |
| 114 | + session=http_session, |
| 115 | + base_url=TEST_BASE_URL, |
| 116 | + name=TEST_TOOL_NAME, |
| 117 | + description=sample_tool_description, |
| 118 | + params=sample_tool_params, |
| 119 | + required_authn_params={"message": ["service_a"]}, |
| 120 | + required_authz_tokens=["service_b"], |
| 121 | + auth_service_token_getters={"service_x": lambda: "token_x"}, |
| 122 | + bound_params={"fixed_param": "fixed_value"}, |
| 123 | + client_headers={"X-Test-Client": "client_header_value"}, |
| 124 | + ) |
| 125 | + |
| 126 | + |
105 | 127 | def test_create_func_docstring_one_param_real_schema():
|
106 | 128 | """
|
107 | 129 | Tests create_func_docstring with one real ParameterSchema instance.
|
@@ -480,6 +502,82 @@ def test_add_auth_token_getters_unused_token(
|
480 | 502 | tool_instance.add_auth_token_getters(unused_auth_getters)
|
481 | 503 |
|
482 | 504 |
|
| 505 | +def test_toolbox_tool_underscore_name_property(toolbox_tool: ToolboxTool): |
| 506 | + """Tests the _name property.""" |
| 507 | + assert toolbox_tool._name == TEST_TOOL_NAME |
| 508 | + |
| 509 | + |
| 510 | +def test_toolbox_tool_underscore_description_property(toolbox_tool: ToolboxTool): |
| 511 | + """Tests the _description property.""" |
| 512 | + assert ( |
| 513 | + toolbox_tool._description |
| 514 | + == "A sample tool that processes a message and a count." |
| 515 | + ) |
| 516 | + |
| 517 | + |
| 518 | +def test_toolbox_tool_underscore_params_property( |
| 519 | + toolbox_tool: ToolboxTool, sample_tool_params: list[ParameterSchema] |
| 520 | +): |
| 521 | + """Tests the _params property returns a deep copy.""" |
| 522 | + params_copy = toolbox_tool._params |
| 523 | + assert params_copy == sample_tool_params |
| 524 | + assert ( |
| 525 | + params_copy is not toolbox_tool._ToolboxTool__params |
| 526 | + ) # Ensure it's a deepcopy |
| 527 | + # Verify modifying the copy does not affect the original |
| 528 | + params_copy.append( |
| 529 | + ParameterSchema(name="new_param", type="integer", description="A new parameter") |
| 530 | + ) |
| 531 | + assert ( |
| 532 | + len(toolbox_tool._ToolboxTool__params) == 2 |
| 533 | + ) # Original should remain unchanged |
| 534 | + |
| 535 | + |
| 536 | +def test_toolbox_tool_underscore_bound_params_property(toolbox_tool: ToolboxTool): |
| 537 | + """Tests the _bound_params property returns an immutable MappingProxyType.""" |
| 538 | + bound_params = toolbox_tool._bound_params |
| 539 | + assert bound_params == {"fixed_param": "fixed_value"} |
| 540 | + assert isinstance(bound_params, MappingProxyType) |
| 541 | + # Verify immutability |
| 542 | + with pytest.raises(TypeError): |
| 543 | + bound_params["new_param"] = "new_value" |
| 544 | + |
| 545 | + |
| 546 | +def test_toolbox_tool_underscore_required_auth_params_property( |
| 547 | + toolbox_tool: ToolboxTool, |
| 548 | +): |
| 549 | + """Tests the _required_auth_params property returns an immutable MappingProxyType.""" |
| 550 | + required_auth_params = toolbox_tool._required_auth_params |
| 551 | + assert required_auth_params == {"message": ["service_a"]} |
| 552 | + assert isinstance(required_auth_params, MappingProxyType) |
| 553 | + # Verify immutability |
| 554 | + with pytest.raises(TypeError): |
| 555 | + required_auth_params["new_param"] = ["new_service"] |
| 556 | + |
| 557 | + |
| 558 | +def test_toolbox_tool_underscore_auth_service_token_getters_property( |
| 559 | + toolbox_tool: ToolboxTool, |
| 560 | +): |
| 561 | + """Tests the _auth_service_token_getters property returns an immutable MappingProxyType.""" |
| 562 | + auth_getters = toolbox_tool._auth_service_token_getters |
| 563 | + assert "service_x" in auth_getters |
| 564 | + assert auth_getters["service_x"]() == "token_x" |
| 565 | + assert isinstance(auth_getters, MappingProxyType) |
| 566 | + # Verify immutability |
| 567 | + with pytest.raises(TypeError): |
| 568 | + auth_getters["new_service"] = lambda: "new_token" |
| 569 | + |
| 570 | + |
| 571 | +def test_toolbox_tool_underscore_client_headers_property(toolbox_tool: ToolboxTool): |
| 572 | + """Tests the _client_headers property returns an immutable MappingProxyType.""" |
| 573 | + client_headers = toolbox_tool._client_headers |
| 574 | + assert client_headers == {"X-Test-Client": "client_header_value"} |
| 575 | + assert isinstance(client_headers, MappingProxyType) |
| 576 | + # Verify immutability |
| 577 | + with pytest.raises(TypeError): |
| 578 | + client_headers["new_header"] = "new_value" |
| 579 | + |
| 580 | + |
483 | 581 | # --- Test for the HTTP Warning ---
|
484 | 582 | @pytest.mark.parametrize(
|
485 | 583 | "trigger_condition_params",
|
|
0 commit comments