47 / 65 · 15 SQL & Database Testing · Stored Procedures and Triggers← prev⊞ allnext →☰ Read as one page
6.6Testing Procedure Error Handling
def test_transfer_funds_insufficient_balance(db):
"""Transfer should fail if source account has insufficient funds."""
cursor = db.cursor()
# Set up accounts
cursor.execute("""
INSERT INTO accounts (id, balance) VALUES
('acct-a', 100.00),
('acct-b', 50.00)
""")
# Attempt transfer exceeding balance
with pytest.raises(Exception): # Specific exception depends on implementation
cursor.execute("SELECT transfer_funds('acct-a', 'acct-b', 200.00)")
# Verify no money was moved (transaction should be rolled back internally)
cursor.execute("SELECT balance FROM accounts WHERE id = 'acct-a'")
assert cursor.fetchone()[0] == 100.00 # Unchanged
cursor.execute("SELECT balance FROM accounts WHERE id = 'acct-b'")
assert cursor.fetchone()[0] == 50.00 # Unchanged