Skip to content

Commit

Permalink
Defend against missing 'creationTime'/'lastModifiedTime' from back-end.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Aug 5, 2015
1 parent 22bc7cd commit 7210c8c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
6 changes: 4 additions & 2 deletions gcloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ def _set_properties(self, api_response):
"""
self._properties.clear()
cleaned = api_response.copy()
cleaned['creationTime'] = float(cleaned['creationTime'])
cleaned['lastModifiedTime'] = float(cleaned['lastModifiedTime'])
if 'creationTime' in cleaned:
cleaned['creationTime'] = float(cleaned['creationTime'])
if 'lastModifiedTime' in cleaned:
cleaned['lastModifiedTime'] = float(cleaned['lastModifiedTime'])
self._properties.update(cleaned)

def _build_resource(self):
Expand Down
6 changes: 4 additions & 2 deletions gcloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,10 @@ def _set_properties(self, api_response):
"""
self._properties.clear()
cleaned = api_response.copy()
cleaned['creationTime'] = float(cleaned['creationTime'])
cleaned['lastModifiedTime'] = float(cleaned['lastModifiedTime'])
if 'creationTime' in cleaned:
cleaned['creationTime'] = float(cleaned['creationTime'])
if 'lastModifiedTime' in cleaned:
cleaned['lastModifiedTime'] = float(cleaned['lastModifiedTime'])
if 'expirationTime' in cleaned:
cleaned['expirationTime'] = float(cleaned['expirationTime'])
self._properties.update(cleaned)
Expand Down
25 changes: 25 additions & 0 deletions gcloud/bigquery/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ def test_create_w_alternate_client(self):
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(dataset, RESOURCE)

def test_create_w_missing_output_properties(self):
# In the wild, the resource returned from 'dataset.create' sometimes
# lacks 'creationTime' / 'lastModifiedTime'
PATH = 'projects/%s/datasets' % (self.PROJECT,)
RESOURCE = self._makeResource()
del RESOURCE['creationTime']
del RESOURCE['lastModifiedTime']
self.WHEN = None
conn = _Connection(RESOURCE)
CLIENT = _Client(project=self.PROJECT, connection=conn)
dataset = self._makeOne(self.DS_NAME, client=CLIENT)

dataset.create()

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/%s' % PATH)
SENT = {
'datasetReference':
{'projectId': self.PROJECT, 'datasetId': self.DS_NAME},
}
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(dataset, RESOURCE)

def test_exists_miss_w_bound_client(self):
PATH = 'projects/%s/datasets/%s' % (self.PROJECT, self.DS_NAME)
conn = _Connection()
Expand Down
35 changes: 35 additions & 0 deletions gcloud/bigquery/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,41 @@ def test_create_w_alternate_client(self):
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(table, RESOURCE)

def test_create_w_missing_output_properties(self):
# In the wild, the resource returned from 'dataset.create' sometimes
# lacks 'creationTime' / 'lastModifiedTime'
from gcloud.bigquery.table import SchemaField
PATH = 'projects/%s/datasets/%s/tables' % (self.PROJECT, self.DS_NAME)
RESOURCE = self._makeResource()
del RESOURCE['creationTime']
del RESOURCE['lastModifiedTime']
self.WHEN = None
conn = _Connection(RESOURCE)
client = _Client(project=self.PROJECT, connection=conn)
dataset = _Dataset(client)
full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
age = SchemaField('age', 'INTEGER', mode='REQUIRED')
table = self._makeOne(self.TABLE_NAME, dataset,
schema=[full_name, age])

table.create()

self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/%s' % PATH)
SENT = {
'tableReference': {
'projectId': self.PROJECT,
'datasetId': self.DS_NAME,
'tableId': self.TABLE_NAME},
'schema': {'fields': [
{'name': 'full_name', 'type': 'STRING', 'mode': 'REQUIRED'},
{'name': 'age', 'type': 'INTEGER', 'mode': 'REQUIRED'}]},
}
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(table, RESOURCE)

def test_exists_miss_w_bound_client(self):
PATH = 'projects/%s/datasets/%s/tables/%s' % (
self.PROJECT, self.DS_NAME, self.TABLE_NAME)
Expand Down

0 comments on commit 7210c8c

Please sign in to comment.