Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: ensure AccessEntry equality and repr uses the correct entity_type #2182

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

Merged
merged 2 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def __ne__(self, other):
return not self == other

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

def _key(self):
"""A tuple key that uniquely describes this field.
Expand All @@ -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())
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test_dataset.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I did a mini-TDD for this. Both of these tests fail without the changes in dataset.py.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check the array of AccessEntry works too? The g3 failed tests show the None entity type in the array: https://screenshot.googleplex.com/B88hiRSwcAfeE49

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 0cfa704

Also verified that the added test fails until the changes in dataset.py.

"""Check that repr() includes the correct entity_type when the object is initialized from a dictionary."""
api_repr = {"role": "OWNER", "userByEmail": "[email protected]"}
entry = AccessEntry.from_api_repr(api_repr)
entry_str = repr(entry)
assert entry_str == "<AccessEntry: role=OWNER, [email protected]>"


class TestDatasetReference(unittest.TestCase):
@staticmethod
Expand Down Expand Up @@ -1097,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": "[email protected]",
},
{
"role": "READER",
"groupByEmail": "[email protected]",
},
],
}
)
assert (
AccessEntry("OWNER", "userByEmail", "[email protected]")
in dataset.access_entries
)
assert (
AccessEntry("READER", "groupByEmail", "[email protected]")
in dataset.access_entries
)

def test_access_entries_setter_non_list(self):
dataset = self._make_one(self.DS_REF)
with self.assertRaises(TypeError):
Expand Down