Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1b08a4d

Browse files
authored
improve README.md, add PyPi badge
1 parent 653a0cf commit 1b08a4d

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

README.md

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,87 @@
1+
[![PyPI version](https://badge.fury.io/py/testgres.svg)](https://badge.fury.io/py/testgres)
2+
13
# testgres
24

3-
Postgres testing utility
5+
PostgreSQL testing utility.
6+
47

58
## Installation
69

7-
To install `testgres` run:
10+
To install `testgres`, run:
811

912
```
1013
pip install testgres
1114
```
1215

13-
At current state it only works with Python 2.*
16+
We encourage you to use `virtualenv` for your testing environment. Currently `testgres` works only with Python 2.x, but this is going to change soon.
1417

15-
## Usage
1618

19+
## Usage
1720

18-
> Node: by default testgres runs `initdb`, `pg_ctl`, `psql` commands from the `$PATH`. To specify custom postgres installation set environment variable `$PG_CONFIG` and point it to pg_config executable: `export PG_CONFIG=/path/to/pg_config`
21+
> Note: by default testgres runs `initdb`, `pg_ctl`, `psql` provided by `$PATH`. To specify a custom postgres installation, set the environment variable `$PG_CONFIG` pointing to the `pg_config` executable: `export PG_CONFIG=/path/to/pg_config`.
1922
20-
Here is an example of how you can use `testgres`.
23+
Here is an example of what you can do with `testgres`:
2124

2225
```python
23-
from testgres import get_new_node
26+
import testgres
2427

2528
try:
26-
node = get_new_node('master')
27-
node.init()
28-
node.start()
29-
stdout = node.safe_psql('postgres', 'SELECT 1')
30-
print stdout
29+
node = testgres.get_new_node('test').init().start()
30+
print node.safe_psql('postgres', 'SELECT 1')
3131
node.stop()
3232
except ClusterException, e:
33+
print e
34+
finally:
3335
node.cleanup()
3436
```
3537

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

3840
```python
39-
node = get_new_node('master')
41+
node = testgres.get_new_node('master')
4042
```
4143

42-
`master` here is a node's name, not the database's name. The name matters if you're testing replication. Function `get_new_node()` only creates directory structure in `/tmp` for cluster. Next line:
44+
`master` is a node's name, not the database's name. The name matters if you're testing something like replication. Function `get_new_node()` only creates directory structure in `/tmp` for cluster. After that, we have to initialize the PostgreSQL cluster:
4345

4446
```python
4547
node.init()
4648
```
4749

48-
initializes cluster. On low level it runs `initdb` command and adds some basic configuration to `postgresql.conf` and `pg_hba.conf` files. Function `init()` accepts optional parameter `allows_streaming` which configures cluster for streaming replication (default is `False`).
50+
This function runs `initdb` command and adds some basic configuration to `postgresql.conf` and `pg_hba.conf` files. Function `init()` accepts optional parameter `allows_streaming` which configures cluster for streaming replication (default is `False`).
4951
Now we are ready to start:
5052

5153
```python
5254
node.start()
5355
```
5456

55-
After this you are able to run queries over the cluster. There are three functions to do that:
57+
Finally our temporary cluster is able to process queries. There are four ways to run them:
5658

57-
* `node.psql(database, query)` - runs query via `psql` command and returns tuple (error code, stdout, stderr)
58-
* `node.safe_psql(database, query)` - the same as `psql()` except that it returns only `stdout`. If error occures during the execution then it will throw an exception;
59-
* `node.execute(database, query)` - connects with postgresql server using `psycopg2` or `pg8000` library (depends on which is installed in your system) and returns two-dimensional array with data.
59+
* `node.psql(database, query)` - runs query via `psql` command and returns tuple `(error code, stdout, stderr)`
60+
* `node.safe_psql(database, query)` - same as `psql()` except that it returns only `stdout`. If an error occures during the execution, an exception will be thrown.
61+
* `node.execute(database, query)` - connects to postgresql server using `psycopg2` or `pg8000` library (depends on which is installed in your system) and returns two-dimensional array with data.
62+
* `node.connect(database='postgres')` - returns connection wrapper (`NodeConnection`) capable of running several queries within a single transaction.
6063

61-
To stop server run:
64+
The last one is the most powerful: you can use `begin(isolation_level)`, `commit()` and `rollback()`:
65+
```python
66+
with node.connect() as con:
67+
con.begin('serializable')
68+
print con.execute('select %s', 1)
69+
con.rollback()
70+
```
71+
72+
To stop the server, run:
6273

6374
```python
6475
node.stop()
6576
```
6677

78+
It is essential to clean everything up, so make sure to call `node.cleanup()` once you've finished all of your tests.
79+
6780
Please see `testgres/tests` directory for replication configuration example.
81+
> Note: you could take a look at [`pg_pathman`](https://github.com/postgrespro/pg_pathman) to get an idea of `testgres`' capabilities.
82+
83+
84+
## Authors
85+
86+
Ildar Musin <i.musin@postgrespro.ru> Postgres Professional Ltd., Russia
87+
Dmitry Ivanov <d.ivanov@postgrespro.ru> Postgres Professional Ltd., Russia

0 commit comments

Comments
 (0)