testgres 1.8.7__tar.gz → 1.8.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- testgres-1.8.8/PKG-INFO +194 -0
- {testgres-1.8.7 → testgres-1.8.8}/setup.py +1 -1
- {testgres-1.8.7 → testgres-1.8.8}/testgres/node.py +4 -2
- {testgres-1.8.7 → testgres-1.8.8}/testgres/utils.py +4 -1
- testgres-1.8.8/testgres.egg-info/PKG-INFO +194 -0
- testgres-1.8.7/PKG-INFO +0 -197
- testgres-1.8.7/testgres.egg-info/PKG-INFO +0 -197
- {testgres-1.8.7 → testgres-1.8.8}/LICENSE +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/MANIFEST.in +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/README.md +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/setup.cfg +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/__init__.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/api.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/backup.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/cache.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/config.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/connection.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/consts.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/decorators.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/defaults.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/enums.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/exceptions.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/logger.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/pubsub.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres/standby.py +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres.egg-info/SOURCES.txt +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres.egg-info/dependency_links.txt +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres.egg-info/requires.txt +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/testgres.egg-info/top_level.txt +0 -0
- {testgres-1.8.7 → testgres-1.8.8}/tests/test_simple.py +0 -0
testgres-1.8.8/PKG-INFO
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: testgres
|
|
3
|
+
Version: 1.8.8
|
|
4
|
+
Summary: Testing utility for PostgreSQL and its extensions
|
|
5
|
+
Home-page: https://github.com/postgrespro/testgres
|
|
6
|
+
Author: Ildar Musin
|
|
7
|
+
Author-email: zildermann@gmail.com
|
|
8
|
+
License: PostgreSQL
|
|
9
|
+
Description: [](https://app.travis-ci.com/github/postgrespro/testgres/branches)
|
|
10
|
+
[](https://codecov.io/gh/postgrespro/testgres)
|
|
11
|
+
[](https://badge.fury.io/py/testgres)
|
|
12
|
+
|
|
13
|
+
[Documentation](https://postgrespro.github.io/testgres/)
|
|
14
|
+
|
|
15
|
+
# testgres
|
|
16
|
+
|
|
17
|
+
PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
To install `testgres`, run:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install testgres
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
We encourage you to use `virtualenv` for your testing environment.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### Environment
|
|
34
|
+
|
|
35
|
+
> Note: by default testgres runs `initdb`, `pg_ctl`, `psql` provided by `PATH`.
|
|
36
|
+
|
|
37
|
+
There are several ways to specify a custom postgres installation:
|
|
38
|
+
|
|
39
|
+
* export `PG_CONFIG` environment variable pointing to the `pg_config` executable;
|
|
40
|
+
* export `PG_BIN` environment variable pointing to the directory with executable files.
|
|
41
|
+
|
|
42
|
+
Example:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
export PG_BIN=$HOME/pg_10/bin
|
|
46
|
+
python my_tests.py
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Examples
|
|
51
|
+
|
|
52
|
+
Here is an example of what you can do with `testgres`:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
# create a node with random name, port, etc
|
|
56
|
+
with testgres.get_new_node() as node:
|
|
57
|
+
|
|
58
|
+
# run inidb
|
|
59
|
+
node.init()
|
|
60
|
+
|
|
61
|
+
# start PostgreSQL
|
|
62
|
+
node.start()
|
|
63
|
+
|
|
64
|
+
# execute a query in a default DB
|
|
65
|
+
print(node.execute('select 1'))
|
|
66
|
+
|
|
67
|
+
# ... node stops and its files are about to be removed
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
There are four API methods for runnig queries:
|
|
71
|
+
|
|
72
|
+
| Command | Description |
|
|
73
|
+
|----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
74
|
+
| `node.psql(query, ...)` | Runs query via `psql` command and returns tuple `(error code, stdout, stderr)`. |
|
|
75
|
+
| `node.safe_psql(query, ...)` | Same as `psql()` except that it returns only `stdout`. If an error occures during the execution, an exception will be thrown. |
|
|
76
|
+
| `node.execute(query, ...)` | Connects to PostgreSQL using `psycopg2` or `pg8000` (depends on which one is installed in your system) and returns two-dimensional array with data. |
|
|
77
|
+
| `node.connect(dbname, ...)` | Returns connection wrapper (`NodeConnection`) capable of running several queries within a single transaction. |
|
|
78
|
+
|
|
79
|
+
The last one is the most powerful: you can use `begin(isolation_level)`, `commit()` and `rollback()`:
|
|
80
|
+
```python
|
|
81
|
+
with node.connect() as con:
|
|
82
|
+
con.begin('serializable')
|
|
83
|
+
print(con.execute('select %s', 1))
|
|
84
|
+
con.rollback()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### Logging
|
|
89
|
+
|
|
90
|
+
By default, `cleanup()` removes all temporary files (DB files, logs etc) that were created by testgres' API methods.
|
|
91
|
+
If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)` before running any tests.
|
|
92
|
+
|
|
93
|
+
> Note: context managers (aka `with`) call `stop()` and `cleanup()` automatically.
|
|
94
|
+
|
|
95
|
+
`testgres` supports [python logging](https://docs.python.org/3.6/library/logging.html),
|
|
96
|
+
which means that you can aggregate logs from several nodes into one file:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import logging
|
|
100
|
+
|
|
101
|
+
# write everything to /tmp/testgres.log
|
|
102
|
+
logging.basicConfig(filename='/tmp/testgres.log')
|
|
103
|
+
|
|
104
|
+
# enable logging, and create two different nodes
|
|
105
|
+
testgres.configure_testgres(use_python_logging=True)
|
|
106
|
+
node1 = testgres.get_new_node().init().start()
|
|
107
|
+
node2 = testgres.get_new_node().init().start()
|
|
108
|
+
|
|
109
|
+
# execute a few queries
|
|
110
|
+
node1.execute('select 1')
|
|
111
|
+
node2.execute('select 2')
|
|
112
|
+
|
|
113
|
+
# disable logging
|
|
114
|
+
testgres.configure_testgres(use_python_logging=False)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Look at `tests/test_simple.py` file for a complete example of the logging
|
|
118
|
+
configuration.
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
### Backup & replication
|
|
122
|
+
|
|
123
|
+
It's quite easy to create a backup and start a new replica:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
with testgres.get_new_node('master') as master:
|
|
127
|
+
master.init().start()
|
|
128
|
+
|
|
129
|
+
# create a backup
|
|
130
|
+
with master.backup() as backup:
|
|
131
|
+
|
|
132
|
+
# create and start a new replica
|
|
133
|
+
replica = backup.spawn_replica('replica').start()
|
|
134
|
+
|
|
135
|
+
# catch up with master node
|
|
136
|
+
replica.catchup()
|
|
137
|
+
|
|
138
|
+
# execute a dummy query
|
|
139
|
+
print(replica.execute('postgres', 'select 1'))
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Benchmarks
|
|
143
|
+
|
|
144
|
+
`testgres` is also capable of running benchmarks using `pgbench`:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
with testgres.get_new_node('master') as master:
|
|
148
|
+
# start a new node
|
|
149
|
+
master.init().start()
|
|
150
|
+
|
|
151
|
+
# initialize default DB and run bench for 10 seconds
|
|
152
|
+
res = master.pgbench_init(scale=2).pgbench_run(time=10)
|
|
153
|
+
print(res)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
### Custom configuration
|
|
158
|
+
|
|
159
|
+
It's often useful to extend default configuration provided by `testgres`.
|
|
160
|
+
|
|
161
|
+
`testgres` has `default_conf()` function that helps control some basic
|
|
162
|
+
options. The `append_conf()` function can be used to add custom
|
|
163
|
+
lines to configuration lines:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
ext_conf = "shared_preload_libraries = 'postgres_fdw'"
|
|
167
|
+
|
|
168
|
+
# initialize a new node
|
|
169
|
+
with testgres.get_new_node().init() as master:
|
|
170
|
+
|
|
171
|
+
# ... do something ...
|
|
172
|
+
|
|
173
|
+
# reset main config file
|
|
174
|
+
master.default_conf(fsync=True,
|
|
175
|
+
allow_streaming=True)
|
|
176
|
+
|
|
177
|
+
# add a new config line
|
|
178
|
+
master.append_conf('postgresql.conf', ext_conf)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Note that `default_conf()` is called by `init()` function; both of them overwrite
|
|
182
|
+
the configuration file, which means that they should be called before `append_conf()`.
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
## Authors
|
|
186
|
+
|
|
187
|
+
[Ildar Musin](https://github.com/zilder)
|
|
188
|
+
[Dmitry Ivanov](https://github.com/funbringer)
|
|
189
|
+
[Ildus Kurbangaliev](https://github.com/ildus)
|
|
190
|
+
[Yury Zhuravlev](https://github.com/stalkerg)
|
|
191
|
+
|
|
192
|
+
Keywords: test,testing,postgresql
|
|
193
|
+
Platform: UNKNOWN
|
|
194
|
+
Description-Content-Type: text/markdown
|
|
@@ -1593,8 +1593,10 @@ class NodeApp:
|
|
|
1593
1593
|
set_replication=False,
|
|
1594
1594
|
ptrack_enable=False,
|
|
1595
1595
|
initdb_params=[],
|
|
1596
|
-
pg_options={}
|
|
1597
|
-
|
|
1596
|
+
pg_options={},
|
|
1597
|
+
checksum=True):
|
|
1598
|
+
if checksum and '--data-checksums' not in initdb_params:
|
|
1599
|
+
initdb_params.append('--data-checksums')
|
|
1598
1600
|
node = self.make_empty(base_dir)
|
|
1599
1601
|
node.init(
|
|
1600
1602
|
initdb_params=initdb_params, allow_streaming=set_replication)
|
|
@@ -12,7 +12,10 @@ import tempfile
|
|
|
12
12
|
|
|
13
13
|
from contextlib import contextmanager
|
|
14
14
|
from packaging.version import Version
|
|
15
|
-
|
|
15
|
+
try:
|
|
16
|
+
from shutil import which as find_executable
|
|
17
|
+
except ImportError:
|
|
18
|
+
from distutils.spawn import find_executable
|
|
16
19
|
from six import iteritems
|
|
17
20
|
|
|
18
21
|
from .config import testgres_config
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: testgres
|
|
3
|
+
Version: 1.8.8
|
|
4
|
+
Summary: Testing utility for PostgreSQL and its extensions
|
|
5
|
+
Home-page: https://github.com/postgrespro/testgres
|
|
6
|
+
Author: Ildar Musin
|
|
7
|
+
Author-email: zildermann@gmail.com
|
|
8
|
+
License: PostgreSQL
|
|
9
|
+
Description: [](https://app.travis-ci.com/github/postgrespro/testgres/branches)
|
|
10
|
+
[](https://codecov.io/gh/postgrespro/testgres)
|
|
11
|
+
[](https://badge.fury.io/py/testgres)
|
|
12
|
+
|
|
13
|
+
[Documentation](https://postgrespro.github.io/testgres/)
|
|
14
|
+
|
|
15
|
+
# testgres
|
|
16
|
+
|
|
17
|
+
PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
To install `testgres`, run:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
pip install testgres
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
We encourage you to use `virtualenv` for your testing environment.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### Environment
|
|
34
|
+
|
|
35
|
+
> Note: by default testgres runs `initdb`, `pg_ctl`, `psql` provided by `PATH`.
|
|
36
|
+
|
|
37
|
+
There are several ways to specify a custom postgres installation:
|
|
38
|
+
|
|
39
|
+
* export `PG_CONFIG` environment variable pointing to the `pg_config` executable;
|
|
40
|
+
* export `PG_BIN` environment variable pointing to the directory with executable files.
|
|
41
|
+
|
|
42
|
+
Example:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
export PG_BIN=$HOME/pg_10/bin
|
|
46
|
+
python my_tests.py
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
### Examples
|
|
51
|
+
|
|
52
|
+
Here is an example of what you can do with `testgres`:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
# create a node with random name, port, etc
|
|
56
|
+
with testgres.get_new_node() as node:
|
|
57
|
+
|
|
58
|
+
# run inidb
|
|
59
|
+
node.init()
|
|
60
|
+
|
|
61
|
+
# start PostgreSQL
|
|
62
|
+
node.start()
|
|
63
|
+
|
|
64
|
+
# execute a query in a default DB
|
|
65
|
+
print(node.execute('select 1'))
|
|
66
|
+
|
|
67
|
+
# ... node stops and its files are about to be removed
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
There are four API methods for runnig queries:
|
|
71
|
+
|
|
72
|
+
| Command | Description |
|
|
73
|
+
|----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
74
|
+
| `node.psql(query, ...)` | Runs query via `psql` command and returns tuple `(error code, stdout, stderr)`. |
|
|
75
|
+
| `node.safe_psql(query, ...)` | Same as `psql()` except that it returns only `stdout`. If an error occures during the execution, an exception will be thrown. |
|
|
76
|
+
| `node.execute(query, ...)` | Connects to PostgreSQL using `psycopg2` or `pg8000` (depends on which one is installed in your system) and returns two-dimensional array with data. |
|
|
77
|
+
| `node.connect(dbname, ...)` | Returns connection wrapper (`NodeConnection`) capable of running several queries within a single transaction. |
|
|
78
|
+
|
|
79
|
+
The last one is the most powerful: you can use `begin(isolation_level)`, `commit()` and `rollback()`:
|
|
80
|
+
```python
|
|
81
|
+
with node.connect() as con:
|
|
82
|
+
con.begin('serializable')
|
|
83
|
+
print(con.execute('select %s', 1))
|
|
84
|
+
con.rollback()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### Logging
|
|
89
|
+
|
|
90
|
+
By default, `cleanup()` removes all temporary files (DB files, logs etc) that were created by testgres' API methods.
|
|
91
|
+
If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)` before running any tests.
|
|
92
|
+
|
|
93
|
+
> Note: context managers (aka `with`) call `stop()` and `cleanup()` automatically.
|
|
94
|
+
|
|
95
|
+
`testgres` supports [python logging](https://docs.python.org/3.6/library/logging.html),
|
|
96
|
+
which means that you can aggregate logs from several nodes into one file:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import logging
|
|
100
|
+
|
|
101
|
+
# write everything to /tmp/testgres.log
|
|
102
|
+
logging.basicConfig(filename='/tmp/testgres.log')
|
|
103
|
+
|
|
104
|
+
# enable logging, and create two different nodes
|
|
105
|
+
testgres.configure_testgres(use_python_logging=True)
|
|
106
|
+
node1 = testgres.get_new_node().init().start()
|
|
107
|
+
node2 = testgres.get_new_node().init().start()
|
|
108
|
+
|
|
109
|
+
# execute a few queries
|
|
110
|
+
node1.execute('select 1')
|
|
111
|
+
node2.execute('select 2')
|
|
112
|
+
|
|
113
|
+
# disable logging
|
|
114
|
+
testgres.configure_testgres(use_python_logging=False)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Look at `tests/test_simple.py` file for a complete example of the logging
|
|
118
|
+
configuration.
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
### Backup & replication
|
|
122
|
+
|
|
123
|
+
It's quite easy to create a backup and start a new replica:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
with testgres.get_new_node('master') as master:
|
|
127
|
+
master.init().start()
|
|
128
|
+
|
|
129
|
+
# create a backup
|
|
130
|
+
with master.backup() as backup:
|
|
131
|
+
|
|
132
|
+
# create and start a new replica
|
|
133
|
+
replica = backup.spawn_replica('replica').start()
|
|
134
|
+
|
|
135
|
+
# catch up with master node
|
|
136
|
+
replica.catchup()
|
|
137
|
+
|
|
138
|
+
# execute a dummy query
|
|
139
|
+
print(replica.execute('postgres', 'select 1'))
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Benchmarks
|
|
143
|
+
|
|
144
|
+
`testgres` is also capable of running benchmarks using `pgbench`:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
with testgres.get_new_node('master') as master:
|
|
148
|
+
# start a new node
|
|
149
|
+
master.init().start()
|
|
150
|
+
|
|
151
|
+
# initialize default DB and run bench for 10 seconds
|
|
152
|
+
res = master.pgbench_init(scale=2).pgbench_run(time=10)
|
|
153
|
+
print(res)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
### Custom configuration
|
|
158
|
+
|
|
159
|
+
It's often useful to extend default configuration provided by `testgres`.
|
|
160
|
+
|
|
161
|
+
`testgres` has `default_conf()` function that helps control some basic
|
|
162
|
+
options. The `append_conf()` function can be used to add custom
|
|
163
|
+
lines to configuration lines:
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
ext_conf = "shared_preload_libraries = 'postgres_fdw'"
|
|
167
|
+
|
|
168
|
+
# initialize a new node
|
|
169
|
+
with testgres.get_new_node().init() as master:
|
|
170
|
+
|
|
171
|
+
# ... do something ...
|
|
172
|
+
|
|
173
|
+
# reset main config file
|
|
174
|
+
master.default_conf(fsync=True,
|
|
175
|
+
allow_streaming=True)
|
|
176
|
+
|
|
177
|
+
# add a new config line
|
|
178
|
+
master.append_conf('postgresql.conf', ext_conf)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Note that `default_conf()` is called by `init()` function; both of them overwrite
|
|
182
|
+
the configuration file, which means that they should be called before `append_conf()`.
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
## Authors
|
|
186
|
+
|
|
187
|
+
[Ildar Musin](https://github.com/zilder)
|
|
188
|
+
[Dmitry Ivanov](https://github.com/funbringer)
|
|
189
|
+
[Ildus Kurbangaliev](https://github.com/ildus)
|
|
190
|
+
[Yury Zhuravlev](https://github.com/stalkerg)
|
|
191
|
+
|
|
192
|
+
Keywords: test,testing,postgresql
|
|
193
|
+
Platform: UNKNOWN
|
|
194
|
+
Description-Content-Type: text/markdown
|
testgres-1.8.7/PKG-INFO
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: testgres
|
|
3
|
-
Version: 1.8.7
|
|
4
|
-
Summary: Testing utility for PostgreSQL and its extensions
|
|
5
|
-
Home-page: https://github.com/postgrespro/testgres
|
|
6
|
-
Author: Ildar Musin
|
|
7
|
-
Author-email: zildermann@gmail.com
|
|
8
|
-
License: PostgreSQL
|
|
9
|
-
Keywords: test,testing,postgresql
|
|
10
|
-
Platform: UNKNOWN
|
|
11
|
-
Description-Content-Type: text/markdown
|
|
12
|
-
License-File: LICENSE
|
|
13
|
-
|
|
14
|
-
[](https://app.travis-ci.com/github/postgrespro/testgres/branches)
|
|
15
|
-
[](https://codecov.io/gh/postgrespro/testgres)
|
|
16
|
-
[](https://badge.fury.io/py/testgres)
|
|
17
|
-
|
|
18
|
-
[Documentation](https://postgrespro.github.io/testgres/)
|
|
19
|
-
|
|
20
|
-
# testgres
|
|
21
|
-
|
|
22
|
-
PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
## Installation
|
|
26
|
-
|
|
27
|
-
To install `testgres`, run:
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
pip install testgres
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
We encourage you to use `virtualenv` for your testing environment.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
## Usage
|
|
37
|
-
|
|
38
|
-
### Environment
|
|
39
|
-
|
|
40
|
-
> Note: by default testgres runs `initdb`, `pg_ctl`, `psql` provided by `PATH`.
|
|
41
|
-
|
|
42
|
-
There are several ways to specify a custom postgres installation:
|
|
43
|
-
|
|
44
|
-
* export `PG_CONFIG` environment variable pointing to the `pg_config` executable;
|
|
45
|
-
* export `PG_BIN` environment variable pointing to the directory with executable files.
|
|
46
|
-
|
|
47
|
-
Example:
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
export PG_BIN=$HOME/pg_10/bin
|
|
51
|
-
python my_tests.py
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
### Examples
|
|
56
|
-
|
|
57
|
-
Here is an example of what you can do with `testgres`:
|
|
58
|
-
|
|
59
|
-
```python
|
|
60
|
-
# create a node with random name, port, etc
|
|
61
|
-
with testgres.get_new_node() as node:
|
|
62
|
-
|
|
63
|
-
# run inidb
|
|
64
|
-
node.init()
|
|
65
|
-
|
|
66
|
-
# start PostgreSQL
|
|
67
|
-
node.start()
|
|
68
|
-
|
|
69
|
-
# execute a query in a default DB
|
|
70
|
-
print(node.execute('select 1'))
|
|
71
|
-
|
|
72
|
-
# ... node stops and its files are about to be removed
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
There are four API methods for runnig queries:
|
|
76
|
-
|
|
77
|
-
| Command | Description |
|
|
78
|
-
|----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
79
|
-
| `node.psql(query, ...)` | Runs query via `psql` command and returns tuple `(error code, stdout, stderr)`. |
|
|
80
|
-
| `node.safe_psql(query, ...)` | Same as `psql()` except that it returns only `stdout`. If an error occures during the execution, an exception will be thrown. |
|
|
81
|
-
| `node.execute(query, ...)` | Connects to PostgreSQL using `psycopg2` or `pg8000` (depends on which one is installed in your system) and returns two-dimensional array with data. |
|
|
82
|
-
| `node.connect(dbname, ...)` | Returns connection wrapper (`NodeConnection`) capable of running several queries within a single transaction. |
|
|
83
|
-
|
|
84
|
-
The last one is the most powerful: you can use `begin(isolation_level)`, `commit()` and `rollback()`:
|
|
85
|
-
```python
|
|
86
|
-
with node.connect() as con:
|
|
87
|
-
con.begin('serializable')
|
|
88
|
-
print(con.execute('select %s', 1))
|
|
89
|
-
con.rollback()
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
### Logging
|
|
94
|
-
|
|
95
|
-
By default, `cleanup()` removes all temporary files (DB files, logs etc) that were created by testgres' API methods.
|
|
96
|
-
If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)` before running any tests.
|
|
97
|
-
|
|
98
|
-
> Note: context managers (aka `with`) call `stop()` and `cleanup()` automatically.
|
|
99
|
-
|
|
100
|
-
`testgres` supports [python logging](https://docs.python.org/3.6/library/logging.html),
|
|
101
|
-
which means that you can aggregate logs from several nodes into one file:
|
|
102
|
-
|
|
103
|
-
```python
|
|
104
|
-
import logging
|
|
105
|
-
|
|
106
|
-
# write everything to /tmp/testgres.log
|
|
107
|
-
logging.basicConfig(filename='/tmp/testgres.log')
|
|
108
|
-
|
|
109
|
-
# enable logging, and create two different nodes
|
|
110
|
-
testgres.configure_testgres(use_python_logging=True)
|
|
111
|
-
node1 = testgres.get_new_node().init().start()
|
|
112
|
-
node2 = testgres.get_new_node().init().start()
|
|
113
|
-
|
|
114
|
-
# execute a few queries
|
|
115
|
-
node1.execute('select 1')
|
|
116
|
-
node2.execute('select 2')
|
|
117
|
-
|
|
118
|
-
# disable logging
|
|
119
|
-
testgres.configure_testgres(use_python_logging=False)
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Look at `tests/test_simple.py` file for a complete example of the logging
|
|
123
|
-
configuration.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
### Backup & replication
|
|
127
|
-
|
|
128
|
-
It's quite easy to create a backup and start a new replica:
|
|
129
|
-
|
|
130
|
-
```python
|
|
131
|
-
with testgres.get_new_node('master') as master:
|
|
132
|
-
master.init().start()
|
|
133
|
-
|
|
134
|
-
# create a backup
|
|
135
|
-
with master.backup() as backup:
|
|
136
|
-
|
|
137
|
-
# create and start a new replica
|
|
138
|
-
replica = backup.spawn_replica('replica').start()
|
|
139
|
-
|
|
140
|
-
# catch up with master node
|
|
141
|
-
replica.catchup()
|
|
142
|
-
|
|
143
|
-
# execute a dummy query
|
|
144
|
-
print(replica.execute('postgres', 'select 1'))
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Benchmarks
|
|
148
|
-
|
|
149
|
-
`testgres` is also capable of running benchmarks using `pgbench`:
|
|
150
|
-
|
|
151
|
-
```python
|
|
152
|
-
with testgres.get_new_node('master') as master:
|
|
153
|
-
# start a new node
|
|
154
|
-
master.init().start()
|
|
155
|
-
|
|
156
|
-
# initialize default DB and run bench for 10 seconds
|
|
157
|
-
res = master.pgbench_init(scale=2).pgbench_run(time=10)
|
|
158
|
-
print(res)
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
### Custom configuration
|
|
163
|
-
|
|
164
|
-
It's often useful to extend default configuration provided by `testgres`.
|
|
165
|
-
|
|
166
|
-
`testgres` has `default_conf()` function that helps control some basic
|
|
167
|
-
options. The `append_conf()` function can be used to add custom
|
|
168
|
-
lines to configuration lines:
|
|
169
|
-
|
|
170
|
-
```python
|
|
171
|
-
ext_conf = "shared_preload_libraries = 'postgres_fdw'"
|
|
172
|
-
|
|
173
|
-
# initialize a new node
|
|
174
|
-
with testgres.get_new_node().init() as master:
|
|
175
|
-
|
|
176
|
-
# ... do something ...
|
|
177
|
-
|
|
178
|
-
# reset main config file
|
|
179
|
-
master.default_conf(fsync=True,
|
|
180
|
-
allow_streaming=True)
|
|
181
|
-
|
|
182
|
-
# add a new config line
|
|
183
|
-
master.append_conf('postgresql.conf', ext_conf)
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
Note that `default_conf()` is called by `init()` function; both of them overwrite
|
|
187
|
-
the configuration file, which means that they should be called before `append_conf()`.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
## Authors
|
|
191
|
-
|
|
192
|
-
[Ildar Musin](https://github.com/zilder)
|
|
193
|
-
[Dmitry Ivanov](https://github.com/funbringer)
|
|
194
|
-
[Ildus Kurbangaliev](https://github.com/ildus)
|
|
195
|
-
[Yury Zhuravlev](https://github.com/stalkerg)
|
|
196
|
-
|
|
197
|
-
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: testgres
|
|
3
|
-
Version: 1.8.7
|
|
4
|
-
Summary: Testing utility for PostgreSQL and its extensions
|
|
5
|
-
Home-page: https://github.com/postgrespro/testgres
|
|
6
|
-
Author: Ildar Musin
|
|
7
|
-
Author-email: zildermann@gmail.com
|
|
8
|
-
License: PostgreSQL
|
|
9
|
-
Keywords: test,testing,postgresql
|
|
10
|
-
Platform: UNKNOWN
|
|
11
|
-
Description-Content-Type: text/markdown
|
|
12
|
-
License-File: LICENSE
|
|
13
|
-
|
|
14
|
-
[](https://app.travis-ci.com/github/postgrespro/testgres/branches)
|
|
15
|
-
[](https://codecov.io/gh/postgrespro/testgres)
|
|
16
|
-
[](https://badge.fury.io/py/testgres)
|
|
17
|
-
|
|
18
|
-
[Documentation](https://postgrespro.github.io/testgres/)
|
|
19
|
-
|
|
20
|
-
# testgres
|
|
21
|
-
|
|
22
|
-
PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
## Installation
|
|
26
|
-
|
|
27
|
-
To install `testgres`, run:
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
pip install testgres
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
We encourage you to use `virtualenv` for your testing environment.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
## Usage
|
|
37
|
-
|
|
38
|
-
### Environment
|
|
39
|
-
|
|
40
|
-
> Note: by default testgres runs `initdb`, `pg_ctl`, `psql` provided by `PATH`.
|
|
41
|
-
|
|
42
|
-
There are several ways to specify a custom postgres installation:
|
|
43
|
-
|
|
44
|
-
* export `PG_CONFIG` environment variable pointing to the `pg_config` executable;
|
|
45
|
-
* export `PG_BIN` environment variable pointing to the directory with executable files.
|
|
46
|
-
|
|
47
|
-
Example:
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
export PG_BIN=$HOME/pg_10/bin
|
|
51
|
-
python my_tests.py
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
### Examples
|
|
56
|
-
|
|
57
|
-
Here is an example of what you can do with `testgres`:
|
|
58
|
-
|
|
59
|
-
```python
|
|
60
|
-
# create a node with random name, port, etc
|
|
61
|
-
with testgres.get_new_node() as node:
|
|
62
|
-
|
|
63
|
-
# run inidb
|
|
64
|
-
node.init()
|
|
65
|
-
|
|
66
|
-
# start PostgreSQL
|
|
67
|
-
node.start()
|
|
68
|
-
|
|
69
|
-
# execute a query in a default DB
|
|
70
|
-
print(node.execute('select 1'))
|
|
71
|
-
|
|
72
|
-
# ... node stops and its files are about to be removed
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
There are four API methods for runnig queries:
|
|
76
|
-
|
|
77
|
-
| Command | Description |
|
|
78
|
-
|----------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
79
|
-
| `node.psql(query, ...)` | Runs query via `psql` command and returns tuple `(error code, stdout, stderr)`. |
|
|
80
|
-
| `node.safe_psql(query, ...)` | Same as `psql()` except that it returns only `stdout`. If an error occures during the execution, an exception will be thrown. |
|
|
81
|
-
| `node.execute(query, ...)` | Connects to PostgreSQL using `psycopg2` or `pg8000` (depends on which one is installed in your system) and returns two-dimensional array with data. |
|
|
82
|
-
| `node.connect(dbname, ...)` | Returns connection wrapper (`NodeConnection`) capable of running several queries within a single transaction. |
|
|
83
|
-
|
|
84
|
-
The last one is the most powerful: you can use `begin(isolation_level)`, `commit()` and `rollback()`:
|
|
85
|
-
```python
|
|
86
|
-
with node.connect() as con:
|
|
87
|
-
con.begin('serializable')
|
|
88
|
-
print(con.execute('select %s', 1))
|
|
89
|
-
con.rollback()
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
### Logging
|
|
94
|
-
|
|
95
|
-
By default, `cleanup()` removes all temporary files (DB files, logs etc) that were created by testgres' API methods.
|
|
96
|
-
If you'd like to keep logs, execute `configure_testgres(node_cleanup_full=False)` before running any tests.
|
|
97
|
-
|
|
98
|
-
> Note: context managers (aka `with`) call `stop()` and `cleanup()` automatically.
|
|
99
|
-
|
|
100
|
-
`testgres` supports [python logging](https://docs.python.org/3.6/library/logging.html),
|
|
101
|
-
which means that you can aggregate logs from several nodes into one file:
|
|
102
|
-
|
|
103
|
-
```python
|
|
104
|
-
import logging
|
|
105
|
-
|
|
106
|
-
# write everything to /tmp/testgres.log
|
|
107
|
-
logging.basicConfig(filename='/tmp/testgres.log')
|
|
108
|
-
|
|
109
|
-
# enable logging, and create two different nodes
|
|
110
|
-
testgres.configure_testgres(use_python_logging=True)
|
|
111
|
-
node1 = testgres.get_new_node().init().start()
|
|
112
|
-
node2 = testgres.get_new_node().init().start()
|
|
113
|
-
|
|
114
|
-
# execute a few queries
|
|
115
|
-
node1.execute('select 1')
|
|
116
|
-
node2.execute('select 2')
|
|
117
|
-
|
|
118
|
-
# disable logging
|
|
119
|
-
testgres.configure_testgres(use_python_logging=False)
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Look at `tests/test_simple.py` file for a complete example of the logging
|
|
123
|
-
configuration.
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
### Backup & replication
|
|
127
|
-
|
|
128
|
-
It's quite easy to create a backup and start a new replica:
|
|
129
|
-
|
|
130
|
-
```python
|
|
131
|
-
with testgres.get_new_node('master') as master:
|
|
132
|
-
master.init().start()
|
|
133
|
-
|
|
134
|
-
# create a backup
|
|
135
|
-
with master.backup() as backup:
|
|
136
|
-
|
|
137
|
-
# create and start a new replica
|
|
138
|
-
replica = backup.spawn_replica('replica').start()
|
|
139
|
-
|
|
140
|
-
# catch up with master node
|
|
141
|
-
replica.catchup()
|
|
142
|
-
|
|
143
|
-
# execute a dummy query
|
|
144
|
-
print(replica.execute('postgres', 'select 1'))
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Benchmarks
|
|
148
|
-
|
|
149
|
-
`testgres` is also capable of running benchmarks using `pgbench`:
|
|
150
|
-
|
|
151
|
-
```python
|
|
152
|
-
with testgres.get_new_node('master') as master:
|
|
153
|
-
# start a new node
|
|
154
|
-
master.init().start()
|
|
155
|
-
|
|
156
|
-
# initialize default DB and run bench for 10 seconds
|
|
157
|
-
res = master.pgbench_init(scale=2).pgbench_run(time=10)
|
|
158
|
-
print(res)
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
### Custom configuration
|
|
163
|
-
|
|
164
|
-
It's often useful to extend default configuration provided by `testgres`.
|
|
165
|
-
|
|
166
|
-
`testgres` has `default_conf()` function that helps control some basic
|
|
167
|
-
options. The `append_conf()` function can be used to add custom
|
|
168
|
-
lines to configuration lines:
|
|
169
|
-
|
|
170
|
-
```python
|
|
171
|
-
ext_conf = "shared_preload_libraries = 'postgres_fdw'"
|
|
172
|
-
|
|
173
|
-
# initialize a new node
|
|
174
|
-
with testgres.get_new_node().init() as master:
|
|
175
|
-
|
|
176
|
-
# ... do something ...
|
|
177
|
-
|
|
178
|
-
# reset main config file
|
|
179
|
-
master.default_conf(fsync=True,
|
|
180
|
-
allow_streaming=True)
|
|
181
|
-
|
|
182
|
-
# add a new config line
|
|
183
|
-
master.append_conf('postgresql.conf', ext_conf)
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
Note that `default_conf()` is called by `init()` function; both of them overwrite
|
|
187
|
-
the configuration file, which means that they should be called before `append_conf()`.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
## Authors
|
|
191
|
-
|
|
192
|
-
[Ildar Musin](https://github.com/zilder)
|
|
193
|
-
[Dmitry Ivanov](https://github.com/funbringer)
|
|
194
|
-
[Ildus Kurbangaliev](https://github.com/ildus)
|
|
195
|
-
[Yury Zhuravlev](https://github.com/stalkerg)
|
|
196
|
-
|
|
197
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|