70 lines
2.4 KiB
Python
Raw Normal View History

2025-11-04 05:57:23 +01:00
import unittest
import json
from unittest.mock import patch
2025-11-04 08:09:12 +01:00
from rp.core.api import call_api, list_models
2025-11-04 05:57:23 +01:00
class TestApi(unittest.TestCase):
@patch("rp.core.http_client.http_client.post")
@patch("rp.core.api.auto_slim_messages")
def test_call_api_success(self, mock_slim, mock_post):
2025-11-04 08:09:12 +01:00
mock_slim.return_value = [{"role": "user", "content": "test"}]
mock_post.return_value = {
"status": 200,
"text": '{"choices": [{"message": {"content": "response"}}], "usage": {"tokens": 10}}',
"json": lambda: json.loads(
'{"choices": [{"message": {"content": "response"}}], "usage": {"tokens": 10}}'
),
}
2025-11-04 05:57:23 +01:00
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)
mock_post.assert_called_once()
2025-11-04 05:57:23 +01:00
@patch("rp.core.http_client.http_client.post")
@patch("rp.core.api.auto_slim_messages")
def test_call_api_http_error(self, mock_slim, mock_post):
2025-11-04 08:09:12 +01:00
mock_slim.return_value = [{"role": "user", "content": "test"}]
mock_post.return_value = {"error": True, "status": 500, "text": "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
@patch("rp.core.http_client.http_client.post")
@patch("rp.core.api.auto_slim_messages")
def test_call_api_general_error(self, mock_slim, mock_post):
2025-11-04 08:09:12 +01:00
mock_slim.return_value = [{"role": "user", "content": "test"}]
mock_post.return_value = {"error": True, "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
@patch("rp.core.http_client.http_client.get")
def test_list_models_success(self, mock_get):
mock_get.return_value = {
"status": 200,
"text": '{"data": [{"id": "model1"}]}',
"json": lambda: json.loads('{"data": [{"id": "model1"}]}'),
}
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.assertEqual(result, [{"id": "model1"}])
2025-11-04 05:57:23 +01:00
@patch("rp.core.http_client.http_client.get")
def test_list_models_error(self, mock_get):
mock_get.return_value = {"error": True, "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()