0
0
PyTesttesting~10 mins

Database rollback fixtures in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a pytest fixture that rolls back database changes after each test.

PyTest
import pytest

@pytest.fixture
 def db_session():
     session = create_session()
     yield session
     session.[1]()
Drag options to blanks, or click blank then click option'
Acommit
Bflush
Cclose
Drollback
Attempts:
3 left
💡 Hint
Common Mistakes
Using commit instead of rollback causes changes to persist.
Using close does not undo changes.
Using flush only sends changes but does not rollback.
2fill in blank
medium

Complete the code to use the fixture in a test function that queries the database.

PyTest
def test_user_count(db_session):
     count = db_session.query(User).count()
     assert count [1] 0
Drag options to blanks, or click blank then click option'
A==
B>=
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= may pass if users exist, but test expects empty.
Using != may allow unexpected counts.
Using < is incorrect for count checks.
3fill in blank
hard

Fix the error in the fixture to ensure the session is properly closed after rollback.

PyTest
@pytest.fixture
def db_session():
    session = create_session()
    yield session
    session.rollback()
    session.[1]
Drag options to blanks, or click blank then click option'
Acommit()
Bflush()
Cclose()
Drefresh()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling commit after rollback is contradictory.
Flush does not close the session.
Refresh reloads objects but does not close session.
4fill in blank
hard

Fill both blanks to create a fixture that starts a transaction and rolls it back after the test.

PyTest
@pytest.fixture
def transactional_db():
    connection = engine.connect()
    transaction = connection.[1]()
    session = Session(bind=connection)
    yield session
    session.[2]()
    transaction.rollback()
    connection.close()
Drag options to blanks, or click blank then click option'
Abegin
Bcommit
Crollback
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using rollback to start transaction is incorrect.
Not committing session before rollback can cause errors.
Closing connection before rollback causes errors.
5fill in blank
hard

Fill all three blanks to implement a fixture that uses savepoint for nested rollback.

PyTest
@pytest.fixture
def savepoint_db():
    connection = engine.connect()
    transaction = connection.begin()
    savepoint = connection.[1]()
    session = Session(bind=connection)
    yield session
    session.[2]()
    savepoint.[3]()
    transaction.rollback()
    connection.close()
Drag options to blanks, or click blank then click option'
Abegin_nested
Bcommit
Crollback
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using rollback to create savepoint is wrong.
Not rolling back savepoint causes changes to persist.
Closing connection before rollback causes errors.