Skip to content

Commit d7d171d

Browse files
committed
chore: Add unit tests for @Property methods
1 parent bf88ca7 commit d7d171d

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

packages/toolbox-core/tests/test_sync_tool.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,60 @@ def test_toolbox_sync_tool_annotations_property(
127127
assert toolbox_sync_tool.__annotations__ is mock_async_tool.__annotations__
128128

129129

130+
def test_toolbox_sync_tool_underscore_name_property(
131+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
132+
):
133+
"""Tests the _name property."""
134+
assert toolbox_sync_tool._name == mock_async_tool._name
135+
136+
137+
def test_toolbox_sync_tool_underscore_description_property(
138+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
139+
):
140+
"""Tests the _description property."""
141+
assert toolbox_sync_tool._description == mock_async_tool._description
142+
143+
144+
def test_toolbox_sync_tool_underscore_params_property(
145+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
146+
):
147+
"""Tests the _params property."""
148+
assert toolbox_sync_tool._params == mock_async_tool._params
149+
150+
151+
def test_toolbox_sync_tool_underscore_bound_params_property(
152+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
153+
):
154+
"""Tests the _bound_params property."""
155+
assert toolbox_sync_tool._bound_params == mock_async_tool._bound_params
156+
157+
158+
def test_toolbox_sync_tool_underscore_required_auth_params_property(
159+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
160+
):
161+
"""Tests the _required_auth_params property."""
162+
assert (
163+
toolbox_sync_tool._required_auth_params == mock_async_tool._required_auth_params
164+
)
165+
166+
167+
def test_toolbox_sync_tool_underscore_auth_service_token_getters_property(
168+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
169+
):
170+
"""Tests the _auth_service_token_getters property."""
171+
assert (
172+
toolbox_sync_tool._auth_service_token_getters
173+
is mock_async_tool._auth_service_token_getters
174+
)
175+
176+
177+
def test_toolbox_sync_tool_underscore_client_headers_property(
178+
toolbox_sync_tool: ToolboxSyncTool, mock_async_tool: MagicMock
179+
):
180+
"""Tests the _client_headers property."""
181+
assert toolbox_sync_tool._client_headers is mock_async_tool._client_headers
182+
183+
130184
@patch("asyncio.run_coroutine_threadsafe")
131185
def test_toolbox_sync_tool_call(
132186
mock_run_coroutine_threadsafe: MagicMock,

packages/toolbox-core/tests/test_tool.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515

1616
import inspect
17+
from types import MappingProxyType
1718
from typing import AsyncGenerator, Callable, Mapping
1819
from unittest.mock import AsyncMock, Mock
1920
from warnings import catch_warnings, simplefilter
@@ -102,6 +103,27 @@ def unused_auth_getters() -> dict[str, Callable[[], str]]:
102103
return {"unused-auth-service": lambda: "unused-token-value"}
103104

104105

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+
105127
def test_create_func_docstring_one_param_real_schema():
106128
"""
107129
Tests create_func_docstring with one real ParameterSchema instance.
@@ -480,6 +502,82 @@ def test_add_auth_token_getters_unused_token(
480502
tool_instance.add_auth_token_getters(unused_auth_getters)
481503

482504

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+
483581
# --- Test for the HTTP Warning ---
484582
@pytest.mark.parametrize(
485583
"trigger_condition_params",

0 commit comments

Comments
 (0)