Skip to content

Commit

Permalink
fix: allow partial streaming_buffer statistics (#37)
Browse files Browse the repository at this point in the history
* fix: allow partial streaming_buffer statistics.

Previously, the BQ backend would supply all fields of the
streamingBuffer statistics or none.  This is no longer the
case, so we relax construction to not depend on all values
being present.

Related: internal issue b/148720220
  • Loading branch information
shollyman authored Feb 14, 2020
1 parent 3d77a75 commit 645f0fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
17 changes: 11 additions & 6 deletions google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,12 +1139,17 @@ class StreamingBuffer(object):
"""

def __init__(self, resource):
self.estimated_bytes = int(resource["estimatedBytes"])
self.estimated_rows = int(resource["estimatedRows"])
# time is in milliseconds since the epoch.
self.oldest_entry_time = google.cloud._helpers._datetime_from_microseconds(
1000.0 * int(resource["oldestEntryTime"])
)
self.estimated_bytes = None
if "estimatedBytes" in resource:
self.estimated_bytes = int(resource["estimatedBytes"])
self.estimated_rows = None
if "estimatedRows" in resource:
self.estimated_rows = int(resource["estimatedRows"])
self.oldest_entry_time = None
if "oldestEntryTime" in resource:
self.oldest_entry_time = google.cloud._helpers._datetime_from_microseconds(
1000.0 * int(resource["oldestEntryTime"])
)


class Row(object):
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,29 @@ def test_from_api_repr_w_properties(self):
table = klass.from_api_repr(RESOURCE)
self._verifyResourceProperties(table, RESOURCE)

def test_from_api_repr_w_partial_streamingbuffer(self):
import datetime
from google.cloud._helpers import UTC
from google.cloud._helpers import _millis

RESOURCE = self._make_resource()
self.OLDEST_TIME = datetime.datetime(2015, 8, 1, 23, 59, 59, tzinfo=UTC)
RESOURCE["streamingBuffer"] = {"oldestEntryTime": _millis(self.OLDEST_TIME)}
klass = self._get_target_class()
table = klass.from_api_repr(RESOURCE)
self.assertIsNotNone(table.streaming_buffer)
self.assertIsNone(table.streaming_buffer.estimated_rows)
self.assertIsNone(table.streaming_buffer.estimated_bytes)
self.assertEqual(table.streaming_buffer.oldest_entry_time, self.OLDEST_TIME)
# Another partial construction
RESOURCE["streamingBuffer"] = {"estimatedRows": 1}
klass = self._get_target_class()
table = klass.from_api_repr(RESOURCE)
self.assertIsNotNone(table.streaming_buffer)
self.assertEqual(table.streaming_buffer.estimated_rows, 1)
self.assertIsNone(table.streaming_buffer.estimated_bytes)
self.assertIsNone(table.streaming_buffer.oldest_entry_time)

def test_from_api_with_encryption(self):
self._setUpConstants()
RESOURCE = {
Expand Down

0 comments on commit 645f0fd

Please sign in to comment.