Skip to content

Commit 8eaa289

Browse files
NodeApp is refactored totally
1 parent 17fb8c3 commit 8eaa289

File tree

1 file changed

+75
-28
lines changed

1 file changed

+75
-28
lines changed

testgres/node_app.py

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,67 @@
55
import os
66
import platform
77
import tempfile
8+
import typing
89

910

10-
class NodeApp:
11+
T_DICT_STR_STR = typing.Dict[str, str]
12+
T_LIST_STR = typing.List[str]
13+
1114

12-
def __init__(self, test_path=None, nodes_to_cleanup=None, os_ops=None):
15+
class NodeApp:
16+
_os_ops: OsOperations
17+
_test_path: str
18+
19+
def __init__(
20+
self,
21+
test_path: typing.Optional[str] = None,
22+
nodes_to_cleanup: typing.Optional[list] = None,
23+
os_ops: typing.Optional[OsOperations] = None
24+
):
25+
assert test_path is None or type(test_path) == str # noqa: E721
1326
assert os_ops is None or isinstance(os_ops, OsOperations)
1427

1528
if os_ops is None:
1629
os_ops = LocalOperations.get_single_instance()
1730

1831
assert isinstance(os_ops, OsOperations)
32+
self._os_ops = os_ops
1933

20-
if test_path:
21-
if os.path.isabs(test_path):
22-
self.test_path = test_path
23-
else:
24-
self.test_path = os_ops.build_path(os_ops.cwd(), test_path)
34+
if test_path is None:
35+
self._test_path = os_ops.cwd()
36+
elif os.path.isabs(test_path):
37+
self._test_path = test_path
2538
else:
26-
self.test_path = os_ops.cwd()
39+
self._test_path = os_ops.build_path(os_ops.cwd(), test_path)
40+
2741
self.nodes_to_cleanup = nodes_to_cleanup if nodes_to_cleanup else []
28-
self.os_ops = os_ops
42+
43+
@property
44+
def os_ops(self) -> OsOperations:
45+
assert isinstance(self._os_ops, OsOperations)
46+
return self._os_ops
47+
48+
@property
49+
def test_path(self) -> str:
50+
assert isinstance(self._test_path, OsOperations)
51+
return self._test_path
2952

3053
def make_empty(
3154
self,
32-
base_dir=None,
33-
port=None,
34-
bin_dir=None):
35-
real_base_dir = self.os_ops.build_path(self.test_path, base_dir)
36-
self.os_ops.rmdirs(real_base_dir, ignore_errors=True)
37-
self.os_ops.makedirs(real_base_dir)
55+
base_dir: typing.Optional[str] = None,
56+
port: typing.Optional[int] = None,
57+
bin_dir: typing.Optional[str] = None
58+
) -> PostgresNode:
59+
assert base_dir is None or type(base_dir) == str # noqa: E721
60+
assert port is None or type(port) == int # noqa: E721
61+
assert bin_dir is None or type(bin_dir) == str # noqa: E721
62+
63+
assert isinstance(self._os_ops, OsOperations)
64+
assert type(self._test_path) == str # noqa: E721
65+
66+
real_base_dir = self._os_ops.build_path(self._test_path, base_dir)
67+
self._os_ops.rmdirs(real_base_dir, ignore_errors=True)
68+
self._os_ops.makedirs(real_base_dir)
3869

3970
node = PostgresNode(base_dir=real_base_dir, port=port, bin_dir=bin_dir)
4071
self.nodes_to_cleanup.append(node)
@@ -43,24 +74,40 @@ def make_empty(
4374

4475
def make_simple(
4576
self,
46-
base_dir=None,
47-
port=None,
48-
set_replication=False,
49-
ptrack_enable=False,
50-
initdb_params=[],
51-
pg_options={},
52-
checksum=True,
53-
bin_dir=None):
77+
base_dir: typing.Optional[str] = None,
78+
port: typing.Optional[int] = None,
79+
set_replication: bool = False,
80+
ptrack_enable: bool = False,
81+
initdb_params: T_LIST_STR = [],
82+
pg_options: T_DICT_STR_STR = {},
83+
checksum: bool = True,
84+
bin_dir: typing.Optional[str] = None
85+
) -> PostgresNode:
86+
assert base_dir is None or type(base_dir) == str # noqa: E721
87+
assert port is None or type(port) == int # noqa: E721
88+
assert type(set_replication) == bool # noqa: E721
89+
assert type(ptrack_enable) == bool # noqa: E721
90+
assert type(initdb_params) == list # noqa: E721
5491
assert type(pg_options) == dict # noqa: E721
92+
assert type(checksum) == bool # noqa: E721
93+
assert bin_dir is None or type(bin_dir) == str # noqa: E721
5594

5695
if checksum and '--data-checksums' not in initdb_params:
5796
initdb_params.append('--data-checksums')
58-
node = self.make_empty(base_dir, port, bin_dir=bin_dir)
97+
98+
node = self.make_empty(
99+
base_dir,
100+
port,
101+
bin_dir=bin_dir
102+
)
103+
59104
node.init(
60-
initdb_params=initdb_params, allow_streaming=set_replication)
105+
initdb_params=initdb_params,
106+
allow_streaming=set_replication
107+
)
61108

62109
# set major version
63-
pg_version_file = self.os_ops.read(self.os_ops.build_path(node.data_dir, 'PG_VERSION'))
110+
pg_version_file = self._os_ops.read(self._os_ops.build_path(node.data_dir, 'PG_VERSION'))
64111
node.major_version_str = str(pg_version_file.rstrip())
65112
node.major_version = float(node.major_version_str)
66113

@@ -115,7 +162,7 @@ def make_simple(
115162
return node
116163

117164
@staticmethod
118-
def _gettempdir_for_socket():
165+
def _gettempdir_for_socket() -> str:
119166
platform_system_name = platform.system().lower()
120167

121168
if platform_system_name == "windows":
@@ -143,7 +190,7 @@ def _gettempdir_for_socket():
143190
return "/tmp"
144191

145192
@staticmethod
146-
def _gettempdir():
193+
def _gettempdir() -> str:
147194
v = tempfile.gettempdir()
148195

149196
#

0 commit comments

Comments
 (0)