9
9
import time
10
10
import six
11
11
import unittest
12
+ import pytest
12
13
import psutil
13
14
14
15
import logging .config
@@ -134,7 +135,7 @@ def test_custom_init(self):
134
135
def test_double_init (self ):
135
136
with get_new_node ().init () as node :
136
137
# can't initialize node more than once
137
- with self . assertRaises ( InitNodeException ):
138
+ with pytest . raises ( expected_exception = InitNodeException ):
138
139
node .init ()
139
140
140
141
def test_init_after_cleanup (self ):
@@ -172,7 +173,7 @@ def test_init_unique_system_id(self):
172
173
def test_node_exit (self ):
173
174
base_dir = None
174
175
175
- with self . assertRaises ( QueryException ):
176
+ with pytest . raises ( expected_exception = QueryException ):
176
177
with get_new_node ().init () as node :
177
178
base_dir = node .base_dir
178
179
node .safe_psql ('select 1' )
@@ -196,7 +197,7 @@ def test_double_start(self):
196
197
def test_uninitialized_start (self ):
197
198
with get_new_node () as node :
198
199
# node is not initialized yet
199
- with self . assertRaises ( StartNodeException ):
200
+ with pytest . raises ( expected_exception = StartNodeException ):
200
201
node .start ()
201
202
202
203
def test_restart (self ):
@@ -211,7 +212,7 @@ def test_restart(self):
211
212
self .assertEqual (res , [(2 , )])
212
213
213
214
# restart, fail
214
- with self . assertRaises ( StartNodeException ):
215
+ with pytest . raises ( expected_exception = StartNodeException ):
215
216
node .append_conf ('pg_hba.conf' , 'DUMMY' )
216
217
node .restart ()
217
218
@@ -303,13 +304,13 @@ def test_psql(self):
303
304
self .assertEqual (rm_carriage_returns (_sum ), b'6\n ' )
304
305
305
306
# check psql's default args, fails
306
- with self . assertRaises ( QueryException ):
307
+ with pytest . raises ( expected_exception = QueryException ):
307
308
node .psql ()
308
309
309
310
node .stop ()
310
311
311
312
# check psql on stopped node, fails
312
- with self . assertRaises ( QueryException ):
313
+ with pytest . raises ( expected_exception = QueryException ):
313
314
node .safe_psql ('select 1' )
314
315
315
316
def test_safe_psql__expect_error (self ):
@@ -320,11 +321,12 @@ def test_safe_psql__expect_error(self):
320
321
self .assertIn ('ERROR: syntax error at or near "select_or_not_select"' , err )
321
322
322
323
# ---------
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
+ ):
324
328
node .safe_psql ("select 1;" , expect_error = True )
325
329
326
- self .assertEqual (str (ctx .exception ), "Exception was expected, but query finished successfully: `select 1;`." )
327
-
328
330
# ---------
329
331
res = node .safe_psql ("select 1;" , expect_error = False )
330
332
self .assertEqual (rm_carriage_returns (res ), b'1\n ' )
@@ -357,7 +359,7 @@ def test_control_data(self):
357
359
with get_new_node () as node :
358
360
359
361
# node is not initialized yet
360
- with self . assertRaises ( ExecUtilException ):
362
+ with pytest . raises ( expected_exception = ExecUtilException ):
361
363
node .get_control_data ()
362
364
363
365
node .init ()
@@ -374,7 +376,7 @@ def test_backup_simple(self):
374
376
master .init (allow_streaming = True )
375
377
376
378
# node must be running
377
- with self . assertRaises ( BackupException ):
379
+ with pytest . raises ( expected_exception = BackupException ):
378
380
master .backup ()
379
381
380
382
# it's time to start node
@@ -411,15 +413,17 @@ def test_backup_exhaust(self):
411
413
pass
412
414
413
415
# now let's try to create one more node
414
- with self . assertRaises ( BackupException ):
416
+ with pytest . raises ( expected_exception = BackupException ):
415
417
backup .spawn_primary ()
416
418
417
419
def test_backup_wrong_xlog_method (self ):
418
420
with get_new_node () as node :
419
421
node .init (allow_streaming = True ).start ()
420
422
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
+ ):
423
427
node .backup (xlog_method = 'wrong' )
424
428
425
429
def test_pg_ctl_wait_option (self ):
@@ -551,7 +555,7 @@ def test_logical_replication(self):
551
555
self .assertListEqual (res , [(1 , 1 ), (2 , 2 ), (3 , 3 ), (4 , 4 )])
552
556
553
557
# explicitly add table
554
- with self . assertRaises ( ValueError ):
558
+ with pytest . raises ( expected_exception = ValueError ):
555
559
pub .add_tables ([]) # fail
556
560
pub .add_tables (['test2' ])
557
561
node1 .safe_psql ('insert into test2 values (\' c\' )' )
@@ -588,7 +592,7 @@ def test_logical_catchup(self):
588
592
@unittest .skipIf (pg_version_ge ('10' ), 'requires <10' )
589
593
def test_logical_replication_fail (self ):
590
594
with get_new_node () as node :
591
- with self . assertRaises ( InitNodeException ):
595
+ with pytest . raises ( expected_exception = InitNodeException ):
592
596
node .init (allow_logical = True )
593
597
594
598
def test_replication_slots (self ):
@@ -599,15 +603,15 @@ def test_replication_slots(self):
599
603
replica .execute ('select 1' )
600
604
601
605
# cannot create new slot with the same name
602
- with self . assertRaises ( TestgresException ):
606
+ with pytest . raises ( expected_exception = TestgresException ):
603
607
node .replicate (slot = 'slot1' )
604
608
605
609
def test_incorrect_catchup (self ):
606
610
with get_new_node () as node :
607
611
node .init (allow_streaming = True ).start ()
608
612
609
613
# node has no master, can't catch up
610
- with self . assertRaises ( TestgresException ):
614
+ with pytest . raises ( expected_exception = TestgresException ):
611
615
node .catchup ()
612
616
613
617
def test_promotion (self ):
@@ -664,12 +668,12 @@ def test_poll_query_until(self):
664
668
self .assertTrue (end_time - start_time >= 5 )
665
669
666
670
# check 0 columns
667
- with self . assertRaises ( QueryException ):
671
+ with pytest . raises ( expected_exception = QueryException ):
668
672
node .poll_query_until (
669
673
query = 'select from pg_catalog.pg_class limit 1' )
670
674
671
675
# check None, fail
672
- with self . assertRaises ( QueryException ):
676
+ with pytest . raises ( expected_exception = QueryException ):
673
677
node .poll_query_until (query = 'create table abc (val int)' )
674
678
675
679
# check None, ok
@@ -682,7 +686,7 @@ def test_poll_query_until(self):
682
686
expected = None )
683
687
684
688
# check arbitrary expected value, fail
685
- with self . assertRaises ( TimeoutException ):
689
+ with pytest . raises ( expected_exception = TimeoutException ):
686
690
node .poll_query_until (query = 'select 3' ,
687
691
expected = 1 ,
688
692
max_attempts = 3 ,
@@ -692,17 +696,17 @@ def test_poll_query_until(self):
692
696
node .poll_query_until (query = 'select 2' , expected = 2 )
693
697
694
698
# check timeout
695
- with self . assertRaises ( TimeoutException ):
699
+ with pytest . raises ( expected_exception = TimeoutException ):
696
700
node .poll_query_until (query = 'select 1 > 2' ,
697
701
max_attempts = 3 ,
698
702
sleep_time = 0.01 )
699
703
700
704
# check ProgrammingError, fail
701
- with self . assertRaises ( testgres .ProgrammingError ):
705
+ with pytest . raises ( expected_exception = testgres .ProgrammingError ):
702
706
node .poll_query_until (query = 'dummy1' )
703
707
704
708
# check ProgrammingError, ok
705
- with self . assertRaises ( TimeoutException ):
709
+ with pytest . raises ( expected_exception = ( TimeoutException ) ):
706
710
node .poll_query_until (query = 'dummy2' ,
707
711
max_attempts = 3 ,
708
712
sleep_time = 0.01 ,
@@ -809,11 +813,11 @@ def test_pg_config(self):
809
813
810
814
def test_config_stack (self ):
811
815
# no such option
812
- with self . assertRaises ( TypeError ):
816
+ with pytest . raises ( expected_exception = TypeError ):
813
817
configure_testgres (dummy = True )
814
818
815
819
# we have only 1 config in stack
816
- with self . assertRaises ( IndexError ):
820
+ with pytest . raises ( expected_exception = IndexError ):
817
821
pop_config ()
818
822
819
823
d0 = TestgresConfig .cached_initdb_dir
@@ -827,7 +831,7 @@ def test_config_stack(self):
827
831
stack_size = len (testgres .config .config_stack )
828
832
829
833
# try to break a stack
830
- with self . assertRaises ( TypeError ):
834
+ with pytest . raises ( expected_exception = TypeError ):
831
835
with scoped_config (dummy = True ):
832
836
pass
833
837
@@ -903,7 +907,7 @@ def test_isolation_levels(self):
903
907
con .begin (IsolationLevel .Serializable ).commit ()
904
908
905
909
# check wrong level
906
- with self . assertRaises ( QueryException ):
910
+ with pytest . raises ( expected_exception = QueryException ):
907
911
con .begin ('Garbage' ).commit ()
908
912
909
913
def test_ports_management (self ):
@@ -980,7 +984,7 @@ def test_child_pids(self):
980
984
with get_new_node ().init ().start () as master :
981
985
982
986
# master node doesn't have a source walsender!
983
- with self . assertRaises ( TestgresException ):
987
+ with pytest . raises ( expected_exception = TestgresException ):
984
988
master .source_walsender
985
989
986
990
with master .connect () as con :
@@ -1008,7 +1012,7 @@ def test_child_pids(self):
1008
1012
replica .stop ()
1009
1013
1010
1014
# there should be no walsender after we've stopped replica
1011
- with self . assertRaises ( TestgresException ):
1015
+ with pytest . raises ( expected_exception = TestgresException ):
1012
1016
replica .source_walsender
1013
1017
1014
1018
def test_child_process_dies (self ):
@@ -1061,11 +1065,12 @@ def test_the_same_port(self):
1061
1065
self .assertEqual (node2 .port , node .port )
1062
1066
self .assertFalse (node2 ._should_free_port )
1063
1067
1064
- with self .assertRaises (StartNodeException ) as ctx :
1068
+ with pytest .raises (
1069
+ expected_exception = StartNodeException ,
1070
+ match = re .escape ("Cannot start node" )
1071
+ ):
1065
1072
node2 .init ().start ()
1066
1073
1067
- self .assertIn ("Cannot start node" , str (ctx .exception ))
1068
-
1069
1074
# node is still working
1070
1075
self .assertEqual (node .port , node_port_copy )
1071
1076
self .assertTrue (node ._should_free_port )
@@ -1218,11 +1223,12 @@ def test_port_conflict(self):
1218
1223
self .assertTrue (node2 ._should_free_port )
1219
1224
self .assertEqual (node2 .port , node1 .port )
1220
1225
1221
- with self .assertRaises (StartNodeException ) as ctx :
1226
+ with pytest .raises (
1227
+ expected_exception = StartNodeException ,
6A73
td>
1228
+ match = re .escape ("Cannot start node after multiple attempts" )
1229
+ ):
1222
1230
node2 .init ().start ()
1223
1231
1224
- self .assertIn ("Cannot start node after multiple attempts" , str (ctx .exception ))
1225
-
1226
1232
self .assertEqual (node2 .port , node1 .port )
1227
1233
self .assertTrue (node2 ._should_free_port )
1228
1234
self .assertEqual (__class__ .tagPortManagerProxy .sm_DummyPortCurrentUsage , 1 )
0 commit comments