Skip to content

Commit 307ef5f

Browse files
Test refactoring (#236)
* TestUtils is added * TestTestgresCommon::test_node_repr is added * test_get_pg_config2 moved to TestUtils * TestTestgresCommon.test_custom_init is added * TestConfig is added
1 parent 14bc733 commit 307ef5f

File tree

5 files changed

+145
-181
lines changed

5 files changed

+145
-181
lines changed

tests/test_config.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from ..testgres import TestgresConfig
2+
from ..testgres import configure_testgres
3+
from ..testgres import scoped_config
4+
from ..testgres import pop_config
5+
6+
from .. import testgres
7+
8+
import pytest
9+
10+
11+
class TestConfig:
12+
def test_config_stack(self):
13+
# no such option
14+
with pytest.raises(expected_exception=TypeError):
15+
configure_testgres(dummy=True)
16+
17+
# we have only 1 config in stack
18+
with pytest.raises(expected_exception=IndexError):
19+
pop_config()
20+
21+
d0 = TestgresConfig.cached_initdb_dir
22+
d1 = 'dummy_abc'
23+
d2 = 'dummy_def'
24+
25+
with scoped_config(cached_initdb_dir=d1) as c1:
26+
assert (c1.cached_initdb_dir == d1)
27+
28+
with scoped_config(cached_initdb_dir=d2) as c2:
29+
stack_size = len(testgres.config.config_stack)
30+
31+
# try to break a stack
32+
with pytest.raises(expected_exception=TypeError):
33+
with scoped_config(dummy=True):
34+
pass
35+
36+
assert (c2.cached_initdb_dir == d2)
37+
assert (len(testgres.config.config_stack) == stack_size)
38+
39+
assert (c1.cached_initdb_dir == d1)
40+
41+
assert (TestgresConfig.cached_initdb_dir == d0)

tests/test_testgres_common.py

+26-28
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from ..testgres.node import PgVer
77
from ..testgres.node import PostgresNode
88
from ..testgres.utils import get_pg_version2
9-
from ..testgres.utils import get_pg_config2
109
from ..testgres.utils import file_tail
1110
from ..testgres.utils import get_bin_path2
1211
from ..testgres import ProcessType
@@ -106,6 +105,32 @@ def test_version_management(self, node_svc: PostgresNodeService):
106105
assert (isinstance(node.version, PgVer))
107106
assert (node.version == PgVer(version))
108107

108+
def test_node_repr(self, node_svc: PostgresNodeService):
109+
with __class__.helper__get_node(node_svc).init() as node:
110+
pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)"
111+
assert re.match(pattern, str(node)) is not None
112+
113+
def test_custom_init(self, node_svc: PostgresNodeService):
114+
assert isinstance(node_svc, PostgresNodeService)
115+
116+
with __class__.helper__get_node(node_svc) as node:
117+
# enable page checksums
118+
node.init(initdb_params=['-k']).start()
119+
120+
with __class__.helper__get_node(node_svc) as node:
121+
node.init(
122+
allow_streaming=True,
123+
initdb_params=['--auth-local=reject', '--auth-host=reject'])
124+
125+
hba_file = os.path.join(node.data_dir, 'pg_hba.conf')
126+
lines = node.os_ops.readlines(hba_file)
127+
128+
# check number of lines
129+
assert (len(lines) >= 6)
130+
131+
# there should be no trust entries at all
132+
assert not (any('trust' in s for s in lines))
133+
109134
def test_double_init(self, node_svc: PostgresNodeService):
110135
assert isinstance(node_svc, PostgresNodeService)
111136

@@ -1075,33 +1100,6 @@ def test_dump(self, node_svc: PostgresNodeService):
10751100
res = node3.execute(query_select)
10761101
assert (res == [(1, ), (2, )])
10771102

1078-
def test_get_pg_config2(self, node_svc: PostgresNodeService):
1079-
assert isinstance(node_svc, PostgresNodeService)
1080-
1081-
# check same instances
1082-
a = get_pg_config2(node_svc.os_ops, None)
1083-
b = get_pg_config2(node_svc.os_ops, None)
1084-
assert (id(a) == id(b))
1085-
1086-
# save right before config change
1087-
c1 = get_pg_config2(node_svc.os_ops, None)
1088-
1089-
# modify setting for this scope
1090-
with scoped_config(cache_pg_config=False) as config:
1091-
# sanity check for value
1092-
assert not (config.cache_pg_config)
1093-
1094-
# save right after config change
1095-
c2 = get_pg_config2(node_svc.os_ops, None)
1096-
1097-
# check different instances after config change
1098-
assert (id(c1) != id(c2))
1099-
1100-
# check different instances
1101-
a = get_pg_config2(node_svc.os_ops, None)
1102-
b = get_pg_config2(node_svc.os_ops, None)
1103-
assert (id(a) != id(b))
1104-
11051103
def test_pgbench(self, node_svc: PostgresNodeService):
11061104
assert isinstance(node_svc, PostgresNodeService)
11071105

tests/test_testgres_local.py

+10-86
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,18 @@
99

1010
from .. import testgres
1111

12-
from ..testgres import \
13-
StartNodeException, \
14-
ExecUtilException, \
15-
NodeApp
16-
17-
from ..testgres import \
18-
TestgresConfig, \
19-
configure_testgres, \
20-
scoped_config, \
21-
pop_config
22-
23-
from ..testgres import \
24-
get_new_node
25-
26-
from ..testgres import \
27-
get_bin_path, \
28-
get_pg_config, \
29-
get_pg_version
12+
from ..testgres import StartNodeException
13+
from ..testgres import ExecUtilException
14+
from ..testgres import NodeApp
15+
from ..testgres import scoped_config
16+
from ..testgres import get_new_node
17+
from ..testgres import get_bin_path
18+
from ..testgres import get_pg_config
19+
from ..testgres import get_pg_version
3020

3121
# NOTE: those are ugly imports
32-
from ..testgres import bound_ports
33-
from ..testgres.utils import PgVer, parse_pg_version
22+
from ..testgres.utils import bound_ports
23+
from ..testgres.utils import PgVer
3424
from ..testgres.node import ProcessProxy
3525

3626

@@ -75,31 +65,6 @@ def rm_carriage_returns(out):
7565

7666

7767
class TestTestgresLocal:
78-
def test_node_repr(self):
79-
with get_new_node() as node:
80-
pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)"
81-
assert re.match(pattern, str(node)) is not None
82-
83-
def test_custom_init(self):
84-
with get_new_node() as node:
85-
# enable page checksums
86-
node.init(initdb_params=['-k']).start()
87-
88-
with get_new_node() as node:
89-
node.init(
90-
allow_streaming=True,
91-
initdb_params=['--auth-local=reject', '--auth-host=reject'])
92-
93-
hba_file = os.path.join(node.data_dir, 'pg_hba.conf')
94-
with open(hba_file, 'r') as conf:
95-
lines = conf.readlines()
96-
97-
# check number of lines
98-
assert (len(lines) >= 6)
99-
100-
# there should be no trust entries at all
101-
assert not (any('trust' in s for s in lines))
102-
10368
def test_pg_config(self):
10469
# check same instances
10570
a = get_pg_config()
@@ -125,37 +90,6 @@ def test_pg_config(self):
12590
b = get_pg_config()
12691
assert (id(a) != id(b))
12792

128-
def test_config_stack(self):
129-
# no such option
130-
with pytest.raises(expected_exception=TypeError):
131-
configure_testgres(dummy=True)
132-
133-
# we have only 1 config in stack
134-
with pytest.raises(expected_exception=IndexError):
135-
pop_config()
136-
137-
d0 = TestgresConfig.cached_initdb_dir
138-
d1 = 'dummy_abc'
139-
d2 = 'dummy_def'
140-
141-
with scoped_config(cached_initdb_dir=d1) as c1:
142-
assert (c1.cached_initdb_dir == d1)
143-
144-
with scoped_config(cached_initdb_dir=d2) as c2:
145-
stack_size = len(testgres.config.config_stack)
146-
147-
# try to break a stack
148-
with pytest.raises(expected_exception=TypeError):
149-
with scoped_config(dummy=True):
150-
pass
151-
152-
assert (c2.cached_initdb_dir == d2)
153-
assert (len(testgres.config.config_stack) == stack_size)
154-
155-
assert (c1.cached_initdb_dir == d1)
156-
157-
assert (TestgresConfig.cached_initdb_dir == d0)
158-
15993
def test_ports_management(self):
16094
assert bound_ports is not None
16195
assert type(bound_ports) == set # noqa: E721
@@ -234,16 +168,6 @@ def test_upgrade_node(self):
234168
node_new.start()
235169
assert (b'Upgrade Complete' in res)
236170

