Skip to content

Commit 33c3b7b

Browse files
committed
rewrite tests using assertRaises()
1 parent 07b42ae commit 33c3b7b

File tree

1 file changed

+35
-81
lines changed

1 file changed

+35
-81
lines changed

tests/test_simple.py

+35-81
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,28 @@
3131
class SimpleTest(unittest.TestCase):
3232
def test_double_init(self):
3333
with get_new_node('test') as node:
34-
got_exception = False
35-
36-
try:
34+
# can't initialize node more than once
35+
with self.assertRaises(InitNodeException):
3736
node.init()
3837
node.init()
39-
except InitNodeException as e:
40-
got_exception = True
4138

42-
self.assertTrue(got_exception)
39+
def test_init_after_cleanup(self):
40+
with get_new_node('test') as node:
41+
node.init().start()
42+
node.status()
43+
node.safe_psql('postgres', 'select 1')
44+
45+
node.cleanup()
46+
47+
node.init().start()
48+
node.status()
49+
node.safe_psql('postgres', 'select 1')
4350

4451
def test_uninitialized_start(self):
4552
with get_new_node('test') as node:
46-
got_exception = False
47-
48-
try:
53+
# node is not initialized yet
54+
with self.assertRaises(StartNodeException):
4955
node.start()
50-
except StartNodeException as e:
51-
got_exception = True
52-
53-
self.assertTrue(got_exception)
5456

5557
def test_restart(self):
5658
with get_new_node('test') as node:
@@ -66,12 +68,8 @@ def test_psql(self):
6668
node.init().start()
6769

6870
# check default params
69-
got_exception = False
70-
try:
71+
with self.assertRaises(QueryException):
7172
node.psql('postgres')
72-
except QueryException as e:
73-
got_exception = True
74-
self.assertTrue(got_exception)
7573

7674
# check returned values
7775
res = node.psql('postgres', 'select 1')
@@ -86,12 +84,8 @@ def test_psql(self):
8684
node.stop()
8785

8886
# check psql on stopped node
89-
got_exception = False
90-
try:
87+
with self.assertRaises(QueryException):
9188
node.safe_psql('postgres', 'select 1')
92-
except QueryException as e:
93-
got_exception = True
94-
self.assertTrue(got_exception)
9589

9690
def test_status(self):
9791
# check NodeStatus cast to bool
@@ -181,28 +175,17 @@ def test_transactions(self):
181175

182176
def test_control_data(self):
183177
with get_new_node('test') as node:
184-
got_exception = False
185178

186-
try:
179+
# node is not initialized yet
180+
with self.assertRaises(ExecUtilException):
187181
node.get_control_data()
188-
except ExecUtilException as e:
189-
got_exception = True
190-
self.assertTrue(got_exception)
191182

192-
got_exception = False
193-
194-
try:
195-
node.init()
196-
data = node.get_control_data()
197-
198-
# check returned dict
199-
self.assertIsNotNone(data)
200-
self.assertTrue(any('pg_control' in s for s in data.keys()))
183+
node.init()
184+
data = node.get_control_data()
201185

202-
except ExecUtilException as e:
203-
print(e.message)
204-
got_exception = True
205-
self.assertFalse(got_exception)
186+
# check returned dict
187+
self.assertIsNotNone(data)
188+
self.assertTrue(any('pg_control' in s for s in data.keys()))
206189

207190
def test_backup_simple(self):
208191
with get_new_node('master') as master:
@@ -238,19 +221,15 @@ def test_backup_exhaust(self):
238221
node.init(allow_streaming=True).start()
239222

240223
with node.backup(xlog_method='fetch') as backup:
224+
225+
# exhaust backup by creating new node
241226
with backup.spawn_primary('node1') as node1:
242227
pass
243228

244-
got_exception = False
245-
try:
229+
# now let's try to create one more node
230+
with self.assertRaises(BackupException):
246231
with backup.spawn_primary('node2') as node2:
247232
pass
248-
except BackupException as e:
249-
got_exception = True
250-
except Exception as e:
251-
pass
252-
253-
self.assertTrue(got_exception)
254233

255234
def test_backup_and_replication(self):
256235
with get_new_node('node') as node, get_new_node('repl') as replica:
@@ -296,12 +275,9 @@ def test_incorrect_catchup(self):
296275
with get_new_node('node') as node:
297276
node.init(allow_streaming=True).start()
298277

299-
got_exception = False
300-
try:
278+
# node has no master, can't catch up
279+
with self.assertRaises(CatchUpException):
301280
node.catchup()
302-
except CatchUpException as e:
303-
got_exception = True
304-
self.assertTrue(got_exception)
305281

306282
def test_dump(self):
307283
with get_new_node('node1') as node1:
@@ -348,41 +324,24 @@ def test_poll_query_until(self):
348324
self.assertTrue(end_time - start_time >= 5)
349325

350326
# check 0 rows
351-
got_exception = False
352-
try:
327+
with self.assertRaises(QueryException):
353328
node.poll_query_until(
354329
'postgres', 'select * from pg_class where true = false')
355-
except QueryException as e:
356-
got_exception = True
357-
self.assertTrue(got_exception)
358330

359331
# check 0 columns
360-
got_exception = False
361-
try:
332+
with self.assertRaises(QueryException):
362333
node.poll_query_until('postgres',
363334
'select from pg_class limit 1')
364-
except QueryException as e:
365-
got_exception = True
366-
self.assertTrue(got_exception)
367-
368335
# check None
369-
got_exception = False
370-
try:
336+
with self.assertRaises(QueryException):
371337
node.poll_query_until('postgres', 'create table abc (val int)')
372-
except QueryException as e:
373-
got_exception = True
374-
self.assertTrue(got_exception)
375338

376339
# check timeout
377-
got_exception = False
378-
try:
340+
with self.assertRaises(TimeoutException):
379341
node.poll_query_until(dbname='postgres',
380342
query='select 1 > 2',
381343
max_attempts=5,
382344
sleep_time=0.2)
383-
except TimeoutException as e:
384-
got_exception = True
385-
self.assertTrue(got_exception)
386345

387346
def test_logging(self):
388347
logfile = tempfile.NamedTemporaryFile('w', delete=True)
@@ -511,13 +470,8 @@ def test_isolation_levels(self):
511470
con.begin(IsolationLevel.Serializable).commit()
512471

513472
# check wrong level
514-
got_exception = False
515-
try:
473+
with self.assertRaises(QueryException):
516474
con.begin('Garbage').commit()
517-
except QueryException:
518-
got_exception = True
519-
520-
self.assertTrue(got_exception)
521475

522476

523477
if __name__ == '__main__':

0 commit comments

Comments
 (0)