Skip to content

Commit a7b395a

Browse files
committed
Merge pull request #1252 from dhermes/bigquery-int-values
Returning ints from BigQuery Table.num_rows/num_bytes.
2 parents b4e7534 + 5c253ec commit a7b395a

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

gcloud/bigquery/table.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ def num_bytes(self):
172172
:rtype: integer, or ``NoneType``
173173
:returns: the byte count (None until set from the server).
174174
"""
175-
return self._properties.get('numBytes')
175+
num_bytes_as_str = self._properties.get('numBytes')
176+
if num_bytes_as_str is not None:
177+
return int(num_bytes_as_str)
176178

177179
@property
178180
def num_rows(self):
@@ -181,7 +183,9 @@ def num_rows(self):
181183
:rtype: integer, or ``NoneType``
182184
:returns: the row count (None until set from the server).
183185
"""
184-
return self._properties.get('numRows')
186+
num_rows_as_str = self._properties.get('numRows')
187+
if num_rows_as_str is not None:
188+
return int(num_rows_as_str)
185189

186190
@property
187191
def self_link(self):
@@ -248,7 +252,7 @@ def expires(self):
248252

249253
@expires.setter
250254
def expires(self, value):
251-
"""Update atetime at which the table will be removed.
255+
"""Update datetime at which the table will be removed.
252256
253257
:type value: ``datetime.datetime``, or ``NoneType``
254258
:param value: the new expiration time, or None

gcloud/bigquery/test_table.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,50 @@ def test_ctor_w_schema(self):
216216
schema=[full_name, age])
217217
self.assertEqual(table.schema, [full_name, age])
218218

219+
def test_num_bytes_getter(self):
220+
client = _Client(self.PROJECT)
221+
dataset = _Dataset(client)
222+
table = self._makeOne(self.TABLE_NAME, dataset)
223+
224+
# Check with no value set.
225+
self.assertEqual(table.num_bytes, None)
226+
227+
num_bytes = 1337
228+
# Check with integer value set.
229+
table._properties = {'numBytes': num_bytes}
230+
self.assertEqual(table.num_bytes, num_bytes)
231+
232+
# Check with a string value set.
233+
table._properties = {'numBytes': str(num_bytes)}
234+
self.assertEqual(table.num_bytes, num_bytes)
235+
236+
# Check with invalid int value.
237+
table._properties = {'numBytes': 'x'}
238+
with self.assertRaises(ValueError):
239+
getattr(table, 'num_bytes')
240+
241+
def test_num_rows_getter(self):
242+
client = _Client(self.PROJECT)
243+
dataset = _Dataset(client)
244+
table = self._makeOne(self.TABLE_NAME, dataset)
245+
246+
# Check with no value set.
247+
self.assertEqual(table.num_rows, None)
248+
249+
num_rows = 42
250+
# Check with integer value set.
251+
table._properties = {'numRows': num_rows}
252+
self.assertEqual(table.num_rows, num_rows)
253+
254+
# Check with a string value set.
255+
table._properties = {'numRows': str(num_rows)}
256+
self.assertEqual(table.num_rows, num_rows)
257+
258+
# Check with invalid int value.
259+
table._properties = {'numRows': 'x'}
260+
with self.assertRaises(ValueError):
261+
getattr(table, 'num_rows')
262+
219263
def test_schema_setter_non_list(self):
220264
client = _Client(self.PROJECT)
221265
dataset = _Dataset(client)

0 commit comments

Comments
 (0)