|
| 1 | +[](https://badge.fury.io/py/testgres) |
| 2 | + |
1 | 3 | # testgres
|
2 | 4 |
|
3 |
| -Postgres testing utility |
| 5 | +PostgreSQL testing utility. |
| 6 | + |
4 | 7 |
|
5 | 8 | ## Installation
|
6 | 9 |
|
7 |
| -To install `testgres` run: |
| 10 | +To install `testgres`, run: |
8 | 11 |
|
9 | 12 | ```
|
10 | 13 | pip install testgres
|
11 | 14 | ```
|
12 | 15 |
|
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. |
14 | 17 |
|
15 |
| -## Usage |
16 | 18 |
|
| 19 | +## Usage |
17 | 20 |
|
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`. |
19 | 22 |
|
20 |
| -Here is an example of how you can use `testgres`. |
| 23 | +Here is an example of what you can do with `testgres`: |
21 | 24 |
|
22 | 25 | ```python
|
23 |
| -from testgres import get_new_node |
| 26 | +import testgres |
24 | 27 |
|
25 | 28 | 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') |
31 | 31 | node.stop()
|
32 | 32 | except ClusterException, e:
|
| 33 | + print e |
| 34 | +finally: |
33 | 35 | node.cleanup()
|
34 | 36 | ```
|
35 | 37 |
|
36 | 38 | Let's walk through the code. First you create new node:
|
37 | 39 |
|
38 | 40 | ```python
|
39 |
| -node = get_new_node('master') |
| 41 | +node = testgres.get_new_node('master') |
40 | 42 | ```
|
41 | 43 |
|
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: |
43 | 45 |
|
44 | 46 | ```python
|
45 | 47 | node.init()
|
46 | 48 | ```
|
47 | 49 |
|
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`). |
49 | 51 | Now we are ready to start:
|
50 | 52 |
|
51 | 53 | ```python
|
52 | 54 | node.start()
|
53 | 55 | ```
|
54 | 56 |
|
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: |
56 | 58 |
|
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. |
60 | 63 |
|
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: |
62 | 73 |
|
63 | 74 | ```python
|
64 | 75 | node.stop()
|
65 | 76 | ```
|
66 | 77 |
|
| 78 | +It is essential to clean everything up, so make sure to call `node.cleanup()` once you've finished all of your tests. |
| 79 | + |
67 | 80 | 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