Skip to content

Commit 3e91753

Browse files
committed
improve tests for coverage
1 parent 0d53798 commit 3e91753

File tree

1 file changed

+32
-53
lines changed

1 file changed

+32
-53
lines changed

tests/test_simple.py

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# coding: utf-8
33

44
import os
5+
import shutil
56
import subprocess
67
import tempfile
78
import testgres
@@ -60,7 +61,6 @@ def test_custom_init(self):
6061
with get_new_node() as node:
6162
# enable page checksums
6263
node.init(initdb_params=['-k']).start()
63-
node.safe_psql('select 1')
6464

6565
with get_new_node() as node:
6666
node.init(
@@ -79,27 +79,36 @@ def test_custom_init(self):
7979

8080
def test_double_init(self):
8181
with get_new_node().init() as node:
82-
8382
# can't initialize node more than once
8483
with self.assertRaises(InitNodeException):
8584
node.init()
8685

8786
def test_init_after_cleanup(self):
8887
with get_new_node() as node:
89-
node.init().start()
90-
node.status()
91-
node.safe_psql('select 1')
92-
88+
node.init().start().execute('select 1')
9389
node.cleanup()
90+
node.init().start().execute('select 1')
9491

95-
node.init().start()
96-
node.status()
97-
node.safe_psql('select 1')
92+
def test_node_exit(self):
93+
base_dir = None
9894

99-
def test_double_start(self):
100-
with get_new_node() as node:
101-
node.init().start()
95+
with self.assertRaises(QueryException):
96+
with get_new_node().init() as node:
97+
base_dir = node.base_dir
98+
node.safe_psql('select 1')
99+
100+
# we should save the DB for "debugging"
101+
self.assertTrue(os.path.exists(base_dir))
102+
shutil.rmtree(base_dir, ignore_errors=True)
103+
104+
with get_new_node().init() as node:
105+
base_dir = node.base_dir
106+
107+
# should have been removed by default
108+
self.assertFalse(os.path.exists(base_dir))
102109

110+
def test_double_start(self):
111+
with get_new_node().init().start() as node:
103112
# can't start node more than once
104113
with self.assertRaises(StartNodeException):
105114
node.start()
@@ -150,38 +159,33 @@ def test_pg_ctl(self):
150159
self.assertTrue('PID' in status)
151160

152161
def test_status(self):
153-
# check NodeStatus cast to bool
154162
self.assertTrue(NodeStatus.Running)
155-
156-
# check NodeStatus cast to bool
157163
self.assertFalse(NodeStatus.Stopped)
158-
159-
# check NodeStatus cast to bool
160164
self.assertFalse(NodeStatus.Uninitialized)
161165

162166
# check statuses after each operation
163167
with get_new_node() as node:
164-
self.assertEqual(node.get_pid(), 0)
168+
self.assertEqual(node.pid, 0)
165169
self.assertEqual(node.status(), NodeStatus.Uninitialized)
166170

167171
node.init()
168172

169-
self.assertEqual(node.get_pid(), 0)
173+
self.assertEqual(node.pid, 0)
170174
self.assertEqual(node.status(), NodeStatus.Stopped)
171175

172176
node.start()
173177

174-
self.assertTrue(node.get_pid() > 0)
178+
self.assertNotEqual(node.pid, 0)
175179
self.assertEqual(node.status(), NodeStatus.Running)
176180

177181
node.stop()
178182

179-
self.assertEqual(node.get_pid(), 0)
183+
self.assertEqual(node.pid, 0)
180184
self.assertEqual(node.status(), NodeStatus.Stopped)
181185

182186
node.cleanup()
183187

184-
self.assertEqual(node.get_pid(), 0)
188+
self.assertEqual(node.pid, 0)
185189
self.assertEqual(node.status(), NodeStatus.Uninitialized)
186190

187191
def test_psql(self):
@@ -283,8 +287,7 @@ def test_backup_simple(self):
283287
master.psql('create table test as select generate_series(1, 4) i')
284288

285289
with master.backup(xlog_method='stream') as backup:
286-
with backup.spawn_primary() as slave:
287-
slave.start()
290+
with backup.spawn_primary().start() as slave:
288291
res = slave.execute('select * from test order by i asc')
289292
self.assertListEqual(res, [(1, ), (2, ), (3, ), (4, )])
290293

@@ -310,36 +313,12 @@ def test_backup_exhaust(self):
310313
with node.backup(xlog_method='fetch') as backup:
311314

312315
# exhaust backup by creating new node
313-
with backup.spawn_primary() as node1: # noqa
316+
with backup.spawn_primary():
314317
pass
315318

316319
# now let's try to create one more node
317320
with self.assertRaises(BackupException):
318-
with backup.spawn_primary() as node2: # noqa
319-
pass
320-
321-
def test_backup_and_replication(self):
322-
with get_new_node() as node:
323-
node.init(allow_streaming=True).start()
324-
325-
node.psql('create table abc(a int, b int)')
326-
node.psql('insert into abc values (1, 2)')
327-
328-
backup = node.backup()
329-
330-
with backup.spawn_replica().start() as replica:
331-
res = replica.execute('select * from abc')
332-
self.assertListEqual(res, [(1, 2)])
333-
334-
# Insert into master node
335-
node.psql('insert into abc values (3, 4)')
336-
337-
# Wait until data syncronizes
338-
replica.catchup()
339-
340-
# Check that this record was exported to replica
341-
res = replica.execute('select * from abc')
342-
self.assertListEqual(res, [(1, 2), (3, 4)])
321+
backup.spawn_primary()
343322

344323
def test_replicate(self):
345324
with get_new_node() as node:
@@ -574,8 +553,6 @@ def test_auto_name(self):
574553
self.assertTrue(r.status())
575554

576555
# check their names
577-
self.assertIsNotNone(m.name)
578-
self.assertIsNotNone(r.name)
579556
self.assertNotEqual(m.name, r.name)
580557
self.assertTrue('testgres' in m.name)
581558
self.assertTrue('testgres' in r.name)
@@ -588,7 +565,9 @@ def test_file_tail(self):
588565
s3 = "def\n"
589566

590567
with tempfile.NamedTemporaryFile(mode='r+', delete=True) as f:
591-
for i in range(1, 5000):
568+
sz = 0
569+
while sz < 3 * 8192:
570+
sz += len(s1)
592571
f.write(s1)
593572
f.write(s2)
594573
f.write(s3)

0 commit comments

Comments
 (0)