Skip to content

Commit

Permalink
Add 'Transaction.current' staticmethod.
Browse files Browse the repository at this point in the history
Returns top of stack IFF it is a transaction, or None.
  • Loading branch information
tseaver committed Jan 15, 2015
1 parent dd7a598 commit 301498d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gcloud/datastore/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ def test_ctor_with_env(self):
self.assertEqual(xact.dataset_id, DATASET_ID)
self.assertEqual(xact.connection, CONNECTION)

def test_current(self):
from gcloud.datastore.test_api import _NoCommitBatch
_DATASET = 'DATASET'
connection = _Connection()
xact1 = self._makeOne(_DATASET, connection)
xact2 = self._makeOne(_DATASET, connection)
self.assertTrue(xact1.current() is None)
self.assertTrue(xact2.current() is None)
with xact1:
self.assertTrue(xact1.current() is xact1)
self.assertTrue(xact2.current() is xact1)
with _NoCommitBatch(_DATASET, _Connection):
self.assertTrue(xact1.current() is None)
self.assertTrue(xact2.current() is None)
with xact2:
self.assertTrue(xact1.current() is xact2)
self.assertTrue(xact2.current() is xact2)
with _NoCommitBatch(_DATASET, _Connection):
self.assertTrue(xact1.current() is None)
self.assertTrue(xact2.current() is None)
self.assertTrue(xact1.current() is xact1)
self.assertTrue(xact2.current() is xact1)
self.assertTrue(xact1.current() is None)
self.assertTrue(xact2.current() is None)

def test_begin(self):
_DATASET = 'DATASET'
connection = _Connection(234)
Expand Down
13 changes: 13 additions & 0 deletions gcloud/datastore/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ def id(self):
"""
return self._id

@staticmethod
def current():
"""Return the topmost transaction.
.. note:: if the topmost element on the stack is not a transaction,
returns None.
:rtype: :class:`gcloud.datastore.transaction.Transaction` or None
"""
top = Batch.current()
if isinstance(top, Transaction):
return top

def begin(self):
"""Begins a transaction.
Expand Down

0 comments on commit 301498d

Please sign in to comment.