diff --git a/CHANGELOG.md b/CHANGELOG.md index d8eb7e08..1a3fd6d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ [1]: https://pypi.org/project/google-cloud-datastore/#history +### [2.0.1](https://www.github.com/googleapis/python-datastore/compare/v2.0.0...v2.0.1) (2020-11-13) + + +### Bug Fixes + +* fix id_or_name property of key class ([#115](https://www.github.com/googleapis/python-datastore/issues/115)) ([6f28b84](https://www.github.com/googleapis/python-datastore/commit/6f28b84fcc8c593bf7fbd6335999f3cc6da56cd4)) +* normalize / test deprecation of 'Client.reserve_ids' ([#103](https://www.github.com/googleapis/python-datastore/issues/103)) ([5851522](https://www.github.com/googleapis/python-datastore/commit/5851522900fc07c9cc13e1af2cf7b54d709c9ddb)), closes [#101](https://www.github.com/googleapis/python-datastore/issues/101) [#100](https://www.github.com/googleapis/python-datastore/issues/100) + ## [2.0.0](https://www.github.com/googleapis/python-datastore/compare/v1.15.3...v2.0.0) (2020-11-06) diff --git a/google/cloud/datastore/client.py b/google/cloud/datastore/client.py index e06b8e60..24b53b54 100644 --- a/google/cloud/datastore/client.py +++ b/google/cloud/datastore/client.py @@ -60,6 +60,10 @@ DISABLE_GRPC = "GOOGLE_CLOUD_DISABLE_GRPC" """Environment variable acting as flag to disable gRPC.""" +_RESERVE_IDS_DEPRECATED_MESSAGE = """\ +Client.reserve_ids is deprecated. Please use \ +Client.reserve_ids_multi or Client.reserve_ids_sequential""" + _USE_GRPC = _HAVE_GRPC and not os.getenv(DISABLE_GRPC, False) @@ -890,11 +894,7 @@ def reserve_ids(self, complete_key, num_ids, retry=None, timeout=None): Please use either :meth:`reserve_ids_multi` (recommended) or :meth:`reserve_ids_sequential`. """ - message = ( - "Client.reserve_ids is deprecated. Please use " - "Client.reserve_ids_multi or Client.reserve_ids_sequential", - ) - warnings.warn(message, DeprecationWarning) + warnings.warn(_RESERVE_IDS_DEPRECATED_MESSAGE, DeprecationWarning) return self.reserve_ids_sequential( complete_key, num_ids, retry=retry, timeout=timeout ) diff --git a/google/cloud/datastore/key.py b/google/cloud/datastore/key.py index d03359bc..c9beaeb2 100644 --- a/google/cloud/datastore/key.py +++ b/google/cloud/datastore/key.py @@ -441,7 +441,9 @@ def id_or_name(self): :returns: The last element of the key's path if it is either an ``id`` or a ``name``. """ - return self.id or self.name + if self.id is None: + return self.name + return self.id @property def project(self): diff --git a/google/cloud/datastore/version.py b/google/cloud/datastore/version.py index a12de3d2..956a957b 100644 --- a/google/cloud/datastore/version.py +++ b/google/cloud/datastore/version.py @@ -12,4 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -__version__ = "2.0.0" +__version__ = "2.0.1" diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 59588f10..55a45c7f 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -1113,6 +1113,8 @@ def test_reserve_ids_sequential_w_non_numeric_key_name(self): client.reserve_ids_sequential(complete_key, num_ids) def test_reserve_ids_w_completed_key(self): + import warnings + num_ids = 2 creds = _make_credentials() client = self._make_one(credentials=creds, _use_grpc=False) @@ -1122,7 +1124,8 @@ def test_reserve_ids_w_completed_key(self): client._datastore_api_internal = ds_api self.assertTrue(not complete_key.is_partial) - client.reserve_ids(complete_key, num_ids) + with warnings.catch_warnings(record=True) as warned: + client.reserve_ids(complete_key, num_ids) reserved_keys = ( _Key(_Key.kind, id) @@ -1133,7 +1136,12 @@ def test_reserve_ids_w_completed_key(self): request={"project_id": self.PROJECT, "keys": expected_keys} ) + self.assertEqual(len(warned), 1) + self.assertIn("Client.reserve_ids is deprecated.", str(warned[0].message)) + def test_reserve_ids_w_completed_key_w_retry_w_timeout(self): + import warnings + num_ids = 2 retry = mock.Mock() timeout = 100000 @@ -1146,7 +1154,8 @@ def test_reserve_ids_w_completed_key_w_retry_w_timeout(self): ds_api = mock.Mock(reserve_ids=reserve_ids, spec=["reserve_ids"]) client._datastore_api_internal = ds_api - client.reserve_ids(complete_key, num_ids, retry=retry, timeout=timeout) + with warnings.catch_warnings(record=True) as warned: + client.reserve_ids(complete_key, num_ids, retry=retry, timeout=timeout) reserved_keys = ( _Key(_Key.kind, id) @@ -1159,7 +1168,12 @@ def test_reserve_ids_w_completed_key_w_retry_w_timeout(self): timeout=timeout, ) + self.assertEqual(len(warned), 1) + self.assertIn("Client.reserve_ids is deprecated.", str(warned[0].message)) + def test_reserve_ids_w_completed_key_w_ancestor(self): + import warnings + num_ids = 2 creds = _make_credentials() client = self._make_one(credentials=creds, _use_grpc=False) @@ -1169,7 +1183,8 @@ def test_reserve_ids_w_completed_key_w_ancestor(self): client._datastore_api_internal = ds_api self.assertTrue(not complete_key.is_partial) - client.reserve_ids(complete_key, num_ids) + with warnings.catch_warnings(record=True) as warned: + client.reserve_ids(complete_key, num_ids) reserved_keys = ( _Key("PARENT", "SINGLETON", _Key.kind, id) @@ -1180,29 +1195,50 @@ def test_reserve_ids_w_completed_key_w_ancestor(self): request={"project_id": self.PROJECT, "keys": expected_keys} ) + self.assertEqual(len(warned), 1) + self.assertIn("Client.reserve_ids is deprecated.", str(warned[0].message)) + def test_reserve_ids_w_partial_key(self): + import warnings + num_ids = 2 incomplete_key = _Key(_Key.kind, None) creds = _make_credentials() client = self._make_one(credentials=creds) with self.assertRaises(ValueError): - client.reserve_ids(incomplete_key, num_ids) + with warnings.catch_warnings(record=True) as warned: + client.reserve_ids(incomplete_key, num_ids) + + self.assertEqual(len(warned), 1) + self.assertIn("Client.reserve_ids is deprecated.", str(warned[0].message)) def test_reserve_ids_w_wrong_num_ids(self): + import warnings + num_ids = "2" complete_key = _Key() creds = _make_credentials() client = self._make_one(credentials=creds) with self.assertRaises(ValueError): - client.reserve_ids(complete_key, num_ids) + with warnings.catch_warnings(record=True) as warned: + client.reserve_ids(complete_key, num_ids) + + self.assertEqual(len(warned), 1) + self.assertIn("Client.reserve_ids is deprecated.", str(warned[0].message)) def test_reserve_ids_w_non_numeric_key_name(self): + import warnings + num_ids = 2 complete_key = _Key(_Key.kind, "batman") creds = _make_credentials() client = self._make_one(credentials=creds) with self.assertRaises(ValueError): - client.reserve_ids(complete_key, num_ids) + with warnings.catch_warnings(record=True) as warned: + client.reserve_ids(complete_key, num_ids) + + self.assertEqual(len(warned), 1) + self.assertIn("Client.reserve_ids is deprecated.", str(warned[0].message)) def test_reserve_ids_multi(self): creds = _make_credentials() diff --git a/tests/unit/test_key.py b/tests/unit/test_key.py index 73565ead..9d130fb4 100644 --- a/tests/unit/test_key.py +++ b/tests/unit/test_key.py @@ -488,6 +488,11 @@ def test_id_or_name_w_name_only(self): key = self._make_one("KIND", _NAME, project=self._DEFAULT_PROJECT) self.assertEqual(key.id_or_name, _NAME) + def test_id_or_name_w_id_zero(self): + _ID = 0 + key = self._make_one("KIND", _ID, project=self._DEFAULT_PROJECT) + self.assertEqual(key.id_or_name, _ID) + def test_parent_default(self): key = self._make_one("KIND", project=self._DEFAULT_PROJECT) self.assertIsNone(key.parent)