pgtestdb Python clone
Project description
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 DATABASE
so 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
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
Hashes for pgtestdbpy-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 844dc07ac29e437069446ed98a29ed315ec33373779564e32d2270626ac2cb35 |
|
MD5 | 6acde00424a41e7eac75137557365a0e |
|
BLAKE2b-256 | 5739c0e9275b683cf8fa15c89e990708d709fc4ae0e7255a06dbe602d3612082 |