Skip to content

Commit 978fa99

Browse files
committed
Making datastore Connection.allocate_ids() return low-level protobuf.
1 parent 7e0a526 commit 978fa99

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
@@ -466,14 +466,12 @@ def allocate_ids(self, project, key_pbs):
466466
:class:`.entity_pb2.Key`
467467
:param key_pbs: The keys for which the backend should allocate IDs.
468468
469-
:rtype: list of :class:`.entity_pb2.Key`
470-
:returns: An equal number of keys, with IDs filled in by the backend.
469+
:rtype: :class:`.datastore_pb2.AllocateIdsResponse`
470+
:returns: The protobuf response from an allocate IDs request.
471471
"""
472472
request = _datastore_pb2.AllocateIdsRequest()
473473
_add_keys_to_request(request.keys, key_pbs)
474-
# Nothing to do with this response, so just execute the method.
475-
response = self._datastore_api.allocate_ids(project, request)
476-
return list(response.keys)
474+
return self._datastore_api.allocate_ids(project, request)
477475

478476

479477
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
@@ -791,55 +791,68 @@ def test_rollback_ok(self):
791791
def test_allocate_ids_empty(self):
792792
from google.cloud.proto.datastore.v1 import datastore_pb2
793793

794-
PROJECT = 'PROJECT'
794+
project = 'PROJECT'
795795
rsp_pb = datastore_pb2.AllocateIdsResponse()
796+
797+
# Create mock HTTP and client with response.
796798
http = Http({'status': '200'}, rsp_pb.SerializeToString())
797799
client = mock.Mock(_http=http, spec=['_http'])
800+
801+
# Make request.
798802
conn = self._make_one(client)
799-
URI = '/'.join([
803+
response = conn.allocate_ids(project, [])
804+
805+
# Check the result and verify the callers.
806+
self.assertEqual(list(response.keys), [])
807+
self.assertEqual(response, rsp_pb)
808+
uri = '/'.join([
800809
conn.api_base_url,
801810
conn.API_VERSION,
802811
'projects',
803-
PROJECT + ':allocateIds',
812+
project + ':allocateIds',
804813
])
805-
self.assertEqual(conn.allocate_ids(PROJECT, []), [])
806814
cw = http._called_with
807-
self._verify_protobuf_call(cw, URI, conn)
808-
rq_class = datastore_pb2.AllocateIdsRequest
809-
request = rq_class()
815+
self._verify_protobuf_call(cw, uri, conn)
816+
request = datastore_pb2.AllocateIdsRequest()
810817
request.ParseFromString(cw['body'])
811818
self.assertEqual(list(request.keys), [])
812819

813820
def test_allocate_ids_non_empty(self):
814821
from google.cloud.proto.datastore.v1 import datastore_pb2
815822

816-
PROJECT = 'PROJECT'
823+
project = 'PROJECT'
817824
before_key_pbs = [
818-
self._make_key_pb(PROJECT, id_=None),
819-
self._make_key_pb(PROJECT, id_=None),
825+
self._make_key_pb(project, id_=None),
826+
self._make_key_pb(project, id_=None),
820827
]
821828
after_key_pbs = [
822-
self._make_key_pb(PROJECT),
823-
self._make_key_pb(PROJECT, id_=2345),
829+
self._make_key_pb(project),
830+
self._make_key_pb(project, id_=2345),
824831
]
825832
rsp_pb = datastore_pb2.AllocateIdsResponse()
826833
rsp_pb.keys.add().CopyFrom(after_key_pbs[0])
827834
rsp_pb.keys.add().CopyFrom(after_key_pbs[1])
835+
836+
# Create mock HTTP and client with response.
828837
http = Http({'status': '200'}, rsp_pb.SerializeToString())
829838
client = mock.Mock(_http=http, spec=['_http'])
839+
840+
# Make request.
830841
conn = self._make_one(client)
831-
URI = '/'.join([
842+
response = conn.allocate_ids(project, before_key_pbs)
843+
844+
# Check the result and verify the callers.
845+
self.assertEqual(list(response.keys), after_key_pbs)
846+
self.assertEqual(response, rsp_pb)
847+
uri = '/'.join([
832848
conn.api_base_url,
833849
conn.API_VERSION,
834850
'projects',
835-
PROJECT + ':allocateIds',
851+
project + ':allocateIds',
836852
])
837-
self.assertEqual(conn.allocate_ids(PROJECT, before_key_pbs),
838-
after_key_pbs)
839853
cw = http._called_with
840-
self._verify_protobuf_call(cw, URI, conn)
841-
rq_class = datastore_pb2.AllocateIdsRequest
842-
request = rq_class()
854+
self._verify_protobuf_call(cw, uri, conn)
855+
request = datastore_pb2.AllocateIdsRequest()
843856
request.ParseFromString(cw['body'])
844857
self.assertEqual(len(request.keys), len(before_key_pbs))
845858
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)