Skip to content

Commit 7683063

Browse files
committed
Move tests to different classes
1 parent 738b984 commit 7683063

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

packages/toolbox-core/tests/test_e2e.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818
from toolbox_core.tool import ToolboxTool
1919

2020

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+
39+
2140
@pytest.mark.asyncio
2241
@pytest.mark.usefixtures("toolbox_server")
23-
class TestE2EClient:
24-
@pytest_asyncio.fixture(scope="function")
25-
async def toolbox(self):
26-
toolbox = ToolboxClient("http://localhost:5000")
27-
try:
28-
yield toolbox
29-
finally:
30-
await toolbox.close()
31-
32-
@pytest_asyncio.fixture(scope="function")
33-
async def get_n_rows_tool(self, toolbox: ToolboxClient) -> ToolboxTool:
34-
"""Load a tool."""
35-
tool = await toolbox.load_tool("get-n-rows")
36-
assert tool.__name__ == "get-n-rows"
37-
return tool
38-
39-
#### Basic e2e tests
42+
class TestBasicE2E:
4043
@pytest.mark.parametrize(
4144
"toolset_name, expected_length, expected_tools",
4245
[
@@ -66,7 +69,7 @@ async def test_run_tool(self, get_n_rows_tool: ToolboxTool):
6669
assert "row2" in response
6770
assert "row3" not in response
6871

69-
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):
7073
"""Invoke a tool with missing params."""
7174
with pytest.raises(TypeError, match="missing a required argument: 'num_rows'"):
7275
await get_n_rows_tool()
@@ -79,39 +82,49 @@ async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool):
7982
):
8083
await get_n_rows_tool(num_rows=2)
8184

82-
##### Bind param tests
83-
async def test_bind_params(self, toolbox, get_n_rows_tool):
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+
):
8492
"""Bind a param to an existing tool."""
8593
new_tool = get_n_rows_tool.bind_parameters({"num_rows": "3"})
8694
response = await new_tool()
87-
8895
assert isinstance(response, str)
8996
assert "row1" in response
9097
assert "row2" in response
9198
assert "row3" in response
9299
assert "row4" not in response
93100

94-
async def test_bind_params_callable(self, toolbox, get_n_rows_tool):
101+
async def test_bind_params_callable(
102+
self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool
103+
):
95104
"""Bind a callable param to an existing tool."""
96105
new_tool = get_n_rows_tool.bind_parameters({"num_rows": lambda: "3"})
97106
response = await new_tool()
98-
99107
assert isinstance(response, str)
100108
assert "row1" in response
101109
assert "row2" in response
102110
assert "row3" in response
103111
assert "row4" not in response
104112

105-
##### Auth tests
106-
async def test_run_tool_unauth_with_auth(self, toolbox, auth_token2):
113+
114+
@pytest.mark.asyncio
115+
@pytest.mark.usefixtures("toolbox_server")
116+
class TestAuth:
117+
async def test_run_tool_unauth_with_auth(
118+
self, toolbox: ToolboxClient, auth_token2: str
119+
):
107120
"""Tests running a tool that doesn't require auth, with auth provided."""
108121
tool = await toolbox.load_tool(
109122
"get-row-by-id", auth_token_getters={"my-test-auth": lambda: auth_token2}
110123
)
111124
response = await tool(id="2")
112125
assert "row2" in response
113126

114-
async def test_run_tool_no_auth(self, toolbox):
127+
async def test_run_tool_no_auth(self, toolbox: ToolboxClient):
115128
"""Tests running a tool requiring auth without providing auth."""
116129
tool = await toolbox.load_tool("get-row-by-id-auth")
117130
with pytest.raises(
@@ -120,7 +133,7 @@ async def test_run_tool_no_auth(self, toolbox):
120133
):
121134
await tool(id="2")
122135

123-
async def test_run_tool_wrong_auth(self, toolbox, auth_token2):
136+
async def test_run_tool_wrong_auth(self, toolbox: ToolboxClient, auth_token2: str):
124137
"""Tests running a tool with incorrect auth. The tool
125138
requires a different authentication than the one provided."""
126139
tool = await toolbox.load_tool("get-row-by-id-auth")
@@ -131,14 +144,14 @@ async def test_run_tool_wrong_auth(self, toolbox, auth_token2):
131144
):
132145
await auth_tool(id="2")
133146

134-
async def test_run_tool_auth(self, toolbox, auth_token1):
147+
async def test_run_tool_auth(self, toolbox: ToolboxClient, auth_token1: str):
135148
"""Tests running a tool with correct auth."""
136149
tool = await toolbox.load_tool("get-row-by-id-auth")
137150
auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token1})
138151
response = await auth_tool(id="2")
139152
assert "row2" in response
140153

141-
async def test_run_tool_param_auth_no_auth(self, toolbox):
154+
async def test_run_tool_param_auth_no_auth(self, toolbox: ToolboxClient):
142155
"""Tests running a tool with a param requiring auth, without auth."""
143156
tool = await toolbox.load_tool("get-row-by-email-auth")
144157
with pytest.raises(
@@ -147,7 +160,7 @@ async def test_run_tool_param_auth_no_auth(self, toolbox):
147160
):
148161
await tool()
149162

150-
async def test_run_tool_param_auth(self, toolbox, auth_token1):
163+
async def test_run_tool_param_auth(self, toolbox: ToolboxClient, auth_token1: str):
151164
"""Tests running a tool with a param requiring auth, with correct auth."""
152165
tool = await toolbox.load_tool(
153166
"get-row-by-email-auth",
@@ -158,7 +171,9 @@ async def test_run_tool_param_auth(self, toolbox, auth_token1):
158171
assert "row5" in response
159172
assert "row6" in response
160173

161-
async def test_run_tool_param_auth_no_field(self, toolbox, auth_token1):
174+
async def test_run_tool_param_auth_no_field(
175+
self, toolbox: ToolboxClient, auth_token1: str
176+
):
162177
"""Tests running a tool with a param requiring auth, with insufficient auth."""
163178
tool = await toolbox.load_tool(
164179
"get-row-by-content-auth",

0 commit comments

Comments
 (0)