From 645f0fdb35ee0e81ee70f7459e796a42a1f03210 Mon Sep 17 00:00:00 2001 From: shollyman Date: Fri, 14 Feb 2020 12:35:48 -0800 Subject: [PATCH] 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 --- google/cloud/bigquery/table.py | 17 +++++++++++------ tests/unit/test_table.py | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/google/cloud/bigquery/table.py b/google/cloud/bigquery/table.py index 555f529f3..1da061720 100644 --- a/google/cloud/bigquery/table.py +++ b/google/cloud/bigquery/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): diff --git a/tests/unit/test_table.py b/tests/unit/test_table.py index 079ec6e00..c1611c084 100644 --- a/tests/unit/test_table.py +++ b/tests/unit/test_table.py @@ -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 = {