Skip to content
Permalink
Browse files
fix: allow partial streaming_buffer statistics (#37)
* 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 committed Feb 14, 2020
1 parent 3d77a75 commit 645f0fdb35ee0e81ee70f7459e796a42a1f03210
Showing with 34 additions and 6 deletions.
  1. +11 −6 google/cloud/bigquery/table.py
  2. +23 −0 tests/unit/test_table.py
@@ -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):
@@ -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 = {

0 comments on commit 645f0fd

Please sign in to comment.