|
| 1 | +from pathlib import Path |
| 2 | +import unittest |
| 3 | + |
| 4 | +from defusedxml.ElementTree import fromstring |
| 5 | +import pytest |
| 6 | +import requests_mock |
| 7 | + |
| 8 | +import tableauserverclient as TSC |
| 9 | +from tableauserverclient.datetime_helpers import parse_datetime |
| 10 | +from tableauserverclient.models.linked_tasks_item import LinkedTaskItem, LinkedTaskStepItem, LinkedTaskFlowRunItem |
| 11 | + |
| 12 | +asset_dir = (Path(__file__).parent / "assets").resolve() |
| 13 | + |
| 14 | +GET_LINKED_TASKS = asset_dir / "linked_tasks_get.xml" |
| 15 | +RUN_LINKED_TASK_NOW = asset_dir / "linked_tasks_run_now.xml" |
| 16 | + |
| 17 | + |
| 18 | +class TestLinkedTasks(unittest.TestCase): |
| 19 | + def setUp(self) -> None: |
| 20 | + self.server = TSC.Server("http://test", False) |
| 21 | + self.server.version = "3.15" |
| 22 | + |
| 23 | + # Fake signin |
| 24 | + self.server._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67" |
| 25 | + self.server._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM" |
| 26 | + |
| 27 | + self.baseurl = self.server.linked_tasks.baseurl |
| 28 | + |
| 29 | + def test_parse_linked_task_flow_run(self): |
| 30 | + xml = fromstring(GET_LINKED_TASKS.read_bytes()) |
| 31 | + task_runs = LinkedTaskFlowRunItem._parse_element(xml, self.server.namespace) |
| 32 | + assert 1 == len(task_runs) |
| 33 | + task = task_runs[0] |
| 34 | + assert task.flow_run_id == "e3d1fc25-5644-4e32-af35-58dcbd1dbd73" |
| 35 | + assert task.flow_run_priority == 1 |
| 36 | + assert task.flow_run_consecutive_failed_count == 3 |
| 37 | + assert task.flow_run_task_type == "runFlow" |
| 38 | + assert task.flow_id == "ab1231eb-b8ca-461e-a131-83f3c2b6a673" |
| 39 | + assert task.flow_name == "flow-name" |
| 40 | + |
| 41 | + def test_parse_linked_task_step(self): |
| 42 | + xml = fromstring(GET_LINKED_TASKS.read_bytes()) |
| 43 | + steps = LinkedTaskStepItem.from_task_xml(xml, self.server.namespace) |
| 44 | + assert 1 == len(steps) |
| 45 | + step = steps[0] |
| 46 | + assert step.id == "f554a4df-bb6f-4294-94ee-9a709ef9bda0" |
| 47 | + assert step.stop_downstream_on_failure |
| 48 | + assert step.step_number == 1 |
| 49 | + assert 1 == len(step.task_details) |
| 50 | + task = step.task_details[0] |
| 51 | + assert task.flow_run_id == "e3d1fc25-5644-4e32-af35-58dcbd1dbd73" |
| 52 | + assert task.flow_run_priority == 1 |
| 53 | + assert task.flow_run_consecutive_failed_count == 3 |
| 54 | + assert task.flow_run_task_type == "runFlow" |
| 55 | + assert task.flow_id == "ab1231eb-b8ca-461e-a131-83f3c2b6a673" |
| 56 | + assert task.flow_name == "flow-name" |
| 57 | + |
| 58 | + def test_parse_linked_task(self): |
| 59 | + tasks = LinkedTaskItem.from_response(GET_LINKED_TASKS.read_bytes(), self.server.namespace) |
| 60 | + assert 1 == len(tasks) |
| 61 | + task = tasks[0] |
| 62 | + assert task.id == "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 63 | + assert task.num_steps == 1 |
| 64 | + assert task.schedule is not None |
| 65 | + assert task.schedule.id == "be077332-d01d-481b-b2f3-917e463d4dca" |
| 66 | + |
| 67 | + def test_get_linked_tasks(self): |
| 68 | + with requests_mock.mock() as m: |
| 69 | + m.get(self.baseurl, text=GET_LINKED_TASKS.read_text()) |
| 70 | + tasks, pagination_item = self.server.linked_tasks.get() |
| 71 | + |
| 72 | + assert 1 == len(tasks) |
| 73 | + task = tasks[0] |
| 74 | + assert task.id == "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 75 | + assert task.num_steps == 1 |
| 76 | + assert task.schedule is not None |
| 77 | + assert task.schedule.id == "be077332-d01d-481b-b2f3-917e463d4dca" |
| 78 | + |
| 79 | + def test_get_by_id_str_linked_task(self): |
| 80 | + id_ = "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 81 | + |
| 82 | + with requests_mock.mock() as m: |
| 83 | + m.get(f"{self.baseurl}/{id_}", text=GET_LINKED_TASKS.read_text()) |
| 84 | + task = self.server.linked_tasks.get_by_id(id_) |
| 85 | + |
| 86 | + assert task.id == "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 87 | + assert task.num_steps == 1 |
| 88 | + assert task.schedule is not None |
| 89 | + assert task.schedule.id == "be077332-d01d-481b-b2f3-917e463d4dca" |
| 90 | + |
| 91 | + def test_get_by_id_obj_linked_task(self): |
| 92 | + id_ = "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 93 | + in_task = LinkedTaskItem() |
| 94 | + in_task.id = id_ |
| 95 | + |
| 96 | + with requests_mock.mock() as m: |
| 97 | + m.get(f"{self.baseurl}/{id_}", text=GET_LINKED_TASKS.read_text()) |
| 98 | + task = self.server.linked_tasks.get_by_id(in_task) |
| 99 | + |
| 100 | + assert task.id == "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 101 | + assert task.num_steps == 1 |
| 102 | + assert task.schedule is not None |
| 103 | + assert task.schedule.id == "be077332-d01d-481b-b2f3-917e463d4dca" |
| 104 | + |
| 105 | + def test_run_now_str_linked_task(self): |
| 106 | + id_ = "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 107 | + |
| 108 | + with requests_mock.mock() as m: |
| 109 | + m.post(f"{self.baseurl}/{id_}/runNow", text=RUN_LINKED_TASK_NOW.read_text()) |
| 110 | + job = self.server.linked_tasks.run_now(id_) |
| 111 | + |
| 112 | + assert job.id == "269a1e5a-1220-4a13-ac01-704982693dd8" |
| 113 | + assert job.status == "InProgress" |
| 114 | + assert job.created_at == parse_datetime("2022-02-15T00:22:22Z") |
| 115 | + assert job.linked_task_id == id_ |
| 116 | + |
| 117 | + def test_run_now_obj_linked_task(self): |
| 118 | + id_ = "1b8211dc-51a8-45ce-a831-b5921708e03e" |
| 119 | + in_task = LinkedTaskItem() |
| 120 | + in_task.id = id_ |
| 121 | + |
| 122 | + with requests_mock.mock() as m: |
| 123 | + m.post(f"{self.baseurl}/{id_}/runNow", text=RUN_LINKED_TASK_NOW.read_text()) |
| 124 | + job = self.server.linked_tasks.run_now(in_task) |
| 125 | + |
| 126 | + assert job.id == "269a1e5a-1220-4a13-ac01-704982693dd8" |
| 127 | + assert job.status == "InProgress" |
| 128 | + assert job.created_at == parse_datetime("2022-02-15T00:22:22Z") |
| 129 | + assert job.linked_task_id == id_ |
0 commit comments