Create temporary databases for testing.
Project description
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tempdb-0.1.0.tar.gz.
File metadata
- Download URL: tempdb-0.1.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d28727726ffd319fe3f32ef766fbae27d2ae5902ffa85e4a981d1f06ed89dc75
|
|
| MD5 |
ab376eec80671c5b581821c6cb848a5f
|
|
| BLAKE2b-256 |
8b89cb7dda989052a8b1ec8fa1e9d8841632757e77ac3cd166efb78a9e7c3eb9
|
File details
Details for the file tempdb-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: tempdb-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3f3b584289e2d8a54477bd0979cf77ce1057306770696ec5166e5d2a86a7e05
|
|
| MD5 |
d59c23dd124ba002a2cef9029b8f6705
|
|
| BLAKE2b-256 |
ee7a1b16bf794cfd44014dac8256b357a26148dcc7d06b03a04c04ac058c9a71
|