237-
def test_parse_pg_version(self):
238-
# Linux Mint
239-
assert parse_pg_version("postgres (PostgreSQL) 15.5 (Ubuntu 15.5-1.pgdg22.04+1)") == "15.5"
240-
# Linux Ubuntu
241-
assert parse_pg_version("postgres (PostgreSQL) 12.17") == "12.17"
242-
# Windows
243-
assert parse_pg_version("postgres (PostgreSQL) 11.4") == "11.4"
244-
# Macos
245-
assert parse_pg_version("postgres (PostgreSQL) 14.9 (Homebrew)") == "14.9"
246-
247171
class tagPortManagerProxy:
248172
sm_prev_testgres_reserve_port = None
249173
sm_prev_testgres_release_port = None

tests/test_testgres_remote.py

+6-67
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# coding: utf-8
22
import os
3-
import re
43

54
import pytest
65
import logging
@@ -10,19 +9,14 @@
109

1110
from .. import testgres
1211

13-
from ..testgres.exceptions import \
14-
InitNodeException, \
15-
ExecUtilException
12+
from ..testgres.exceptions import InitNodeException
13+
from ..testgres.exceptions import ExecUtilException
1614

17-
from ..testgres.config import \
18-
TestgresConfig, \
19-
configure_testgres, \
20-
scoped_config, \
21-
pop_config, testgres_config
15+
from ..testgres.config import scoped_config
16+
from ..testgres.config import testgres_config
2217

23-
from ..testgres import \
24-
get_bin_path, \
25-
get_pg_config
18+
from ..testgres import get_bin_path
19+
from ..testgres import get_pg_config
2620

2721
# NOTE: those are ugly imports
2822

@@ -58,30 +52,6 @@ def implicit_fixture(self):
5852
testgres_config.set_os_ops(os_ops=prev_ops)
5953
assert testgres_config.os_ops is prev_ops
6054

61-
def test_node_repr(self):
62-
with __class__.helper__get_node() as node:
63-
pattern = r"PostgresNode\(name='.+', port=.+, base_dir='.+'\)"
64-
assert re.match(pattern, str(node)) is not None
65-
66-
def test_custom_init(self):
67-
with __class__.helper__get_node() as node:
68-
# enable page checksums
69-
node.init(initdb_params=['-k']).start()
70-
71-
with __class__.helper__get_node() as node:
72-
node.init(
73-
allow_streaming=True,
74-
initdb_params=['--auth-local=reject', '--auth-host=reject'])
75-
76-
hba_file = os.path.join(node.data_dir, 'pg_hba.conf')
77-
lines = node.os_ops.readlines(hba_file)
78-
79-
# check number of lines
80-
assert (len(lines) >= 6)
81-
82-
# there should be no trust entries at all
83-
assert not (any('trust' in s for s in lines))
84-
8555
def test_init__LANG_С(self):
8656
# PBCKP-1744
8757
prev_LANG = os.environ.get("LANG")
@@ -193,37 +163,6 @@ def test_pg_config(self):
193163
b = get_pg_config()
194164
assert (id(a) != id(b))
195165

196-
def test_config_stack(self):
197-
# no such option
198-
with pytest.raises(expected_exception=TypeError):
199-
configure_testgres(dummy=True)
200-
201-
# we have only 1 config in stack
202-
with pytest.raises(expected_exception=IndexError):
203-
pop_config()
204-
205-
d0 = TestgresConfig.cached_initdb_dir
206-
d1 = 'dummy_abc'
207-
d2 = 'dummy_def'
208-
209-
with scoped_config(cached_initdb_dir=d1) as c1:
210-
assert (c1.cached_initdb_dir == d1)
211-
212-
with scoped_config(cached_initdb_dir=d2) as c2:
213-
stack_size = len(testgres.config.config_stack)
214-
215-
# try to break a stack
216-
with pytest.raises(expected_exception=TypeError):
217-
with scoped_config(dummy=True):
218-
pass
219-
220-
assert (c2.cached_initdb_dir == d2)
221-
assert (len(testgres.config.config_stack) == stack_size)
222-
223-
assert (c1.cached_initdb_dir == d1)
224-
225-
assert (TestgresConfig.cached_initdb_dir == d0)
226-
227166
@staticmethod
228167
def helper__get_node(name=None):
229168
svc = PostgresNodeServices.sm_remote

0 commit comments

Comments
 (0)