Skip to content

Commit 333c52a

Browse files
hrfullertseaver
authored andcommitted
BigQuery: use _get_sub_prop helper so missing load stats don't raise. (#6269)
1 parent a74dc33 commit 333c52a

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

bigquery/google/cloud/bigquery/job.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,9 +1374,9 @@ def input_file_bytes(self):
13741374
:returns: the count (None until set from the server).
13751375
:raises: ValueError for invalid value types.
13761376
"""
1377-
statistics = self._properties.get('statistics')
1378-
if statistics is not None:
1379-
return int(statistics['load']['inputFileBytes'])
1377+
return _helpers._int_or_none(_helpers._get_sub_prop(
1378+
self._properties, ['statistics', 'load', 'inputFileBytes']
1379+
))
13801380

13811381
@property
13821382
def input_files(self):
@@ -1385,9 +1385,9 @@ def input_files(self):
13851385
:rtype: int, or ``NoneType``
13861386
:returns: the count (None until set from the server).
13871387
"""
1388-
statistics = self._properties.get('statistics')
1389-
if statistics is not None:
1390-
return int(statistics['load']['inputFiles'])
1388+
return _helpers._int_or_none(_helpers._get_sub_prop(
1389+
self._properties, ['statistics', 'load', 'inputFiles']
1390+
))
13911391

13921392
@property
13931393
def output_bytes(self):
@@ -1396,9 +1396,9 @@ def output_bytes(self):
13961396
:rtype: int, or ``NoneType``
13971397
:returns: the count (None until set from the server).
13981398
"""
1399-
statistics = self._properties.get('statistics')
1400-
if statistics is not None:
1401-
return int(statistics['load']['outputBytes'])
1399+
return _helpers._int_or_none(_helpers._get_sub_prop(
1400+
self._properties, ['statistics', 'load', 'outputBytes']
1401+
))
14021402

14031403
@property
14041404
def output_rows(self):
@@ -1407,9 +1407,9 @@ def output_rows(self):
14071407
:rtype: int, or ``NoneType``
14081408
:returns: the count (None until set from the server).
14091409
"""
1410-
statistics = self._properties.get('statistics')
1411-
if statistics is not None:
1412-
return int(statistics['load']['outputRows'])
1410+
return _helpers._int_or_none(_helpers._get_sub_prop(
1411+
self._properties, ['statistics', 'load', 'outputRows']
1412+
))
14131413

14141414
def to_api_repr(self):
14151415
"""Generate a resource for :meth:`_begin`."""

bigquery/tests/unit/test_job.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,11 +1985,6 @@ def test_props_set_by_server(self):
19851985
statistics['creationTime'] = _millis(CREATED)
19861986
statistics['startTime'] = _millis(STARTED)
19871987
statistics['endTime'] = _millis(ENDED)
1988-
load_stats = statistics['load'] = {}
1989-
load_stats['inputFileBytes'] = 12345
1990-
load_stats['inputFiles'] = 1
1991-
load_stats['outputBytes'] = 23456
1992-
load_stats['outputRows'] = 345
19931988

19941989
self.assertEqual(job.etag, 'ETAG')
19951990
self.assertEqual(job.self_link, URL)
@@ -1999,6 +1994,15 @@ def test_props_set_by_server(self):
19991994
self.assertEqual(job.started, STARTED)
20001995
self.assertEqual(job.ended, ENDED)
20011996

1997+
# running jobs have no load stats not yet set.
1998+
self.assertIsNone(job.output_bytes)
1999+
2000+
load_stats = statistics['load'] = {}
2001+
load_stats['inputFileBytes'] = 12345
2002+
load_stats['inputFiles'] = 1
2003+
load_stats['outputBytes'] = 23456
2004+
load_stats['outputRows'] = 345
2005+
20022006
self.assertEqual(job.input_file_bytes, 12345)
20032007
self.assertEqual(job.input_files, 1)
20042008
self.assertEqual(job.output_bytes, 23456)

0 commit comments

Comments
 (0)