|
| 1 | +from tableauserverclient.server.endpoint.fileuploads_endpoint import Fileuploads |
1 | 2 | import unittest
|
2 | 3 | from io import BytesIO
|
3 | 4 | import os
|
|
22 | 23 | PUBLISH_XML_ASYNC = 'datasource_publish_async.xml'
|
23 | 24 | REFRESH_XML = 'datasource_refresh.xml'
|
24 | 25 | UPDATE_XML = 'datasource_update.xml'
|
| 26 | +UPDATE_DATA_XML = 'datasource_data_update.xml' |
25 | 27 | UPDATE_CONNECTION_XML = 'datasource_connection_update.xml'
|
26 | 28 |
|
27 | 29 |
|
@@ -355,6 +357,86 @@ def test_refresh_object(self):
|
355 | 357 | # We only check the `id`; remaining fields are already tested in `test_refresh_id`
|
356 | 358 | self.assertEqual('7c3d599e-949f-44c3-94a1-f30ba85757e4', new_job.id)
|
357 | 359 |
|
| 360 | + def test_update_data_datasource_object(self): |
| 361 | + """Calling `update_data` with a `DatasourceItem` should update that datasource""" |
| 362 | + self.server.version = "3.13" |
| 363 | + self.baseurl = self.server.datasources.baseurl |
| 364 | + |
| 365 | + datasource = TSC.DatasourceItem('') |
| 366 | + datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' |
| 367 | + response_xml = read_xml_asset(UPDATE_DATA_XML) |
| 368 | + with requests_mock.mock() as m: |
| 369 | + m.patch(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/data', |
| 370 | + status_code=202, headers={"requestid": "test_id"}, text=response_xml) |
| 371 | + new_job = self.server.datasources.update_data(datasource, request_id="test_id", actions=[]) |
| 372 | + |
| 373 | + self.assertEqual('5c0ba560-c959-424e-b08a-f32ef0bfb737', new_job.id) |
| 374 | + self.assertEqual('UpdateUploadedFile', new_job.type) |
| 375 | + self.assertEqual(None, new_job.progress) |
| 376 | + self.assertEqual('2021-09-18T09:40:12Z', format_datetime(new_job.created_at)) |
| 377 | + self.assertEqual(-1, new_job.finish_code) |
| 378 | + |
| 379 | + def test_update_data_connection_object(self): |
| 380 | + """Calling `update_data` with a `ConnectionItem` should update that connection""" |
| 381 | + self.server.version = "3.13" |
| 382 | + self.baseurl = self.server.datasources.baseurl |
| 383 | + |
| 384 | + connection = TSC.ConnectionItem() |
| 385 | + connection._datasource_id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' |
| 386 | + connection._id = '7ecaccd8-39b0-4875-a77d-094f6e930019' |
| 387 | + response_xml = read_xml_asset(UPDATE_DATA_XML) |
| 388 | + with requests_mock.mock() as m: |
| 389 | + m.patch(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/connections/7ecaccd8-39b0-4875-a77d-094f6e930019/data', |
| 390 | + status_code=202, headers={"requestid": "test_id"}, text=response_xml) |
| 391 | + new_job = self.server.datasources.update_data(connection, request_id="test_id", actions=[]) |
| 392 | + |
| 393 | + # We only check the `id`; remaining fields are already tested in `test_update_data_datasource_object` |
| 394 | + self.assertEqual('5c0ba560-c959-424e-b08a-f32ef0bfb737', new_job.id) |
| 395 | + |
| 396 | + def test_update_data_datasource_string(self): |
| 397 | + """For convenience, calling `update_data` with a `str` should update the datasource with the corresponding UUID""" |
| 398 | + self.server.version = "3.13" |
| 399 | + self.baseurl = self.server.datasources.baseurl |
| 400 | + |
| 401 | + datasource_id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' |
| 402 | + response_xml = read_xml_asset(UPDATE_DATA_XML) |
| 403 | + with requests_mock.mock() as m: |
| 404 | + m.patch(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/data', |
| 405 | + status_code=202, headers={"requestid": "test_id"}, text=response_xml) |
| 406 | + new_job = self.server.datasources.update_data(datasource_id, request_id="test_id", actions=[]) |
| 407 | + |
| 408 | + # We only check the `id`; remaining fields are already tested in `test_update_data_datasource_object` |
| 409 | + self.assertEqual('5c0ba560-c959-424e-b08a-f32ef0bfb737', new_job.id) |
| 410 | + |
| 411 | + def test_update_data_datasource_payload_file(self): |
| 412 | + """If `payload` is present, we upload it and associate the job with it""" |
| 413 | + self.server.version = "3.13" |
| 414 | + self.baseurl = self.server.datasources.baseurl |
| 415 | + |
| 416 | + datasource_id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' |
| 417 | + mock_upload_id = '10051:c3e56879876842d4b3600f20c1f79876-0:0' |
| 418 | + response_xml = read_xml_asset(UPDATE_DATA_XML) |
| 419 | + with requests_mock.mock() as rm, \ |
| 420 | + unittest.mock.patch.object(Fileuploads, "upload", return_value=mock_upload_id): |
| 421 | + rm.patch(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/data?uploadSessionId=' + mock_upload_id, |
| 422 | + status_code=202, headers={"requestid": "test_id"}, text=response_xml) |
| 423 | + new_job = self.server.datasources.update_data(datasource_id, request_id="test_id", |
| 424 | + actions=[], payload=asset('World Indicators.hyper')) |
| 425 | + |
| 426 | + # We only check the `id`; remaining fields are already tested in `test_update_data_datasource_object` |
| 427 | + self.assertEqual('5c0ba560-c959-424e-b08a-f32ef0bfb737', new_job.id) |
| 428 | + |
| 429 | + def test_update_data_datasource_invalid_payload_file(self): |
| 430 | + """If `payload` points to a non-existing file, we report an error""" |
| 431 | + self.server.version = "3.13" |
| 432 | + self.baseurl = self.server.datasources.baseurl |
| 433 | + datasource_id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb' |
| 434 | + with self.assertRaises(IOError) as cm: |
| 435 | + self.server.datasources.update_data(datasource_id, request_id="test_id", |
| 436 | + actions=[], payload='no/such/file.missing') |
| 437 | + exception = cm.exception |
| 438 | + self.assertEqual(str(exception), "File path does not lead to an existing file.") |
| 439 | + |
358 | 440 | def test_delete(self):
|
359 | 441 | with requests_mock.mock() as m:
|
360 | 442 | m.delete(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', status_code=204)
|
|
0 commit comments