|
1 | 1 | import unittest |
2 | | -# import time |
3 | | -# from testgres import clean_all |
4 | 2 | from testgres import get_new_node, stop_all |
5 | 3 |
|
6 | 4 |
|
7 | 5 | class SimpleTest(unittest.TestCase): |
8 | 6 |
|
9 | | - def teardown(self): |
10 | | - # clean_all() |
11 | | - stop_all() |
| 7 | + def teardown(self): |
| 8 | + # clean_all() |
| 9 | + stop_all() |
12 | 10 |
|
13 | | - @unittest.skip("demo") |
14 | | - def test_start_stop(self): |
15 | | - node = get_new_node('test') |
16 | | - node.init() |
17 | | - node.start() |
18 | | - res = node.execute('postgres', 'select 1') |
19 | | - self.assertEqual(len(res), 1) |
20 | | - self.assertEqual(res[0][0], 1) |
21 | | - node.stop() |
| 11 | + @unittest.skip("demo") |
| 12 | + def test_start_stop(self): |
| 13 | + node = get_new_node('test') |
| 14 | + node.init() |
| 15 | + node.start() |
| 16 | + res = node.execute('postgres', 'select 1') |
| 17 | + self.assertEqual(len(res), 1) |
| 18 | + self.assertEqual(res[0][0], 1) |
| 19 | + node.stop() |
22 | 20 |
|
23 | | - def test_backup_and_replication(self): |
24 | | - node = get_new_node('test') |
25 | | - replica = get_new_node('repl') |
| 21 | + def test_backup_and_replication(self): |
| 22 | + node = get_new_node('test') |
| 23 | + replica = get_new_node('repl') |
26 | 24 |
|
27 | | - node.init(allows_streaming=True) |
28 | | - node.start() |
29 | | - node.psql('postgres', 'create table abc(a int, b int)') |
30 | | - node.psql('postgres', 'insert into abc values (1, 2)') |
31 | | - node.backup('my_backup') |
| 25 | + node.init(allows_streaming=True) |
| 26 | + node.start() |
| 27 | + node.psql('postgres', 'create table abc(a int, b int)') |
| 28 | + node.psql('postgres', 'insert into abc values (1, 2)') |
| 29 | + node.backup('my_backup') |
32 | 30 |
|
33 | | - replica.init_from_backup(node, 'my_backup', has_streaming=True) |
34 | | - replica.start() |
35 | | - res = replica.execute('postgres', 'select * from abc') |
36 | | - self.assertEqual(len(res), 1) |
37 | | - self.assertEqual(res[0], (1, 2)) |
| 31 | + replica.init_from_backup(node, 'my_backup', has_streaming=True) |
| 32 | + replica.start() |
| 33 | + res = replica.execute('postgres', 'select * from abc') |
| 34 | + self.assertEqual(len(res), 1) |
| 35 | + self.assertEqual(res[0], (1, 2)) |
38 | 36 |
|
39 | | - # Insert into master node |
40 | | - node.psql('postgres', 'insert into abc values (3, 4)') |
41 | | - # Wait until data syncronizes |
42 | | - node.poll_query_until( |
43 | | - 'postgres', |
44 | | - 'SELECT pg_current_xlog_location() <= replay_location ' |
45 | | - 'FROM pg_stat_replication WHERE application_name = \'%s\'' |
46 | | - % replica.name) |
47 | | - # time.sleep(0.5) |
48 | | - # Check that this record was exported to replica |
49 | | - res = replica.execute('postgres', 'select * from abc') |
50 | | - self.assertEqual(len(res), 2) |
51 | | - self.assertEqual(res[1], (3, 4)) |
| 37 | + # Insert into master node |
| 38 | + node.psql('postgres', 'insert into abc values (3, 4)') |
| 39 | + # Wait until data syncronizes |
| 40 | + node.poll_query_until( |
| 41 | + 'postgres', |
| 42 | + 'SELECT pg_current_xlog_location() <= replay_location ' |
| 43 | + 'FROM pg_stat_replication WHERE application_name = \'%s\'' |
| 44 | + % replica.name) |
| 45 | + # time.sleep(0.5) |
| 46 | + # Check that this record was exported to replica |
| 47 | + res = replica.execute('postgres', 'select * from abc') |
| 48 | + self.assertEqual(len(res), 2) |
| 49 | + self.assertEqual(res[1], (3, 4)) |
52 | 50 |
|
53 | | - node.stop() |
54 | | - replica.stop() |
| 51 | + node.stop() |
| 52 | + replica.stop() |
| 53 | + |
| 54 | + def test_dump(self): |
| 55 | + node = get_new_node('test') |
| 56 | + node.init().start() |
| 57 | + node.safe_psql( |
| 58 | + 'postgres', |
| 59 | + 'create table abc as ' |
| 60 | + 'select g as a, g as b from generate_series(1, 10) as g' |
| 61 | + ) |
| 62 | + node.psql('postgres', 'create database test') |
| 63 | + node.dump('postgres', 'test.sql') |
| 64 | + node.restore('test', 'test.sql') |
| 65 | + self.assertEqual( |
| 66 | + node.psql('postgres', 'select * from abc'), |
| 67 | + node.psql('test', 'select * from abc'), |
| 68 | + ) |
| 69 | + node.stop() |
| 70 | + |
| 71 | + def test_users(self): |
| 72 | + node = get_new_node('master') |
| 73 | + node.init().start() |
| 74 | + node.psql('postgres', 'create role test_user login') |
| 75 | + value = node.safe_psql('postgres', 'select 1', username='test_user') |
| 76 | + self.assertEqual(value, '1\n') |
55 | 77 |
|
56 | | - def test_dump(self): |
57 | | - node = get_new_node('test') |
58 | | - node.init().start() |
59 | | - node.safe_psql( |
60 | | - 'postgres', |
61 | | - 'create table abc as ' |
62 | | - 'select g as a, g as b from generate_series(1, 10) as g' |
63 | | - ) |
64 | | - node.psql('postgres', 'create database test') |
65 | | - node.dump('postgres', 'test.sql') |
66 | | - node.restore('test', 'test.sql') |
67 | | - self.assertEqual( |
68 | | - node.psql('postgres', 'select * from abc'), |
69 | | - node.psql('test', 'select * from abc'), |
70 | | - ) |
71 | | - node.stop() |
72 | 78 |
|
73 | 79 | if __name__ == '__main__': |
74 | | - unittest.main() |
| 80 | + unittest.main() |
0 commit comments