Skip to content

Commit

Permalink
fix: AccessEntry API representation parsing (#1682)
Browse files Browse the repository at this point in the history
* fix: AccessEntry API representation parsing

Overriding the `AccessEntry#_properties` with a deep copy of the API resource overwrites the `role` property set in `AccessEntry.__init__` which isn't present in the resource if the `role` is set to `None`. This causes `AccessEntry`s generated from API representations to no longer evaluate to equal with equivalent `AccessEntry` resources instantiated through `AccessEntry.__init__`. The added unit test fails without the change and passes with the change.

* build: formatting

---------

Co-authored-by: Lingqing Gan <lingqing.gan@gmail.com>
  • Loading branch information
jonathan-ostrander and Linchin committed Oct 19, 2023
1 parent 8f187e6 commit a40d7ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 1 addition & 3 deletions google/cloud/bigquery/dataset.py
Expand Up @@ -501,9 +501,7 @@ def from_api_repr(cls, resource: dict) -> "AccessEntry":
if len(entry) != 0:
raise ValueError("Entry has unexpected keys remaining.", entry)

config = cls(role, entity_type, entity_id)
config._properties = copy.deepcopy(resource)
return config
return cls(role, entity_type, entity_id)


class Dataset(object):
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_dataset.py
Expand Up @@ -152,6 +152,22 @@ def test_from_api_repr_w_unknown_entity_type(self):
exp_resource = entry.to_api_repr()
self.assertEqual(resource, exp_resource)

def test_from_api_repr_wo_role(self):
resource = {
"view": {
"projectId": "my-project",
"datasetId": "my_dataset",
"tableId": "my_table",
}
}
entry = self._get_target_class().from_api_repr(resource)
exp_entry = self._make_one(
role=None,
entity_type="view",
entity_id=resource["view"],
)
self.assertEqual(entry, exp_entry)

def test_to_api_repr_w_extra_properties(self):
resource = {
"role": "READER",
Expand Down

0 comments on commit a40d7ae

Please sign in to comment.