Skip to content

Commit

Permalink
fix: normalize / test deprecation of 'Client.reserve_ids' (#103)
Browse files Browse the repository at this point in the history
Closes #101
Closes #100

Co-authored-by: Christopher Wilcox <crwilcox@google.com>
  • Loading branch information
tseaver and crwilcox committed Nov 11, 2020
1 parent 556c8f0 commit 5851522
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
10 changes: 5 additions & 5 deletions google/cloud/datastore/client.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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
)
Expand Down
48 changes: 42 additions & 6 deletions tests/unit/test_client.py
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 5851522

Please sign in to comment.