Skip to content

Commit ce72925

Browse files
authored
Merge branch 'main' into twisha-core-docstring
2 parents 9e63c37 + 827129f commit ce72925

File tree

5 files changed

+71
-64
lines changed

5 files changed

+71
-64
lines changed

packages/toolbox-core/src/toolbox_core/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import re
1514
import types
1615
from typing import Any, Callable, Mapping, Optional, Union
1716

packages/toolbox-core/src/toolbox_core/tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515

1616
import asyncio
17-
import copy
1817
import types
19-
from inspect import Signature
18+
from inspect import Parameter, Signature
2019
from typing import (
2120
Any,
2221
Callable,
2322
Iterable,
2423
Mapping,
2524
Optional,
25+
Sequence,
2626
Union,
2727
)
2828

packages/toolbox-core/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ def toolbox_server(toolbox_version: str, tools_file_path: str) -> Generator[None
163163

164164
# Clean up toolbox server
165165
toolbox_server.terminate()
166-
toolbox_server.wait()
166+
toolbox_server.wait(timeout=5)

packages/toolbox-core/tests/test_e2e.py

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,34 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""End-to-end tests for the toolbox SDK interacting with the toolbox server.
16-
17-
This file covers the following test cases:
18-
19-
1. Loading a tool.
20-
2. Loading a specific toolset.
21-
3. Loading the default toolset (contains all tools).
22-
4. Running a tool with
23-
a. Missing params.
24-
b. Wrong param type.
25-
5. Running a tool with no required auth, with auth provided.
26-
6. Running a tool with required auth:
27-
a. No auth provided.
28-
b. Wrong auth provided: The tool requires a different authentication
29-
than the one provided.
30-
c. Correct auth provided.
31-
7. Running a tool with a parameter that requires auth:
32-
a. No auth provided.
33-
b. Correct auth provided.
34-
c. Auth provided does not contain the required claim.
35-
"""
3615
import pytest
3716
import pytest_asyncio
3817

3918
from toolbox_core.client import ToolboxClient
4019
from toolbox_core.tool import ToolboxTool
4120

21+
# --- Shared Fixtures Defined at Module Level ---
22+
@pytest_asyncio.fixture(scope="function")
23+
async def toolbox():
24+
"""Creates a ToolboxClient instance shared by all tests in this module."""
25+
toolbox = ToolboxClient("http://localhost:5000")
26+
try:
27+
yield toolbox
28+
finally:
29+
await toolbox.close()
30+
31+
32+
@pytest_asyncio.fixture(scope="function")
33+
async def get_n_rows_tool(toolbox: ToolboxClient) -> ToolboxTool:
34+
"""Load the 'get-n-rows' tool using the shared toolbox client."""
35+
tool = await toolbox.load_tool("get-n-rows")
36+
assert tool.__name__ == "get-n-rows"
37+
return tool
38+
4239

4340
@pytest.mark.asyncio
4441
@pytest.mark.usefixtures("toolbox_server")
45-
class TestE2EClient:
46-
@pytest_asyncio.fixture(scope="function")
47-
async def toolbox(self):
48-
toolbox = ToolboxClient("http://localhost:5000")
49-
try:
50-
yield toolbox
51-
finally:
52-
await toolbox.close()
53-
54-
@pytest_asyncio.fixture(scope="function")
55-
async def get_n_rows_tool(self, toolbox: ToolboxClient) -> ToolboxTool:
56-
tool = await toolbox.load_tool("get-n-rows")
57-
assert tool.__name__ == "get-n-rows"
58-
return tool
59-
60-
#### Basic e2e tests
42+
class TestBasicE2E:
6143
@pytest.mark.parametrize(
6244
"toolset_name, expected_length, expected_tools",
6345
[
@@ -72,83 +54,105 @@ async def test_load_toolset_specific(
7254
expected_length: int,
7355
expected_tools: list[str],
7456
):
57+
"""Load a specific toolset"""
7558
toolset = await toolbox.load_toolset(toolset_name)
7659
assert len(toolset) == expected_length
7760
tool_names = {tool.__name__ for tool in toolset}
7861
assert tool_names == set(expected_tools)
7962

8063
async def test_run_tool(self, get_n_rows_tool: ToolboxTool):
64+
"""Invoke a tool."""
8165
response = await get_n_rows_tool(num_rows="2")
8266

8367
assert isinstance(response, str)
8468
assert "row1" in response
8569
assert "row2" in response
8670
assert "row3" not in response
8771

88-
async def test_run_tool_missing_params(self, get_n_rows_tool):
72+
async def test_run_tool_missing_params(self, get_n_rows_tool: ToolboxTool):
73+
"""Invoke a tool with missing params."""
8974
with pytest.raises(TypeError, match="missing a required argument: 'num_rows'"):
9075
await get_n_rows_tool()
9176

9277
async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool):
78+
"""Invoke a tool with wrong param type."""
9379
with pytest.raises(
9480
Exception,
9581
match='provided parameters were invalid: unable to parse value for "num_rows": .* not type "string"',
9682
):
9783
await get_n_rows_tool(num_rows=2)
9884

99-
##### Bind param tests
100-
async def test_bind_params(self, toolbox, get_n_rows_tool):
101-
new_tool = get_n_rows_tool.bind_parameters({"num_rows": lambda: "3"})
85+
86+
@pytest.mark.asyncio
87+
@pytest.mark.usefixtures("toolbox_server")
88+
class TestBindParams:
89+
async def test_bind_params(
90+
self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool
91+
):
92+
"""Bind a param to an existing tool."""
93+
new_tool = get_n_rows_tool.bind_parameters({"num_rows": "3"})
10294
response = await new_tool()
95+
assert isinstance(response, str)
96+
assert "row1" in response
97+
assert "row2" in response
98+
assert "row3" in response
99+
assert "row4" not in response
103100

101+
async def test_bind_params_callable(
102+
self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool
103+
):
104+
"""Bind a callable param to an existing tool."""
105+
new_tool = get_n_rows_tool.bind_parameters({"num_rows": lambda: "3"})
106+
response = await new_tool()
104107
assert isinstance(response, str)
105108
assert "row1" in response
106109
assert "row2" in response
107110
assert "row3" in response
108111
assert "row4" not in response
109112

110-
##### Auth tests
111-
async def test_run_tool_unauth_with_auth(self, toolbox, auth_token2):
113+
@pytest.mark.asyncio
114+
@pytest.mark.usefixtures("toolbox_server")
115+
class TestAuth:
116+
async def test_run_tool_unauth_with_auth(
117+
self, toolbox: ToolboxClient, auth_token2: str
118+
):
112119
"""Tests running a tool that doesn't require auth, with auth provided."""
113120
tool = await toolbox.load_tool(
114121
"get-row-by-id", auth_token_getters={"my-test-auth": lambda: auth_token2}
115122
)
116123
response = await tool(id="2")
117124
assert "row2" in response
118125

119-
async def test_run_tool_no_auth(self, toolbox):
126+
127+
async def test_run_tool_no_auth(self, toolbox: ToolboxClient):
120128
"""Tests running a tool requiring auth without providing auth."""
121-
tool = await toolbox.load_tool(
122-
"get-row-by-id-auth",
123-
)
129+
tool = await toolbox.load_tool("get-row-by-id-auth")
124130
with pytest.raises(
125131
Exception,
126132
match="tool invocation not authorized. Please make sure your specify correct auth headers",
127133
):
128134
await tool(id="2")
129135

130-
async def test_run_tool_wrong_auth(self, toolbox, auth_token2):
131-
"""Tests running a tool with incorrect auth."""
132-
tool = await toolbox.load_tool(
133-
"get-row-by-id-auth",
134-
)
136+
async def test_run_tool_wrong_auth(self, toolbox: ToolboxClient, auth_token2: str):
137+
"""Tests running a tool with incorrect auth. The tool
138+
requires a different authentication than the one provided."""
139+
tool = await toolbox.load_tool("get-row-by-id-auth")
135140
auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token2})
136141
with pytest.raises(
137142
Exception,
138143
match="tool invocation not authorized",
139144
):
140145
await auth_tool(id="2")
141146

