Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Testing framework for PostgreSQL and its extensions

License

NotificationsYou must be signed in to change notification settings

postgrespro/testgres

Repository files navigation

Build StatuscodecovPyPI version

testgres

PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.

Installation

To installtestgres, run:

pip install testgres

We encourage you to usevirtualenv for your testing environment.

Usage

Environment

Note: by default testgres runsinitdb,pg_ctl,psql provided byPATH.

There are several ways to specify a custom postgres installation:

  • exportPG_CONFIG environment variable pointing to thepg_config executable;
  • exportPG_BIN environment variable pointing to the directory with executable files.

Example:

export PG_BIN=$HOME/pg_10/binpython my_tests.py

Logging

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 (akawith) 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')

Examples

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:

CommandDescription
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()

Backup & replication

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'))

Benchmarks

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)

Authors

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

Stars

Watchers

Forks

Packages

No packages published

Contributors32


[8]ページ先頭

©2009-2025 Movatter.jp