Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error when time partitioning is populated with empty dict #7904

Merged
merged 4 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bigquery/google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ def type_(self):
"""google.cloud.bigquery.table.TimePartitioningType: The type of time
partitioning to use.
"""
return self._properties["type"]
return self._properties.get("type")

@type_.setter
def type_(self, value):
Expand Down Expand Up @@ -1849,7 +1849,7 @@ def from_api_repr(cls, api_repr):
google.cloud.bigquery.table.TimePartitioning:
The ``TimePartitioning`` object.
"""
instance = cls(api_repr["type"])
instance = cls()
tseaver marked this conversation as resolved.
Show resolved Hide resolved
instance._properties = api_repr
return instance

Expand Down
72 changes: 72 additions & 0 deletions bigquery/tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,54 @@ def test__build_resource_w_custom_field_not_in__properties(self):
with self.assertRaises(ValueError):
table._build_resource(["bad"])

def test_time_partitioning_getter(self):
from google.cloud.bigquery.table import TimePartitioning
from google.cloud.bigquery.table import TimePartitioningType

dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
table = self._make_one(table_ref)

table._properties["timePartitioning"] = {
"type": "DAY",
"field": "col1",
"expirationMs": "123456",
"requirePartitionFilter": False,
}
self.assertIsInstance(table.time_partitioning, TimePartitioning)
self.assertEqual(table.time_partitioning.type_, TimePartitioningType.DAY)
self.assertEqual(table.time_partitioning.field, "col1")
self.assertEqual(table.time_partitioning.expiration_ms, 123456)
self.assertFalse(table.time_partitioning.require_partition_filter)

def test_time_partitioning_getter_w_none(self):
dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
table = self._make_one(table_ref)

table._properties["timePartitioning"] = None
self.assertIsNone(table.time_partitioning)

del table._properties["timePartitioning"]
self.assertIsNone(table.time_partitioning)

def test_time_partitioning_getter_w_empty(self):
from google.cloud.bigquery.table import TimePartitioning

dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
table = self._make_one(table_ref)

# Even though there are required properties according to the API
# specification, sometimes time partitioning is populated as an empty
# object. See internal bug 131167013.
table._properties["timePartitioning"] = {}
self.assertIsInstance(table.time_partitioning, TimePartitioning)
self.assertIsNone(table.time_partitioning.type_)
self.assertIsNone(table.time_partitioning.field)
self.assertIsNone(table.time_partitioning.expiration_ms)
self.assertIsNone(table.time_partitioning.require_partition_filter)

tseaver marked this conversation as resolved.
Show resolved Hide resolved
def test_time_partitioning_setter(self):
from google.cloud.bigquery.table import TimePartitioning
from google.cloud.bigquery.table import TimePartitioningType
Expand Down Expand Up @@ -2211,6 +2259,22 @@ def test_constructor_explicit(self):
self.assertEqual(time_partitioning.expiration_ms, 10000)
self.assertTrue(time_partitioning.require_partition_filter)

def test_from_api_repr_empty(self):
from google.cloud.bigquery.table import TimePartitioningType

klass = self._get_target_class()

# Even though there are required properties according to the API
# specification, sometimes time partitioning is populated as an empty
# object. See internal bug 131167013.
api_repr = {}
time_partitioning = klass.from_api_repr(api_repr)

self.assertIsNone(time_partitioning.type_)
self.assertIsNone(time_partitioning.field)
self.assertIsNone(time_partitioning.expiration_ms)
self.assertIsNone(time_partitioning.require_partition_filter)

def test_from_api_repr_minimal(self):
from google.cloud.bigquery.table import TimePartitioningType

Expand All @@ -2223,6 +2287,14 @@ def test_from_api_repr_minimal(self):
self.assertIsNone(time_partitioning.expiration_ms)
self.assertIsNone(time_partitioning.require_partition_filter)

def test_from_api_repr_doesnt_override_type(self):
from google.cloud.bigquery.table import TimePartitioningType

klass = self._get_target_class()
api_repr = {"type": "HOUR"}
time_partitioning = klass.from_api_repr(api_repr)
self.assertEqual(time_partitioning.type_, "HOUR")

def test_from_api_repr_explicit(self):
from google.cloud.bigquery.table import TimePartitioningType

Expand Down