Skip to content

Commit f44e598

Browse files
committed
Making datastore Connection.begin_transaction() return low-level protobuf.
1 parent 1b482f0 commit f44e598

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

datastore/google/cloud/datastore/_http.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,11 @@ def begin_transaction(self, project):
399399
:type project: str
400400
:param project: The project to which the transaction applies.
401401
402-
:rtype: bytes
402+
:rtype: :class:`.datastore_pb2.BeginTransactionResponse`
403403
:returns: The serialized transaction that was begun.
404404
"""
405405
request = _datastore_pb2.BeginTransactionRequest()
406-
response = self._datastore_api.begin_transaction(project, request)
407-
return response.transaction
406+
return self._datastore_api.begin_transaction(project, request)
408407

409408
def commit(self, project, request, transaction_id):
410409
"""Commit mutations in context of current transaction (if any).

datastore/google/cloud/datastore/transaction.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ def begin(self):
184184
"""
185185
super(Transaction, self).begin()
186186
try:
187-
self._id = self._client._connection.begin_transaction(
187+
response_pb = self._client._connection.begin_transaction(
188188
self.project)
189+
self._id = response_pb.transaction
189190
except: # noqa: E722 do not use bare except, specify exception instead
190191
self._status = self._ABORTED
191192
raise

datastore/unit_tests/test__http.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -672,25 +672,33 @@ def test_run_query_w_namespace_nonempty_result(self):
672672
def test_begin_transaction(self):
673673
from google.cloud.proto.datastore.v1 import datastore_pb2
674674

675-
PROJECT = 'PROJECT'
676-
TRANSACTION = b'TRANSACTION'
675+
project = 'PROJECT'
676+
transaction = b'TRANSACTION'
677677
rsp_pb = datastore_pb2.BeginTransactionResponse()
678-
rsp_pb.transaction = TRANSACTION
678+
rsp_pb.transaction = transaction
679+
680+
# Create mock HTTP and client with response.
679681
http = Http({'status': '200'}, rsp_pb.SerializeToString())
680682
client = mock.Mock(_http=http, spec=['_http'])
683+
684+
# Make request.
681685
conn = self._make_one(client)
682-
URI = '/'.join([
686+
response = conn.begin_transaction(project)
687+
688+
# Check the result and verify the callers.
689+
self.assertEqual(response, rsp_pb)
690+
uri = '/'.join([
683691
conn.api_base_url,
684692
conn.API_VERSION,
685693
'projects',
686-
PROJECT + ':beginTransaction',
694+
project + ':beginTransaction',
687695
])
688-
self.assertEqual(conn.begin_transaction(PROJECT), TRANSACTION)
689696
cw = http._called_with
690-
self._verify_protobuf_call(cw, URI, conn)
691-
rq_class = datastore_pb2.BeginTransactionRequest
692-
request = rq_class()
697+
self._verify_protobuf_call(cw, uri, conn)
698+
request = datastore_pb2.BeginTransactionRequest()
693699
request.ParseFromString(cw['body'])
700+
# The RPC-over-HTTP request does not set the project in the request.
701+
self.assertEqual(request.project_id, u'')
694702

695703
def test_commit_wo_transaction(self):
696704
from google.cloud.proto.datastore.v1 import datastore_pb2

datastore/unit_tests/test_transaction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ def __init__(self, xact_id=123, keys=()):
206206
mutation_results=mutation_results)
207207

208208
def begin_transaction(self, project):
209+
import mock
210+
209211
self._begun = project
210212
if self._side_effect is None:
211-
return self._xact_id
213+
return mock.Mock(
214+
transaction=self._xact_id, spec=['transaction'])
212215
else:
213216
raise self._side_effect
214217

0 commit comments

Comments
 (0)