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/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()