From a40d7ae03149708fc34c962b43a6ac198780b6aa Mon Sep 17 00:00:00 2001 From: Jonathan Ostrander Date: Thu, 19 Oct 2023 16:07:37 -0400 Subject: [PATCH] fix: AccessEntry API representation parsing (#1682) * 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 --- google/cloud/bigquery/dataset.py | 4 +--- tests/unit/test_dataset.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/google/cloud/bigquery/dataset.py b/google/cloud/bigquery/dataset.py index b7fed61c7..0f1a0f3cc 100644 --- a/google/cloud/bigquery/dataset.py +++ b/google/cloud/bigquery/dataset.py @@ -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): diff --git a/tests/unit/test_dataset.py b/tests/unit/test_dataset.py index 7d7091092..0a709ab43 100644 --- a/tests/unit/test_dataset.py +++ b/tests/unit/test_dataset.py @@ -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",