46 / 65 · 15 SQL & Database Testing · Stored Procedures and Triggers← prev⊞ allnext →☰ Read as one page
6.5Testing Computed/Derived Values
Some systems compute values in the database (materialized views, generated columns):
def test_user_order_count_computed(db):
"""Verify computed order_count matches actual orders."""
cursor = db.cursor()
# Create user with orders
cursor.execute("INSERT INTO users (id, name, email) VALUES ('count-test', 'Count User', 'count@test.com')")
cursor.execute("""
INSERT INTO orders (user_id, status, total) VALUES
('count-test', 'completed', 50.00),
('count-test', 'completed', 75.00),
('count-test', 'cancelled', 25.00)
""")
# If there is a materialized view or computed column:
cursor.execute("SELECT order_count FROM user_stats WHERE user_id = 'count-test'")
computed_count = cursor.fetchone()[0]
# Verify against actual count
cursor.execute("SELECT COUNT(*) FROM orders WHERE user_id = 'count-test'")
actual_count = cursor.fetchone()[0]
assert computed_count == actual_count