Skip to content

Commit a25b2e4

Browse files
lukesneeringerlandrito
authored andcommitted
session.run_in_transaction returns the callback's return value. (googleapis#3753)
1 parent c7f9bc1 commit a25b2e4

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

spanner/google/cloud/spanner/session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,9 @@ def run_in_transaction(self, func, *args, **kw):
268268
If passed, "timeout_secs" will be removed and used to
269269
override the default timeout.
270270
271-
:rtype: :class:`datetime.datetime`
272-
:returns: timestamp of committed transaction
271+
:rtype: Any
272+
:returns: The return value of ``func``.
273+
273274
:raises Exception:
274275
reraises any non-ABORT execptions raised by ``func``.
275276
"""
@@ -284,7 +285,7 @@ def run_in_transaction(self, func, *args, **kw):
284285
if txn._transaction_id is None:
285286
txn.begin()
286287
try:
287-
func(txn, *args, **kw)
288+
return_value = func(txn, *args, **kw)
288289
except GaxError as exc:
289290
_delay_until_retry(exc, deadline)
290291
del self._transaction
@@ -299,8 +300,7 @@ def run_in_transaction(self, func, *args, **kw):
299300
_delay_until_retry(exc, deadline)
300301
del self._transaction
301302
else:
302-
committed = txn.committed
303-
return committed
303+
return return_value
304304

305305

306306
# pylint: disable=misplaced-bare-raise

spanner/tests/unit/test_database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from google.cloud.spanner import __version__
2323

2424

25-
def _make_credentials():
25+
def _make_credentials(): # pragma: NO COVER
2626
import google.auth.credentials
2727

2828
class _CredentialsWithScopes(
@@ -223,7 +223,7 @@ def __init__(self, scopes=(), source=None):
223223
self._scopes = scopes
224224
self._source = source
225225

226-
def requires_scopes(self):
226+
def requires_scopes(self): # pragma: NO COVER
227227
return True
228228

229229
def with_scopes(self, scopes):

spanner/tests/unit/test_session.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,16 @@ def test_run_in_transaction_w_args_w_kwargs_wo_abort(self):
513513
def unit_of_work(txn, *args, **kw):
514514
called_with.append((txn, args, kw))
515515
txn.insert(TABLE_NAME, COLUMNS, VALUES)
516+
return 42
516517

517-
committed = session.run_in_transaction(
518+
return_value = session.run_in_transaction(
518519
unit_of_work, 'abc', some_arg='def')
519520

520-
self.assertEqual(committed, now)
521521
self.assertIsNone(session._transaction)
522522
self.assertEqual(len(called_with), 1)
523523
txn, args, kw = called_with[0]
524524
self.assertIsInstance(txn, Transaction)
525-
self.assertEqual(txn.committed, committed)
525+
self.assertEqual(return_value, 42)
526526
self.assertEqual(args, ('abc',))
527527
self.assertEqual(kw, {'some_arg': 'def'})
528528

@@ -561,18 +561,15 @@ def test_run_in_transaction_w_abort_no_retry_metadata(self):
561561
def unit_of_work(txn, *args, **kw):
562562
called_with.append((txn, args, kw))
563563
txn.insert(TABLE_NAME, COLUMNS, VALUES)
564+
return 'answer'
564565

565-
committed = session.run_in_transaction(
566+
return_value = session.run_in_transaction(
566567
unit_of_work, 'abc', some_arg='def')
567568

568-
self.assertEqual(committed, now)
569569
self.assertEqual(len(called_with), 2)
570570
for index, (txn, args, kw) in enumerate(called_with):
571571
self.assertIsInstance(txn, Transaction)
572-
if index == 1:
573-
self.assertEqual(txn.committed, committed)
574-
else:
575-
self.assertIsNone(txn.committed)
572+
self.assertEqual(return_value, 'answer')
576573
self.assertEqual(args, ('abc',))
577574
self.assertEqual(kw, {'some_arg': 'def'})
578575

@@ -621,17 +618,15 @@ def unit_of_work(txn, *args, **kw):
621618
time_module = _FauxTimeModule()
622619

623620
with _Monkey(MUT, time=time_module):
624-
committed = session.run_in_transaction(
625-
unit_of_work, 'abc', some_arg='def')
621+
session.run_in_transaction(unit_of_work, 'abc', some_arg='def')
626622

627623
self.assertEqual(time_module._slept,
628624
RETRY_SECONDS + RETRY_NANOS / 1.0e9)
629-
self.assertEqual(committed, now)
630625
self.assertEqual(len(called_with), 2)
631626
for index, (txn, args, kw) in enumerate(called_with):
632627
self.assertIsInstance(txn, Transaction)
633628
if index == 1:
634-
self.assertEqual(txn.committed, committed)
629+
self.assertEqual(txn.committed, now)
635630
else:
636631
self.assertIsNone(txn.committed)
637632
self.assertEqual(args, ('abc',))
@@ -688,9 +683,8 @@ def unit_of_work(txn, *args, **kw):
688683
time_module = _FauxTimeModule()
689684

690685
with _Monkey(MUT, time=time_module):
691-
committed = session.run_in_transaction(unit_of_work)
686+
session.run_in_transaction(unit_of_work)
692687

693-
self.assertEqual(committed, now)
694688
self.assertEqual(time_module._slept,
695689
RETRY_SECONDS + RETRY_NANOS / 1.0e9)
696690
self.assertEqual(len(called_with), 2)

0 commit comments

Comments
 (0)