Skip to content

Commit

Permalink
Fix #129: use transaction ID from connection's own transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Sep 24, 2014
1 parent b97ce73 commit 4720557
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
9 changes: 3 additions & 6 deletions gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,20 @@ def begin_transaction(self, dataset_id, serializable=False):

return response.transaction

def rollback_transaction(self, dataset_id, transaction_id):
"""Rollback an existing transaction.
def rollback_transaction(self, dataset_id):
"""Rollback the connection's existing transaction.
Raises a ``ValueError``
if the connection isn't currently in a transaction.
:type dataset_id: string
:param dataset_id: The dataset to which the transaction belongs.
:type transaction_id: string
:param transaction_id: The ID of the transaction to roll back.
"""
if not self.transaction() or not self.transaction().id():
raise ValueError('No transaction to rollback.')

request = datastore_pb.RollbackRequest()
request.transaction = transaction_id
request.transaction = self.transaction().id()
# Nothing to do with this response, so just execute the method.
self._rpc(dataset_id, 'rollback', request,
datastore_pb.RollbackResponse)
Expand Down
19 changes: 7 additions & 12 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,31 +244,27 @@ def test_begin_transaction_explicit_serialize(self):

def test_rollback_transaction_wo_existing_transaction(self):
DATASET_ID = 'DATASET'
TRANSACTION_ID = 'TRANSACTION'
conn = self._makeOne()
self.assertRaises(ValueError,
conn.rollback_transaction, DATASET_ID, TRANSACTION_ID)
conn.rollback_transaction, DATASET_ID)

def test_rollback_transaction_w_existing_transaction_no_id(self):
class Xact(object):
def id(self):
return None
DATASET_ID = 'DATASET'
TRANSACTION_ID = 'TRANSACTION'
conn = self._makeOne()
conn.transaction(Xact())
self.assertRaises(ValueError,
conn.rollback_transaction, DATASET_ID, TRANSACTION_ID)
conn.rollback_transaction, DATASET_ID)

def test_rollback_transaction_ok(self):
from gcloud.datastore.connection import datastore_pb
DATASET_ID = 'DATASET'
TRANSACTION = 'xact'
class Xact(object):
def id(self):
return 'xact'
xact = object()

DATASET_ID = 'DATASET'
TRANSACTION = 'TRANSACTION'
return TRANSACTION
rsp_pb = datastore_pb.RollbackResponse()
conn = self._makeOne()
conn.transaction(Xact())
Expand All @@ -280,14 +276,13 @@ def id(self):
'rollback',
])
http = conn._http = Http({'status': '200'}, rsp_pb.SerializeToString())
self.assertEqual(conn.rollback_transaction(DATASET_ID, TRANSACTION),
None)
self.assertEqual(conn.rollback_transaction(DATASET_ID), None)
cw = http._called_with
self.assertEqual(cw['uri'], URI)
self.assertEqual(cw['method'], 'POST')
self.assertEqual(cw['headers'],
{'Content-Type': 'application/x-protobuf',
'Content-Length': '13',
'Content-Length': '6',
})
rq_class = datastore_pb.RollbackRequest
request = rq_class()
Expand Down

0 comments on commit 4720557

Please sign in to comment.