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
from sohstationviewer.conf.constants import (WF_1ST, WF_2ND, WF_3RD)
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
58
59
60
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')
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
211
212
213
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",
)
)
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
class TestCheckChanWildcardsFormat(TestCase):
def test_len1(self):
with self.subTest("Wildcard is *"):
wc = '*'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard isn't *"):
wc = 'L'
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has length=1 which must be '*'."
)
def test_len2(self):
with self.subTest("Wildcard with first char matched"):
wc = 'L*'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with first char not matched"):
wc = 'W*'
pattern = f"[{WF_1ST}*]"
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has first character not match {pattern}."
)
with self.subTest("Wildcard with last char matched"):
wc = '*N'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with last char not matched"):
wc = '*L'
pattern = f"[{WF_3RD}*]"
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has last character not match {pattern}."
)
with self.subTest("Wildcard is **"):
wc = '**'
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' includes '**' which isn't allowed."
)
def test_len3(self):
with self.subTest("Wildcard with first char matched"):
wc = 'HL*'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with first char is a '*'"):
wc = '*L*'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with first char not matched"):
wc = 'WL*'
pattern = f"[{WF_1ST}*]"
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has first character not match {pattern}."
)
with self.subTest("Wildcard with last char matched"):
wc = '*LN'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with last char is a '*'"):
wc = 'HL*'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with last char not matched"):
wc = '*LL'
pattern = f"[{WF_3RD}*]"
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has last character not match {pattern}."
)
with self.subTest("Wildcard with second char matched"):
wc = 'HHN'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with second char is a star"):
wc = 'H*N'
try:
check_chan_wildcards_format(wc)
except Exception:
self.fail(f"Wildcard '{wc}' raise Exception unexpectedly")
with self.subTest("Wildcard with second char not matched"):
wc = 'HWE'
pattern = f"[{WF_2ND}*]"
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has second character not match {pattern}."
)
with self.subTest("Wildcard start with '**'"):
wc = '**E'
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' includes '**' which isn't allowed."
)
with self.subTest("Wildcard end with '**'"):
wc = 'H**'
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' includes '**' which isn't allowed."
)
def test_len_gt_3(self):
wc = 'HHLL'
with self.assertRaises(Exception) as context:
check_chan_wildcards_format(wc)
self.assertEqual(
str(context.exception),
f"Request '{wc}' has length={len(wc)} > 3 which isn't allowed."
)
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
class TestCheckMassPos(TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.mp_data = {'MP1': {'chan_id': 'MP1', 'samplerate': 1},
'MP3': {'chan_id': 'MP3', 'samplerate': 1},
'MP4': {'chan_id': 'MP4', 'samplerate': 1}}
cls.sel_key = '1378'
def test_include_mp123(self):
with self.assertRaises(Exception) as context:
check_masspos(self.mp_data, self.sel_key,
include_mp123=True, include_mp456=False)
self.assertEqual(
str(context.exception),
f"Data set {self.sel_key} doesn't include mass position 2")
def test_include_mp456(self):
with self.assertRaises(Exception) as context:
check_masspos(self.mp_data, self.sel_key,
include_mp123=False, include_mp456=True)
self.assertEqual(
str(context.exception),
f"Data set {self.sel_key} doesn't include mass position 5,6")
def test_include_mp123456(self):
with self.assertRaises(Exception) as context:
check_masspos(self.mp_data, self.sel_key,
include_mp123=True, include_mp456=True)
self.assertEqual(
str(context.exception),
f"Data set {self.sel_key} doesn't include mass position 2,5,6")
def test_not_include_mp(self):
try:
check_masspos(self.mp_data, self.sel_key,
include_mp123=False, include_mp456=False)
except Exception:
self.fail("check_masspos() raise Exception unexpectedly")