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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyPI version

testgres

PostgreSQL testing utility.

Installation

To installtestgres, run:

pip install testgres

We encourage you to usevirtualenv for your testing environment. Currentlytestgres works only with Python 2.x, but this is going to change soon.

Usage

Note: by default testgres runsinitdb,pg_ctl,psql provided by$PATH. To specify a custom postgres installation, set the environment variable$PG_CONFIG pointing to thepg_config executable:export PG_CONFIG=/path/to/pg_config.

Here is an example of what you can do withtestgres:

importtestgrestry:node=testgres.get_new_node('test').init().start()printnode.safe_psql('postgres','SELECT 1')node.stop()exceptClusterException,e:printefinally:node.cleanup()

Let's walk through the code. First you create new node:

node=testgres.get_new_node('master')

master is a node's name, not the database's name. The name matters if you're testing something like replication. Functionget_new_node() only creates directory structure in/tmp 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 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) - connects to postgresql server usingpsycopg2 orpg8000 library (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')printcon.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 atpg_pathman to get an idea oftestgres' capabilities.

Authors

Ildar Musini.musin@postgrespro.ru Postgres Professional Ltd., RussiaDmitry Ivanovd.ivanov@postgrespro.ru Postgres Professional Ltd., Russia


[8]ページ先頭

©2009-2025 Movatter.jp