Creates a temporary, local PostgreSQL database cluster and server for unittesting, and cleans up after itself
Project description
pgtest
Creates a temporary, local PostgreSQL database cluster and server specifically for unittesting, and cleans up after itself.
PGTest(username='postgres', port=None, log_file=None, no_cleanup=False,
copy_cluster=None, base_dir=None, pg_ctl=None, max_connections=5)
Args:
username - str, username for default database superuser
port - int, port to connect on; you must ensure that the port is unused
log_file - str, path to place the log file
no_cleanup - bool, don't clean up dirs after PGTest.close() is called
copy_cluster - str, copies cluster from this path
base_dir - str, path to the base directory to init the cluster
pg_ctl - str, path to the pg_ctl executable to use
max_connections - int, maximum number of connections to the cluster
Attributes:
PGTest.port - int, port number bound by PGTest
PGTest.cluster - str, cluster directory generated by PGTest
PGTest.username - str, username used by PGTest. Default is 'postgres'
PGTest.log_file - str, path to postgres log file
PGTest.pg_ctl - str, path to pg_ctl executable
PGTest.url - str, url for default postgres database on the cluster
PGTest.dsn - dict, dictionary containing dsn key-value pairs for the
default postgres database on the cluster
Methods:
close() - Closes this instance of PGTest, cleans up directories
Usage as an instance
>>> from pgtest.pgtest import PGTest
>>> import psycopg2
>>> pg = PGTest()
Server started: postgresql://postgres@localhost:47251/postgres
>>> pg.port
47251
>>> pg.cluster
'/tmp/tmpiDtBjs/data'
>>> pg.username
'postgres'
>>> pg.log_file
'/tmp/tmpiDtBjs/pgtest_log.txt'
>>> pg.pg_ctl
u'/usr/lib/postgresql/9.4/bin/pg_ctl'
>>> pg.url
'postgresql://postgres@localhost:47251/postgres'
>>> pg.dsn
{'user': 'postgres', 'host': 'localhost',
'port': 47251, 'database': 'postgres'}
>>> # Connect with other db driver here, e.g. psql, psycopg2,
>>> # sqlalchemy etc
>>> psycopg2.connect(**pg.dsn)
>>> pg.close()
Server stopped
Or use as a context:
>>> from pgtest.pgtest import PGTest
>>> import psycopg2
>>> with PGTest() as pg:
... # connect to db with psycopg/sqlalchemy etc
... psycopg2.connect(**pg.dsn)
... # Do other database actions here
>>> # When the context exits, the db cluster and service is deleted unless specified
Use with unittest
in the setUp()
and tearDown()
methods:
import unittest
from pgtest.pgtest import PGTest
class TestThirdPartyDrivers(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.pg = pgtest.PGTest()
cls.base_dir = cls.pg._base_dir
@classmethod
def tearDownClass(cls):
cls.pg.close()
def test_something(self):
self.assertTrue(isinstance(self.pg, PGTest)
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
pgtest-1.3.2.tar.gz
(9.3 kB
view details)
Built Distribution
File details
Details for the file pgtest-1.3.2.tar.gz
.
File metadata
- Download URL: pgtest-1.3.2.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | edbb5ed5f5c46ae6f6a58eeffcf9f2ef1467bfee4eb753f0bca4ab2cb302ee91 |
|
MD5 | ec7271b8ed7c51bf0a98b2a93ebbe851 |
|
BLAKE2b-256 | 5241ff29129bc427bbdc6cbe4559a4830a4f4d71739ce33c728b4fa610f5b12e |
File details
Details for the file pgtest-1.3.2-py2.py3-none-any.whl
.
File metadata
- Download URL: pgtest-1.3.2-py2.py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a80a5b6080c36a207cdbbc44c45c829f721b0e643dd9b821bd31b8f26849c36b |
|
MD5 | cb0b0a0040614562dee115a13cfe59a4 |
|
BLAKE2b-256 | d3477b58a3599232c80d1e87facaf24c67a826eed7342a12d5b6e7f8b7735163 |