Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tableauserverclient/models/target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Target class meant to abstract mappings to other objects"""


class Target():
def __init__(self, id_, target_type):
self.id = id_
self.type = target_type

def __repr__(self):
return "<Target#{id}, {type}>".format(**self.__dict__)
21 changes: 18 additions & 3 deletions tableauserverclient/models/task_item.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import xml.etree.ElementTree as ET
from .. import NAMESPACE
from .schedule_item import ScheduleItem
from .target import Target


class TaskItem(object):
def __init__(self, id_, task_type, priority, consecutive_failed_count=0, schedule_id=None):
def __init__(self, id_, task_type, priority, consecutive_failed_count=0, schedule_id=None, target=None):
self.id = id_
self.task_type = task_type
self.priority = priority
self.consecutive_failed_count = consecutive_failed_count
self.schedule_id = schedule_id
self.target = target

def __repr__(self):
return "<Task#{id} {task_type} pri({priority}) failed({consecutive_failed_count}) schedule_id({" \
"schedule_id})>".format(**self.__dict__)
"schedule_id}) target({target})>".format(**self.__dict__)

@classmethod
def from_response(cls, xml):
Expand All @@ -28,11 +30,24 @@ def from_response(cls, xml):
@classmethod
def _parse_element(cls, element):
schedule = None
target = None
schedule_element = element.find('.//t:schedule', namespaces=NAMESPACE)
workbook_element = element.find('.//t:workbook', namespaces=NAMESPACE)
datasource_element = element.find('.//t:datasource', namespaces=NAMESPACE)
if schedule_element is not None:
schedule = schedule_element.get('id', None)

# according to the Tableau Server REST API documentation,
# there should be only one of workbook or datasource
if workbook_element is not None:
workbook_id = workbook_element.get('id', None)
target = Target(workbook_id, "workbook")
if datasource_element is not None:
datasource_id = datasource_element.get('id', None)
target = Target(datasource_id, "datasource")

task_type = element.get('type', None)
priority = int(element.get('priority', -1))
consecutive_failed_count = int(element.get('consecutiveFailedCount', 0))
id_ = element.get('id', None)
return cls(id_, task_type, priority, consecutive_failed_count, schedule)
return cls(id_, task_type, priority, consecutive_failed_count, schedule, target)
22 changes: 22 additions & 0 deletions test/assets/tasks_no_workbook_or_datasource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse
xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
<tasks>
<task>
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
</extractRefresh>
</task>
<task>
<extractRefresh id="6e8255f1-142b-4e17-bd9a-9fe6736812a1" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="82974721-6831-4c78-875c-b7c2d5237bc2" name="Refresh daily [15:00 - 17:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:39:39Z" updatedAt="2017-07-12T22:00:05Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T22:00:00Z" />
</extractRefresh>
</task>
<task>
<extractRefresh id="90cbc49a-b668-4355-9321-46f7fefd4197" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="c39a5a41-ab5b-4661-90f6-78384916748c" name="Refresh daily [00:00 - 02:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:33:51Z" updatedAt="2017-07-12T07:00:08Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T07:00:00Z" />
</extractRefresh>
</task>
</tasks>
</tsResponse>
13 changes: 13 additions & 0 deletions test/assets/tasks_with_datasource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse
xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
<tasks>
<task>
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
<datasource id="c7a9327e-1cda-4504-b026-ddb43b976d1d" />
</extractRefresh>
</task>
</tasks>
</tsResponse>
13 changes: 13 additions & 0 deletions test/assets/tasks_with_workbook.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse
xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
<tasks>
<task>
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
<workbook id="c7a9327e-1cda-4504-b026-ddb43b976d1d" />
</extractRefresh>
</task>
</tasks>
</tsResponse>
25 changes: 25 additions & 0 deletions test/assets/tasks_with_workbook_and_datasource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse
xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
<tasks>
<task>
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
<workbook id="c7a9327e-1cda-4504-b026-ddb43b976d1d" />
</extractRefresh>
</task>
<task>
<extractRefresh id="6e8255f1-142b-4e17-bd9a-9fe6736812a1" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="82974721-6831-4c78-875c-b7c2d5237bc2" name="Refresh daily [15:00 - 17:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:39:39Z" updatedAt="2017-07-12T22:00:05Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T22:00:00Z" />
<datasource id="61334430-fb0c-43af-a5a3-8db3323644b6" />
</extractRefresh>
</task>
<task>
<extractRefresh id="90cbc49a-b668-4355-9321-46f7fefd4197" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
<schedule id="c39a5a41-ab5b-4661-90f6-78384916748c" name="Refresh daily [00:00 - 02:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:33:51Z" updatedAt="2017-07-12T07:00:08Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T07:00:00Z" />
<workbook id="952663f4-1bb4-4fed-91f8-5967f48c533a" />
</extractRefresh>
</task>
</tasks>
</tsResponse>
78 changes: 78 additions & 0 deletions test/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import unittest
import os
import requests_mock
import tableauserverclient as TSC

TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")

GET_XML_NO_WORKBOOK = os.path.join(TEST_ASSET_DIR, "tasks_no_workbook_or_datasource.xml")
GET_XML_WITH_WORKBOOK = os.path.join(TEST_ASSET_DIR, "tasks_with_workbook.xml")
GET_XML_WITH_DATASOURCE = os.path.join(TEST_ASSET_DIR, "tasks_with_datasource.xml")
GET_XML_WITH_WORKBOOK_AND_DATASOURCE = os.path.join(TEST_ASSET_DIR, "tasks_with_workbook_and_datasource.xml")


class TaskTests(unittest.TestCase):
def setUp(self):
self.server = TSC.Server("http://test")
self.server.version = '2.6'

# Fake Signin
self.server._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67"
self.server._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM"

self.baseurl = self.server.tasks.baseurl

def test_get_tasks_with_no_workbook(self):
with open(GET_XML_NO_WORKBOOK, "rb") as f:
response_xml = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_tasks, pagination_item = self.server.tasks.get()

task = all_tasks[0]
self.assertEqual(None, task.target)

def test_get_tasks_with_workbook(self):
with open(GET_XML_WITH_WORKBOOK, "rb") as f:
response_xml = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_tasks, pagination_item = self.server.tasks.get()

task = all_tasks[0]
self.assertEqual('c7a9327e-1cda-4504-b026-ddb43b976d1d', task.target.id)
self.assertEqual('workbook', task.target.type)

def test_get_tasks_with_datasource(self):
with open(GET_XML_WITH_DATASOURCE, "rb") as f:
response_xml = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_tasks, pagination_item = self.server.tasks.get()

task = all_tasks[0]
self.assertEqual('c7a9327e-1cda-4504-b026-ddb43b976d1d', task.target.id)
self.assertEqual('datasource', task.target.type)

def test_get_tasks_with_workbook_and_datasource(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically never possible, right? (though a test for it is fine -- we may do this someday)

with open(GET_XML_WITH_WORKBOOK_AND_DATASOURCE, "rb") as f:
response_xml = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_tasks, pagination_item = self.server.tasks.get()

self.assertEqual('workbook', all_tasks[0].target.type)
self.assertEqual('datasource', all_tasks[1].target.type)
self.assertEqual('workbook', all_tasks[2].target.type)

def test_get_task_with_schedule(self):
with open(GET_XML_WITH_WORKBOOK, "rb") as f:
response_xml = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_tasks, pagination_item = self.server.tasks.get()

task = all_tasks[0]
self.assertEqual('c7a9327e-1cda-4504-b026-ddb43b976d1d', task.target.id)
self.assertEqual('workbook', task.target.type)
self.assertEqual('b60b4efd-a6f7-4599-beb3-cb677e7abac1', task.schedule_id)