MCPcopy
hub / github.com/psycopg/psycopg / test_named_savepoints_successful_exit

Function test_named_savepoints_successful_exit

tests/test_transaction.py:363–414  ·  view source on GitHub ↗

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

(conn, commands)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

popallMethod · 0.80
transactionMethod · 0.45
executeMethod · 0.45
cursorMethod · 0.45
rollbackMethod · 0.45

Tested by

no test coverage detected