Getting Started
SqlProof is a Python property-based testing library for PostgreSQL. It generates random valid datasets that respect your schema constraints, runs your properties against them, and reports the minimal counterexample when one fails.
Prerequisites
- Python 3.11+
- PostgreSQL 13+ or Docker, if using testcontainers
Install
pip install sqlproofQuick Start
Given a schema file:
-- schema.sqlCREATE TABLE customers ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(255) NOT NULL UNIQUE);
CREATE TABLE orders ( id SERIAL PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES customers(id), total NUMERIC(10,2) NOT NULL CHECK (total >= 0));Write a pytest property:
from sqlproof import SqlProof, sqlproof
proof = SqlProof.from_schema_file("./schema.sql")
@sqlproof(proof, sizes={"customers": 10, "orders": 50}, runs=50)def test_order_totals_non_negative(db): rows = db.query("SELECT total FROM orders") assert all(row["total"] >= 0 for row in rows)Run it:
pytestConnection Modes
SqlProof supports two primary connection modes:
| Mode | Options | Docker needed? | When to use |
|---|---|---|---|
| Testcontainers | schema_file only | Yes | Local development with no external DB |
| Connection string | connection_string + schema | No | CI Postgres, staging, Supabase, Render |
What Happens Under the Hood
- Schema parsing reads your
.sqlfile or introspects a live DB. - Topological sort orders tables by FK dependencies.
- Data generation maps Postgres types to Hypothesis strategies.
- Run isolation inserts generated data, runs your property, then rolls back or drops the run schema.
- Shrinking uses Hypothesis to minimize failing counterexamples.