Skip to content

Commit 63b6bd6

Browse files
committed
reimplement support for pg_dump, fix tests, provide a hack for pg8000
1 parent 8de1f11 commit 63b6bd6

File tree

2 files changed

+69
-61
lines changed

2 files changed

+69
-61
lines changed

testgres/testgres.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ def execute(self, query, *args):
228228
self.cursor.execute(query, args)
229229

230230
try:
231-
return self.cursor.fetchall()
231+
res = self.cursor.fetchall()
232+
233+
if isinstance(res, tuple):
234+
res = [tuple(t) for t in res]
235+
236+
return res
232237
except Exception:
233238
return None
234239

@@ -752,37 +757,41 @@ def safe_psql(self, dbname, query, username=None):
752757
raise QueryException(six.text_type(err))
753758
return out
754759

755-
def dump(self, dbname, filename):
760+
def dump(self, dbname, filename=None):
756761
"""
757762
Dump database using pg_dump.
758763
759764
Args:
760765
dbname: database name to connect to (str).
761766
filename: output file (str).
767+
768+
Returns:
769+
Path to file containing dump.
762770
"""
763771

764-
path = os.path.join(self.base_dir, filename)
772+
f, filename = filename or tempfile.mkstemp()
773+
os.close(f)
774+
765775
_params = [
766776
"-p{}".format(self.port),
767-
"-f", path, dbname
777+
"-f{}".format(filename),
778+
dbname
768779
]
769780

770781
_execute_utility("pg_dump", _params, self.utils_logname)
771782

772-
def restore(self, dbname, filename, node=None):
783+
return filename
784+
785+
def restore(self, dbname, filename):
773786
"""
774787
Restore database from pg_dump's file.
775788
776789
Args:
777790
dbname: database name to connect to (str).
778-
filename: output file (str).
791+
filename: database dump taken by pg_dump (str).
779792
"""
780793

781-
if not node:
782-
node = self
783-
784-
path = os.path.join(node.base_dir, filename)
785-
self.psql(dbname, filename=path)
794+
self.psql(dbname, filename=filename)
786795

