55import pytest
66from fastapi import HTTPException , status
77from semantic_kernel .exceptions .agent_exceptions import AgentException as RealAgentException
8- from azure .ai .agents .models import MessageRole
98
109
1110
@@ -244,16 +243,8 @@ async def mock_invoke_stream(*args, **kwargs):
244243 assert "I cannot answer this question with the current data" in chunks [0 ]
245244
246245 @pytest .mark .asyncio
247- @patch ('services.chat_service.uuid.uuid4' )
248- @patch ('services.chat_service.time.time' )
249- @patch ('services.chat_service.format_stream_response' )
250- async def test_stream_chat_request_success (self , mock_format_stream , mock_time , mock_uuid , chat_service ):
246+ async def test_stream_chat_request_success (self , chat_service ):
251247 """Test successful stream chat request."""
252- # Setup mocks
253- mock_uuid .return_value = "test-uuid"
254- mock_time .return_value = 1234567890
255- mock_format_stream .return_value = {"formatted" : "response" }
256-
257248 # Mock stream_openai_text
258249 async def mock_stream_openai_text (conversation_id , query ):
259250 yield "Hello"
@@ -272,7 +263,11 @@ async def mock_stream_openai_text(conversation_id, query):
272263 # Verify the chunks contain expected structure
273264 for chunk in chunks :
274265 chunk_data = json .loads (chunk .strip ())
275- assert "formatted" in chunk_data
266+ assert "choices" in chunk_data
267+ assert len (chunk_data ["choices" ]) > 0
268+ assert "messages" in chunk_data ["choices" ][0 ]
269+ assert len (chunk_data ["choices" ][0 ]["messages" ]) > 0
270+ assert chunk_data ["choices" ][0 ]["messages" ][0 ]["role" ] == "assistant"
276271
277272 @pytest .mark .asyncio
278273 async def test_stream_chat_request_agent_exception_rate_limit (self , chat_service ):
@@ -345,46 +340,4 @@ async def mock_stream_openai_text_generic_error(conversation_id, query):
345340 error_data = json .loads (chunks [0 ].strip ())
346341 assert "error" in error_data
347342 assert "An error occurred while processing the request." == error_data ["error" ]
348-
349- @pytest .mark .asyncio
350- async def test_complete_chat_request_success (self , chat_service ):
351- mock_chart_data = {
352- "type" : "bar" ,
353- "data" : {
354- "labels" : ["A" ],
355- "datasets" : [{"data" : [1 ]}]
356- }
357- }
358-
359- chat_service .process_rag_response = AsyncMock (return_value = mock_chart_data )
360-
361- result = await chat_service .complete_chat_request ("Query" , last_rag_response = "RAG response" )
362-
363- assert result ["object" ]["type" ] == "bar"
364-
365-
366- @pytest .mark .asyncio
367- async def test_complete_chat_request_no_rag_response (self , chat_service ):
368- """Test complete chat request without RAG response."""
369- result = await chat_service .complete_chat_request ("Query" , last_rag_response = None )
370-
371- assert "error" in result
372- assert result ["error" ] == "A previous RAG response is required to generate a chart."
373-
374- @pytest .mark .asyncio
375- async def test_complete_chat_request_chart_error (self , chat_service ):
376- chat_service .process_rag_response = AsyncMock (return_value = {"error" : "Chart generation failed" })
377-
378- result = await chat_service .complete_chat_request ("Query" , last_rag_response = "RAG response" )
379-
380- assert "error" in result
381-
382-
383- @pytest .mark .asyncio
384- async def test_complete_chat_request_empty_chart_data (self , chat_service ):
385- chat_service .process_rag_response = AsyncMock (return_value = None )
386-
387- result = await chat_service .complete_chat_request ("Query" , last_rag_response = "RAG response" )
388-
389- assert "error" in result
390343
0 commit comments