|
1 | 1 | import pytest |
2 | | -from asyncpg.transaction import TransactionState |
3 | 2 |
|
4 | 3 | from .models import db, User, qsize |
5 | 4 |
|
@@ -44,6 +43,9 @@ async def test_connection_ctx(bind, mocker): |
44 | 43 | 'asyncpg.transaction.Transaction.commit').side_effect = IndexError |
45 | 44 | with pytest.raises(IndexError): |
46 | 45 | await tx.__aexit__(None, None, None) |
| 46 | + # clean up, and to simulate commit failed |
| 47 | + mocker.stopall() |
| 48 | + await tx._tx.rollback() |
47 | 49 | assert await get_name() == 'commit' |
48 | 50 | assert await get_name() == 'commit' |
49 | 51 |
|
@@ -252,10 +254,12 @@ async def test_base_exception(engine): |
252 | 254 | assert False, 'Should not reach here' |
253 | 255 |
|
254 | 256 |
|
255 | | -async def test_rollback_failed_transaction(engine): |
256 | | - # for transaction whose state is rolled back or failed, rollback() won't |
257 | | - # have any effect and shouldn't throw exceptions |
258 | | - for state in (TransactionState.ROLLEDBACK, TransactionState.FAILED): |
259 | | - async with engine.transaction() as tx: |
260 | | - tx._tx._tx._state = state |
261 | | - tx.raise_rollback() |
| 257 | +async def test_no_rollback_on_commit_fail(engine, mocker): |
| 258 | + mocker.patch( |
| 259 | + 'asyncpg.transaction.Transaction.commit').side_effect = IndexError |
| 260 | + async with engine.acquire() as conn: |
| 261 | + tx = await conn.transaction().__aenter__() |
| 262 | + rollback = mocker.patch.object(tx._tx, 'rollback') |
| 263 | + with pytest.raises(IndexError): |
| 264 | + await tx.__aexit__(None, None, None) |
| 265 | + assert not rollback.called |
0 commit comments