Newer
Older
import io
from contextlib import redirect_stdout
from pathlib import Path
import tempfile
from unittest import TestCase
from sohstationviewer.view.util.functions import (
get_soh_messages_for_view, log_str, is_doc_file,
create_search_results_file, create_table_of_content_file)
from sohstationviewer.conf import constants as const
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class TestGetSOHMessageForView(TestCase):
def test_no_or_empty_textlog(self):
soh_msg_channels = {"ACE": ["test1\ntest2", "test3"],
"LOG": ["test4"]}
soh_msg_for_view = {'ACE': ['test1', 'test2', 'test3'],
'LOG': ['test4']}
with self.subTest('test_no_TEXT_str_dataset_key'):
soh_messages = {"key1": soh_msg_channels}
ret = get_soh_messages_for_view("key1", soh_messages)
self.assertNotIn('TEXT', list(ret.keys()))
self.assertEqual(ret, soh_msg_for_view)
with self.subTest('test_empty_TEXT_tupple_dataset_key'):
soh_messages = {"TEXT": [], ("key1", "key2"): soh_msg_channels}
ret = get_soh_messages_for_view(("key1", "key2"), soh_messages)
self.assertNotIn('TEXT', list(ret.keys()))
self.assertEqual(ret, soh_msg_for_view)
# no key "TEXT", dataset has no channels
with self.subTest('test_no_TEXT_no_SOH_channels_for_dataset'):
soh_messages = {"key1": {}}
ret = get_soh_messages_for_view("key1", soh_messages)
self.assertEqual(ret, {})
def test_some_empty_soh_message(self):
soh_messages = {"TEXT": ['text1', 'text2\ntext3'],
"key1": {"ACE": ["test1\ntest2", "test3"],
"LOG": []}}
# channel LOG is empty
ret = get_soh_messages_for_view("key1", soh_messages)
self.assertEqual(ret,
{'TEXT': ['text1', 'text2', 'text3'],
'ACE': ['test1', 'test2', 'test3'],
'LOG': []})
class TestLogStr(TestCase):
def test_log_str(self):
log = ('info line 1', LogType.INFO)
ret = log_str(log)
self.assertEqual(ret, 'INFO: info line 1')
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class TestIsDocFile(TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.temp_dir = tempfile.TemporaryDirectory()
def _run_is_doc_file(self, filename, include_table_of_contents=False):
test_file = Path(self.temp_dir.name).joinpath(filename)
with open(test_file, 'w'):
pass
return is_doc_file(
test_file, include_table_of_contents=include_table_of_contents)
def test_not_md_file(self):
self.assertFalse(self._run_is_doc_file("doc.md"))
def test_table_of_contents_file(self):
self.assertFalse(self._run_is_doc_file(
"./" + const.TABLE_CONTENTS,
include_table_of_contents=False))
self.assertTrue(self._run_is_doc_file(
"./" + const.TABLE_CONTENTS,
include_table_of_contents=True))
def test_doc_file(self):
self.assertTrue(self._run_is_doc_file('doc.help.md'))
class TestCreateSearchResultFile(TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.temp_dir = tempfile.TemporaryDirectory()
cls.temp_dir_path = Path(cls.temp_dir.name)
with open(cls.temp_dir_path.joinpath('file1.help.md'), 'w') as file1:
file1.write('exist1')
with open(cls.temp_dir_path.joinpath(
'01 _ file2.help.md'), 'w') as file2:
file2.write('exist2')
with open(cls.temp_dir_path.joinpath('file3.md'), 'w') as file3:
file3.write('exist')
with open(cls.temp_dir_path.joinpath(const.SEARCH_RESULTS), 'w'):
pass
with open(cls.temp_dir_path.joinpath(const.TABLE_CONTENTS), 'w'):
pass
def test_search_text_in_no_files(self):
search_result_filename = create_search_results_file(
self.temp_dir_path, 'non_exist')
self.assertEqual(search_result_filename.name, const.SEARCH_RESULTS)
with open(search_result_filename, 'r') as file:
content = file.read()
self.assertEqual(
content,
"# Search results\n\nText 'non_exist' not found.")
def test_search_text_in_one_file(self):
search_result_filename = create_search_results_file(
self.temp_dir_path, 'exist1')
self.assertEqual(search_result_filename.name, const.SEARCH_RESULTS)
with open(search_result_filename, 'r') as file:
content = file.read()
self.assertEqual(
content,
"# Search results\n\n"
"Text 'exist1' found in the following files:\n\n"
"---------------------------\n\n"
"+ [file1](file1.help.md)\n\n")
def test_search_text_in_all_files(self):
search_result_filename = create_search_results_file(
self.temp_dir_path, 'exist')
self.assertEqual(search_result_filename.name, const.SEARCH_RESULTS)
with open(search_result_filename, 'r') as file:
content = file.read()
self.assertEqual(
content,
"# Search results\n\n"
"Text 'exist' found in the following files:\n\n"
"---------------------------\n\n"
"+ [file2](01%20_%20file2.help.md)\n\n"
"+ [file1](file1.help.md)\n\n")
def test_empty_search_text(self):
# This case is excluded in help_view
search_result_filename = create_search_results_file(
self.temp_dir_path, '')
self.assertEqual(search_result_filename.name, const.SEARCH_RESULTS)
with open(search_result_filename, 'r') as file:
content = file.read()
self.assertEqual(
content,
"# Search results\n\n"
"Text '' found in the following files:\n\n"
"---------------------------\n\n"
"+ [file2](01%20_%20file2.help.md)\n\n"
"+ [file1](file1.help.md)\n\n")
def test_no_search_result_file_exist(self):
search_result_file_path = self.temp_dir_path.joinpath(
const.SEARCH_RESULTS)
search_result_file_path.unlink() # remove file
search_result_filename = create_search_results_file(
self.temp_dir_path, 'exist2')
self.assertEqual(search_result_filename.name, const.SEARCH_RESULTS)
with open(search_result_filename, 'r') as file:
content = file.read()
self.assertEqual(
content,
"# Search results\n\n"
"Text 'exist2' found in the following files:\n\n"
"---------------------------\n\n"
"+ [file2](01%20_%20file2.help.md)\n\n")
class TestCreateTableOfContentFile(TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.temp_dir = tempfile.TemporaryDirectory()
cls.temp_dir_path = Path(cls.temp_dir.name)
with open(cls.temp_dir_path.joinpath('file1.help.md'), 'w') as file1:
file1.write('exist1')
with open(cls.temp_dir_path.joinpath(
'01 _ file2.help.md'), 'w') as file2:
file2.write('exist2')
with open(cls.temp_dir_path.joinpath('file3.md'), 'w') as file3:
file3.write('exist')
with open(cls.temp_dir_path.joinpath(const.SEARCH_RESULTS), 'w'):
pass
with open(cls.temp_dir_path.joinpath(const.TABLE_CONTENTS), 'w'):
pass
def test_create_table_of_contents_file(self):
table_contents_path = self.temp_dir_path.joinpath(const.TABLE_CONTENTS)
f = io.StringIO()
with redirect_stdout(f):
create_table_of_content_file(self.temp_dir_path)
output = f.getvalue()
self.assertEqual(
f"{table_contents_path.as_posix()} has been created.",
output.strip())
self.assertIn(table_contents_path, list(self.temp_dir_path.iterdir()))
with open(table_contents_path, 'r') as file:
content = file.read()
self.assertTrue(content.endswith(
"# Table of Contents\n\n"
"+ [Table of Contents](01%20_%20Table%20of%20Contents.help.md)"
"\n\n"
"+ [file2](01%20_%20file2.help.md)\n\n"
"+ [file1](file1.help.md)\n\n",
)
)