Skip to main content

Spawn temporary PostgreSQL clusters for use in unit tests. This is alpha quality software and the API may change between version.

Usage

The following example shows how to create a temporary database with pytest.

import psycopg2
import pytest

from contextlib import closing
from tempdb import find_postgres_bin_dir, PostgresFactory


@pytest.fixture
def conn():
    # Try to discover a directory where PostgreSQL is installed. If you
    # have installed it in a non-standard location you must replace
    # pg_bin_dir with the path to the binary directory for your
    # installation.
    #
    # It is possible to provide a specific version as an argument if there
    # are multiple versions to choose from. The highest available version
    # is chosen by default.
    pg_bin_dir = find_postgres_bin_dir()
    if pg_bin_dir is None:
        pytest.skip("Unable to locate a PostgreSQL installation")

    # The factory is bound to a particular PostgreSQL version and can be
    # used to create an arbitrary number of clusters
    factory = PostgresFactory(pg_bin_dir)

    # A cluster is an actual running instance of PostgreSQL. By default a
    # cluster doesn't have any databases apart from the defaults. It is
    # possible to create new databases using the method
    # .create_database(name, template=None)
    with closing(factory.create_temporary_cluster()) as cluster:
        tmp_db = cluster.create_database("tmp")
        yield psycopg2.connect(tmp_db.dsn)


def test_create_tables(conn):
    with conn.cursor() as c:
        c.execute("""
            CREATE TABLE test(
                id SERIAL,
                name VARCHAR
            )
        """)
        c.execute("INSERT INTO test(name) VALUES (%s), (%s)", [
            "Abel",
            "Cain",
        ])
        c.execute("SELECT name FROM test ORDER BY id")

        assert [name for name, in c.fetchall()] == [
            "Abel",
            "Cain",
        ]

Changelog

Version 0.1.0

Released on 3rd June, 2019

  • Initial release

Release files for tempdb 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tempdb 0.1.0
File Size Uploaded
tempdb-0.1.0.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tempdb 0.1.0
File Interpreter ABI Platform
tempdb-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 19.4 kB

Release files / tempdb-0.1.0.tar.gz

Download URL tempdb-0.1.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d28727726ffd319fe3f32ef766fbae27d2ae5902ffa85e4a981d1f06ed89dc75
BLAKE2b-256 checksum
How to use checksums
8b89cb7dda989052a8b1ec8fa1e9d8841632757e77ac3cd166efb78a9e7c3eb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15rc1

Release files / tempdb-0.1.0-py2.py3-none-any.whl

Download URL tempdb-0.1.0-py2.py3-none-any.whl
Size 9.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d3f3b584289e2d8a54477bd0979cf77ce1057306770696ec5166e5d2a86a7e05
BLAKE2b-256 checksum
How to use checksums
ee7a1b16bf794cfd44014dac8256b357a26148dcc7d06b03a04c04ac058c9a71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/2.7.15rc1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page