Skip to content

Commit f848a63

Browse files
PostgresNode::safe_psql raises InvalidOperationException
If expect_error is True and no error is detected, safe_psql raises InvalidOperationException exception. Tests (local, remote) are added.
1 parent 45b8dc0 commit f848a63

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

testgres/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
CatchUpException, \
2424
StartNodeException, \
2525
InitNodeException, \
26-
BackupException
26+
BackupException, \
27+
InvalidOperationException
2728

2829
from .enums import \
2930
XLogMethod, \

testgres/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,6 @@ class InitNodeException(TestgresException):
103103

104104
class BackupException(TestgresException):
105105
pass
106+
107+
class InvalidOperationException(TestgresException):
108+
pass

testgres/node.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
TimeoutException, \
7474
InitNodeException, \
7575
TestgresException, \
76-
BackupException
76+
BackupException, \
77+
InvalidOperationException
7778

7879
from .logger import TestgresLogger
7980

@@ -1089,7 +1090,7 @@ def safe_psql(self, query=None, expect_error=False, **kwargs):
10891090
return e.error
10901091

10911092
if expect_error:
1092-
assert False, "Exception was expected, but query finished successfully: `{}` ".format(query)
1093+
raise InvalidOperationException("Exception was expected, but query finished successfully: `{}`.".format(query))
10931094

10941095
return out
10951096

tests/test_simple.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
BackupException, \
2424
QueryException, \
2525
TimeoutException, \
26-
TestgresException, NodeApp
26+
TestgresException, \
27+
InvalidOperationException, \
28+
NodeApp
2729

2830
from testgres import \
2931
TestgresConfig, \
@@ -310,6 +312,23 @@ def test_psql(self):
310312
with self.assertRaises(QueryException):
311313
node.safe_psql('select 1')
312314

315+
def test_safe_psql__expect_error(self):
316+
with get_new_node().init().start() as node:
317+
err = node.safe_psql('select_or_not_select 1', expect_error=True)
318+
self.assertTrue(type(err) == str) # noqa: E721
319+
self.assertIn('select_or_not_select', err)
320+
self.assertIn('ERROR: syntax error at or near "select_or_not_select"', err)
321+
322+
# ---------
323+
with self.assertRaises(InvalidOperationException) as ctx:
324+
node.safe_psql("select 1;", expect_error=True)
325+
326+
self.assertEqual(str(ctx.exception), "Exception was expected, but query finished successfully: `select 1;`.")
327+
328+
# ---------
329+
res = node.safe_psql("select 1;", expect_error=False)
330+
self.assertEqual(res, b'1\n')
331+
313332
def test_transactions(self):
314333
with get_new_node().init().start() as node:
315334

tests/test_simple_remote.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
BackupException, \
2424
QueryException, \
2525
TimeoutException, \
26-
TestgresException
26+
TestgresException, \
27+
InvalidOperationException
2728

2829
from testgres.config import \
2930
TestgresConfig, \
@@ -295,6 +296,23 @@ def test_psql(self):
295296
with self.assertRaises(QueryException):
296297
node.safe_psql('select 1')
297298

299+
def test_safe_psql__expect_error(self):
300+
with get_remote_node(conn_params=conn_params).init().start() as node:
301+
err = node.safe_psql('select_or_not_select 1', expect_error=True)
302+
self.assertTrue(type(err) == str) # noqa: E721
303+
self.assertIn('select_or_not_select', err)
304+
self.assertIn('ERROR: syntax error at or near "select_or_not_select"', err)
305+
306+
# ---------
307+
with self.assertRaises(InvalidOperationException) as ctx:
308+
node.safe_psql("select 1;", expect_error=True)
309+
310+
self.assertEqual(str(ctx.exception), "Exception was expected, but query finished successfully: `select 1;`.")
311+
312+
# ---------
313+
res = node.safe_psql("select 1;", expect_error=False)
314+
self.assertEqual(res, b'1\n')
315+
298316
def test_transactions(self):
299317
with get_remote_node(conn_params=conn_params).init().start() as node:
300318
with node.connect() as con:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy