Skip to content

Commit 5508425

Browse files
committed
Merge branch 'master' of github.com:postgrespro/testgres
2 parents 7a7dfdd + 19bbb72 commit 5508425

File tree

7 files changed

+47
-33
lines changed

7 files changed

+47
-33
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
[![codecov](https://codecov.io/gh/postgrespro/testgres/branch/master/graph/badge.svg)](https://codecov.io/gh/postgrespro/testgres)
33
[![PyPI version](https://badge.fury.io/py/testgres.svg)](https://badge.fury.io/py/testgres)
44

5+
[Documentation](https://postgrespro.github.io/testgres/)
6+
57
# testgres
68

79
PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
readme = f.read()
2222

2323
setup(
24-
version='1.7.0',
24+
version='1.8.2',
2525
name='testgres',
2626
packages=['testgres'],
2727
description='Testing utility for PostgreSQL and its extensions',

testgres/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
from .connection import \
1212
NodeConnection, \
13+
DatabaseError, \
1314
InternalError, \
14-
ProgrammingError
15+
ProgrammingError, \
16+
OperationalError
1517

1618
from .exceptions import *
1719
from .enums import *

testgres/backup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ def spawn_primary(self, name=None, destroy=True):
143143
base_dir = self._prepare_dir(destroy)
144144

145145
# Build a new PostgresNode
146-
from .node import PostgresNode
147-
with clean_on_error(PostgresNode(name=name, base_dir=base_dir)) as node:
146+
NodeClass = self.original_node.__class__
147+
with clean_on_error(NodeClass(name=name, base_dir=base_dir)) as node:
148148

149149
# New nodes should always remove dir tree
150150
node._should_rm_dirs = True

testgres/connection.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
from .exceptions import QueryException
1919

20-
# export these exceptions
20+
# export some exceptions
21+
DatabaseError = pglib.DatabaseError
2122
InternalError = pglib.InternalError
2223
ProgrammingError = pglib.ProgrammingError
24+
OperationalError = pglib.OperationalError
2325

2426

2527
class NodeConnection(object):

testgres/node.py

+34-26
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919

2020
from .config import testgres_config
2121

22-
from .connection import \
23-
NodeConnection, \
24-
InternalError, \
25-
ProgrammingError
22+
from .connection import NodeConnection
2623

2724
from .consts import \
2825
DATA_DIR, \
@@ -541,10 +538,10 @@ def append_conf(self, line='', filename=PG_CONF_FILE, **kwargs):
541538
This instance of :class:`.PostgresNode`.
542539
543540
Examples:
544-
append_conf(fsync=False)
545-
append_conf('log_connections = yes')
546-
append_conf(random_page_cost=1.5, fsync=True, ...)
547-
append_conf('postgresql.conf', 'synchronous_commit = off')
541+
>>> append_conf(fsync=False)
542+
>>> append_conf('log_connections = yes')
543+
>>> append_conf(random_page_cost=1.5, fsync=True, ...)
544+
>>> append_conf('postgresql.conf', 'synchronous_commit = off')
548545
"""
549546

550547
lines = [line]
@@ -815,7 +812,8 @@ def psql(self,
815812
filename=None,
816813
dbname=None,
817814
username=None,
818-
input=None):
815+
input=None,
816+
**variables):
819817
"""
820818
Execute a query using psql.
821819
@@ -825,9 +823,15 @@ def psql(self,
825823
dbname: database name to connect to.
826824
username: database user name.
827825
input: raw input to be passed.
826+
**variables: vars to be set before execution.
828827
829828
Returns:
830829
A tuple of (code, stdout, stderr).
830+
831+
Examples:
832+
>>> psql('select 1')
833+
>>> psql('postgres', 'select 2')
834+
>>> psql(query='select 3', ON_ERROR_STOP=1)
831835
"""
832836

833837
# Set default arguments
@@ -846,6 +850,10 @@ def psql(self,
846850
dbname
847851
] # yapf: disable
848852

853+
# set variables before execution
854+
for key, value in iteritems(variables):
855+
psql_params.extend(["--set", '{}={}'.format(key, value)])
856+
849857
# select query source
850858
if query:
851859
psql_params.extend(("-c", query))
@@ -877,10 +885,15 @@ def safe_psql(self, query=None, **kwargs):
877885
username: database user name.
878886
input: raw input to be passed.
879887
888+
**kwargs are passed to psql().
889+
880890
Returns:
881891
psql's output as str.
882892
"""
883893

894+
# force this setting
895+
kwargs['ON_ERROR_STOP'] = 1
896+
884897
ret, out, err = self.psql(query=query, **kwargs)
885898
if ret:
886899
raise QueryException((err or b'').decode('utf-8'), query)
@@ -980,8 +993,7 @@ def poll_query_until(self,
980993
sleep_time=1,
981994
expected=True,
982995
commit=True,
983-
raise_programming_error=True,
984-
raise_internal_error=True):
996+
suppress=None):
985997
"""
986998
Run a query once per second until it returns 'expected'.
987999
Query should return a single value (1 row, 1 column).
@@ -994,13 +1006,13 @@ def poll_query_until(self,
9941006
sleep_time: how much should we sleep after a failure?
9951007
expected: what should be returned to break the cycle?
9961008
commit: should (possible) changes be committed?
997-
raise_programming_error: enable ProgrammingError?
998-
raise_internal_error: enable InternalError?
1009+
suppress: a collection of exceptions to be suppressed.
9991010
10001011
Examples:
1001-
poll_query_until('select true')
1002-
poll_query_until('postgres', "select now() > '01.01.2018'")
1003-
poll_query_until('select false', expected=True, max_attempts=4)
1012+
>>> poll_query_until('select true')
1013+
>>> poll_query_until('postgres', "select now() > '01.01.2018'")
1014+
>>> poll_query_until('select false', expected=True, max_attempts=4)
1015+
>>> poll_query_until('select 1', suppress={testgres.OperationalError})
10041016
"""
10051017

10061018
# sanity checks
@@ -1032,13 +1044,8 @@ def poll_query_until(self,
10321044
elif expected is None:
10331045
return # done
10341046

1035-
except ProgrammingError as e:
1036-
if raise_programming_error:
1037-
raise e
1038-
1039-
except InternalError as e:
1040-
if raise_internal_error:
1041-
raise e
1047+
except tuple(suppress or []):
1048+
pass # we're suppressing them
10421049

10431050
time.sleep(sleep_time)
10441051
attempts += 1
@@ -1229,13 +1236,14 @@ def pgbench_run(self, dbname=None, username=None, options=[], **kwargs):
12291236
options: additional options for pgbench (list).
12301237
12311238
**kwargs: named options for pgbench.
1232-
Examples:
1233-
pgbench_run(initialize=True, scale=2)
1234-
pgbench_run(time=10)
12351239
Run pgbench --help to learn more.
12361240
12371241
Returns:
12381242
Stdout produced by pgbench.
1243+
1244+
Examples:
1245+
>>> pgbench_run(initialize=True, scale=2)
1246+
>>> pgbench_run(time=10)
12391247
"""
12401248

12411249
# Set default arguments

tests/test_simple.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def test_poll_query_until(self):
626626
query='dummy2',
627627
max_attempts=3,
628628
sleep_time=0.01,
629-
raise_programming_error=False)
629+
suppress={testgres.ProgrammingError})
630630

631631
# check 1 arg, ok
632632
node.poll_query_until('select true')
@@ -862,7 +862,7 @@ def test_version_management(self):
862862
with get_new_node() as node:
863863
self.assertTrue(isinstance(version, six.string_types))
864864
self.assertTrue(isinstance(node.version, PgVer))
865-
self.assertTrue(node.version == version)
865+
self.assertEqual(node.version, str(version))
866866

867867
def test_child_pids(self):
868868
master_processes = [

0 commit comments

Comments
 (0)