8000 Using pytest [pytest.raises] · postgrespro/testgres@f37c299 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit f37c299

Browse files
Using pytest [pytest.raises]
1 parent 40eaf7d commit f37c299

File tree

2 files changed

+76
-66
lines changed

2 files changed

+76
-66
lines changed

tests/test_simple.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010
import six
1111
import unittest
12+
import pytest
1213
import psutil
1314

1415
import logging.config
@@ -134,7 +135,7 @@ def test_custom_init(self):
134135
def test_double_init(self):
135136
with get_new_node().init() as node:
136137
# can't initialize node more than once
137-
with self.assertRaises(InitNodeException):
138+
with pytest.raises(expected_exception=InitNodeException):
138139
node.init()
139140

140141
def test_init_after_cleanup(self):
@@ -172,7 +173,7 @@ def test_init_unique_system_id(self):
172173
def test_node_exit(self):
173174
base_dir = None
174175

175-
with self.assertRaises(QueryException):
176+
with pytest.raises(expected_exception=QueryException):
176177
with get_new_node().init() as node:
177178
base_dir = node.base_dir
178179
node.safe_psql('select 1')
@@ -196,7 +197,7 @@ def test_double_start(self):
196197
def test_uninitialized_start(self):
197198
with get_new_node() as node:
198199
# node is not initialized yet
199-
with self.assertRaises(StartNodeException):
200+
with pytest.raises(expected_exception=StartNodeException):
200201
node.start()
201202

202203
def test_restart(self):
@@ -211,7 +212,7 @@ def test_restart(self):
211212
self.assertEqual(res, [(2, )])
212213

213214
# restart, fail
214-
with self.assertRaises(StartNodeException):
215+
with pytest.raises(expected_exception=StartNodeException):
215216
node.append_conf('pg_hba.conf', 'DUMMY')
216217
node.restart()
217218

@@ -303,13 +304,13 @@ def test_psql(self):
303304
self.assertEqual(rm_carriage_returns(_sum), b'6\n')
304305

305306
# check psql's default args, fails
306-
with self.assertRaises(QueryException):
307+
with pytest.raises(expected_exception=QueryException):
307308
node.psql()
308309

309310
node.stop()
310311

311312
# check psql on stopped node, fails
312-
with self.assertRaises(QueryException):
313+
with pytest.raises(expected_exception=QueryException):
313314
node.safe_psql('select 1')
314315

315316
def test_safe_psql__expect_error(self):
@@ -320,11 +321,12 @@ def test_safe_psql__expect_error(self):
320321
self.assertIn('ERROR: syntax error at or near "select_or_not_select"', err)
321322

322323
# ---------
323-
with self.assertRaises(InvalidOperationException) as ctx:
324+
with pytest.raises(
325+
expected_exception=InvalidOperationException,
326+
match="^" + re.escape("Exception was expected, but query finished successfully: `select 1;`.") + "$"
327+
):
324328
node.safe_psql("select 1;", expect_error=True)
325329

326-
self.assertEqual(str(ctx.exception), "Exception was expected, but query finished successfully: `select 1;`.")
327-
328330
# ---------
329331
res = node.safe_psql("select 1;", expect_error=False)
330332
self.assertEqual(rm_carriage_returns(res), b'1\n')
@@ -357,7 +359,7 @@ def test_control_data(self):
357359
with get_new_node() as node:
358360

359361
# node is not initialized yet
360-
with self.assertRaises(ExecUtilException):
362+
with pytest.raises(expected_exception=ExecUtilException):
361363
node.get_control_data()
362364

363365
node.init()
@@ -374,7 +376,7 @@ def test_backup_simple(self):
374376
master.init(allow_streaming=True)
375377

376378
# node must be running
377-
with self.assertRaises(BackupException):
379+
with pytest.raises(expected_exception=BackupException):
378380
master.backup()
379381

380382
# it's time to start node
@@ -411,15 +413,17 @@ def test_backup_exhaust(self):
411413
pass
412414

413415
# now let's try to create one more node
414-
with self.assertRaises(BackupException):
416+
with pytest.raises(expected_exception=BackupException):
415417
backup.spawn_primary()
416418

417419
def test_backup_wrong_xlog_method(self):
418420
with get_new_node() as node:
419421
node.init(allow_streaming=True).start()
420422

421-
with self.assertRaises(BackupException,
422-
msg='Invalid xlog_method "wrong"'):
423+
with pytest.raises(
424+
expected_exception=BackupException,
425+
match="^" + re.escape('Invalid xlog_method "wrong"') + "$"
426+
):
423427
node.backup(xlog_method='wrong')
424428

425429
def test_pg_ctl_wait_option(self):
@@ -551,7 +555,7 @@ def test_logical_replication(self):
551555
self.assertListEqual(res, [(1, 1), (2, 2), (3, 3), (4, 4)])
552556

553557
# explicitly add table
554-
with self.assertRaises(ValueError):
558+
with pytest.raises(expected_exception=ValueError):
555559
pub.add_tables([]) # fail
556560
pub.add_tables(['test2'])
557561
node1.safe_psql('insert into test2 values (\'c\')')
@@ -588,7 +592,7 @@ def test_logical_catchup(self):
588592
@unittest.skipIf(pg_version_ge('10'), 'requires <10')
589593
def test_logical_replication_fail(self):
590594
with get_new_node() as node:
591-
with self.assertRaises(InitNodeException):
595+
with pytest.raises(expected_exception=InitNodeException):
592596
node.init(allow_logical=True)
593597

594598
def test_replication_slots(self):
@@ -599,15 +603,15 @@ def test_replication_slots(self):
599603
replica.execute('select 1')
600604

601605
# cannot create new slot with the same name
602-
with self.assertRaises(TestgresException):
606+
with pytest.raises(expected_exception=TestgresException):
603607
node.replicate(slot='slot1')
604608

605609
def test_incorrect_catchup(self):
606610
with get_new_node() as node:
607611
node.init(allow_streaming=True).start()
608612

609613
# node has no master, can't catch up
610-
with self.assertRaises(TestgresException):
614+
with pytest.raises(expected_exception=TestgresException):
611615
node.catchup()
612616

613617
def test_promotion(self):
@@ -664,12 +668,12 @@ def test_poll_query_until(self):
664668
self.assertTrue(end_time - start_time >= 5)
665669

666670
# check 0 columns
667-
with self.assertRaises(QueryException):
671+
with pytest.raises(expected_exception=QueryException):
668672
node.poll_query_until(
669673
query='select from pg_catalog.pg_class limit 1')
670674

671675
# check None, fail
672-
with self.assertRaises(QueryException):
676+
with pytest.raises(expected_exception=QueryException):
673677
node.poll_query_until(query='create table abc (val int)')
674678

675679
# check None, ok
@@ -682,7 +686,7 @@ def test_poll_query_until(self):
682686
expected=None)
683687

684688
# check arbitrary expected value, fail
685-
with self.assertRaises(TimeoutException):
689+
with pytest.raises(expected_exception=TimeoutException):
686690
node.poll_query_until(query='select 3',
687691
expected=1,
688692
max_attempts=3,
@@ -692,17 +696,17 @@ def test_poll_query_until(self):
692696
node.poll_query_until(query='select 2', expected=2)
693697

694698
# check timeout
695-
with self.assertRaises(TimeoutException):
699+
with pytest.raises(expected_exception=TimeoutException):
696700
node.poll_query_until(query='select 1 > 2',
697701
max_attempts=3,
698702
sleep_time=0.01)
699703

700704
# check ProgrammingError, fail
701-
with self.assertRaises(testgres.ProgrammingError):
705+
with pytest.raises(expected_exception=testgres.ProgrammingError):
702706
node.poll_query_until(query='dummy1')
703707

704708
# check ProgrammingError, ok
705-
with self.assertRaises(TimeoutException):
709+
with pytest.raises(expected_exception=(TimeoutException)):
706710
node.poll_query_until(query='dummy2',
707711
max_attempts=3,
708712
sleep_time=0.01,
@@ -809,11 +813,11 @@ def test_pg_config(self):
809813

810814
def test_config_stack(self):
811815
# no such option
812-
with self.assertRaises(TypeError):
816+
with pytest.raises(expected_exception=TypeError):
813817
configure_testgres(dummy=True)
814818

815819
# we have only 1 config in stack
816-
with self.assertRaises(IndexError):
820+
with pytest.raises(expected_exception=IndexError):
817821
pop_config()
818822

819823
d0 = TestgresConfig.cached_initdb_dir
@@ -827,7 +831,7 @@ def test_config_stack(self):
827831
stack_size = len(testgres.config.config_stack)
828832

829833
# try to break a stack
830-
with self.assertRaises(TypeError):
834+
with pytest.raises(expected_exception=TypeError):
831835
with scoped_config(dummy=True):
832836
pass
833837

@@ -903,7 +907,7 @@ def test_isolation_levels(self):
903907
con.begin(IsolationLevel.Serializable).commit()
904908

905909
# check wrong level
906-
with self.assertRaises(QueryException):
910+
with pytest.raises(expected_exception=QueryException):
907911
con.begin('Garbage').commit()
908912

909913
def test_ports_management(self):
@@ -980,7 +984,7 @@ def test_child_pids(self):
980984
with get_new_node().init().start() as master:
981985

982986
# master node doesn't have a source walsender!
983-
with self.assertRaises(TestgresException):
987+
with pytest.raises(expected_exception=TestgresException):
984988
master.source_walsender
985989

986990
with master.connect() as con:
@@ -1008,7 +1012,7 @@ def test_child_pids(self):
10081012
replica.stop()
10091013

10101014
# there should be no walsender after we've stopped replica
1011-
with self.assertRaises(TestgresException):
1015+
with pytest.raises(expected_exception=TestgresException):
10121016
replica.source_walsender
10131017

10141018
def test_child_process_dies(self):
@@ -1061,11 +1065,12 @@ def test_the_same_port(self):
10611065
self.assertEqual(node2.port, node.port)
10621066
self.assertFalse(node2._should_free_port)
10631067

1064-
with self.assertRaises(StartNodeException) as ctx:
1068+
with pytest.raises(
1069+
expected_exception=StartNodeException,
1070+
match=re.escape("Cannot start node")
1071+
):
10651072
node2.init().start()
10661073

1067-
self.assertIn("Cannot start node", str(ctx.exception))
1068-
10691074
# node is still working
10701075
self.assertEqual(node.port, node_port_copy)
10711076
self.assertTrue(node._should_free_port)
@@ -1218,11 +1223,12 @@ def test_port_conflict(self):
12181223
self.assertTrue(node2._should_free_port)
12191224
self.assertEqual(node2.port, node1.port)
12201225

1221-
with self.assertRaises(StartNodeException) as ctx:
1226+
with pytest.raises(
1227+
expected_exception=StartNodeException,
1228+
match=re.escape("Cannot start node after multiple attempts")
1229+
):
12221230
node2.init().start()
12231231

1224-
self.assertIn("Cannot start node after multiple attempts", str(ctx.exception))
1225-
12261232
self.assertEqual(node2.port, node1.port)
12271233
self.assertTrue(node2._should_free_port)
12281234
self.assertEqual(__class__.tagPortManagerProxy.sm_DummyPortCurrentUsage, 1)

0 commit comments

Comments
 (0)
0