MCPcopy Create free account
hub / github.com/Open-Bee/DataStudio / TestAsyncRequest

Class TestAsyncRequest

tests/models/test_openai_api.py:259–318  ·  view source on GitHub ↗

Tests for async request methods using mocked aiohttp.

Source from the content-addressed store, hash-verified

257
258
259class TestAsyncRequest:
260 """Tests for async request methods using mocked aiohttp."""
261
262 @pytest.mark.asyncio
263 async def test_request_non_streaming_success(
264 self, api, mock_openai_success_response
265 ):
266 mock_response = AsyncMock()
267 mock_response.status = 200
268 mock_response.json = AsyncMock(return_value=mock_openai_success_response)
269 mock_response.__aenter__ = AsyncMock(return_value=mock_response)
270 mock_response.__aexit__ = AsyncMock(return_value=False)
271
272 mock_session = AsyncMock()
273 mock_session.post = MagicMock(return_value=mock_response)
274
275 message = [{"role": "user", "content": "Hello"}]
276 ret_code, result = await api._request_non_streaming(mock_session, message)
277 assert ret_code == 0
278 assert result == "Test response content"
279
280 @pytest.mark.asyncio
281 async def test_request_non_streaming_http_error(self, api):
282 mock_response = AsyncMock()
283 mock_response.status = 500
284 mock_response.text = AsyncMock(return_value="Internal Server Error")
285 mock_response.__aenter__ = AsyncMock(return_value=mock_response)
286 mock_response.__aexit__ = AsyncMock(return_value=False)
287
288 mock_session = AsyncMock()
289 mock_session.post = MagicMock(return_value=mock_response)
290
291 message = [{"role": "user", "content": "Hello"}]
292 ret_code, result = await api._request_non_streaming(mock_session, message)
293 assert ret_code == 1
294 assert "500" in result
295
296 @pytest.mark.asyncio
297 async def test_request_non_streaming_timeout(self, api):
298 mock_session = AsyncMock()
299 mock_session.post = MagicMock(side_effect=asyncio.TimeoutError())
300
301 message = [{"role": "user", "content": "Hello"}]
302 # _request_non_streaming does not catch TimeoutError; it propagates
303 with pytest.raises(asyncio.TimeoutError):
304 await api._request_non_streaming(mock_session, message)
305
306 @pytest.mark.asyncio
307 async def test_request_non_streaming_client_error(self, api):
308 import aiohttp
309
310 mock_session = AsyncMock()
311 mock_session.post = MagicMock(
312 side_effect=aiohttp.ClientError("Connection refused")
313 )
314
315 message = [{"role": "user", "content": "Hello"}]
316 # _request_non_streaming does not catch ClientError; it propagates

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected