|
| 1 | +# Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT. |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from typing import Any, Dict |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import databricks.sdk.core as client |
| 9 | +from databricks.sdk.common import lro |
| 10 | +from tests.databricks.sdk.service.lrotesting import ( |
| 11 | + DatabricksServiceExceptionWithDetailsProto, ErrorCode, LroTestingAPI, |
| 12 | + Operation, TestResource, TestResourceOperationMetadata) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "fixtures,want_result,want_err", |
| 17 | + [ |
| 18 | + pytest.param( |
| 19 | + [ |
| 20 | + { |
| 21 | + "method": "POST", |
| 22 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 23 | + "response": Operation( |
| 24 | + done=False, |
| 25 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 26 | + name="operations/test-resource-create-12345", |
| 27 | + ), |
| 28 | + }, |
| 29 | + { |
| 30 | + "method": "GET", |
| 31 | + "url": "http://localhost/api/2.0/lro-testing/operations/operations/test-resource-create-12345", |
| 32 | + "response": Operation( |
| 33 | + done=False, |
| 34 | + metadata={"resource_id": "test-resource-123", "progress_percent": 75}, |
| 35 | + name="operations/test-resource-create-12345", |
| 36 | + ), |
| 37 | + }, |
| 38 | + { |
| 39 | + "method": "GET", |
| 40 | + "url": "http://localhost/api/2.0/lro-testing/operations/operations/test-resource-create-12345", |
| 41 | + "response": Operation( |
| 42 | + done=True, |
| 43 | + metadata={"resource_id": "test-resource-123", "progress_percent": 100}, |
| 44 | + name="operations/test-resource-create-12345", |
| 45 | + response={"id": "test-resource-123", "name": "test-resource"}, |
| 46 | + ), |
| 47 | + }, |
| 48 | + ], |
| 49 | + TestResource( |
| 50 | + id="test-resource-123", |
| 51 | + name="test-resource", |
| 52 | + ), |
| 53 | + False, |
| 54 | + id="Success", |
| 55 | + ), |
| 56 | + pytest.param( |
| 57 | + [ |
| 58 | + { |
| 59 | + "method": "POST", |
| 60 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 61 | + "response": Operation( |
| 62 | + done=False, |
| 63 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 64 | + name="operations/test-resource-create-12345", |
| 65 | + ), |
| 66 | + }, |
| 67 | + { |
| 68 | + "method": "GET", |
| 69 | + "url": "http://localhost/api/2.0/lro-testing/operations/operations/test-resource-create-12345", |
| 70 | + "response": Operation( |
| 71 | + done=True, |
| 72 | + error=DatabricksServiceExceptionWithDetailsProto( |
| 73 | + error_code=ErrorCode.INTERNAL_ERROR, |
| 74 | + message="Test error message", |
| 75 | + ), |
| 76 | + name="operations/test-resource-create-12345", |
| 77 | + ), |
| 78 | + }, |
| 79 | + ], |
| 80 | + None, |
| 81 | + True, |
| 82 | + id="Error", |
| 83 | + ), |
| 84 | + ], |
| 85 | +) |
| 86 | +def test_lro_create_test_resource_wait( |
| 87 | + config, requests_mock, fixtures: Dict[str, Any], want_result: TestResource, want_err: bool |
| 88 | +): |
| 89 | + for fixture in fixtures: |
| 90 | + method = getattr(requests_mock, fixture["method"].lower()) |
| 91 | + assert isinstance(fixture["response"], Operation) |
| 92 | + method(fixture["url"], json=fixture["response"].as_dict()) |
| 93 | + |
| 94 | + api_client = client.ApiClient(config) |
| 95 | + service = LroTestingAPI(api_client) |
| 96 | + lro_op = service.create_test_resource(resource=TestResource()) |
| 97 | + |
| 98 | + if want_err: |
| 99 | + with pytest.raises(Exception): |
| 100 | + lro_op.wait(opts=lro.LroOptions(timeout=timedelta(minutes=1))) |
| 101 | + else: |
| 102 | + result = lro_op.wait(opts=lro.LroOptions(timeout=timedelta(minutes=1))) |
| 103 | + assert result == want_result |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + "fixtures,want_err", |
| 108 | + [ |
| 109 | + pytest.param( |
| 110 | + [ |
| 111 | + { |
| 112 | + "method": "POST", |
| 113 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 114 | + "response": Operation( |
| 115 | + done=False, |
| 116 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 117 | + name="operations/test-resource-create-12345", |
| 118 | + ), |
| 119 | + }, |
| 120 | + { |
| 121 | + "method": "POST", |
| 122 | + "url": "http://localhost/api/2.0/lro-testing/operations/operations/test-resource-create-12345/cancel", |
| 123 | + "response": Operation( |
| 124 | + done=True, |
| 125 | + name="operations/test-resource-create-12345", |
| 126 | + ), |
| 127 | + }, |
| 128 | + ], |
| 129 | + False, |
| 130 | + id="Success", |
| 131 | + ), |
| 132 | + ], |
| 133 | +) |
| 134 | +def test_lro_cancel_test_resource_cancel(config, requests_mock, fixtures: Dict[str, Any], want_err: bool): |
| 135 | + for fixture in fixtures: |
| 136 | + method = getattr(requests_mock, fixture["method"].lower()) |
| 137 | + method(fixture["url"], json=fixture["response"].as_dict()) |
| 138 | + |
| 139 | + api_client = client.ApiClient(config) |
| 140 | + service = LroTestingAPI(api_client) |
| 141 | + lro_op = service.create_test_resource(resource=TestResource()) |
| 142 | + |
| 143 | + if want_err: |
| 144 | + with pytest.raises(Exception): |
| 145 | + lro_op.cancel() |
| 146 | + else: |
| 147 | + lro_op.cancel() |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.parametrize( |
| 151 | + "fixtures,want_name", |
| 152 | + [ |
| 153 | + pytest.param( |
| 154 | + [ |
| 155 | + { |
| 156 | + "method": "POST", |
| 157 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 158 | + "response": Operation( |
| 159 | + done=False, |
| 160 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 161 | + name="operations/test-resource-create-12345", |
| 162 | + ), |
| 163 | + }, |
| 164 | + ], |
| 165 | + "operations/test-resource-create-12345", |
| 166 | + id="Success", |
| 167 | + ), |
| 168 | + ], |
| 169 | +) |
| 170 | +def test_lro_create_test_resource_name(config, requests_mock, fixtures: Dict[str, Any], want_name: str): |
| 171 | + for fixture in fixtures: |
| 172 | + method = getattr(requests_mock, fixture["method"].lower()) |
| 173 | + method(fixture["url"], json=fixture["response"].as_dict()) |
| 174 | + |
| 175 | + api_client = client.ApiClient(config) |
| 176 | + service = LroTestingAPI(api_client) |
| 177 | + lro_op = service.create_test_resource(resource=TestResource()) |
| 178 | + |
| 179 | + name = lro_op.name() |
| 180 | + assert name == want_name |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.parametrize( |
| 184 | + "fixtures,want_metadata,want_err", |
| 185 | + [ |
| 186 | + pytest.param( |
| 187 | + [ |
| 188 | + { |
| 189 | + "method": "POST", |
| 190 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 191 | + "response": Operation( |
| 192 | + done=False, |
| 193 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 194 | + name="operations/test-resource-create-12345", |
| 195 | + ), |
| 196 | + }, |
| 197 | + ], |
| 198 | + TestResourceOperationMetadata( |
| 199 | + progress_percent=5, |
| 200 | + resource_id="test-resource-123", |
| 201 | + ), |
| 202 | + False, |
| 203 | + id="Success", |
| 204 | + ), |
| 205 | + ], |
| 206 | +) |
| 207 | +def test_lro_create_test_resource_metadata( |
| 208 | + config, requests_mock, fixtures: Dict[str, Any], want_metadata: TestResourceOperationMetadata, want_err: bool |
| 209 | +): |
| 210 | + for fixture in fixtures: |
| 211 | + method = getattr(requests_mock, fixture["method"].lower()) |
| 212 | + method(fixture["url"], json=fixture["response"].as_dict()) |
| 213 | + |
| 214 | + api_client = client.ApiClient(config) |
| 215 | + service = LroTestingAPI(api_client) |
| 216 | + lro_op = service.create_test_resource(resource=TestResource()) |
| 217 | + |
| 218 | + if want_err: |
| 219 | + with pytest.raises(Exception): |
| 220 | + lro_op.metadata() |
| 221 | + else: |
| 222 | + metadata = lro_op.metadata() |
| 223 | + assert metadata == want_metadata |
| 224 | + |
| 225 | + |
| 226 | +@pytest.mark.parametrize( |
| 227 | + "fixtures,want_done,want_err", |
| 228 | + [ |
| 229 | + pytest.param( |
| 230 | + [ |
| 231 | + { |
| 232 | + "method": "POST", |
| 233 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 234 | + "response": Operation( |
| 235 | + done=False, |
| 236 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 237 | + name="operations/test-resource-create-12345", |
| 238 | + ), |
| 239 | + }, |
| 240 | + { |
| 241 | + "method": "GET", |
| 242 | + "url": "http://localhost/api/2.0/lro-testing/operations/operations/test-resource-create-12345", |
| 243 | + "response": Operation( |
| 244 | + done=True, |
| 245 | + metadata={"resource_id": "test-resource-123", "progress_percent": 100}, |
| 246 | + name="operations/test-resource-create-12345", |
| 247 | + response={"id": "test-resource-123", "name": "test-resource"}, |
| 248 | + ), |
| 249 | + }, |
| 250 | + ], |
| 251 | + True, |
| 252 | + False, |
| 253 | + id="True", |
| 254 | + ), |
| 255 | + pytest.param( |
| 256 | + [ |
| 257 | + { |
| 258 | + "method": "POST", |
| 259 | + "url": "http://localhost/api/2.0/lro-testing/resources", |
| 260 | + "response": Operation( |
| 261 | + done=False, |
| 262 | + metadata={"resource_id": "test-resource-123", "progress_percent": 5}, |
| 263 | + name="operations/test-resource-create-12345", |
| 264 | + ), |
| 265 | + }, |
| 266 | + { |
| 267 | + "method": "GET", |
| 268 | + "url": "http://localhost/api/2.0/lro-testing/operations/operations/test-resource-create-12345", |
| 269 | + "response": Operation( |
| 270 | + done=False, |
| 271 | + metadata={"resource_id": "test-resource-123", "progress_percent": 75}, |
| 272 | + name="operations/test-resource-create-12345", |
| 273 | + ), |
| 274 | + }, |
| 275 | + ], |
| 276 | + False, |
| 277 | + False, |
| 278 | + id="False", |
| 279 | + ), |
| 280 | + ], |
| 281 | +) |
| 282 | +def test_lro_create_test_resource_done( |
| 283 | + config, requests_mock, fixtures: Dict[str, Any], want_done: bool, want_err: bool |
| 284 | +): |
| 285 | + for fixture in fixtures: |
| 286 | + method = getattr(requests_mock, fixture["method"].lower()) |
| 287 | + method(fixture["url"], json=fixture["response"].as_dict()) |
| 288 | + |
| 289 | + api_client = client.ApiClient(config) |
| 290 | + service = LroTestingAPI(api_client) |
| 291 | + lro_op = service.create_test_resource(resource=TestResource()) |
| 292 | + |
| 293 | + if want_err: |
| 294 | + with pytest.raises(Exception): |
| 295 | + lro_op.done() |
| 296 | + else: |
| 297 | + done = lro_op.done() |
| 298 | + assert done == want_done |
0 commit comments