test: enhance test for formatter and add test for LogMessage

This commit is contained in:
shenjack 2023-11-18 23:36:29 +08:00
parent 7f478a0abb
commit 100e985e3b
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 60 additions and 25 deletions

View File

@ -7,10 +7,7 @@
import inspect import inspect
from lib_not_dr.logger.structure import LogMessage from lib_not_dr.logger.structure import LogMessage
from lib_not_dr.logger.formatter import (StdFormatter, from lib_not_dr.logger.formatter import (StdFormatter)
TimeFormatter,
LevelFormatter,
TraceFormatter)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -57,6 +57,8 @@ class LogMessage(Options):
:param kwargs: other options :param kwargs: other options
""" """
# 为了方便使用 单独覆盖了 __init__ 方法来提供代码补全的选项 # 为了方便使用 单独覆盖了 __init__ 方法来提供代码补全的选项
if messages is None:
messages = []
super().__init__(messages=messages, super().__init__(messages=messages,
end=end, end=end,
split=split, split=split,
@ -76,6 +78,8 @@ class LogMessage(Options):
return False return False
def format_message(self) -> str: def format_message(self) -> str:
if self.split is None:
self.split = ' '
return self.split.join(self.messages) + self.end return self.split.join(self.messages) + self.end
def format_for_message(self) -> Dict[str, str]: def format_for_message(self) -> Dict[str, str]:

View File

@ -7,7 +7,7 @@
import inspect import inspect
import unittest import unittest
from lib_not_dr.logger.formatter import BaseFormatter, TimeFormatter, LevelFormatter, TraceFormatter from lib_not_dr.logger.formatter import BaseFormatter, MainFormatter
from lib_not_dr.logger.structure import LogMessage from lib_not_dr.logger.structure import LogMessage
@ -26,22 +26,8 @@ class FormatterTest(unittest.TestCase):
self.assertEqual(message.logger_name, 'root') self.assertEqual(message.logger_name, 'root')
self.assertEqual(message.logger_tag, 'root') self.assertEqual(message.logger_tag, 'root')
def test_create_empty_tag_message(self): def test_format_level(self):
message = LogMessage() formatter = MainFormatter()
self.assertIsNone(message.logger_tag)
self.assertEqual(message.format_for_message()['logger_tag'], ' ')
def test_formatter(self):
formatter = BaseFormatter()
formatter.info()
def test_time_formatter(self):
formatter = TimeFormatter()
formatter.info()
def test_level_formatter(self):
formatter = LevelFormatter()
formatter.info() formatter.info()
message = LogMessage(messages=['test'], message = LogMessage(messages=['test'],
level=0) level=0)
@ -55,7 +41,3 @@ class FormatterTest(unittest.TestCase):
def test_std_formatter(self): def test_std_formatter(self):
formatter = BaseFormatter() formatter = BaseFormatter()
formatter.info() formatter.info()
def test_trace_formatter(self):
formatter = TraceFormatter()
formatter.info()

52
test/logger/structure.py Normal file
View File

@ -0,0 +1,52 @@
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
import time
import unittest
from lib_not_dr.logger.structure import LogMessage
class LogMessageTest(unittest.TestCase):
def test_create_empty_message(self):
message = LogMessage()
self.assertEqual(message.messages, [])
self.assertEqual(message.level, 20)
self.assertEqual(message.logger_name, 'root')
self.assertEqual(message.logger_tag, None)
def test_format_message_empty(self):
"""
测试空消息的格式化
因为才发现空信息之前的格式化是有问题的(因为没有初始化 messages)
"""
message = LogMessage()
self.assertEqual(message.format_message(), '\n')
def test_format_message_end(self):
"""
测试消息的结尾
"""
message = LogMessage(messages=['test'],
end='\n')
for testing in ('\n', '\r\n', '\r'):
message.end = testing
self.assertEqual(message.format_message(), 'test' + testing)
def test_format_message_split(self):
"""
测试消息的分割
"""
message = LogMessage(messages=['test', 'test2'], end='')
for testing in (' ', ' | ', ' | '):
message.split = testing
self.assertEqual(message.format_message(), 'test' + testing + 'test2')
def test_msec_3(self):
start_time = time.time()
message = LogMessage(log_time=start_time)
msec_3 = int(start_time / 1000000) % 1000
self.assertEqual(message.create_msec_3, msec_3)