- Notifications
You must be signed in to change notification settings - Fork0
Testing framework for PostgreSQL and its extensions
License
KoMiI/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 testgres
We encourage you to usevirtualenv
for your testing environment.
Note: by default testgres runs
initdb
,pg_ctl
,psql
provided byPATH
.
There are several ways to specify a custom postgres installation:
- export
PG_CONFIG
environment variable pointing to thepg_config
executable; - export
PG_BIN
environment variable pointing to the directory with executable files.
Example:
export PG_BIN=$HOME/pg_10/binpython my_tests.py
Here is an example of what you can do withtestgres
:
# create a node with random name, port, etcwithtestgres.get_new_node()asnode:# run inidbnode.init()# start PostgreSQLnode.start()# execute a query in a default DBprint(node.execute('select 1'))# ... node stops and its files are about to be removed
There are four API methods for runnig queries:
Command | Description |
---|---|
node.psql(query, ...) | Runs query viapsql command and returns tuple(error code, stdout, stderr) . |
node.safe_psql(query, ...) | Same aspsql() except that it returns onlystdout . If an error occures during the execution, an exception will be thrown. |
node.execute(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, ...) | 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()
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')# enable logging, and create two different nodestestgres.configure_testgres(use_python_logging=True)node1=testgres.get_new_node().init().start()node2=testgres.get_new_node().init().start()# execute a few queriesnode1.execute('select 1')node2.execute('select 2')# disable loggingtestgres.configure_testgres(use_python_logging=False)
Look attests/test_simple.py
file for a complete example of the loggingconfiguration.
It's quite easy to create a backup and start a new replica:
withtestgres.get_new_node('master')asmaster:master.init().start()# create a backupwithmaster.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
is also capable of running benchmarks usingpgbench
:
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)
It's often useful to extend default configuration provided bytestgres
.
testgres
hasdefault_conf()
function that helps control some basicoptions. Theappend_conf()
function can be used to add customlines to configuration lines:
ext_conf="shared_preload_libraries = 'postgres_fdw'"# initialize a new nodewithtestgres.get_new_node().init()asmaster:# ... do something ...# reset main config filemaster.default_conf(fsync=True,allow_streaming=True)# add a new config linemaster.append_conf('postgresql.conf',ext_conf)
Note thatdefault_conf()
is called byinit()
function; both of them overwritethe configuration file, which means that they should be called beforeappend_conf()
.
About
Testing framework for PostgreSQL and its extensions
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Languages
- Python98.4%
- Shell1.6%