142-
async def test_run_tool_auth(self, toolbox, auth_token1):
147+
async def test_run_tool_auth(self, toolbox: ToolboxClient, auth_token1: str):
143148
"""Tests running a tool with correct auth."""
144-
tool = await toolbox.load_tool(
145-
"get-row-by-id-auth",
146-
)
149+
tool = await toolbox.load_tool("get-row-by-id-auth")
147150
auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token1})
148151
response = await auth_tool(id="2")
149152
assert "row2" in response
150153

151-
async def test_run_tool_param_auth_no_auth(self, toolbox):
154+
155+
async def test_run_tool_param_auth_no_auth(self, toolbox: ToolboxClient):
152156
"""Tests running a tool with a param requiring auth, without auth."""
153157
tool = await toolbox.load_tool("get-row-by-email-auth")
154158
with pytest.raises(
@@ -157,7 +161,8 @@ async def test_run_tool_param_auth_no_auth(self, toolbox):
157161
):
158162
await tool()
159163

160-
async def test_run_tool_param_auth(self, toolbox, auth_token1):
164+
165+
async def test_run_tool_param_auth(self, toolbox: ToolboxClient, auth_token1: str):
161166
"""Tests running a tool with a param requiring auth, with correct auth."""
162167
tool = await toolbox.load_tool(
163168
"get-row-by-email-auth",
@@ -168,7 +173,9 @@ async def test_run_tool_param_auth(self, toolbox, auth_token1):
168173
assert "row5" in response
169174
assert "row6" in response
170175

171-
async def test_run_tool_param_auth_no_field(self, toolbox, auth_token1):
176+
async def test_run_tool_param_auth_no_field(
177+
self, toolbox: ToolboxClient, auth_token1: str
178+
):
172179
"""Tests running a tool with a param requiring auth, with insufficient auth."""
173180
tool = await toolbox.load_tool(
174181
"get-row-by-content-auth",

release-please-config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
"bump-minor-pre-major": true,
55
"bump-patch-for-minor-pre-major": true,
66
"separate-pull-requests": true,
7-
"include-component-in-tag": true,
87
"changelog-sections": [
98
{ "type": "feat", "section": "Features" },
109
{ "type": "fix", "section": "Bug Fixes" },
1110
{ "type": "chore", "section": "Miscellaneous Chores", "hidden": false }
1211
],
1312
"packages": {
1413
"packages/toolbox-core": {
14+
"component": "toolbox-core",
1515
"extra-files": [
1616
"src/toolbox_core/version.py"
1717
]
1818
},
1919
"packages/toolbox-langchain": {
20+
"component": "toolbox-langchain",
2021
"extra-files": [
2122
"src/toolbox_langchain/version.py"
2223
]

0 commit comments

Comments
 (0)