From d5b1455a210e69516c8701eedac08e37b9a1a761 Mon Sep 17 00:00:00 2001 From: Tim Swena Date: Tue, 13 May 2025 13:23:17 -0500 Subject: [PATCH 1/2] fix: ensure AccessEntry equality and repr uses the correct `entity_type` --- google/cloud/bigquery/dataset.py | 4 ++-- tests/unit/test_dataset.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/google/cloud/bigquery/dataset.py b/google/cloud/bigquery/dataset.py index d225b7106..f788275cd 100644 --- a/google/cloud/bigquery/dataset.py +++ b/google/cloud/bigquery/dataset.py @@ -512,7 +512,7 @@ def __ne__(self, other): return not self == other def __repr__(self): - return f"" + return f"" def _key(self): """A tuple key that uniquely describes this field. @@ -531,7 +531,7 @@ def _key(self): properties["condition"] = condition_key prop_tup = tuple(sorted(properties.items())) - return (self.role, self._entity_type, self.entity_id, prop_tup) + return (self.role, self.entity_type, self.entity_id, prop_tup) def __hash__(self): return hash(self._key()) diff --git a/tests/unit/test_dataset.py b/tests/unit/test_dataset.py index 941430827..11cc3c8fe 100644 --- a/tests/unit/test_dataset.py +++ b/tests/unit/test_dataset.py @@ -613,6 +613,15 @@ def test_equality_and_hash_without_condition(self): assert hash(entry1) == hash(entry2) assert hash(entry1) != hash(entry3) # Usually true + def test_equality_and_hash_from_api_repr(self): + """Compare equal entries where one was created via from_api_repr.""" + entry1 = AccessEntry("OWNER", "specialGroup", "projectOwners") + entry2 = AccessEntry.from_api_repr( + {"role": "OWNER", "specialGroup": "projectOwners"} + ) + assert entry1 == entry2 + assert hash(entry1) == hash(entry2) + def test_equality_and_hash_with_condition(self, condition_1, condition_2): cond1a = Condition( condition_1.expression, condition_1.title, condition_1.description @@ -746,6 +755,13 @@ def test_dataset_property_with_condition(self, condition_1): assert "dataset" in entry._properties assert "condition" in entry._properties + def test_repr_from_api_repr(self): + """Check that repr() includes the correct entity_type when the object is initialized from a dictionary.""" + api_repr = {"role": "OWNER", "userByEmail": "owner@example.com"} + entry = AccessEntry.from_api_repr(api_repr) + entry_str = repr(entry) + assert entry_str == "" + class TestDatasetReference(unittest.TestCase): @staticmethod From 0cfa704beb564782d21c5d99f9e4aaf7c6f57b64 Mon Sep 17 00:00:00 2001 From: Tim Swena Date: Tue, 13 May 2025 14:05:04 -0500 Subject: [PATCH 2/2] add a test for access_entries --- tests/unit/test_dataset.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/unit/test_dataset.py b/tests/unit/test_dataset.py index 11cc3c8fe..5cce2a9a7 100644 --- a/tests/unit/test_dataset.py +++ b/tests/unit/test_dataset.py @@ -1113,6 +1113,34 @@ def test_ctor_explicit(self): self.assertIsNone(dataset.location) self.assertEqual(dataset.is_case_insensitive, False) + def test_access_entries_getter_from_api_repr(self): + """Check that `in` works correctly when Dataset is made via from_api_repr().""" + from google.cloud.bigquery.dataset import AccessEntry + + dataset = self._get_target_class().from_api_repr( + { + "datasetReference": {"projectId": "my-proj", "datasetId": "my_dset"}, + "access": [ + { + "role": "OWNER", + "userByEmail": "uilma@example.com", + }, + { + "role": "READER", + "groupByEmail": "rhubbles@example.com", + }, + ], + } + ) + assert ( + AccessEntry("OWNER", "userByEmail", "uilma@example.com") + in dataset.access_entries + ) + assert ( + AccessEntry("READER", "groupByEmail", "rhubbles@example.com") + in dataset.access_entries + ) + def test_access_entries_setter_non_list(self): dataset = self._make_one(self.DS_REF) with self.assertRaises(TypeError):