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
File details
Details for the file pgtestdbpy-0.0.1.tar.gz
.
File metadata
- Download URL: pgtestdbpy-0.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6860716c1091d007a488e62d0484a1e5081570eab10eed18e70513d0fdb75bee |
|
MD5 | 2e081fe033503deb5655fc40e23f58dc |
|
BLAKE2b-256 | 08e1e44b10fa741c44a610a51179b317f1b353e3ea4665a97ef5f4dd1084d2ec |
File details
Details for the file pgtestdbpy-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pgtestdbpy-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 844dc07ac29e437069446ed98a29ed315ec33373779564e32d2270626ac2cb35 |
|
MD5 | 6acde00424a41e7eac75137557365a0e |
|
BLAKE2b-256 | 5739c0e9275b683cf8fa15c89e990708d709fc4ae0e7255a06dbe602d3612082 |