|
5 | 5 |
|
6 | 6 | import json |
7 | 7 | import os |
8 | | -from unittest.mock import Mock, patch |
9 | 8 |
|
| 9 | +import httpx |
10 | 10 | import pytest |
| 11 | +import respx |
11 | 12 |
|
12 | 13 | from stackone_ai.feedback import create_feedback_tool |
13 | 14 | from stackone_ai.models import StackOneError |
@@ -55,165 +56,150 @@ def test_multiple_account_ids_validation(self) -> None: |
55 | 56 | with pytest.raises(StackOneError, match="At least one valid account ID is required"): |
56 | 57 | tool.execute({"feedback": "Great tools!", "account_id": ["", " "], "tool_names": ["test_tool"]}) |
57 | 58 |
|
| 59 | + @respx.mock |
58 | 60 | def test_json_string_input(self) -> None: |
59 | 61 | """Test that JSON string input is properly parsed.""" |
60 | 62 | tool = create_feedback_tool(api_key="test_key") |
61 | 63 |
|
62 | | - with patch("requests.request") as mock_request: |
63 | | - mock_response = Mock() |
64 | | - mock_response.status_code = 200 |
65 | | - mock_response.json.return_value = {"message": "Success"} |
66 | | - mock_response.raise_for_status = Mock() |
67 | | - mock_request.return_value = mock_response |
| 64 | + route = respx.post("https://api.stackone.com/ai/tool-feedback").mock( |
| 65 | + return_value=httpx.Response(200, json={"message": "Success"}) |
| 66 | + ) |
68 | 67 |
|
69 | | - json_string = json.dumps( |
70 | | - {"feedback": "Great tools!", "account_id": "acc_123456", "tool_names": ["test_tool"]} |
71 | | - ) |
72 | | - result = tool.execute(json_string) |
73 | | - assert result["message"] == "Success" |
| 68 | + json_string = json.dumps( |
| 69 | + {"feedback": "Great tools!", "account_id": "acc_123456", "tool_names": ["test_tool"]} |
| 70 | + ) |
| 71 | + result = tool.execute(json_string) |
| 72 | + assert result["message"] == "Success" |
| 73 | + assert route.called |
74 | 74 |
|
75 | 75 |
|
76 | 76 | class TestFeedbackToolExecution: |
77 | 77 | """Test suite for feedback tool execution.""" |
78 | 78 |
|
| 79 | + @respx.mock |
79 | 80 | def test_single_account_execution(self) -> None: |
80 | 81 | """Test execution with single account ID.""" |
81 | 82 | tool = create_feedback_tool(api_key="test_key") |
82 | 83 | api_response = {"message": "Feedback successfully stored", "trace_id": "test-trace-id"} |
83 | 84 |
|
84 | | - with patch("requests.request") as mock_request: |
85 | | - mock_response = Mock() |
86 | | - mock_response.status_code = 200 |
87 | | - mock_response.json.return_value = api_response |
88 | | - mock_response.raise_for_status = Mock() |
89 | | - mock_request.return_value = mock_response |
90 | | - |
91 | | - result = tool.execute( |
92 | | - { |
93 | | - "feedback": "Great tools!", |
94 | | - "account_id": "acc_123456", |
95 | | - "tool_names": ["data_export", "analytics"], |
96 | | - } |
97 | | - ) |
98 | | - |
99 | | - assert result == api_response |
100 | | - mock_request.assert_called_once() |
101 | | - call_kwargs = mock_request.call_args[1] |
102 | | - assert call_kwargs["method"] == "POST" |
103 | | - assert call_kwargs["url"] == "https://api.stackone.com/ai/tool-feedback" |
104 | | - assert call_kwargs["json"]["feedback"] == "Great tools!" |
105 | | - assert call_kwargs["json"]["account_id"] == "acc_123456" |
106 | | - assert call_kwargs["json"]["tool_names"] == ["data_export", "analytics"] |
107 | | - |
| 85 | + route = respx.post("https://api.stackone.com/ai/tool-feedback").mock( |
| 86 | + return_value=httpx.Response(200, json=api_response) |
| 87 | + ) |
| 88 | + |
| 89 | + result = tool.execute( |
| 90 | + { |
| 91 | + "feedback": "Great tools!", |
| 92 | + "account_id": "acc_123456", |
| 93 | + "tool_names": ["data_export", "analytics"], |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + assert result == api_response |
| 98 | + assert route.called |
| 99 | + assert route.call_count == 1 |
| 100 | + request = route.calls[0].request |
| 101 | + body = json.loads(request.content) |
| 102 | + assert body["feedback"] == "Great tools!" |
| 103 | + assert body["account_id"] == "acc_123456" |
| 104 | + assert body["tool_names"] == ["data_export", "analytics"] |
| 105 | + |
| 106 | + @respx.mock |
108 | 107 | def test_call_method_interface(self) -> None: |
109 | 108 | """Test that the .call() method works correctly.""" |
110 | 109 | tool = create_feedback_tool(api_key="test_key") |
111 | 110 | api_response = {"message": "Success", "trace_id": "test-trace-id"} |
112 | 111 |
|
113 | | - with patch("requests.request") as mock_request: |
114 | | - mock_response = Mock() |
115 | | - mock_response.status_code = 200 |
116 | | - mock_response.json.return_value = api_response |
117 | | - mock_response.raise_for_status = Mock() |
118 | | - mock_request.return_value = mock_response |
119 | | - |
120 | | - result = tool.call( |
121 | | - feedback="Testing the .call() method interface.", |
122 | | - account_id="acc_test004", |
123 | | - tool_names=["meta_collect_tool_feedback"], |
124 | | - ) |
| 112 | + route = respx.post("https://api.stackone.com/ai/tool-feedback").mock( |
| 113 | + return_value=httpx.Response(200, json=api_response) |
| 114 | + ) |
125 | 115 |
|
126 | | - assert result == api_response |
127 | | - mock_request.assert_called_once() |
| 116 | + result = tool.call( |
| 117 | + feedback="Testing the .call() method interface.", |
| 118 | + account_id="acc_test004", |
| 119 | + tool_names=["meta_collect_tool_feedback"], |
| 120 | + ) |
128 | 121 |
|
| 122 | + assert result == api_response |
| 123 | + assert route.called |
| 124 | + assert route.call_count == 1 |
| 125 | + |
| 126 | + @respx.mock |
129 | 127 | def test_api_error_handling(self) -> None: |
130 | 128 | """Test that API errors are handled properly.""" |
131 | 129 | tool = create_feedback_tool(api_key="test_key") |
132 | 130 |
|
133 | | - with patch("requests.request") as mock_request: |
134 | | - mock_response = Mock() |
135 | | - mock_response.status_code = 401 |
136 | | - mock_response.text = '{"error": "Unauthorized"}' |
137 | | - mock_response.json.return_value = {"error": "Unauthorized"} |
138 | | - mock_response.raise_for_status.side_effect = Exception("401 Client Error: Unauthorized") |
139 | | - mock_request.return_value = mock_response |
140 | | - |
141 | | - with pytest.raises(StackOneError): |
142 | | - tool.execute( |
143 | | - { |
144 | | - "feedback": "Great tools!", |
145 | | - "account_id": "acc_123456", |
146 | | - "tool_names": ["test_tool"], |
147 | | - } |
148 | | - ) |
149 | | - |
150 | | - def test_multiple_account_ids_execution(self) -> None: |
151 | | - """Test execution with multiple account IDs - both success and mixed scenarios.""" |
152 | | - tool = create_feedback_tool(api_key="test_key") |
153 | | - api_response = {"message": "Feedback successfully stored", "trace_id": "test-trace-id"} |
| 131 | + respx.post("https://api.stackone.com/ai/tool-feedback").mock( |
| 132 | + return_value=httpx.Response(401, json={"error": "Unauthorized"}) |
| 133 | + ) |
154 | 134 |
|
155 | | - # Test all successful case |
156 | | - with patch("requests.request") as mock_request: |
157 | | - mock_response = Mock() |
158 | | - mock_response.status_code = 200 |
159 | | - mock_response.json.return_value = api_response |
160 | | - mock_response.raise_for_status = Mock() |
161 | | - mock_request.return_value = mock_response |
162 | | - |
163 | | - result = tool.execute( |
| 135 | + with pytest.raises(StackOneError): |
| 136 | + tool.execute( |
164 | 137 | { |
165 | 138 | "feedback": "Great tools!", |
166 | | - "account_id": ["acc_123456", "acc_789012", "acc_345678"], |
| 139 | + "account_id": "acc_123456", |
167 | 140 | "tool_names": ["test_tool"], |
168 | 141 | } |
169 | 142 | ) |
170 | 143 |
|
171 | | - assert result["message"] == "Feedback sent to 3 account(s)" |
172 | | - assert result["total_accounts"] == 3 |
173 | | - assert result["successful"] == 3 |
174 | | - assert result["failed"] == 0 |
175 | | - assert len(result["results"]) == 3 |
176 | | - assert mock_request.call_count == 3 |
| 144 | + @respx.mock |
| 145 | + def test_multiple_account_ids_execution(self) -> None: |
| 146 | + """Test execution with multiple account IDs - both success and mixed scenarios.""" |
| 147 | + tool = create_feedback_tool(api_key="test_key") |
| 148 | + api_response = {"message": "Feedback successfully stored", "trace_id": "test-trace-id"} |
177 | 149 |
|
178 | | - # Test mixed success/error case |
179 | | - def mock_request_side_effect(*args, **kwargs): |
180 | | - account_id = kwargs.get("json", {}).get("account_id") |
| 150 | + # Test all successful case |
| 151 | + route = respx.post("https://api.stackone.com/ai/tool-feedback").mock( |
| 152 | + return_value=httpx.Response(200, json=api_response) |
| 153 | + ) |
| 154 | + |
| 155 | + result = tool.execute( |
| 156 | + { |
| 157 | + "feedback": "Great tools!", |
| 158 | + "account_id": ["acc_123456", "acc_789012", "acc_345678"], |
| 159 | + "tool_names": ["test_tool"], |
| 160 | + } |
| 161 | + ) |
| 162 | + |
| 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 |
| 168 | + assert route.call_count == 3 |
| 169 | + |
| 170 | + @respx.mock |
| 171 | + def test_multiple_account_ids_mixed_success(self) -> None: |
| 172 | + """Test execution with multiple account IDs - mixed success and error.""" |
| 173 | + tool = create_feedback_tool(api_key="test_key") |
| 174 | + |
| 175 | + def custom_side_effect(request: httpx.Request) -> httpx.Response: |
| 176 | + body = json.loads(request.content) |
| 177 | + account_id = body.get("account_id") |
181 | 178 | if account_id == "acc_123456": |
182 | | - mock_response = Mock() |
183 | | - mock_response.status_code = 200 |
184 | | - mock_response.json.return_value = {"message": "Success"} |
185 | | - mock_response.raise_for_status = Mock() |
186 | | - return mock_response |
| 179 | + return httpx.Response(200, json={"message": "Success"}) |
187 | 180 | else: |
188 | | - mock_response = Mock() |
189 | | - mock_response.status_code = 401 |
190 | | - mock_response.text = '{"error": "Unauthorized"}' |
191 | | - mock_response.json.return_value = {"error": "Unauthorized"} |
192 | | - mock_response.raise_for_status.side_effect = Exception("401 Client Error: Unauthorized") |
193 | | - return mock_response |
| 181 | + return httpx.Response(401, json={"error": "Unauthorized"}) |
194 | 182 |
|
195 | | - with patch("requests.request") as mock_request: |
196 | | - mock_request.side_effect = mock_request_side_effect |
| 183 | + respx.post("https://api.stackone.com/ai/tool-feedback").mock(side_effect=custom_side_effect) |
197 | 184 |
|
198 | | - result = tool.execute( |
199 | | - { |
200 | | - "feedback": "Great tools!", |
201 | | - "account_id": ["acc_123456", "acc_unauthorized"], |
202 | | - "tool_names": ["test_tool"], |
203 | | - } |
204 | | - ) |
| 185 | + result = tool.execute( |
| 186 | + { |
| 187 | + "feedback": "Great tools!", |
| 188 | + "account_id": ["acc_123456", "acc_unauthorized"], |
| 189 | + "tool_names": ["test_tool"], |
| 190 | + } |
| 191 | + ) |
205 | 192 |
|
206 | | - assert result["total_accounts"] == 2 |
207 | | - assert result["successful"] == 1 |
208 | | - assert result["failed"] == 1 |
209 | | - assert len(result["results"]) == 2 |
| 193 | + assert result["total_accounts"] == 2 |
| 194 | + assert result["successful"] == 1 |
| 195 | + assert result["failed"] == 1 |
| 196 | + assert len(result["results"]) == 2 |
210 | 197 |
|
211 | | - success_result = next(r for r in result["results"] if r["account_id"] == "acc_123456") |
212 | | - assert success_result["status"] == "success" |
| 198 | + success_result = next(r for r in result["results"] if r["account_id"] == "acc_123456") |
| 199 | + assert success_result["status"] == "success" |
213 | 200 |
|
214 | | - error_result = next(r for r in result["results"] if r["account_id"] == "acc_unauthorized") |
215 | | - assert error_result["status"] == "error" |
216 | | - assert "401 Client Error: Unauthorized" in error_result["error"] |
| 201 | + error_result = next(r for r in result["results"] if r["account_id"] == "acc_unauthorized") |
| 202 | + assert error_result["status"] == "error" |
217 | 203 |
|
218 | 204 | def test_tool_integration(self) -> None: |
219 | 205 | """Test that feedback tool integrates properly with toolset.""" |
|
0 commit comments