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 Statuscodecov

testgres (async version)

PostgreSQL testing utility. Only Python 3.5+ are supported. If you need olderversion of python or don't want to use async functions use testgres frommaster branch.

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) callcleanup() automatically.

Nodes support python logging system, so if you have configured loggingin your tests, you can use it to redirect postgres logs to yours.

To do that, just useuse_logging argument:

node=testgres.get_new_node('master',use_logging=True)

You can find working configuration example for logging intests/test_simple.py.

Examples

Here is an example of what you can do withtestgres:

importtestgreswithtestgres.get_new_node('test')asnode:node.init()# run initdbnode.start()# start PostgreSQLprint(awaitnode.fetch('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 name, not a DB's name. 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:

CommandDescription
node.psql(database, query)Runs query viapsql command 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, username=None, commit=True)Connects to PostgreSQL usingpsycopg2 orpg8000 (depends on which one 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():

asyncwithnode.connect()ascon:awaitcon.begin('serializable')print(awaitcon.fetch('select %s',1))awaitcon.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:replica=backup.spawn_replica('replica').start()print(awaitreplica.fetch('postgres','select 1'))

Note: you could take a look atpg_pathman to get an idea oftestgres' capabilities.

Benchmarks

testgres also can help you to make benchmarks usingpgbench from postgres installation:

withtestgres.get_new_node('master')asmaster:master.init().start()p=master.pg_bench_init(scale=10).pgbench(options=['-T','60'])p.wait()

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>


[8]ページ先頭

©2009-2025 Movatter.jp