|
|
@@ -0,0 +1,98 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+import unittest
|
|
|
+from unittest.mock import patch, MagicMock, AsyncMock
|
|
|
+from app.services.llm_service import LlmService
|
|
|
+from app.core.biz_exception import BizLogicError
|
|
|
+
|
|
|
+
|
|
|
+class TestLlmService(unittest.IsolatedAsyncioTestCase):
|
|
|
+ async def test_no_api_key_raises_biz_logic_error(self):
|
|
|
+ with patch.object(LlmService, "get_api_key", return_value=""):
|
|
|
+ with self.assertRaises(BizLogicError) as ctx:
|
|
|
+ await LlmService.parse_data_async(
|
|
|
+ user_text="test",
|
|
|
+ json_schema={"type": "object"},
|
|
|
+ )
|
|
|
+ self.assertEqual(ctx.exception.message, "LLM API Key not configured")
|
|
|
+ self.assertEqual(ctx.exception.code, 40001)
|
|
|
+
|
|
|
+ def _create_mock_session_cls(self, mock_response):
|
|
|
+ mock_post = MagicMock()
|
|
|
+ mock_post.__aenter__ = AsyncMock(return_value=mock_response)
|
|
|
+ mock_post.__aexit__ = AsyncMock(return_value=None)
|
|
|
+
|
|
|
+ mock_session = MagicMock()
|
|
|
+ mock_session.post = MagicMock(return_value=mock_post)
|
|
|
+ mock_session.__aenter__ = AsyncMock(return_value=mock_session)
|
|
|
+ mock_session.__aexit__ = AsyncMock(return_value=None)
|
|
|
+
|
|
|
+ mock_session_cls = MagicMock(return_value=mock_session)
|
|
|
+ return mock_session_cls
|
|
|
+
|
|
|
+ @patch("aiohttp.ClientSession")
|
|
|
+ async def test_empty_choices_raises_biz_logic_error(self, mock_session_cls):
|
|
|
+ mock_response = AsyncMock()
|
|
|
+ mock_response.status = 200
|
|
|
+ mock_response.json = AsyncMock(return_value={"choices": []})
|
|
|
+
|
|
|
+ mock_session_cls.side_effect = self._create_mock_session_cls(mock_response)
|
|
|
+
|
|
|
+ with patch.object(LlmService, "get_api_key", return_value="test-key"):
|
|
|
+ with self.assertRaises(BizLogicError) as ctx:
|
|
|
+ await LlmService.parse_data_async(
|
|
|
+ user_text="test",
|
|
|
+ json_schema={"type": "object"},
|
|
|
+ )
|
|
|
+ self.assertEqual(ctx.exception.message, "LLM response did not contain completion choices")
|
|
|
+
|
|
|
+ @patch("aiohttp.ClientSession")
|
|
|
+ async def test_invalid_json_raises_biz_logic_error(self, mock_session_cls):
|
|
|
+ mock_response = AsyncMock()
|
|
|
+ mock_response.status = 200
|
|
|
+ mock_response.json = AsyncMock(return_value={
|
|
|
+ "choices": [{"message": {"content": "not-valid-json"}}]
|
|
|
+ })
|
|
|
+
|
|
|
+ mock_session_cls.side_effect = self._create_mock_session_cls(mock_response)
|
|
|
+
|
|
|
+ with patch.object(LlmService, "get_api_key", return_value="test-key"):
|
|
|
+ with self.assertRaises(BizLogicError) as ctx:
|
|
|
+ await LlmService.parse_data_async(
|
|
|
+ user_text="test",
|
|
|
+ json_schema={"type": "object"},
|
|
|
+ )
|
|
|
+ self.assertEqual(ctx.exception.message, "Failed to parse valid JSON from LLM response")
|
|
|
+
|
|
|
+ @patch("aiohttp.ClientSession")
|
|
|
+ async def test_valid_json_returns_dict(self, mock_session_cls):
|
|
|
+ mock_response = AsyncMock()
|
|
|
+ mock_response.status = 200
|
|
|
+ mock_response.json = AsyncMock(return_value={
|
|
|
+ "choices": [{"message": {"content": '{"first_name": "JOHN", "last_name": "DOE"}'}}]
|
|
|
+ })
|
|
|
+
|
|
|
+ mock_session_cls.side_effect = self._create_mock_session_cls(mock_response)
|
|
|
+
|
|
|
+ with patch.object(LlmService, "get_api_key", return_value="test-key"):
|
|
|
+ result = await LlmService.parse_data_async(
|
|
|
+ user_text="test",
|
|
|
+ json_schema={"type": "object"},
|
|
|
+ )
|
|
|
+ self.assertEqual(result, {"first_name": "JOHN", "last_name": "DOE"})
|
|
|
+
|
|
|
+ @patch("aiohttp.ClientSession")
|
|
|
+ async def test_http_error_raises_biz_logic_error(self, mock_session_cls):
|
|
|
+ mock_response = AsyncMock()
|
|
|
+ mock_response.status = 500
|
|
|
+ mock_response.text = AsyncMock(return_value="Internal Server Error")
|
|
|
+
|
|
|
+ mock_session_cls.side_effect = self._create_mock_session_cls(mock_response)
|
|
|
+
|
|
|
+ with patch.object(LlmService, "get_api_key", return_value="test-key"):
|
|
|
+ with self.assertRaises(BizLogicError) as ctx:
|
|
|
+ await LlmService.parse_data_async(
|
|
|
+ user_text="test",
|
|
|
+ json_schema={"type": "object"},
|
|
|
+ max_retries=0,
|
|
|
+ )
|
|
|
+ self.assertIn("LLM request failed: HTTP 500", ctx.exception.message)
|