|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from unstructured_client import UnstructuredClient |
| 6 | +from unstructured_client.models import shared, operations |
| 7 | +from unstructured_client.models.errors import SDKError |
| 8 | +from unstructured_client.models.shared import ( |
| 9 | + DestinationConnectorInformationConfig, |
| 10 | + DestinationConnectorType, |
| 11 | + Config, |
| 12 | + UpdateDestinationConnectorConfig, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +def test_list_destinations( |
| 17 | + httpx_mock, client: UnstructuredClient, platform_api_url: str |
| 18 | +): |
| 19 | + url = f"{platform_api_url}/api/v1/destinations/" |
| 20 | + |
| 21 | + httpx_mock.add_response( |
| 22 | + method="GET", |
| 23 | + headers={"Content-Type": "application/json"}, |
| 24 | + json=[ |
| 25 | + { |
| 26 | + "config": {}, |
| 27 | + "created_at": "2025-08-22T08:47:29.802Z", |
| 28 | + "id": "0c363dec-3c70-45ee-8041-481044a6e1cc", |
| 29 | + "name": "test_destination_name", |
| 30 | + "type": "s3", |
| 31 | + } |
| 32 | + ], |
| 33 | + url=url, |
| 34 | + ) |
| 35 | + |
| 36 | + destinations_response = client.destinations.list_destinations( |
| 37 | + request=operations.ListDestinationsRequest() |
| 38 | + ) |
| 39 | + assert destinations_response.status_code == 200 |
| 40 | + |
| 41 | + requests = httpx_mock.get_requests() |
| 42 | + assert len(requests) == 1 |
| 43 | + request = requests[0] |
| 44 | + assert request.method == "GET" |
| 45 | + assert request.url == url |
| 46 | + |
| 47 | + assert len(destinations_response.response_list_destinations) == 1 |
| 48 | + destination = destinations_response.response_list_destinations[0] |
| 49 | + assert destination.id == "0c363dec-3c70-45ee-8041-481044a6e1cc" |
| 50 | + assert destination.name == "test_destination_name" |
| 51 | + assert destination.type == "s3" |
| 52 | + assert destination.config == DestinationConnectorInformationConfig() |
| 53 | + assert destination.created_at == datetime.fromisoformat( |
| 54 | + "2025-08-22T08:47:29.802+00:00" |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_list_destinations_empty( |
| 59 | + httpx_mock, client: UnstructuredClient, platform_api_url: str |
| 60 | +): |
| 61 | + url = f"{platform_api_url}/api/v1/destinations/" |
| 62 | + |
| 63 | + httpx_mock.add_response( |
| 64 | + method="GET", |
| 65 | + headers={"Content-Type": "application/json"}, |
| 66 | + json=[], |
| 67 | + url=url, |
| 68 | + ) |
| 69 | + |
| 70 | + destinations_response = client.destinations.list_destinations( |
| 71 | + request=operations.ListDestinationsRequest() |
| 72 | + ) |
| 73 | + assert destinations_response.status_code == 200 |
| 74 | + |
| 75 | + requests = httpx_mock.get_requests() |
| 76 | + assert len(requests) == 1 |
| 77 | + request = requests[0] |
| 78 | + assert request.method == "GET" |
| 79 | + assert request.url == url |
| 80 | + |
| 81 | + assert len(destinations_response.response_list_destinations) == 0 |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "error_status_code", |
| 86 | + [400, 401, 403, 404, 500, 502, 503, 504], |
| 87 | +) |
| 88 | +@pytest.mark.httpx_mock(can_send_already_matched_responses=True) # in case of retries |
| 89 | +def test_list_destinations_5xx_code( |
| 90 | + httpx_mock, |
| 91 | + client: UnstructuredClient, |
| 92 | + platform_api_url: str, |
| 93 | + error_status_code: int, |
| 94 | +): |
| 95 | + url = f"{platform_api_url}/api/v1/destinations/" |
| 96 | + |
| 97 | + httpx_mock.add_response( |
| 98 | + method="GET", |
| 99 | + headers={"Content-Type": "application/json"}, |
| 100 | + status_code=error_status_code, |
| 101 | + url=url, |
| 102 | + ) |
| 103 | + |
| 104 | + with pytest.raises(SDKError) as excinfo: |
| 105 | + client.destinations.list_destinations( |
| 106 | + request=operations.ListDestinationsRequest() |
| 107 | + ) |
| 108 | + requests = httpx_mock.get_requests() |
| 109 | + assert len(requests) >= 1 |
| 110 | + assert excinfo.value.message == "API error occurred" |
| 111 | + assert excinfo.value.status_code == error_status_code |
| 112 | + |
| 113 | + |
| 114 | +def test_get_destination(httpx_mock, client: UnstructuredClient, platform_api_url: str): |
| 115 | + url = f"{platform_api_url}/api/v1/destinations/1" |
| 116 | + |
| 117 | + httpx_mock.add_response( |
| 118 | + method="GET", |
| 119 | + headers={"Content-Type": "application/json"}, |
| 120 | + json={ |
| 121 | + "config": {}, |
| 122 | + "created_at": "2025-08-22T08:47:29.802Z", |
| 123 | + "id": "0c363dec-3c70-45ee-8041-481044a6e1cc", |
| 124 | + "name": "test_destination_name", |
| 125 | + "type": "s3", |
| 126 | + }, |
| 127 | + url=url, |
| 128 | + ) |
| 129 | + |
| 130 | + destination_response = client.destinations.get_destination( |
| 131 | + request=operations.GetDestinationRequest(destination_id="1") |
| 132 | + ) |
| 133 | + assert destination_response.status_code == 200 |
| 134 | + |
| 135 | + requests = httpx_mock.get_requests() |
| 136 | + assert len(requests) == 1 |
| 137 | + request = requests[0] |
| 138 | + assert request.method == "GET" |
| 139 | + assert request.url == url |
| 140 | + |
| 141 | + destination = destination_response.destination_connector_information |
| 142 | + assert destination.id == "0c363dec-3c70-45ee-8041-481044a6e1cc" |
| 143 | + assert destination.name == "test_destination_name" |
| 144 | + assert destination.type == "s3" |
| 145 | + assert destination.config == DestinationConnectorInformationConfig() |
| 146 | + assert destination.created_at == datetime.fromisoformat( |
| 147 | + "2025-08-22T08:47:29.802+00:00" |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +def test_get_destination_not_found( |
| 152 | + httpx_mock, client: UnstructuredClient, platform_api_url: str |
| 153 | +): |
| 154 | + url = f"{platform_api_url}/api/v1/destinations/1" |
| 155 | + |
| 156 | + httpx_mock.add_response( |
| 157 | + method="GET", |
| 158 | + headers={"Content-Type": "application/json"}, |
| 159 | + status_code=404, |
| 160 | + url=url, |
| 161 | + ) |
| 162 | + |
| 163 | + with pytest.raises(SDKError) as excinfo: |
| 164 | + client.destinations.get_destination( |
| 165 | + request=operations.GetDestinationRequest(destination_id="1") |
| 166 | + ) |
| 167 | + |
| 168 | + requests = httpx_mock.get_requests() |
| 169 | + assert len(requests) == 1 |
| 170 | + assert excinfo.value.message == "API error occurred" |
| 171 | + assert excinfo.value.status_code == 404 |
| 172 | + |
| 173 | + |
| 174 | +def test_create_destination( |
| 175 | + httpx_mock, client: UnstructuredClient, platform_api_url: str |
| 176 | +): |
| 177 | + url = f"{platform_api_url}/api/v1/destinations/" |
| 178 | + |
| 179 | + httpx_mock.add_response( |
| 180 | + method="POST", |
| 181 | + headers={"Content-Type": "application/json"}, |
| 182 | + json={ |
| 183 | + "config": {}, |
| 184 | + "created_at": "2023-09-15T01:06:53.146Z", |
| 185 | + "id": "b25d4161-77a0-4e08-b65e-86f398ce15ad", |
| 186 | + "name": "test_destination_name", |
| 187 | + "type": "s3", |
| 188 | + }, |
| 189 | + url=url, |
| 190 | + ) |
| 191 | + |
| 192 | + destination_response = client.destinations.create_destination( |
| 193 | + request=operations.CreateDestinationRequest( |
| 194 | + create_destination_connector=shared.CreateDestinationConnector( |
| 195 | + name="test_destination_name", |
| 196 | + type=DestinationConnectorType.S3, |
| 197 | + config=Config(), |
| 198 | + ) |
| 199 | + ) |
| 200 | + ) |
| 201 | + assert destination_response.status_code == 200 |
| 202 | + |
| 203 | + requests = httpx_mock.get_requests() |
| 204 | + assert len(requests) == 1 |
| 205 | + request = requests[0] |
| 206 | + assert request.method == "POST" |
| 207 | + assert request.url == url |
| 208 | + |
| 209 | + destination = destination_response.destination_connector_information |
| 210 | + assert destination.id == "b25d4161-77a0-4e08-b65e-86f398ce15ad" |
| 211 | + assert destination.name == "test_destination_name" |
| 212 | + assert destination.type == "s3" |
| 213 | + assert destination.config == DestinationConnectorInformationConfig() |
| 214 | + assert destination.created_at == datetime.fromisoformat( |
| 215 | + "2023-09-15T01:06:53.146+00:00" |
| 216 | + ) |
| 217 | + |
| 218 | + |
| 219 | +def test_update_destination( |
| 220 | + httpx_mock, client: UnstructuredClient, platform_api_url: str |
| 221 | +): |
| 222 | + url = f"{platform_api_url}/api/v1/destinations/1" |
| 223 | + |
| 224 | + httpx_mock.add_response( |
| 225 | + method="PUT", |
| 226 | + headers={"Content-Type": "application/json"}, |
| 227 | + json={ |
| 228 | + "config": {}, |
| 229 | + "created_at": "2023-09-15T01:06:53.146Z", |
| 230 | + "id": "b25d4161-77a0-4e08-b65e-86f398ce15ad", |
| 231 | + "name": "test_destination_name", |
| 232 | + "type": "s3", |
| 233 | + }, |
| 234 | + url=url, |
| 235 | + ) |
| 236 | + |
| 237 | + destination_update_response = client.destinations.update_destination( |
| 238 | + request=operations.UpdateDestinationRequest( |
| 239 | + destination_id="1", |
| 240 | + update_destination_connector=shared.UpdateDestinationConnector( |
| 241 | + config=UpdateDestinationConnectorConfig() |
| 242 | + ), |
| 243 | + ) |
| 244 | + ) |
| 245 | + |
| 246 | + destination_update_response.status_code == 200 |
| 247 | + |
| 248 | + requests = httpx_mock.get_requests() |
| 249 | + assert len(requests) == 1 |
| 250 | + request = requests[0] |
| 251 | + assert request.method == "PUT" |
| 252 | + assert request.url == url |
| 253 | + |
| 254 | + updated_destination = destination_update_response.destination_connector_information |
| 255 | + assert updated_destination.id == "b25d4161-77a0-4e08-b65e-86f398ce15ad" |
| 256 | + assert updated_destination.name == "test_destination_name" |
| 257 | + assert updated_destination.type == "s3" |
| 258 | + assert updated_destination.config == DestinationConnectorInformationConfig() |
| 259 | + assert updated_destination.created_at == datetime.fromisoformat( |
| 260 | + "2023-09-15T01:06:53.146+00:00" |
| 261 | + ) |
| 262 | + |
| 263 | + |
| 264 | +def test_delete_destination( |
| 265 | + httpx_mock, client: UnstructuredClient, platform_api_url: str |
| 266 | +): |
| 267 | + url = f"{platform_api_url}/api/v1/destinations/1" |
| 268 | + |
| 269 | + httpx_mock.add_response( |
| 270 | + method="DELETE", |
| 271 | + headers={"Content-Type": "application/json"}, |
| 272 | + status_code=200, |
| 273 | + json={"detail": "Destination with id 1 successfully deleted."}, |
| 274 | + url=url, |
| 275 | + ) |
| 276 | + |
| 277 | + response = client.destinations.delete_destination( |
| 278 | + request=operations.DeleteDestinationRequest(destination_id="1") |
| 279 | + ) |
| 280 | + assert response.status_code == 200 |
| 281 | + |
| 282 | + requests = httpx_mock.get_requests() |
| 283 | + assert len(requests) == 1 |
| 284 | + request = requests[0] |
| 285 | + assert request.method == "DELETE" |
| 286 | + assert request.url == url |
0 commit comments