Skip to main content

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.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgtestdbpy-0.0.1.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

pgtestdbpy-0.0.1-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page