pgtestdbpy
Python clone of pgtestdb.
pip install pgtestdbpy
In summary, it's a couple of helper functions that allow you to quickly clone Postgres databases that you've applied migrations to. In a small number of milliseconds, each test (including tests running in parallel) gets a fresh db with empty tables, all the sequences reset, etc.
In developing this on my mac, for reasons I don't quite understand, running with Postgres in docker (via colima) was substantially quicker that running Postgres natively. So I agree with Peter's advice, just copy this file and docker compose up -d db.
There are two context managers that can be used in conjunction or independently depending on test setup:
pgtestdbpy.templates(config, migrators):- Creates a new user and database for a migrator.
- Runs the set of migrations.
- Marks the database as a
TEMPLATE DATABASEso that it can be cheaply cloned. - Yields.
- Drops the template database and the user.
pgtestdbpy.clone(config, migrator):- Does a
CREATE DATABASE WITH TEMPLATE(from a template database made above) giving it a unique random name. - Yields a Postgres url for it.
- Drops the database.
- Does a
Example conftest.py usage below, in theory (I haven't tested this) it should be easy to run tests in parallel using the conn fixture - each with a separate database instance - and pytest-xdist or equivalent. In this example we just
from typing import Iterator
import pgtestdbpy
import psycopg
import pytest
def migrate(url: str) -> None:
with psycopg.connect(url) as conn:
conn.execute("CREATE TABLE foo (a INT)")
migrator = pgtestdbpy.Migrator(migrate)
config = pgtestdbpy.Config()
@pytest.fixture(scope="session")
def db() -> Iterator[None]:
with pgtestdbpy.templates(config, migrator):
yield
@pytest.fixture()
def conn(db) -> Iterator[pgtestdbpy.PsycoConn]:
with pgtestdbpy.clone(config, migrator) as url:
with psycopg.connect(url) as _conn:
yield _conn
Metadata
Release files for pgtestdbpy 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pgtestdbpy-0.0.1.tar.gz | 4.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pgtestdbpy-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / pgtestdbpy-0.0.1.tar.gz
| Download URL | pgtestdbpy-0.0.1.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6860716c1091d007a488e62d0484a1e5081570eab10eed18e70513d0fdb75bee
|
|
BLAKE2b-256 checksum How to use checksums |
08e1e44b10fa741c44a610a51179b317f1b353e3ea4665a97ef5f4dd1084d2ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Release files / pgtestdbpy-0.0.1-py3-none-any.whl
| Download URL | pgtestdbpy-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
844dc07ac29e437069446ed98a29ed315ec33373779564e32d2270626ac2cb35
|
|
BLAKE2b-256 checksum How to use checksums |
5739c0e9275b683cf8fa15c89e990708d709fc4ae0e7255a06dbe602d3612082
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Security for pgtestdbpy 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.