def test_implicit_transaction_commit_failure(engine, mytable):
# Patch commit to raise an exception. We can then check that a) the
# transaction is rolled back, and b) that the exception is reraised.
patch_commit = patch.object(
AsyncioTransaction, 'commit', side_effect=RuntimeError)
# Patch a coroutine in place of AsyncioTransaction.rollback that calls
# a Mock which we can later check.
mock_rollback = Mock()
async def mock_coro(*args, **kwargs):
mock_rollback(*args, **kwargs)
patch_rollback = patch.object(AsyncioTransaction, 'rollback', mock_coro)
with pytest.raises(RuntimeError):
with patch_commit, patch_rollback:
async with engine.connect() as conn:
await conn.execute(CreateTable(mytable))
async with conn.begin() as trans:
await conn.execute(mytable.insert())
assert mock_rollback.call_count == 1
评论列表
文章目录