- Notifications
You must be signed in to change notification settings - Fork0
sd2017/testgres
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
PostgreSQL testing utility.
To installtestgres, run:
pip install testgresWe encourage you to usevirtualenv for your testing environment. Both Python 2.7 and 3.5 are supported.
Note: by default testgres runs
initdb,pg_ctl,psqlprovided by$PATH. To specify a custom postgres installation, set the environment variable$PG_CONFIGpointing to thepg_configexecutable:export PG_CONFIG=/path/to/pg_config.
Here is an example of what you can do withtestgres:
importtestgresnode=Nonetry:node=testgres.get_new_node('test').init().start()print(node.execute('postgres','select 1'))excepttestgres.ClusterExceptionase:print(e)finally:ifnodeisnotNone:node.cleanup()
Let's walk through the code. First you create new node:
node=testgres.get_new_node('master')
or:
node=testgres.get_new_node('master','/path/to/base')
master is a node's name, not the DB's name. The name matters if you're testing something like replication. Functionget_new_node() only creates directory structure in specified directory (or somewhere in '/tmp' if we 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:
node.psql(database, query)- runs query viapsqlcommand and returns tuple(error code, stdout, stderr)node.safe_psql(database, query)- same aspsql()except that it returns onlystdout. If an error occures during the execution, an exception will be thrown.node.execute(database, query)- connects to postgresql server usingpsycopg2orpg8000library (depends on which is installed in your system) and returns two-dimensional array with data.node.connect(database='postgres')- 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 is essential to clean everything up, so make sure to callnode.cleanup() once you've finished all of your tests.
Please seetestgres/tests directory for replication configuration example.
Note: you could take a look at
pg_pathmanto get an idea oftestgres' capabilities.
Ildar Musini.musin@postgrespro.ru Postgres Professional Ltd., RussiaDmitry Ivanovd.ivanov@postgrespro.ru Postgres Professional Ltd., Russia
About
Testing framework for PostgreSQL extensions
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- Python100.0%