2025-11-04 05:57:23 +01:00
|
|
|
import unittest
|
|
|
|
|
import urllib.error
|
2025-11-04 08:09:12 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
from pr.core.api import call_api, list_models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestApi(unittest.TestCase):
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("pr.core.api.urllib.request.urlopen")
|
|
|
|
|
@patch("pr.core.api.auto_slim_messages")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_call_api_success(self, mock_slim, mock_urlopen):
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_slim.return_value = [{"role": "user", "content": "test"}]
|
2025-11-04 05:57:23 +01:00
|
|
|
mock_response = MagicMock()
|
2025-11-04 08:10:37 +01:00
|
|
|
mock_response.read.return_value = (
|
|
|
|
|
b'{"choices": [{"message": {"content": "response"}}], "usage": {"tokens": 10}}'
|
|
|
|
|
)
|
2025-11-04 05:57:23 +01:00
|
|
|
mock_urlopen.return_value.__enter__.return_value = mock_response
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
result = call_api([], "model", "http://url", "key", True, [{"name": "tool"}])
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
self.assertIn("choices", result)
|
2025-11-04 05:57:23 +01:00
|
|
|
mock_urlopen.assert_called_once()
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("urllib.request.urlopen")
|
|
|
|
|
@patch("pr.core.api.auto_slim_messages")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_call_api_http_error(self, mock_slim, mock_urlopen):
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_slim.return_value = [{"role": "user", "content": "test"}]
|
|
|
|
|
mock_urlopen.side_effect = urllib.error.HTTPError(
|
|
|
|
|
"http://url", 500, "error", None, MagicMock()
|
|
|
|
|
)
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
result = call_api([], "model", "http://url", "key", False, [])
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
self.assertIn("error", result)
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("urllib.request.urlopen")
|
|
|
|
|
@patch("pr.core.api.auto_slim_messages")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_call_api_general_error(self, mock_slim, mock_urlopen):
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_slim.return_value = [{"role": "user", "content": "test"}]
|
|
|
|
|
mock_urlopen.side_effect = Exception("test error")
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
result = call_api([], "model", "http://url", "key", False, [])
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
self.assertIn("error", result)
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("urllib.request.urlopen")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_list_models_success(self, mock_urlopen):
|
|
|
|
|
mock_response = MagicMock()
|
|
|
|
|
mock_response.read.return_value = b'{"data": [{"id": "model1"}]}'
|
|
|
|
|
mock_urlopen.return_value.__enter__.return_value = mock_response
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
result = list_models("http://url", "key")
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
self.assertEqual(result, [{"id": "model1"}])
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("urllib.request.urlopen")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_list_models_error(self, mock_urlopen):
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_urlopen.side_effect = Exception("error")
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
result = list_models("http://url", "key")
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
self.assertIn("error", result)
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
if __name__ == "__main__":
|
2025-11-04 05:57:23 +01:00
|
|
|
unittest.main()
|