Skip to content

Commit b7b577e

Browse files
authored
Merge pull request #3093 from dhermes/fix-2746-allocate_ids
Making datastore Connection.allocate_ids() return low-level protobuf.
2 parents b032a66 + 978fa99 commit b7b577e

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-34
lines changed

packages/google-cloud-datastore/google/cloud/datastore/_http.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,12 @@ def allocate_ids(self, project, key_pbs):
468468
:class:`.entity_pb2.Key`
469469
:param key_pbs: The keys for which the backend should allocate IDs.
470470
471-
:rtype: list of :class:`.entity_pb2.Key`
472-
:returns: An equal number of keys, with IDs filled in by the backend.
471+
:rtype: :class:`.datastore_pb2.AllocateIdsResponse`
472+
:returns: The protobuf response from an allocate IDs request.
473473
"""
474474
request = _datastore_pb2.AllocateIdsRequest()
475475
_add_keys_to_request(request.keys, key_pbs)
476-
# Nothing to do with this response, so just execute the method.
477-
response = self._datastore_api.allocate_ids(project, request)
478-
return list(response.keys)
476+
return self._datastore_api.allocate_ids(project, request)
479477

480478

481479
def _set_read_options(request, eventual, transaction_id):

packages/google-cloud-datastore/google/cloud/datastore/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ def allocate_ids(self, incomplete_key, num_ids):
425425
incomplete_key_pbs = [incomplete_key_pb] * num_ids
426426

427427
conn = self._connection
428-
allocated_key_pbs = conn.allocate_ids(incomplete_key.project,
429-
incomplete_key_pbs)
428+
response_pb = conn.allocate_ids(
429+
incomplete_key.project, incomplete_key_pbs)
430430
allocated_ids = [allocated_key_pb.path[-1].id
431-
for allocated_key_pb in allocated_key_pbs]
431+
for allocated_key_pb in response_pb.keys]
432432
return [incomplete_key.completed_key(allocated_id)
433433
for allocated_id in allocated_ids]
434434

packages/google-cloud-datastore/unit_tests/test__http.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -804,55 +804,68 @@ def test_rollback_ok(self):
804804
def test_allocate_ids_empty(self):
805805
from google.cloud.proto.datastore.v1 import datastore_pb2
806806

807-
PROJECT = 'PROJECT'
807+
project = 'PROJECT'
808808
rsp_pb = datastore_pb2.AllocateIdsResponse()
809+
810+
# Create mock HTTP and client with response.
809811
http = Http({'status': '200'}, rsp_pb.SerializeToString())
810812
client = mock.Mock(_http=http, spec=['_http'])
813+
814+
# Make request.
811815
conn = self._make_one(client)
812-
URI = '/'.join([
816+
response = conn.allocate_ids(project, [])
817+
818+
# Check the result and verify the callers.
819+
self.assertEqual(list(response.keys), [])
820+
self.assertEqual(response, rsp_pb)
821+
uri = '/'.join([
813822
conn.api_base_url,
814823
conn.API_VERSION,
815824
'projects',
816-
PROJECT + ':allocateIds',
825+
project + ':allocateIds',
817826
])
818-
self.assertEqual(conn.allocate_ids(PROJECT, []), [])
819827
cw = http._called_with
820-
self._verify_protobuf_call(cw, URI, conn)
821-
rq_class = datastore_pb2.AllocateIdsRequest
822-
request = rq_class()
828+
self._verify_protobuf_call(cw, uri, conn)
829+
request = datastore_pb2.AllocateIdsRequest()
823830
request.ParseFromString(cw['body'])
824831
self.assertEqual(list(request.keys), [])
825832

826833
def test_allocate_ids_non_empty(self):
827834
from google.cloud.proto.datastore.v1 import datastore_pb2
828835

829-
PROJECT = 'PROJECT'
836+
project = 'PROJECT'
830837
before_key_pbs = [
831-
self._make_key_pb(PROJECT, id_=None),
832-
self._make_key_pb(PROJECT, id_=None),
838+
self._make_key_pb(project, id_=None),
839+
self._make_key_pb(project, id_=None),
833840
]
834841
after_key_pbs = [
835-
self._make_key_pb(PROJECT),
836-
self._make_key_pb(PROJECT, id_=2345),
842+
self._make_key_pb(project),
843+
self._make_key_pb(project, id_=2345),
837844
]
838845
rsp_pb = datastore_pb2.AllocateIdsResponse()
839846
rsp_pb.keys.add().CopyFrom(after_key_pbs[0])
840847
rsp_pb.keys.add().CopyFrom(after_key_pbs[1])
848+
849+
# Create mock HTTP and client with response.
841850
http = Http({'status': '200'}, rsp_pb.SerializeToString())
842851
client = mock.Mock(_http=http, spec=['_http'])
852+
853+
# Make request.
843854
conn = self._make_one(client)
844-
URI = '/'.join([
855+
response = conn.allocate_ids(project, before_key_pbs)
856+
857+
# Check the result and verify the callers.
858+
self.assertEqual(list(response.keys), after_key_pbs)
859+
self.assertEqual(response, rsp_pb)
860+
uri = '/'.join([
845861
conn.api_base_url,
846862
conn.API_VERSION,
847863
'projects',
848-
PROJECT + ':allocateIds',
864+
project + ':allocateIds',
849865
])
850-
self.assertEqual(conn.allocate_ids(PROJECT, before_key_pbs),
851-
after_key_pbs)
852866
cw = http._called_with
853-
self._verify_protobuf_call(cw, URI, conn)
854-
rq_class = datastore_pb2.AllocateIdsRequest
855-
request = rq_class()
867+
self._verify_protobuf_call(cw, uri, conn)
868+
request = datastore_pb2.AllocateIdsRequest()
856869
request.ParseFromString(cw['body'])
857870
self.assertEqual(len(request.keys), len(before_key_pbs))
858871
for key_before, key_after in zip(before_key_pbs, request.keys):

packages/google-cloud-datastore/unit_tests/test_client.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def test_ctor_w_project_no_environ(self):
149149
# this test would fail artificially.
150150
patch = mock.patch(
151151
'google.cloud.datastore.client._base_default_project',
152-
new=lambda project: None)
152+
return_value=None)
153153
with patch:
154154
self.assertRaises(EnvironmentError, self._make_one, None)
155155

@@ -679,18 +679,18 @@ def test_delete_multi_w_existing_transaction(self):
679679
self.assertEqual(len(client._connection._commit_cw), 0)
680680

681681
def test_allocate_ids_w_partial_key(self):
682-
NUM_IDS = 2
682+
num_ids = 2
683683

684-
INCOMPLETE_KEY = _Key(self.PROJECT)
685-
INCOMPLETE_KEY._id = None
684+
incomplete_key = _Key(self.PROJECT)
685+
incomplete_key._id = None
686686

687687
creds = _make_credentials()
688688
client = self._make_one(credentials=creds)
689689

690-
result = client.allocate_ids(INCOMPLETE_KEY, NUM_IDS)
690+
result = client.allocate_ids(incomplete_key, num_ids)
691691

692692
# Check the IDs returned.
693-
self.assertEqual([key._id for key in result], list(range(NUM_IDS)))
693+
self.assertEqual([key._id for key in result], list(range(num_ids)))
694694

695695
def test_allocate_ids_with_completed_key(self):
696696
creds = _make_credentials()
@@ -954,7 +954,8 @@ def commit(self, project, commit_request, transaction_id):
954954
def allocate_ids(self, project, key_pbs):
955955
self._alloc_cw.append((project, key_pbs))
956956
num_pbs = len(key_pbs)
957-
return [_KeyPB(i) for i in list(range(num_pbs))]
957+
keys = [_KeyPB(i) for i in list(range(num_pbs))]
958+
return mock.Mock(keys=keys, spec=['keys'])
958959

959960

960961
class _NoCommitBatch(object):

0 commit comments

Comments
 (0)