2023-11-18 23:36:29 +08:00
|
|
|
# -------------------------------
|
|
|
|
# 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):
|
2023-11-18 23:42:10 +08:00
|
|
|
"""
|
|
|
|
测试空消息的创建
|
|
|
|
这玩意甚至是 codeiume/github copilot 自动补全的
|
|
|
|
"""
|
2023-11-18 23:36:29 +08:00
|
|
|
message = LogMessage()
|
|
|
|
self.assertEqual(message.messages, [])
|
|
|
|
self.assertEqual(message.level, 20)
|
2023-12-03 16:36:01 +08:00
|
|
|
self.assertEqual(message.logger_name, "root")
|
2023-11-18 23:36:29 +08:00
|
|
|
self.assertEqual(message.logger_tag, None)
|
|
|
|
|
|
|
|
def test_format_message_empty(self):
|
|
|
|
"""
|
|
|
|
测试空消息的格式化
|
|
|
|
因为才发现空信息之前的格式化是有问题的(因为没有初始化 messages)
|
|
|
|
"""
|
|
|
|
message = LogMessage()
|
2023-12-03 16:36:01 +08:00
|
|
|
self.assertEqual(message.format_message(), "\n")
|
2023-11-18 23:36:29 +08:00
|
|
|
|
|
|
|
def test_format_message_end(self):
|
|
|
|
"""
|
|
|
|
测试消息的结尾
|
|
|
|
"""
|
2023-12-03 16:36:01 +08:00
|
|
|
message = LogMessage(messages=["test"], end="\n")
|
|
|
|
for testing in ("\n", "\r\n", "\r"):
|
2023-11-18 23:36:29 +08:00
|
|
|
message.end = testing
|
2023-12-03 16:36:01 +08:00
|
|
|
self.assertEqual(message.format_message(), "test" + testing)
|
2023-11-18 23:36:29 +08:00
|
|
|
|
|
|
|
def test_format_message_split(self):
|
|
|
|
"""
|
|
|
|
测试消息的分割
|
|
|
|
"""
|
2023-12-03 16:36:01 +08:00
|
|
|
message = LogMessage(messages=["test", "test2"], end="")
|
|
|
|
for testing in (" ", " | ", " | "):
|
2023-11-18 23:36:29 +08:00
|
|
|
message.split = testing
|
2023-12-03 16:36:01 +08:00
|
|
|
self.assertEqual(
|
|
|
|
message.format_message(), "test" + testing + "test2"
|
|
|
|
)
|
2023-11-18 23:36:29 +08:00
|
|
|
|
2023-11-18 23:42:10 +08:00
|
|
|
def test_format_message_end_with_split(self):
|
|
|
|
"""
|
|
|
|
测试消息的结尾和分割
|
|
|
|
"""
|
2023-12-03 16:36:01 +08:00
|
|
|
message = LogMessage(messages=["test", "test2"], end="\n")
|
|
|
|
for test_split in (" ", " | ", " | "):
|
|
|
|
for test_end in ("\n", "\r\n", "\r"):
|
2023-11-18 23:42:10 +08:00
|
|
|
message.split = test_split
|
|
|
|
message.end = test_end
|
2023-12-03 16:36:01 +08:00
|
|
|
self.assertEqual(
|
|
|
|
message.format_message(),
|
|
|
|
"test" + test_split + "test2" + test_end,
|
|
|
|
)
|
2023-11-18 23:42:10 +08:00
|
|
|
|
2023-11-18 23:36:29 +08:00
|
|
|
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)
|