Skip to main content

Creates a temporary, local PostgreSQL database cluster and server for unittesting, and cleans up after itself

Project description

pgtest Build Status

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.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

pgtest-1.3.1-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pgtest-1.3.1.tar.gz.

File metadata

  • Download URL: pgtest-1.3.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for pgtest-1.3.1.tar.gz
Algorithm Hash digest
SHA256 b1aa5516b3f1215643c265e11c83099a7d6978a4bd1e59e63963b33fe7da08e6
MD5 f5178f83f94ce59370d9bb7f8f6fc995
BLAKE2b-256 07cb9c29de31f75a32591956ac01ad1feb3122962873ab570a7f41ce7a0d69e1

See more details on using hashes here.

File details

Details for the file pgtest-1.3.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pgtest-1.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for pgtest-1.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 efc128ce49cf0a5e2ed6380d33b317570161577e9234a5a9e9679f2090159933
MD5 a64516459311c44430cccabd47a1f3e8
BLAKE2b-256 c78f43c0ee58e17c77fd5005e3a8b238980ba6daec3f0f651eb9e78140815aac

See more details on using hashes here.

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