Python testing library for PGlite - in-memory PostgreSQL for tests
Project description
py-pglite
Instant PostgreSQL for Python testing ⚡
pip install py-pglite
def test_users(pglite_session):
user = User(name="Alice")
pglite_session.add(user)
pglite_session.commit()
assert user.id == 1 # It's real PostgreSQL!
That's it. No Docker, no setup, no config files. Real PostgreSQL, instant testing.
Why py-pglite?
# ❌ Traditional testing
def test_old_way():
# 1. Install PostgreSQL
# 2. Configure connection
# 3. Manage test databases
# 4. Handle cleanup
# 5. Docker containers...
pass
# ✅ py-pglite way
def test_new_way(pglite_session):
User.objects.create(name="Alice") # Just works!
The magic:
- 🎯 Zero config - No setup, no Docker, no servers
- ⚡ Instant - 2-3s startup vs 30-60s Docker
- 🔄 Isolated - Fresh database per test
- 🐘 Real PostgreSQL - JSON, arrays, window functions
- 🚀 Any client - SQLAlchemy, Django, psycopg, asyncpg
Install
# Core (framework-agnostic)
pip install py-pglite
# With your stack
pip install py-pglite[sqlalchemy] # SQLAlchemy + SQLModel
pip install py-pglite[django] # Django + pytest-django
pip install py-pglite[asyncpg] # Pure async client
pip install py-pglite[all] # Everything
Quick Start
SQLAlchemy (Zero imports needed)
def test_sqlalchemy_just_works(pglite_session):
user = User(name="Alice", email="alice@test.com")
pglite_session.add(user)
pglite_session.commit()
assert user.id is not None
assert User.query.count() == 1 # Real PostgreSQL!
Django (Auto-configured)
def test_django_just_works(db):
Post.objects.create(title="Hello", content="World")
assert Post.objects.count() == 1 # Real PostgreSQL!
Any PostgreSQL client
def test_any_client_works(pglite_manager):
# Extract connection details
engine = pglite_manager.get_engine()
host, port, database = str(engine.url.host), engine.url.port, engine.url.database
# Use with any PostgreSQL client
# conn = psycopg.connect(host=host, port=port, dbname=database)
# conn = await asyncpg.connect(host=host, port=port, database=database)
# engine = create_async_engine(f"postgresql+asyncpg://{host}:{port}/{database}")
Examples
FastAPI + SQLModel
from fastapi.testclient import TestClient
def test_api_endpoint(client: TestClient):
response = client.post("/users/", json={"name": "Alice"})
assert response.status_code == 201
response = client.get("/users/")
assert len(response.json()) == 1
PostgreSQL Features
def test_postgresql_power(pglite_session):
pglite_session.execute(text("""
CREATE TABLE analytics (
data JSONB,
tags TEXT[],
created TIMESTAMP DEFAULT NOW()
)
"""))
pglite_session.execute(text("""
INSERT INTO analytics (data, tags) VALUES
('{"clicks": 100}', ARRAY['web', 'mobile'])
"""))
result = pglite_session.execute(text("""
SELECT data->>'clicks' as clicks,
array_length(tags, 1) as tag_count
FROM analytics
WHERE data->>'clicks' > '50'
""")).fetchone()
assert result.clicks == '100'
Advanced
🔧 Production Configuration
from py_pglite import PGliteConfig
from py_pglite.sqlalchemy import SQLAlchemyPGliteManager
config = PGliteConfig(
timeout=60, # Extended timeout for CI/CD
log_level="INFO", # Balanced logging
cleanup_on_exit=True, # Automatic cleanup
work_dir=Path("./test-data") # Custom directory
)
with SQLAlchemyPGliteManager(config) as manager:
engine = manager.get_engine(
pool_recycle=3600, # Connection recycling
echo=False # SQL logging
)
🔄 Client Compatibility
# py-pglite provides a REAL PostgreSQL server - any client works!
with SQLAlchemyPGliteManager() as manager:
engine = manager.get_engine()
url = engine.url
# Extract connection details for any PostgreSQL client
host, port, database = str(url.host), url.port, url.database
# Examples for different clients:
# psycopg: psycopg.connect(host=host, port=port, dbname=database)
# asyncpg: await asyncpg.connect(host=host, port=port, database=database)
# Django: Uses custom py-pglite backend automatically
Installation Matrix:
| Client | Install | Use Case |
|---|---|---|
[sqlalchemy] |
SQLAlchemy + SQLModel | ORM, modern Python |
[django] |
Django + pytest-django | Django projects |
[psycopg] |
psycopg (sync/async) | Raw SQL, custom |
[asyncpg] |
Pure async client | High-performance async |
[all] |
Everything | Full compatibility |
🎯 Framework Isolation
# Perfect isolation - no framework bleeding
pytest -m sqlalchemy -p no:django # Pure SQLAlchemy
pytest -m django -p no:sqlalchemy # Pure Django
pytest tests/sqlalchemy/ # Directory isolation
Built for developers who want PostgreSQL testing without the complexity.
🎯 Examples • 📚 Contributing • 🐛 Issues
py-pglite: Because testing should be simple. ⚡
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_pglite-0.3.1.tar.gz.
File metadata
- Download URL: py_pglite-0.3.1.tar.gz
- Upload date:
- Size: 230.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2c25ad924dc1f7303f1ab2cb9cbb8d95498c26f89446d8bb9202c8b08d2168f
|
|
| MD5 |
cbe22486d04061c0187066dc8f6fdd39
|
|
| BLAKE2b-256 |
c85320d9d0589a2732639cd0699543f5e6136983cc512ba5814d59e9f3c4f7a7
|
Provenance
The following attestation bundles were made for py_pglite-0.3.1.tar.gz:
Publisher:
release.yml on wey-gu/py-pglite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_pglite-0.3.1.tar.gz -
Subject digest:
e2c25ad924dc1f7303f1ab2cb9cbb8d95498c26f89446d8bb9202c8b08d2168f - Sigstore transparency entry: 237151040
- Sigstore integration time:
-
Permalink:
wey-gu/py-pglite@8d762c590d0341406a5e8cead22a3516e2b65823 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/wey-gu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8d762c590d0341406a5e8cead22a3516e2b65823 -
Trigger Event:
release
-
Statement type:
File details
Details for the file py_pglite-0.3.1-py3-none-any.whl.
File metadata
- Download URL: py_pglite-0.3.1-py3-none-any.whl
- Upload date:
- Size: 39.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bba36d9ee24beb77eaa56db048b055783bf7f083283001324ba85e1ffe480950
|
|
| MD5 |
b7e832eb39a865d0a321b3707dc55b98
|
|
| BLAKE2b-256 |
635a15ebfa5e734f680b44fd04d7f908bac4fb75021cbd7667b93cf695e8a95c
|
Provenance
The following attestation bundles were made for py_pglite-0.3.1-py3-none-any.whl:
Publisher:
release.yml on wey-gu/py-pglite
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_pglite-0.3.1-py3-none-any.whl -
Subject digest:
bba36d9ee24beb77eaa56db048b055783bf7f083283001324ba85e1ffe480950 - Sigstore transparency entry: 237151049
- Sigstore integration time:
-
Permalink:
wey-gu/py-pglite@8d762c590d0341406a5e8cead22a3516e2b65823 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/wey-gu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8d762c590d0341406a5e8cead22a3516e2b65823 -
Trigger Event:
release
-
Statement type: