Skip to content

Commit b032a66

Browse files
authored
Merge pull request #3092 from dhermes/fix-2746-rollback
Making datastore Connection.rollback() return low-level protobuf.
2 parents 42cdf66 + 696eec9 commit b032a66

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,14 @@ def rollback(self, project, transaction_id):
447447
:type transaction_id: str
448448
:param transaction_id: The transaction ID returned from
449449
:meth:`begin_transaction`.
450+
451+
:rtype: :class:`.datastore_pb2.RollbackResponse`
452+
:returns: The returned protobuf response object.
450453
"""
451454
request = _datastore_pb2.RollbackRequest()
452455
request.transaction = transaction_id
453-
# Nothing to do with this response, so just execute the method.
454-
self._datastore_api.rollback(project, request)
456+
# Response is empty (i.e. no fields) but we return it anyway.
457+
return self._datastore_api.rollback(project, request)
455458

456459
def allocate_ids(self, project, key_pbs):
457460
"""Obtain backend-generated IDs for a set of keys.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def rollback(self):
200200
- Sets the current transaction's ID to None.
201201
"""
202202
try:
203+
# No need to use the response it contains nothing.
203204
self._client._connection.rollback(self.project, self._id)
204205
finally:
205206
super(Transaction, self).rollback()

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -775,26 +775,31 @@ def test_commit_w_transaction(self):
775775
def test_rollback_ok(self):
776776
from google.cloud.proto.datastore.v1 import datastore_pb2
777777

778-
PROJECT = 'PROJECT'
779-
TRANSACTION = b'xact'
780-
778+
project = 'PROJECT'
779+
transaction = b'xact'
781780
rsp_pb = datastore_pb2.RollbackResponse()
781+
782+
# Create mock HTTP and client with response.
782783
http = Http({'status': '200'}, rsp_pb.SerializeToString())
783784
client = mock.Mock(_http=http, spec=['_http'])
785+
786+
# Make request.
784787
conn = self._make_one(client)
785-
URI = '/'.join([
788+
response = conn.rollback(project, transaction)
789+
790+
# Check the result and verify the callers.
791+
self.assertEqual(response, rsp_pb)
792+
uri = '/'.join([
786793
conn.api_base_url,
787794
conn.API_VERSION,
788795
'projects',
789-
PROJECT + ':rollback',
796+
project + ':rollback',
790797
])
791-
self.assertIsNone(conn.rollback(PROJECT, TRANSACTION))
792798
cw = http._called_with
793-
self._verify_protobuf_call(cw, URI, conn)
794-
rq_class = datastore_pb2.RollbackRequest
795-
request = rq_class()
799+
self._verify_protobuf_call(cw, uri, conn)
800+
request = datastore_pb2.RollbackRequest()
796801
request.ParseFromString(cw['body'])
797-
self.assertEqual(request.transaction, TRANSACTION)
802+
self.assertEqual(request.transaction, transaction)
798803

799804
def test_allocate_ids_empty(self):
800805
from google.cloud.proto.datastore.v1 import datastore_pb2

0 commit comments

Comments
 (0)