Skip to content

Commit e640e66

Browse files
authored
fix: await on to_wrap in AsyncTransactional (#147)
1 parent 55da695 commit e640e66

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

google/cloud/firestore_v1/async_transaction.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,31 @@ class _AsyncTransactional(_BaseTransactional):
188188
:func:`~google.cloud.firestore_v1.async_transaction.transactional`.
189189
190190
Args:
191-
to_wrap (Callable[[:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`, ...], Any]):
192-
A callable that should be run (and retried) in a transaction.
191+
to_wrap (Coroutine[[:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`, ...], Any]):
192+
A coroutine that should be run (and retried) in a transaction.
193193
"""
194194

195195
def __init__(self, to_wrap) -> None:
196196
super(_AsyncTransactional, self).__init__(to_wrap)
197197

198198
async def _pre_commit(self, transaction, *args, **kwargs) -> Coroutine:
199-
"""Begin transaction and call the wrapped callable.
199+
"""Begin transaction and call the wrapped coroutine.
200200
201-
If the callable raises an exception, the transaction will be rolled
201+
If the coroutine raises an exception, the transaction will be rolled
202202
back. If not, the transaction will be "ready" for ``Commit`` (i.e.
203203
it will have staged writes).
204204
205205
Args:
206206
transaction
207207
(:class:`~google.cloud.firestore_v1.async_transaction.AsyncTransaction`):
208-
A transaction to execute the callable within.
208+
A transaction to execute the coroutine within.
209209
args (Tuple[Any, ...]): The extra positional arguments to pass
210-
along to the wrapped callable.
210+
along to the wrapped coroutine.
211211
kwargs (Dict[str, Any]): The extra keyword arguments to pass
212-
along to the wrapped callable.
212+
along to the wrapped coroutine.
213213
214214
Returns:
215-
Any: result of the wrapped callable.
215+
Any: result of the wrapped coroutine.
216216
217217
Raises:
218218
Exception: Any failure caused by ``to_wrap``.
@@ -226,7 +226,7 @@ async def _pre_commit(self, transaction, *args, **kwargs) -> Coroutine:
226226
if self.retry_id is None:
227227
self.retry_id = self.current_id
228228
try:
229-
return self.to_wrap(transaction, *args, **kwargs)
229+
return await self.to_wrap(transaction, *args, **kwargs)
230230
except: # noqa
231231
# NOTE: If ``rollback`` fails this will lose the information
232232
# from the original failure.

tests/unit/v1/test_async_transaction.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def test_constructor(self):
339339

340340
@pytest.mark.asyncio
341341
async def test__pre_commit_success(self):
342-
to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
342+
to_wrap = AsyncMock(return_value=mock.sentinel.result, spec=[])
343343
wrapped = self._make_one(to_wrap)
344344

345345
txn_id = b"totes-began"
@@ -368,7 +368,7 @@ async def test__pre_commit_success(self):
368368
async def test__pre_commit_retry_id_already_set_success(self):
369369
from google.cloud.firestore_v1.types import common
370370

371-
to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
371+
to_wrap = AsyncMock(return_value=mock.sentinel.result, spec=[])
372372
wrapped = self._make_one(to_wrap)
373373
txn_id1 = b"already-set"
374374
wrapped.retry_id = txn_id1
@@ -401,7 +401,7 @@ async def test__pre_commit_retry_id_already_set_success(self):
401401
@pytest.mark.asyncio
402402
async def test__pre_commit_failure(self):
403403
exc = RuntimeError("Nope not today.")
404-
to_wrap = mock.Mock(side_effect=exc, spec=[])
404+
to_wrap = AsyncMock(side_effect=exc, spec=[])
405405
wrapped = self._make_one(to_wrap)
406406

407407
txn_id = b"gotta-fail"
@@ -438,7 +438,7 @@ async def test__pre_commit_failure_with_rollback_failure(self):
438438
from google.api_core import exceptions
439439

440440
exc1 = ValueError("I will not be only failure.")
441-
to_wrap = mock.Mock(side_effect=exc1, spec=[])
441+
to_wrap = AsyncMock(side_effect=exc1, spec=[])
442442
wrapped = self._make_one(to_wrap)
443443

444444
txn_id = b"both-will-fail"
@@ -614,7 +614,7 @@ async def test__maybe_commit_failure_cannot_retry(self):
614614

615615
@pytest.mark.asyncio
616616
async def test___call__success_first_attempt(self):
617-
to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
617+
to_wrap = AsyncMock(return_value=mock.sentinel.result, spec=[])
618618
wrapped = self._make_one(to_wrap)
619619

620620
txn_id = b"whole-enchilada"
@@ -650,7 +650,7 @@ async def test___call__success_second_attempt(self):
650650
from google.cloud.firestore_v1.types import firestore
651651
from google.cloud.firestore_v1.types import write
652652

653-
to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
653+
to_wrap = AsyncMock(return_value=mock.sentinel.result, spec=[])
654654
wrapped = self._make_one(to_wrap)
655655

656656
txn_id = b"whole-enchilada"
@@ -707,7 +707,7 @@ async def test___call__failure(self):
707707
_EXCEED_ATTEMPTS_TEMPLATE,
708708
)
709709

710-
to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
710+
to_wrap = AsyncMock(return_value=mock.sentinel.result, spec=[])
711711
wrapped = self._make_one(to_wrap)
712712

713713
txn_id = b"only-one-shot"

0 commit comments

Comments
 (0)