- Notifications
You must be signed in to change notification settings - Fork37
Testing framework for PostgreSQL and its extensions
License
postgrespro/testgres
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.
To installtestgres, run:
pip install testgresWe encourage you to usevirtualenv for your testing environment.
Note: by default testgres runs
initdb,pg_ctl,psqlprovided byPATH.
There are several ways to specify a custom postgres installation:
- export
PG_CONFIGenvironment variable pointing to thepg_configexecutable; - export
PG_BINenvironment variable pointing to the directory with executable files.
Example:
export PG_BIN=$HOME/pg_10/binpython my_tests.py
By default,cleanup() removes all temporary files (DB files, logs etc) that were created by testgres' API methods.If you'd like to keep logs, executeconfigure_testgres(node_cleanup_full=False) before running any tests.
Note: context managers (aka
with) callstop()andcleanup()automatically.
testgres supportspython logging,which means that you can aggregate logs from several nodes into one file:
importlogging# write everything to /tmp/testgres.loglogging.basicConfig(filename='/tmp/testgres.log')# create two different nodes with loggingnode1=testgres.get_new_node(use_logging=True).init().start()node2=testgres.get_new_node(use_logging=True).init().start()# execute a few queriesnode1.execute('postgres','select 1')node2.execute('postgres','select 2')
Here is an example of what you can do withtestgres:
withtestgres.get_new_node('test')asnode:node.init()# run initdbnode.start()# start PostgreSQLprint(node.execute('postgres','select 1'))node.stop()# stop PostgreSQL
Let's walk through the code. First, you create a new node using:
withtestgres.get_new_node('master')asnode:
or
withtestgres.get_new_node('master','/path/to/DB')asnode:
wheremaster is a node's application name. Name matters if you're testing something like replication.Functionget_new_node() only creates directory structure in specified directory (or somewhere in '/tmp' ifwe did not specify base directory) for cluster. After that, we have to initialize the PostgreSQL cluster:
node.init()
This function runsinitdb command and adds some basic configuration topostgresql.conf andpg_hba.conf files.Functioninit() accepts optional parameterallows_streaming which configures cluster for streaming replication (default isFalse).Now we are ready to start:
node.start()
Finally, our temporary cluster is able to process queries. There are four ways to run them:
| Command | Description |
|---|---|
node.psql(dbname, query) | Runs query viapsql command and returns tuple(error code, stdout, stderr). |
node.safe_psql(dbname, query) | Same aspsql() except that it returns onlystdout. If an error occures during the execution, an exception will be thrown. |
node.execute(dbname, query) | Connects to PostgreSQL usingpsycopg2 orpg8000 (depends on which one is installed in your system) and returns two-dimensional array with data. |
node.connect(dbname, username) | Returns connection wrapper (NodeConnection) capable of running several queries within a single transaction. |
The last one is the most powerful: you can usebegin(isolation_level),commit() androllback():
withnode.connect()ascon:con.begin('serializable')print(con.execute('select %s',1))con.rollback()
To stop the server, run:
node.stop()
It's quite easy to create a backup and start a new replica:
withtestgres.get_new_node('master')asmaster:master.init().start()withmaster.backup()asbackup:# create and start a new replicareplica=backup.spawn_replica('replica').start()# catch up with master nodereplica.catchup()# execute a dummy queryprint(replica.execute('postgres','select 1'))
testgres also can help you to make benchmarks usingpgbench from postgres installation:
withtestgres.get_new_node('master')asmaster:# start a new nodemaster.init().start()# initialize default DB and run bench for 10 secondsres=master.pgbench_init(scale=2).pgbench_run(time=10)print(res)
Ildar Musin <i.musin(at)postgrespro.ru> Postgres Professional Ltd., Russia
Dmitry Ivanov <d.ivanov(at)postgrespro.ru> Postgres Professional Ltd., Russia
Ildus Kurbangaliev <i.kurbangaliev(at)postgrespro.ru> Postgres Professional Ltd., Russia
Yury Zhuravlev <stalkerg(at)gmail.com>
About
Testing framework for PostgreSQL and its extensions
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.