787796
def poll_query_until(self, dbname, query):
788797
"""

testgres/tests/test_simple.py

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ def test_simple_queries(self):
9797
self.assertEqual(res, b'1\n')
9898

9999
res = node.execute('postgres', 'select 1')
100-
self.assertEqual(res, ([1], ))
100+
self.assertListEqual(res, [(1, )])
101101

102102
with node.connect('postgres') as con:
103103
res = con.execute('select 1')
104-
self.assertEqual(res, ([1], ))
104+
self.assertListEqual(res, [(1, )])
105105

106106
def test_transactions(self):
107107
with get_new_node('test') as node:
@@ -116,12 +116,12 @@ def test_transactions(self):
116116
con.begin()
117117
con.execute('insert into test values (2)')
118118
res = con.execute('select * from test order by val asc')
119-
self.assertEqual(res, ([1], [2]))
119+
self.assertListEqual(res, [(1, ), (2, )])
120120
con.rollback()
121121

122122
con.begin()
123123
res = con.execute('select * from test')
124-
self.assertEqual(res, ([1], ))
124+
self.assertListEqual(res, [(1, )])
125125
con.rollback()
126126

127127
con.begin()
@@ -165,7 +165,7 @@ def test_backup_simple(self):
165165
slave.start()
166166
res = slave.execute('postgres',
167167
'select * from test order by i asc')
168-
self.assertEqual(res, ([1], [2], [3], [4]))
168+
self.assertListEqual(res, [(1, ), (2, ), (3, ), (4, )])
169169

170170
def test_backup_multiple(self):
171171
with get_new_node('node') as node:
@@ -210,53 +210,52 @@ def test_backup_and_replication(self):
210210

211211
backup = node.backup()
212212

213-
replica = backup.spawn_replica('replica')
214-
replica.start()
215-
res = replica.execute('postgres', 'select * from abc')
216-
self.assertEqual(len(res), 1)
217-
self.assertEqual(res[0][0], 1)
218-
self.assertEqual(res[0][1], 2)
219-
220-
cur_ver = LooseVersion(get_pg_config()['VERSION_NUM'])
221-
min_ver = LooseVersion('10')
222-
223-
# Prepare the query which would check whether record reached replica
224-
# (It is slightly different for Postgres 9.6 and Postgres 10+)
225-
if cur_ver >= min_ver:
226-
wait_lsn = 'SELECT pg_current_wal_lsn() <= replay_lsn ' \
227-
'FROM pg_stat_replication WHERE application_name = \'%s\'' \
228-
% replica.name
229-
else:
230-
wait_lsn = 'SELECT pg_current_xlog_location() <= replay_location '\
231-
'FROM pg_stat_replication WHERE application_name = \'%s\'' \
232-
% replica.name
233-
234-
# Insert into master node
235-
node.psql('postgres', 'insert into abc values (3, 4)')
236-
# Wait until data syncronizes
237-
node.poll_query_until('postgres', wait_lsn)
238-
# Check that this record was exported to replica
239-
res = replica.execute('postgres', 'select * from abc')
240-
self.assertEqual(len(res), 2)
241-
self.assertEqual(res[1][0], 3)
242-
self.assertEqual(res[1][1], 4)
243-
244-
# check manual cleanup
245-
replica.cleanup()
246-
replica.free_port()
213+
with backup.spawn_replica('replica') as replica:
214+
replica.start()
215+
res = replica.execute('postgres', 'select * from abc')
216+
self.assertListEqual(res, [(1, 2)])
217+
218+
cur_ver = LooseVersion(get_pg_config()['VERSION_NUM'])
219+
min_ver = LooseVersion('10')
220+
221+
# Prepare the query which would check whether record reached replica
222+
# (It is slightly different for Postgres 9.6 and Postgres 10+)
223+
if cur_ver >= min_ver:
224+
wait_lsn = 'SELECT pg_current_wal_lsn() <= replay_lsn ' \
225+
'FROM pg_stat_replication WHERE application_name = \'%s\'' \
226+
% replica.name
227+
else:
228+
wait_lsn = 'SELECT pg_current_xlog_location() <= replay_location '\
229+
'FROM pg_stat_replication WHERE application_name = \'%s\'' \
230+
% replica.name
231+
232+
# Insert into master node
233+
node.psql('postgres', 'insert into abc values (3, 4)')
234+
# Wait until data syncronizes
235+
node.poll_query_until('postgres', wait_lsn)
236+
# Check that this record was exported to replica
237+
res = replica.execute('postgres', 'select * from abc')
238+
self.assertListEqual(res, [(1, 2), (3, 4)])
247239

248240
def test_dump(self):
249-
with get_new_node('test') as node:
250-
node.init().start()
251-
node.safe_psql(
252-
'postgres', 'create table abc as '
253-
'select g as a, g as b from generate_series(1, 10) as g')
254-
node.psql('postgres', 'create database test')
255-
node.dump('postgres', 'test.sql')
256-
node.restore('test', 'test.sql')
257-
self.assertEqual(
258-
node.psql('postgres', 'select * from abc'),
259-
node.psql('test', 'select * from abc'), )
241+
with get_new_node('node1') as node1:
242+
node1.init().start()
243+
244+
with node1.connect('postgres') as con:
245+
con.begin()
246+
con.execute('create table test (val int)')
247+
con.execute('insert into test values (1), (2)')
248+
con.commit()
249+
250+
# take a new dump
251+
dump = node1.dump('postgres')
252+
253+
with get_new_node('node2') as node2:
254+
node2.init().start().restore('postgres', dump)
255+
256+
res = node2.execute('postgres',
257+
'select * from test order by val asc')
258+
self.assertListEqual(res, [(1, ), (2, )])
260259

261260
def test_users(self):
262261
with get_new_node('master') as node:

0 commit comments

Comments
 (0)