Skip to content

Commit 5c253ec

Browse files
committed
Returning ints from BigQuery Table.num_rows/num_bytes.
Also fixing Table.expires docstring typo.
1 parent 0f8bd8e commit 5c253ec

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
@@ -171,7 +171,9 @@ def num_bytes(self):
171171
:rtype: integer, or ``NoneType``
172172
:returns: the byte count (None until set from the server).
173173
"""
174-
return self._properties.get('numBytes')
174+
num_bytes_as_str = self._properties.get('numBytes')
175+
if num_bytes_as_str is not None:
176+
return int(num_bytes_as_str)
175177

176178
@property
177179
def num_rows(self):
@@ -180,7 +182,9 @@ def num_rows(self):
180182
:rtype: integer, or ``NoneType``
181183
:returns: the row count (None until set from the server).
182184
"""
183-
return self._properties.get('numRows')
185+
num_rows_as_str = self._properties.get('numRows')
186+
if num_rows_as_str is not None:
187+
return int(num_rows_as_str)
184188

185189
@property
186190
def self_link(self):
@@ -247,7 +251,7 @@ def expires(self):
247251

248252
@expires.setter
249253
def expires(self, value):
250-
"""Update atetime at which the table will be removed.
254+
"""Update datetime at which the table will be removed.
251255
252256
:type value: ``datetime.datetime``, or ``NoneType``
253257
: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)