Skip to content

Commit

Permalink
Merge pull request googleapis#3007 from tseaver/3004-bigquery-total_r…
Browse files Browse the repository at this point in the history
…ows_integer

 Return int from 'total_rows'/'total_bytes_processed', if present.

Merging without waiting for the speech fix to land.
  • Loading branch information
tseaver authored Feb 13, 2017
2 parents 57f4c09 + 7f83dc5 commit f7911b4
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 5 deletions.
8 changes: 6 additions & 2 deletions bigquery/google/cloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ def total_rows(self):
:rtype: int, or ``NoneType``
:returns: Count generated on the server (None until set by the server).
"""
return self._properties.get('totalRows')
total_rows = self._properties.get('totalRows')
if total_rows is not None:
return int(total_rows)

@property
def total_bytes_processed(self):
Expand All @@ -220,7 +222,9 @@ def total_bytes_processed(self):
:rtype: int, or ``NoneType``
:returns: Count generated on the server (None until set by the server).
"""
return self._properties.get('totalBytesProcessed')
total_bytes_processed = self._properties.get('totalBytesProcessed')
if total_bytes_processed is not None:
return int(total_bytes_processed)

@property
def rows(self):
Expand Down
126 changes: 123 additions & 3 deletions bigquery/unit_tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ def _verifyResourceProperties(self, query, resource):
self.assertEqual(query.complete, resource.get('jobComplete'))
self.assertEqual(query.errors, resource.get('errors'))
self.assertEqual(query.page_token, resource.get('pageToken'))
self.assertEqual(query.total_rows, resource.get('totalRows'))
self.assertEqual(query.total_bytes_processed,
resource.get('totalBytesProcessed'))
if 'totalRows' in resource:
self.assertEqual(query.total_rows, int(resource['totalRows']))
else:
self.assertIsNone(query.total_rows)
if 'totalBytesProcessed' in resource:
self.assertEqual(query.total_bytes_processed,
int(resource['totalBytesProcessed']))
else:
self.assertIsNone(query.total_bytes_processed)

if 'jobReference' in resource:
self.assertEqual(query.name, resource['jobReference']['jobId'])
Expand Down Expand Up @@ -251,6 +257,120 @@ def test_job_w_jobid(self):
fetched_later = query.job
self.assertIs(fetched_later, job)

def test_cache_hit_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.cache_hit)

def test_cache_hit_present(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'cacheHit': True}
query._set_properties(resource)
self.assertTrue(query.cache_hit)

def test_complete_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.complete)

def test_complete_present(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'jobComplete': True}
query._set_properties(resource)
self.assertTrue(query.complete)

def test_errors_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.errors)

def test_errors_present(self):
ERRORS = [
{'reason': 'testing'},
]
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'errors': ERRORS}
query._set_properties(resource)
self.assertEqual(query.errors, ERRORS)

def test_name_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.name)

def test_name_broken_job_reference(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'jobReference': {'bogus': 'BOGUS'}}
query._set_properties(resource)
self.assertIsNone(query.name)

def test_name_present(self):
JOB_ID = 'JOB_ID'
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'jobReference': {'jobId': JOB_ID}}
query._set_properties(resource)
self.assertEqual(query.name, JOB_ID)

def test_page_token_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.page_token)

def test_page_token_present(self):
TOKEN = 'TOKEN'
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'pageToken': TOKEN}
query._set_properties(resource)
self.assertEqual(query.page_token, TOKEN)

def test_total_rows_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.total_rows)

def test_total_rows_present_integer(self):
TOTAL_ROWS = 42
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'totalRows': TOTAL_ROWS}
query._set_properties(resource)
self.assertEqual(query.total_rows, TOTAL_ROWS)

def test_total_rows_present_string(self):
TOTAL_ROWS = 42
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'totalRows': str(TOTAL_ROWS)}
query._set_properties(resource)
self.assertEqual(query.total_rows, TOTAL_ROWS)

def test_total_bytes_processed_missing(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
self.assertIsNone(query.total_bytes_processed)

def test_total_bytes_processed_present_integer(self):
TOTAL_BYTES_PROCESSED = 123456
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'totalBytesProcessed': TOTAL_BYTES_PROCESSED}
query._set_properties(resource)
self.assertEqual(query.total_bytes_processed, TOTAL_BYTES_PROCESSED)

def test_total_bytes_processed_present_string(self):
TOTAL_BYTES_PROCESSED = 123456
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
resource = {'totalBytesProcessed': str(TOTAL_BYTES_PROCESSED)}
query._set_properties(resource)
self.assertEqual(query.total_bytes_processed, TOTAL_BYTES_PROCESSED)

def test_schema(self):
client = _Client(self.PROJECT)
query = self._make_one(self.QUERY, client)
Expand Down

0 comments on commit f7911b4

Please sign in to comment.