18
18
from toolbox_core .tool import ToolboxTool
19
19
20
20
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
+
21
40
@pytest .mark .asyncio
22
41
@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 :
40
43
@pytest .mark .parametrize (
41
44
"toolset_name, expected_length, expected_tools" ,
42
45
[
@@ -66,7 +69,7 @@ async def test_run_tool(self, get_n_rows_tool: ToolboxTool):
66
69
assert "row2" in response
67
70
assert "row3" not in response
68
71
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 ):
70
73
"""Invoke a tool with missing params."""
71
74
with pytest .raises (TypeError , match = "missing a required argument: 'num_rows'" ):
72
75
await get_n_rows_tool ()
@@ -79,39 +82,49 @@ async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool):
79
82
):
80
83
await get_n_rows_tool (num_rows = 2 )
81
84
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
+ ):
84
92
"""Bind a param to an existing tool."""
85
93
new_tool = get_n_rows_tool .bind_parameters ({"num_rows" : "3" })
86
94
response = await new_tool ()
87
-
88
95
assert isinstance (response , str )
89
96
assert "row1" in response
90
97
assert "row2" in response
91
98
assert "row3" in response
92
99
assert "row4" not in response
93
100
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
+ ):
95
104
"""Bind a callable param to an existing tool."""
96
105
new_tool = get_n_rows_tool .bind_parameters ({"num_rows" : lambda : "3" })
97
106
response = await new_tool ()
98
-
99
107
assert isinstance (response , str )
100
108
assert "row1" in response
101
109
assert "row2" in response
102
110
assert "row3" in response
103
111
assert "row4" not in response
104
112
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
+ ):
107
120
"""Tests running a tool that doesn't require auth, with auth provided."""
108
121
tool = await toolbox .load_tool (
109
122
"get-row-by-id" , auth_token_getters = {"my-test-auth" : lambda : auth_token2 }
110
123
)
111
124
response = await tool (id = "2" )
112
125
assert "row2" in response
113
126
114
- async def test_run_tool_no_auth (self , toolbox ):
127
+ async def test_run_tool_no_auth (self , toolbox : ToolboxClient ):
115
128
"""Tests running a tool requiring auth without providing auth."""
116
129
tool = await toolbox .load_tool ("get-row-by-id-auth" )
117
130
with pytest .raises (
@@ -120,7 +133,7 @@ async def test_run_tool_no_auth(self, toolbox):
120
133
):
121
134
await tool (id = "2" )
122
135
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 ):
124
137
"""Tests running a tool with incorrect auth. The tool
125
138
requires a different authentication than the one provided."""
126
139
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):
131
144
):
132
145
await auth_tool (id = "2" )
133
146
134
- async def test_run_tool_auth (self , toolbox , auth_token1 ):
147
+ async def test_run_tool_auth (self , toolbox : ToolboxClient , auth_token1 : str ):
135
148
"""Tests running a tool with correct auth."""
136
149
tool = await toolbox .load_tool ("get-row-by-id-auth" )
137
150
auth_tool = tool .add_auth_token_getters ({"my-test-auth" : lambda : auth_token1 })
138
151
response = await auth_tool (id = "2" )
139
152
assert "row2" in response
140
153
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 ):
142
155
"""Tests running a tool with a param requiring auth, without auth."""
143
156
tool = await toolbox .load_tool ("get-row-by-email-auth" )
144
157
with pytest .raises (
@@ -147,7 +160,7 @@ async def test_run_tool_param_auth_no_auth(self, toolbox):
147
160
):
148
161
await tool ()
149
162
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 ):
151
164
"""Tests running a tool with a param requiring auth, with correct auth."""
152
165
tool = await toolbox .load_tool (
153
166
"get-row-by-email-auth" ,
@@ -158,7 +171,9 @@ async def test_run_tool_param_auth(self, toolbox, auth_token1):
158
171
assert "row5" in response
159
172
assert "row6" in response
160
173
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
+ ):
162
177
"""Tests running a tool with a param requiring auth, with insufficient auth."""
163
178
tool = await toolbox .load_tool (
164
179
"get-row-by-content-auth" ,
0 commit comments