Skip to content

Commit 7404a57

Browse files
authored
test(network): improve response assertions in network tests (#66)
Replace partial assertions with complete response object comparisons and add HTTP status code verification for all mocked network requests. Changes: - Assert full response objects instead of checking individual keys - Add status code assertions (200/401) for all respx routes - Explicitly write out account_id values in multi-account tests - Store route references for error handling tests to verify status This makes test failures more explicit and easier to debug by showing exact response mismatches rather than partial field comparisons.
1 parent f9ae232 commit 7404a57

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

tests/test_feedback.py

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def test_json_string_input(self) -> None:
6969
{"feedback": "Great tools!", "account_id": "acc_123456", "tool_names": ["test_tool"]}
7070
)
7171
result = tool.execute(json_string)
72-
assert result["message"] == "Success"
72+
assert result == {"message": "Success"}
7373
assert route.called
74+
assert route.calls[0].response.status_code == 200
7475

7576

7677
class TestFeedbackToolExecution:
@@ -97,6 +98,7 @@ def test_single_account_execution(self) -> None:
9798
assert result == api_response
9899
assert route.called
99100
assert route.call_count == 1
101+
assert route.calls[0].response.status_code == 200
100102
request = route.calls[0].request
101103
body = json.loads(request.content)
102104
assert body["feedback"] == "Great tools!"
@@ -122,13 +124,14 @@ def test_call_method_interface(self) -> None:
122124
assert result == api_response
123125
assert route.called
124126
assert route.call_count == 1
127+
assert route.calls[0].response.status_code == 200
125128

126129
@respx.mock
127130
def test_api_error_handling(self) -> None:
128131
"""Test that API errors are handled properly."""
129132
tool = create_feedback_tool(api_key="test_key")
130133

131-
respx.post("https://api.stackone.com/ai/tool-feedback").mock(
134+
route = respx.post("https://api.stackone.com/ai/tool-feedback").mock(
132135
return_value=httpx.Response(401, json={"error": "Unauthorized"})
133136
)
134137

@@ -141,6 +144,9 @@ def test_api_error_handling(self) -> None:
141144
}
142145
)
143146

147+
assert route.called
148+
assert route.calls[0].response.status_code == 401
149+
144150
@respx.mock
145151
def test_multiple_account_ids_execution(self) -> None:
146152
"""Test execution with multiple account IDs - both success and mixed scenarios."""
@@ -160,12 +166,33 @@ def test_multiple_account_ids_execution(self) -> None:
160166
}
161167
)
162168

163-
assert result["message"] == "Feedback sent to 3 account(s)"
164-
assert result["total_accounts"] == 3
165-
assert result["successful"] == 3
166-
assert result["failed"] == 0
167-
assert len(result["results"]) == 3
169+
assert result == {
170+
"message": "Feedback sent to 3 account(s)",
171+
"total_accounts": 3,
172+
"successful": 3,
173+
"failed": 0,
174+
"results": [
175+
{
176+
"account_id": "acc_123456",
177+
"status": "success",
178+
"result": {"message": "Feedback successfully stored", "trace_id": "test-trace-id"},
179+
},
180+
{
181+
"account_id": "acc_789012",
182+
"status": "success",
183+
"result": {"message": "Feedback successfully stored", "trace_id": "test-trace-id"},
184+
},
185+
{
186+
"account_id": "acc_345678",
187+
"status": "success",
188+
"result": {"message": "Feedback successfully stored", "trace_id": "test-trace-id"},
189+
},
190+
],
191+
}
168192
assert route.call_count == 3
193+
assert route.calls[0].response.status_code == 200
194+
assert route.calls[1].response.status_code == 200
195+
assert route.calls[2].response.status_code == 200
169196

170197
@respx.mock
171198
def test_multiple_account_ids_mixed_success(self) -> None:
@@ -180,7 +207,7 @@ def custom_side_effect(request: httpx.Request) -> httpx.Response:
180207
else:
181208
return httpx.Response(401, json={"error": "Unauthorized"})
182209

183-
respx.post("https://api.stackone.com/ai/tool-feedback").mock(side_effect=custom_side_effect)
210+
route = respx.post("https://api.stackone.com/ai/tool-feedback").mock(side_effect=custom_side_effect)
184211

185212
result = tool.execute(
186213
{
@@ -190,18 +217,32 @@ def custom_side_effect(request: httpx.Request) -> httpx.Response:
190217
}
191218
)
192219

193-
assert result["total_accounts"] == 2
194-
assert result["successful"] == 1
195-
assert result["failed"] == 1
196-
assert len(result["results"]) == 2
197-
198-
success_result = next(r for r in result["results"] if r["account_id"] == "acc_123456")
199-
assert success_result["status"] == "success"
200-
201-
error_result = next(r for r in result["results"] if r["account_id"] == "acc_unauthorized")
202-
assert error_result["status"] == "error"
203-
assert "error" in error_result
204-
assert "401" in error_result["error"]
220+
assert result == {
221+
"message": "Feedback sent to 2 account(s)",
222+
"total_accounts": 2,
223+
"successful": 1,
224+
"failed": 1,
225+
"results": [
226+
{
227+
"account_id": "acc_123456",
228+
"status": "success",
229+
"result": {"message": "Success"},
230+
},
231+
{
232+
"account_id": "acc_unauthorized",
233+
"status": "error",
234+
"error": (
235+
"Client error '401 Unauthorized' for url "
236+
"'https://api.stackone.com/ai/tool-feedback'\n"
237+
"For more information check: "
238+
"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401"
239+
),
240+
},
241+
],
242+
}
243+
assert route.call_count == 2
244+
assert route.calls[0].response.status_code == 200
245+
assert route.calls[1].response.status_code == 401
205246

206247
def test_tool_integration(self) -> None:
207248
"""Test that feedback tool integrates properly with toolset."""

tests/test_meta_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ def test_execute_tool_call(self, tools_collection):
223223
# Call the meta execute tool
224224
result = execute_tool.call(toolName="hibob_list_employee", params={"limit": 10})
225225

226-
assert result is not None
227-
assert "success" in result or "employees" in result
226+
assert result == {"success": True, "employees": []}
228227
assert route.called
228+
assert route.calls[0].response.status_code == 200
229229

230230

231231
class TestToolsMetaTools:

0 commit comments

Comments
 (0)