Content-Length: 435265 | pFad | https://github.com/googleapis/python-bigquery/commit/02176377d5e2fc25b5cd4f46aa6ebfb1b6a960a6

93 fix: ensure AccessEntry equality and repr uses the correct `entity_ty… · googleapis/python-bigquery@0217637 · GitHub
Skip to content

Commit 0217637

Browse files
authored
fix: ensure AccessEntry equality and repr uses the correct entity_type (#2182)
* fix: ensure AccessEntry equality and repr uses the correct `entity_type` * add a test for access_entries
1 parent 2d173a5 commit 0217637

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

google/cloud/bigquery/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def __ne__(self, other):
512512
return not self == other
513513

514514
def __repr__(self):
515-
return f"<AccessEntry: role={self.role}, {self._entity_type}={self.entity_id}>"
515+
return f"<AccessEntry: role={self.role}, {self.entity_type}={self.entity_id}>"
516516

517517
def _key(self):
518518
"""A tuple key that uniquely describes this field.
@@ -531,7 +531,7 @@ def _key(self):
531531
properties["condition"] = condition_key
532532

533533
prop_tup = tuple(sorted(properties.items()))
534-
return (self.role, self._entity_type, self.entity_id, prop_tup)
534+
return (self.role, self.entity_type, self.entity_id, prop_tup)
535535

536536
def __hash__(self):
537537
return hash(self._key())

tests/unit/test_dataset.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,15 @@ def test_equality_and_hash_without_condition(self):
613613
assert hash(entry1) == hash(entry2)
614614
assert hash(entry1) != hash(entry3) # Usually true
615615

616+
def test_equality_and_hash_from_api_repr(self):
617+
"""Compare equal entries where one was created via from_api_repr."""
618+
entry1 = AccessEntry("OWNER", "specialGroup", "projectOwners")
619+
entry2 = AccessEntry.from_api_repr(
620+
{"role": "OWNER", "specialGroup": "projectOwners"}
621+
)
622+
assert entry1 == entry2
623+
assert hash(entry1) == hash(entry2)
624+
616625
def test_equality_and_hash_with_condition(self, condition_1, condition_2):
617626
cond1a = Condition(
618627
condition_1.expression, condition_1.title, condition_1.description
@@ -746,6 +755,13 @@ def test_dataset_property_with_condition(self, condition_1):
746755
assert "dataset" in entry._properties
747756
assert "condition" in entry._properties
748757

758+
def test_repr_from_api_repr(self):
759+
"""Check that repr() includes the correct entity_type when the object is initialized from a dictionary."""
760+
api_repr = {"role": "OWNER", "userByEmail": "owner@example.com"}
761+
entry = AccessEntry.from_api_repr(api_repr)
762+
entry_str = repr(entry)
763+
assert entry_str == "<AccessEntry: role=OWNER, userByEmail=owner@example.com>"
764+
749765

750766
class TestDatasetReference(unittest.TestCase):
751767
@staticmethod
@@ -1097,6 +1113,34 @@ def test_ctor_explicit(self):
10971113
self.assertIsNone(dataset.location)
10981114
self.assertEqual(dataset.is_case_insensitive, False)
10991115

1116+
def test_access_entries_getter_from_api_repr(self):
1117+
"""Check that `in` works correctly when Dataset is made via from_api_repr()."""
1118+
from google.cloud.bigquery.dataset import AccessEntry
1119+
1120+
dataset = self._get_target_class().from_api_repr(
1121+
{
1122+
"datasetReference": {"projectId": "my-proj", "datasetId": "my_dset"},
1123+
"access": [
1124+
{
1125+
"role": "OWNER",
1126+
"userByEmail": "uilma@example.com",
1127+
},
1128+
{
1129+
"role": "READER",
1130+
"groupByEmail": "rhubbles@example.com",
1131+
},
1132+
],
1133+
}
1134+
)
1135+
assert (
1136+
AccessEntry("OWNER", "userByEmail", "uilma@example.com")
1137+
in dataset.access_entries
1138+
)
1139+
assert (
1140+
AccessEntry("READER", "groupByEmail", "rhubbles@example.com")
1141+
in dataset.access_entries
1142+
)
1143+
11001144
def test_access_entries_setter_non_list(self):
11011145
dataset = self._make_one(self.DS_REF)
11021146
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/googleapis/python-bigquery/commit/02176377d5e2fc25b5cd4f46aa6ebfb1b6a960a6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy