|
3 | 3 |
|
4 | 4 | from mock import patch |
5 | 5 |
|
| 6 | +from clipped.utils.paths import delete_path |
| 7 | + |
6 | 8 | from polyaxon import settings |
7 | 9 | from polyaxon._client.client import PolyaxonClient |
| 10 | +from polyaxon._contexts import paths as ctx_paths |
8 | 11 | from polyaxon._constants.globals import NO_AUTH |
9 | 12 | from polyaxon._schemas.client import ClientConfig |
10 | 13 | from polyaxon._sdk.api import ( |
| 14 | + AgentsV1Api, |
11 | 15 | AuthV1Api, |
12 | 16 | ProjectsV1Api, |
13 | 17 | RunsV1Api, |
14 | 18 | UsersV1Api, |
15 | | - VersionsV1Api, AgentsV1Api, |
| 19 | + VersionsV1Api, |
16 | 20 | ) |
17 | | -from polyaxon._utils.test_utils import BaseTestCase |
| 21 | +from polyaxon._utils.test_utils import BaseTestCase, patch_settings |
| 22 | +from polyaxon.exceptions import PolyaxonClientException |
| 23 | + |
| 24 | + |
| 25 | +class SDKClientMock: |
| 26 | + def __init__(self, name=None, events=None): |
| 27 | + self.name = name |
| 28 | + self.events = events |
| 29 | + self.close_calls = 0 |
| 30 | + |
| 31 | + def close(self): |
| 32 | + self.close_calls += 1 |
| 33 | + if self.events is not None: |
| 34 | + self.events.append("{}:close".format(self.name)) |
| 35 | + |
| 36 | + |
| 37 | +class AsyncSDKClientMock(SDKClientMock): |
| 38 | + async def close(self): |
| 39 | + self.close_calls += 1 |
| 40 | + if self.events is not None: |
| 41 | + self.events.append("{}:close".format(self.name)) |
| 42 | + |
| 43 | + |
| 44 | +def setup_async_test_settings(): |
| 45 | + delete_path(ctx_paths.CONTEXT_USER_POLYAXON_PATH) |
| 46 | + patch_settings() |
18 | 47 |
|
19 | 48 |
|
20 | 49 | @pytest.mark.client_mark |
@@ -104,3 +133,149 @@ def test_load_token(self): |
104 | 133 | assert client.config.is_managed is False |
105 | 134 | assert client.config.host == "http://localhost:8000" |
106 | 135 | assert client.config.token == "test2" |
| 136 | + |
| 137 | + def test_close_raises_on_async_instance(self): |
| 138 | + sdk_client = AsyncSDKClientMock() |
| 139 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 140 | + client = PolyaxonClient(is_async=True) |
| 141 | + |
| 142 | + with pytest.raises(PolyaxonClientException): |
| 143 | + client.close() |
| 144 | + |
| 145 | + def test_reset_raises_on_async_instance(self): |
| 146 | + sdk_client = AsyncSDKClientMock() |
| 147 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 148 | + client = PolyaxonClient(is_async=True) |
| 149 | + |
| 150 | + with pytest.raises(PolyaxonClientException): |
| 151 | + client.reset() |
| 152 | + |
| 153 | + def test_close_calls_api_client_close(self): |
| 154 | + sdk_client = SDKClientMock() |
| 155 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 156 | + client = PolyaxonClient() |
| 157 | + client.close() |
| 158 | + |
| 159 | + assert sdk_client.close_calls == 1 |
| 160 | + |
| 161 | + def test_reset_closes_previous_after_replacement(self): |
| 162 | + events = [] |
| 163 | + previous = SDKClientMock(name="previous", events=events) |
| 164 | + replacement = SDKClientMock(name="replacement", events=events) |
| 165 | + |
| 166 | + def get_client(client): |
| 167 | + if not events: |
| 168 | + events.append("previous:make") |
| 169 | + return previous |
| 170 | + events.append("replacement:make") |
| 171 | + return replacement |
| 172 | + |
| 173 | + with patch.object(PolyaxonClient, "_get_client", autospec=True) as mock_get: |
| 174 | + mock_get.side_effect = get_client |
| 175 | + client = PolyaxonClient() |
| 176 | + client.reset() |
| 177 | + |
| 178 | + assert client.api_client is replacement |
| 179 | + assert events == ["previous:make", "replacement:make", "previous:close"] |
| 180 | + |
| 181 | + def test_supports_sync_context_manager(self): |
| 182 | + sdk_client = SDKClientMock() |
| 183 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 184 | + with PolyaxonClient() as client: |
| 185 | + assert client.api_client is sdk_client |
| 186 | + |
| 187 | + assert sdk_client.close_calls == 1 |
| 188 | + |
| 189 | + def test_sync_context_manager_raises_on_async_instance(self): |
| 190 | + sdk_client = AsyncSDKClientMock() |
| 191 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 192 | + client = PolyaxonClient(is_async=True) |
| 193 | + |
| 194 | + with pytest.raises(PolyaxonClientException): |
| 195 | + with client: |
| 196 | + pass |
| 197 | + |
| 198 | + |
| 199 | +@pytest.mark.client_mark |
| 200 | +@pytest.mark.asyncio |
| 201 | +async def test_aclose_raises_on_sync_instance(): |
| 202 | + setup_async_test_settings() |
| 203 | + sdk_client = SDKClientMock() |
| 204 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 205 | + client = PolyaxonClient() |
| 206 | + |
| 207 | + with pytest.raises(PolyaxonClientException): |
| 208 | + await client.aclose() |
| 209 | + |
| 210 | + |
| 211 | +@pytest.mark.client_mark |
| 212 | +@pytest.mark.asyncio |
| 213 | +async def test_areset_raises_on_sync_instance(): |
| 214 | + setup_async_test_settings() |
| 215 | + sdk_client = SDKClientMock() |
| 216 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 217 | + client = PolyaxonClient() |
| 218 | + |
| 219 | + with pytest.raises(PolyaxonClientException): |
| 220 | + await client.areset() |
| 221 | + |
| 222 | + |
| 223 | +@pytest.mark.client_mark |
| 224 | +@pytest.mark.asyncio |
| 225 | +async def test_aclose_awaits_api_client_close(): |
| 226 | + setup_async_test_settings() |
| 227 | + sdk_client = AsyncSDKClientMock() |
| 228 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 229 | + client = PolyaxonClient(is_async=True) |
| 230 | + await client.aclose() |
| 231 | + |
| 232 | + assert sdk_client.close_calls == 1 |
| 233 | + |
| 234 | + |
| 235 | +@pytest.mark.client_mark |
| 236 | +@pytest.mark.asyncio |
| 237 | +async def test_areset_closes_previous_after_replacement(): |
| 238 | + setup_async_test_settings() |
| 239 | + events = [] |
| 240 | + previous = AsyncSDKClientMock(name="previous", events=events) |
| 241 | + replacement = AsyncSDKClientMock(name="replacement", events=events) |
| 242 | + |
| 243 | + def get_client(client): |
| 244 | + if not events: |
| 245 | + events.append("previous:make") |
| 246 | + return previous |
| 247 | + events.append("replacement:make") |
| 248 | + return replacement |
| 249 | + |
| 250 | + with patch.object(PolyaxonClient, "_get_client", autospec=True) as mock_get: |
| 251 | + mock_get.side_effect = get_client |
| 252 | + client = PolyaxonClient(is_async=True) |
| 253 | + await client.areset() |
| 254 | + |
| 255 | + assert client.api_client is replacement |
| 256 | + assert events == ["previous:make", "replacement:make", "previous:close"] |
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.client_mark |
| 260 | +@pytest.mark.asyncio |
| 261 | +async def test_supports_async_context_manager(): |
| 262 | + setup_async_test_settings() |
| 263 | + sdk_client = AsyncSDKClientMock() |
| 264 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 265 | + async with PolyaxonClient(is_async=True) as client: |
| 266 | + assert client.api_client is sdk_client |
| 267 | + |
| 268 | + assert sdk_client.close_calls == 1 |
| 269 | + |
| 270 | + |
| 271 | +@pytest.mark.client_mark |
| 272 | +@pytest.mark.asyncio |
| 273 | +async def test_async_context_manager_raises_on_sync_instance(): |
| 274 | + setup_async_test_settings() |
| 275 | + sdk_client = SDKClientMock() |
| 276 | + with patch.object(PolyaxonClient, "_get_client", return_value=sdk_client): |
| 277 | + client = PolyaxonClient() |
| 278 | + |
| 279 | + with pytest.raises(PolyaxonClientException): |
| 280 | + async with client: |
| 281 | + pass |
0 commit comments