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

feat: add support for policy tags #77

Merged
merged 22 commits into from May 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions google/cloud/bigquery/schema.py
Expand Up @@ -360,14 +360,29 @@ def names(self, value):
if "names" in self._properties:
del self._properties["names"]

def _key(self):
"""A tuple key that uniquely describes this PolicyTagList.

Used to compute this instance's hashcode and evaluate equality.

Returns:
Tuple: The contents of this :class:`~google.cloud.bigquery.schema.PolicyTagList`.
"""
return tuple(sorted(self._properties.items()))

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return False
if not isinstance(other, PolicyTagList):
return NotImplemented
return self._key() == other._key()

def __ne__(self, other):
return not self.__eq__(other)
return not self == other

def __hash__(self):
return hash(self._key())
plamut marked this conversation as resolved.
Show resolved Hide resolved

def __repr__(self):
return "PolicyTagList{}".format(self._key())

@classmethod
def from_api_repr(cls, api_repr):
Expand Down
8 changes: 4 additions & 4 deletions tests/system.py
Expand Up @@ -352,11 +352,11 @@ def test_create_table_with_policy(self):
]
)
policy_2 = PolicyTagList(
names=(
names=[
"projects/{}/locations/us/taxonomies/3/policyTags/4".format(
Config.CLIENT.project
),
)
]
)

schema = [
Expand Down Expand Up @@ -387,8 +387,8 @@ def test_create_table_with_policy(self):
)

table.schema = new_schema
table2 = Config.CLIENT.update_table(table, ["schema",])
self.assertEqual(policy_2, table.schema[1].policy_tags)
table2 = Config.CLIENT.update_table(table, ["schema", ])
self.assertEqual(policy_2, table2.schema[1].policy_tags)

def test_create_table_w_time_partitioning_w_clustering_fields(self):
from google.cloud.bigquery.table import TimePartitioning
Expand Down
21 changes: 16 additions & 5 deletions tests/unit/test_schema.py
Expand Up @@ -692,19 +692,19 @@ def test_constructor(self):
empty_policy_tags = self._make_one()
self.assertIsNotNone(empty_policy_tags.names)
self.assertEqual(len(empty_policy_tags.names), 0)
policy_tags = self._make_one(("foo", "bar"))
self.assertEqual(policy_tags.names, ("foo", "bar"))
policy_tags = self._make_one(["foo", "bar"])
self.assertEqual(policy_tags.names, ["foo", "bar"])

def test_from_api_repr(self):
klass = self._get_target_class()
api_repr = {"names": ("foo")}
api_repr = {"names": ["foo"]}
policy_tags = klass.from_api_repr(api_repr)
self.assertEqual(policy_tags.to_api_repr(), api_repr)

def test_to_api_repr(self):
taglist = self._make_one(names=("foo", "bar"))
taglist = self._make_one(names=["foo", "bar"])
self.assertEqual(
taglist.to_api_repr(), {"names": ("foo", "bar")},
taglist.to_api_repr(), {"names": ["foo", "bar"]},
)

def test_setter(self):
Expand All @@ -716,3 +716,14 @@ def test_setter(self):
self.assertEqual(policy_tags.names, ["foo", "bar"])
policy_tags.names = None
self.assertEqual(policy_tags.names, [])

def test___eq___wrong_type(self):
policy = self._make_one(names=["foo", ])
other = object()
self.assertNotEqual(policy, other)
self.assertEqual(policy, mock.ANY)

def test___eq___names_mismatch(self):
policy = self._make_one(names=["foo", "bar"])
other = self._make_one(names=["bar", "baz"])
self.assertNotEqual(policy, other)