postgresql-integration-test is a python module that creates a temporary PostgreSQL instance to use for testing your application. You will need a working PostgreSQL install. It does not have to be running, the binaries are needed.
To install use pip:
$ pip install postgresql-integration-test
Or clone the repo:
$ git clone https://github.com/jasondcamp/postgresql-integration-test.git
The following class arguments can be overridden by passing them in, these arguments will override the config file arguments.
Argument | Description | Default |
---|---|---|
username | Username for database | root |
host | Host to bind | 127.0.0.1 |
port | Port to bind | random |
postgres_binary | Location of postgres binary | Searches paths |
timeout_start | Timeout to start PostgreSQL | 30 seconds |
timeout_stop | Timeout to stop PostgreSQL | 30 seconds |
log_level | Log level | INFO |
config_file | Configuration file | postgresql-integration-test.cfg |
Default settings can be overridden in a config file. The default name is postgresql-integration-test.cfg
in the local directory and can be overridden by passing in the config
option to the instance creation.
database:
host: '127.0.0.1'
port: '9999'
username: 'root'
postgresql_binary: '/usr/sbin/postgres'
general:
log_level: 'DEBUG'
timeout_start: 30
timeout_stop: 30
from postgresql_integration_test import PostgreSQL
Starts up the postgresql server
postgresql = PostgreSQL()
instance = postgresql.run()
Stops the postgres server
postgresql.stop()
#!/usr/bin/env python3
from postgresql_integration_test import PostgreSQL
import psycopg2
postgres = PostgreSQL(config='/some/dir/postgresql-integration-test.cfg')
instance = postgres.run()
# Make query to database
cnx = psycopg2.connect(
user=instance.username,
host=instance.host,
port=instance.port,
database="test",
)
cursor = cnx.cursor()
cursor.execute("SELECT id FROM some_table")
for _result in cursor:
result = _result
cursor.close()
cnx.close()
postgres.stop()