forked from eval-protocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_platform_api.py
More file actions
166 lines (134 loc) · 7.3 KB
/
Copy pathtest_platform_api.py
File metadata and controls
166 lines (134 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import unittest
from unittest.mock import MagicMock, patch
import requests
from eval_protocol import auth
# Removed import of register_evaluator_with_remote_url and REMOTE_EVALUATOR_REGISTRATION_ENDPOINT_TEMPLATE
from eval_protocol.platform_api import PlatformAPIError
# Store original functions
original_get_fireworks_api_key = auth.get_fireworks_api_key
original_get_fireworks_account_id = auth.get_fireworks_account_id
original_get_fireworks_api_base = auth.get_fireworks_api_base
class TestPlatformAPI(unittest.TestCase):
def setUp(self):
# Patch auth functions for isolation
self.mock_api_key = "test_api_key"
self.mock_account_id = "test_account_id"
self.mock_api_base = "https://mockapi.fireworks.ai"
auth.get_fireworks_api_key = MagicMock(return_value=self.mock_api_key)
auth.get_fireworks_account_id = MagicMock(return_value=self.mock_account_id)
auth.get_fireworks_api_base = MagicMock(return_value=self.mock_api_base)
self.evaluator_id = "test-evaluator"
self.remote_url = "https://my-eval.example.com/evaluate"
# self.expected_endpoint_path = REMOTE_EVALUATOR_REGISTRATION_ENDPOINT_TEMPLATE.format(evaluator_id=self.evaluator_id) # Commented out
# self.full_expected_url = f"{self.mock_api_base}{self.expected_endpoint_path}" # Commented out
def tearDown(self):
# Restore original auth functions
auth.get_fireworks_api_key = original_get_fireworks_api_key
auth.get_fireworks_account_id = original_get_fireworks_account_id
auth.get_fireworks_api_base = original_get_fireworks_api_base
# @patch('requests.put')
# def test_register_evaluator_successful(self, mock_put):
# mock_response = MagicMock()
# mock_response.status_code = 200
# mock_response.json.return_value = {"status": "success", "id": self.evaluator_id}
# mock_put.return_value = mock_response
# result = register_evaluator_with_remote_url(self.evaluator_id, self.remote_url)
# mock_put.assert_called_once_with(
# self.full_expected_url,
# headers={
# "Authorization": f"Bearer {self.mock_api_key}",
# "Content-Type": "application/json",
# "X-Account-ID": self.mock_account_id,
# },
# json={"remote_url": self.remote_url},
# timeout=30
# )
# self.assertEqual(result, {"status": "success", "id": self.evaluator_id})
# @patch('requests.put')
# def test_register_evaluator_http_error(self, mock_put):
# mock_response = MagicMock()
# mock_response.status_code = 400
# mock_response.text = "Bad Request"
# mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(response=mock_response)
# mock_put.return_value = mock_response
# with self.assertRaises(PlatformAPIError) as cm:
# register_evaluator_with_remote_url(self.evaluator_id, self.remote_url)
# self.assertEqual(cm.exception.status_code, 400)
# self.assertIn("Failed to register/update evaluator", str(cm.exception))
# self.assertIn("Bad Request", str(cm.exception))
# @patch('requests.put')
# def test_register_evaluator_request_exception(self, mock_put):
# mock_put.side_effect = requests.exceptions.ConnectionError("Connection failed")
# with self.assertRaises(PlatformAPIError) as cm:
# register_evaluator_with_remote_url(self.evaluator_id, self.remote_url)
# self.assertIn("Network or request error", str(cm.exception))
# self.assertIsNone(cm.exception.status_code) # No status code for connection error typically
# def test_missing_api_key(self):
# auth.get_fireworks_api_key = MagicMock(return_value=None)
# with self.assertRaises(PlatformAPIError) as cm:
# register_evaluator_with_remote_url(self.evaluator_id, self.remote_url)
# self.assertIn("Fireworks API key not found", str(cm.exception))
# # Restore for other tests if run in same instance
# auth.get_fireworks_api_key = MagicMock(return_value=self.mock_api_key)
# def test_missing_api_base(self):
# auth.get_fireworks_api_base = MagicMock(return_value=None)
# with self.assertRaises(PlatformAPIError) as cm:
# register_evaluator_with_remote_url(self.evaluator_id, self.remote_url)
# self.assertIn("Fireworks API base URL not found", str(cm.exception))
# # Restore
# auth.get_fireworks_api_base = MagicMock(return_value=self.mock_api_base)
# @patch('requests.put')
# @patch('eval_protocol.platform_api.logger.warning')
# def test_missing_account_id_logs_warning_but_proceeds(self, mock_logger_warning, mock_put):
# auth.get_fireworks_account_id = MagicMock(return_value=None)
# mock_response = MagicMock()
# mock_response.status_code = 200
# mock_response.json.return_value = {"status": "success_no_account_id"}
# mock_put.return_value = mock_response
# register_evaluator_with_remote_url(self.evaluator_id, self.remote_url)
# mock_logger_warning.assert_called_once_with(
# "Fireworks Account ID not found or provided. Proceeding without it if API allows."
# )
# mock_put.assert_called_once()
# # Check that X-Account-ID header is None or not present if that's the desired behavior
# # Current implementation sends "X-Account-ID": None
# called_headers = mock_put.call_args[1]['headers']
# self.assertIsNone(called_headers["X-Account-ID"])
# # Restore
# auth.get_fireworks_account_id = MagicMock(return_value=self.mock_account_id)
# @patch('requests.put')
# def test_register_evaluator_with_direct_creds(self, mock_put):
# mock_response = MagicMock()
# mock_response.status_code = 200
# mock_response.json.return_value = {"status": "success_direct_creds"}
# mock_put.return_value = mock_response
# direct_api_key = "direct_key"
# direct_account_id = "direct_account"
# direct_api_base = "https://direct.api.com"
# register_evaluator_with_remote_url(
# self.evaluator_id,
# self.remote_url,
# api_key=direct_api_key,
# account_id=direct_account_id,
# api_base=direct_api_base
# )
# expected_full_url = f"{direct_api_base}{self.expected_endpoint_path}" # This would cause error as self.expected_endpoint_path is commented
# mock_put.assert_called_once_with(
# expected_full_url,
# headers={
# "Authorization": f"Bearer {direct_api_key}",
# "Content-Type": "application/json",
# "X-Account-ID": direct_account_id,
# },
# json={"remote_url": self.remote_url},
# timeout=30
# )
# # Ensure original mocks for auth.get_... were not called
# auth.get_fireworks_api_key.assert_called_once() # Called during setUp
# auth.get_fireworks_account_id.assert_called_once()
# auth.get_fireworks_api_base.assert_called_once()
def test_placeholder_to_allow_collection(self):
"""This is a placeholder test to ensure the file is collected by pytest."""
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()