Entering a transaction context will do one of these these things: 1. Begin an outer transaction (if one isn't already in progress) 2. Begin an outer transaction and create a savepoint (if one is named) 3. Create a savepoint (if a transaction is already in progress) either usi
(aconn, acommands)
| 365 | |
| 366 | |
| 367 | async def test_named_savepoints_successful_exit(aconn, acommands): |
| 368 | """ |
| 369 | Entering a transaction context will do one of these these things: |
| 370 | 1. Begin an outer transaction (if one isn't already in progress) |
| 371 | 2. Begin an outer transaction and create a savepoint (if one is named) |
| 372 | 3. Create a savepoint (if a transaction is already in progress) |
| 373 | either using the name provided, or auto-generating a savepoint name. |
| 374 | |
| 375 | ...and exiting the context successfully will "commit" the same. |
| 376 | """ |
| 377 | # Case 1 |
| 378 | # Using Transaction explicitly because conn.transaction() enters the contetx |
| 379 | assert not acommands |
| 380 | async with aconn.transaction() as tx: |
| 381 | assert acommands.popall() == ["BEGIN"] |
| 382 | assert not tx.savepoint_name |
| 383 | assert acommands.popall() == ["COMMIT"] |
| 384 | |
| 385 | # Case 1 (with a transaction already started) |
| 386 | await aconn.cursor().execute("select 1") |
| 387 | assert acommands.popall() == ["BEGIN"] |
| 388 | async with aconn.transaction() as tx: |
| 389 | assert acommands.popall() == ['SAVEPOINT "_pg3_1"'] |
| 390 | assert tx.savepoint_name == "_pg3_1" |
| 391 | |
| 392 | assert acommands.popall() == ['RELEASE "_pg3_1"'] |
| 393 | await aconn.rollback() |
| 394 | assert acommands.popall() == ["ROLLBACK"] |
| 395 | |
| 396 | # Case 2 |
| 397 | async with aconn.transaction(savepoint_name="foo") as tx: |
| 398 | assert acommands.popall() == ["BEGIN", 'SAVEPOINT "foo"'] |
| 399 | assert tx.savepoint_name == "foo" |
| 400 | assert acommands.popall() == ["COMMIT"] |
| 401 | |
| 402 | # Case 3 (with savepoint name provided) |
| 403 | async with aconn.transaction(): |
| 404 | assert acommands.popall() == ["BEGIN"] |
| 405 | async with aconn.transaction(savepoint_name="bar") as tx: |
| 406 | assert acommands.popall() == ['SAVEPOINT "bar"'] |
| 407 | assert tx.savepoint_name == "bar" |
| 408 | assert acommands.popall() == ['RELEASE "bar"'] |
| 409 | assert acommands.popall() == ["COMMIT"] |
| 410 | |
| 411 | # Case 3 (with savepoint name auto-generated) |
| 412 | async with aconn.transaction(): |
| 413 | assert acommands.popall() == ["BEGIN"] |
| 414 | async with aconn.transaction() as tx: |
| 415 | assert acommands.popall() == ['SAVEPOINT "_pg3_2"'] |
| 416 | assert tx.savepoint_name == "_pg3_2" |
| 417 | assert acommands.popall() == ['RELEASE "_pg3_2"'] |
| 418 | assert acommands.popall() == ["COMMIT"] |
| 419 | |
| 420 | |
| 421 | async def test_named_savepoints_exception_exit(aconn, acommands): |
nothing calls this directly
no test coverage detected