-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracing_test.py
178 lines (135 loc) · 5.81 KB
/
Tracing_test.py
1
2
3
4
5
6
7
8
9
10
11
12
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
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
import platform
import pytest
from Tracing import LogTrace, LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR
from pathlib import Path
from Unit_test_common import get_test_log_file_name, clear_test_file
TEST_LOGGER = 'test_logger'
ERROR = 0
WARNING = 1
INFO = 2
DEBUG = 3
LOG_TEST_DATA = [{
'LOG_LINE': 'error log trace',
'LINE_TYPE': '| ERROR |'
}, {
'LOG_LINE': 'warning log trace',
'LINE_TYPE': '| WARNING |'
}, {
'LOG_LINE': 'info log trace',
'LINE_TYPE': '| INFO |'
}, {
'LOG_LINE': 'debug log trace',
'LINE_TYPE': '| DEBUG |'
}]
def get_file_content(fileName):
try:
with open(fileName, "r") as file:
content = file.read()
return content
except Exception as e:
print(f"An error occurred: {e}")
def log_contains(value, logContent):
return value in logContent
def count_lines(file_path):
with open(file_path, 'r') as file:
line_count = sum(1 for line in file)
return line_count
def get_log_name(method):
return get_test_log_file_name(__name__ + '-' + method.__name__)
def log_and_verify_file(traceFun, type, shouldLog, method):
fileName = get_log_name(method)
line = LOG_TEST_DATA[type].get('LOG_LINE')
lineType = LOG_TEST_DATA[type].get('LINE_TYPE')
traceFun(line)
content = get_file_content(fileName)
assert log_contains(
line, content) == shouldLog, f"Expected {line} in log to be {shouldLog}"
assert log_contains(
lineType,
content) == shouldLog, f"Expected {lineType} in log to be {shouldLog}"
def check_line_count(method, count):
fileName = get_log_name(method)
actual = count_lines(fileName)
assert actual == count, f"Expected {count} lines in {fileName}, found {actual}"
def verify_assert_and_no_tracing(expectedAssert, testLogger, logFile, level, logFileName):
tracer = None
with pytest.raises(expectedAssert):
tracer = LogTrace(testLogger, logFile, level)
file_path = Path(logFileName)
assert tracer == None, "Expected tracer to be None"
assert file_path.exists(
) == False, f"Expected file {logFileName} to not exist"
def get_logger_and_clean_previous(method, logLevel):
fileName = get_log_name(method)
clear_test_file(fileName, f"{__name__} - {method}")
return LogTrace(method, fileName, logLevel)
def test_debug_logging():
tracer = get_logger_and_clean_previous(test_debug_logging, LOG_LEVEL_DEBUG)
log_and_verify_file(tracer.debug, DEBUG, True, test_debug_logging)
check_line_count(test_debug_logging, 1)
log_and_verify_file(tracer.info, INFO, True, test_debug_logging)
check_line_count(test_debug_logging, 2)
log_and_verify_file(tracer.warning, WARNING, True, test_debug_logging)
check_line_count(test_debug_logging, 3)
log_and_verify_file(tracer.error, ERROR, True, test_debug_logging)
check_line_count(test_debug_logging, 4)
def test_info_logging():
tracer = get_logger_and_clean_previous(test_info_logging, LOG_LEVEL_INFO)
log_and_verify_file(tracer.debug, DEBUG, False, test_info_logging)
check_line_count(test_info_logging, 0)
log_and_verify_file(tracer.info, INFO, True, test_info_logging)
check_line_count(test_info_logging, 1)
log_and_verify_file(tracer.warning, WARNING, True, test_info_logging)
check_line_count(test_info_logging, 2)
log_and_verify_file(tracer.error, ERROR, True, test_info_logging)
check_line_count(test_info_logging, 3)
def test_warn_logging():
tracer = get_logger_and_clean_previous(test_warn_logging, LOG_LEVEL_WARNING)
log_and_verify_file(tracer.debug, DEBUG, False, test_warn_logging)
check_line_count(test_warn_logging, 0)
log_and_verify_file(tracer.info, INFO, False, test_warn_logging)
check_line_count(test_warn_logging, 0)
log_and_verify_file(tracer.warning, WARNING, True, test_warn_logging)
check_line_count(test_warn_logging, 1)
log_and_verify_file(tracer.error, ERROR, True, test_warn_logging)
check_line_count(test_warn_logging, 2)
def test_error_logging():
tracer = get_logger_and_clean_previous(test_error_logging, LOG_LEVEL_ERROR)
log_and_verify_file(tracer.debug, DEBUG, False, test_error_logging)
check_line_count(test_error_logging, 0)
log_and_verify_file(tracer.info, INFO, False, test_error_logging)
check_line_count(test_error_logging, 0)
log_and_verify_file(tracer.warning, WARNING, False, test_error_logging)
check_line_count(test_error_logging, 0)
log_and_verify_file(tracer.error, ERROR, True, test_error_logging)
check_line_count(test_error_logging, 1)
def test_empty_logger_name():
logger_file_name = get_log_name(test_empty_logger_name)
tracer = LogTrace('', logger_file_name, LOG_LEVEL_ERROR)
file_path = Path(logger_file_name)
assert file_path.exists() == True, f"Expected file {logger_file_name} to exist"
def test_none_logger_name():
logger_file_name = get_log_name(test_none_logger_name)
tracer = LogTrace(None, logger_file_name, LOG_LEVEL_ERROR)
file_path = Path(logger_file_name)
assert file_path.exists() == True, f"Expected file {logger_file_name} to exist"
def test_empty_file_name():
expected_assert = None
if platform.system() == "Windows":
expected_assert = PermissionError
else:
expected_assert = IsADirectoryError
verify_assert_and_no_tracing(expected_assert, TEST_LOGGER, '',
LOG_LEVEL_ERROR, get_log_name(test_empty_file_name))
def test_none_file_name():
verify_assert_and_no_tracing(RuntimeError, TEST_LOGGER, None,
LOG_LEVEL_ERROR, get_log_name(test_none_file_name))
def test_bad_type_log_level():
name = get_log_name(test_bad_type_log_level)
verify_assert_and_no_tracing(RuntimeError, TEST_LOGGER, name, 5, name)
def test_bad_value_log_level():
name = get_log_name(test_bad_value_log_level)
verify_assert_and_no_tracing(RuntimeError, TEST_LOGGER, name, "5", name)
def test_none_log_level():
name = get_log_name(test_none_log_level)
verify_assert_and_no_tracing(RuntimeError, TEST_LOGGER, name, None, name)