8000 parse_datetime() should gracefully handle invalid dates (#529) · cfmayden/server-client-python@f8799ba · GitHub
[go: up one dir, main page]

Skip to content

Commit f8799ba

Browse files
authored
parse_datetime() should gracefully handle invalid dates (tableau#529)
* parse_datetime() should gracefully handle invalid dates We've seen instances where enumerating workbooks on our server results in an exception in parse_datetime(). It appears that some workbooks contain invalid dates e.g. `2018-06-31T13:12:00Z` for their created_at or updated_at times which causes the method to throw an exception, breaking enumeration. This change simply catches the parsing failure and returns `None` in that scenario. * Handle date is `None` in format_datetime() * Add test XML file with invalid date * Add test for invalid date * Add missing constant * Remove whitespace
1 parent 0133207 commit f8799ba

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

tableauserverclient/datetime_helpers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ def parse_datetime(date):
2828
if date is None:
2929
return None
3030

31-
return datetime.datetime.strptime(date, TABLEAU_DATE_FORMAT).replace(tzinfo=utc)
31+
try:
32+
return datetime.datetime.strptime(date, TABLEAU_DATE_FORMAT).replace(tzinfo=utc)
33+
except ValueError:
34+
return None
3235

3336

3437
def format_datetime(date):
38+
if date is None:
39+
return None
40+
3541
return date.astimezone(tz=utc).strftime(TABLEAU_DATE_FORMAT)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<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">
3+
<pagination pageNumber="1" pageSize="100" totalAvailable="2" />
4+
<workbooks>
5+
<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">
6+
<project id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760" name="default" />
7+
<owner id="5de011f8-5aa9-4d5b-b991-f462c8dd6bb7" />
8+
<tags />
9+
</workbook>
10+
</workbooks>
11+
</tsResponse>

test/test_workbook.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
ADD_TAGS_XML = os.path.join(TEST_ASSET_DIR, 'workbook_add_tags.xml')
2121
GET_BY_ID_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get_by_id.xml')
2222
GET_EMPTY_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get_empty.xml')
23+
GET_INVALID_DATE_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get_invalid_date.xml')
2324
GET_XML = os.path.join(TEST_ASSET_DIR, 'workbook_get.xml')
2425
POPULATE_CONNECTIONS_XML = os.path.join(TEST_ASSET_DIR, 'workbook_populate_connections.xml')
2526
POPULATE_PDF = os.path.join(TEST_ASSET_DIR, 'populate_pdf.pdf')
@@ -79,6 +80,15 @@ def test_get(self):
7980
self.assertEqual('5de011f8-5aa9-4d5b-b991-f462c8dd6bb7', all_workbooks[1].owner_id)
8081
self.assertEqual(set(['Safari', 'Sample']), all_workbooks[1].tags)
8182

83+
def test_get_ignore_invalid_date(self):
84+
73DC with open(GET_INVALID_DATE_XML, 'rb') as f:
85+
response_xml = f.read().decode('utf-8')
86+
with requests_mock.mock() as m:
87+
m.get(self.baseurl, text=response_xml)
88+
all_workbooks, pagination_item = self.server.workbooks.get()
89+
self.assertEqual(None, format_datetime(all_workbooks[0].created_at))
90+
self.assertEqual('2016-08-04T17:56:41Z', format_datetime(all_workbooks[0].updated_at))
91+
8292
def test_get_before_signin(self):
8393
self.server._auth_token = None
8494
self.assertRaises(TSC.NotSignedInError, self.server.workbooks.get)

0 commit comments

Comments
 (0)
0