61 / 65 · 15 SQL & Database Testing · Data Masking and Anonymization← prev⊞ allnext →☰ Read as one page
8.5Statistical Preservation
For analytics and reporting tests, masked data should preserve statistical properties:
def test_masked_data_preserves_statistics(original_db, masked_db):
"""Distribution of key metrics should be similar after masking."""
# Compare order count distribution
for db, label in [(original_db, "original"), (masked_db, "masked")]:
cursor = db.cursor()
cursor.execute("SELECT COUNT(*) FROM orders")
count = cursor.fetchone()[0]
cursor.execute("SELECT AVG(total) FROM orders")
avg_total = cursor.fetchone()[0]
print(f"{label}: {count} orders, avg total ${avg_total:.2f}")
# Exact numbers will differ, but order of magnitude should be the same