8000 parse_datetime() should gracefully handle invalid dates by paulvic · Pull Request #529 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

parse_datetime() should gracefully handle invalid dates #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 14, 2020
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
8 changes: 7 additions & 1 deletion tableauserverclient/datetime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ def parse_datetime(date):
if date is None:
return None

return datetime.datetime.strptime(date, TABLEAU_DATE_FORMAT).replace(tzinfo=utc)
try:
return datetime.datetime.strptime(date, TABLEAU_DATE_FORMAT).replace(tzinfo=utc)
except ValueError:
return None


def format_datetime(date):
if date is None:
return None

return date.astimezone(tz=utc).strftime(TABLEAU_DATE_FORMAT)
11 changes: 11 additions & 0 deletions test/assets/workbook_get_invalid_date.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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.3.xsd">
<pagination pageNumber="1" pageSize="100" totalAvailable="2" />
<workbooks>
<workbook id="6d13b0ca-043d-4d42-8c9d-3f3313ea3a00" name="Superstore" contentUrl="Superstore" showTabs="false" size="1" createdAt="2016-06-31T20:34:04Z" updatedAt="2016-08-04T17:56:41Z">
<project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" />
<owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" />
<tags />
</workbook>
</workbooks>
</tsResponse>
10 changes: 10 additions & 0 deletions test/test_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ADD_TAGS_XML = os.path.join(TEST_ASSET_DIR, 'workbook_add_tags.xml')
GET_BY_ID_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get_by_id.xml')
GET_EMPTY_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get_empty.xml')
GET_INVALID_DATE_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get_invalid_date.xml')
GET_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get.xml')
POPULATE_CONNECTIONS_XML = os.path.join(TEST_ASSET_DIR, 'workbook_populate_connections.xml')
POPULATE_PDF = os.path.join(TEST_ASSET_DIR, 'populate_pdf.pdf')
Expand Down Expand Up @@ -79,6 +80,15 @@ def test_get(self):
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', all_workbooks[1].owner_id)
self.assertEqual(set(['Safari', 'Sample']), all_workbooks[1].tags)

def test_get_ignore_invalid_date(self):
with open(GET_INVALID_DATE_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.get(self.baseurl, text=response_xml)
all_workbooks, pagination_item = self.server.workbooks.get()
self.assertEqual(None, format_datetime(all_workbooks[0].created_at))
self.assertEqual('2016-08-04T17:56:41Z', format_datetime(all_workbooks[0].updated_at))

def test_get_before_signin(self):
self.server._auth_token = None
self.assertRaises(TSC.NotSignedInError, self.server.workbooks.get)
Expand Down
0