[CI] Add five unittest (#4958)

* add unittest

* Update test_logger.py
This commit is contained in:
Echo-Nie
2025-11-12 10:43:33 +08:00
committed by GitHub
parent a5103eb198
commit 2aabaecbc2
5 changed files with 363 additions and 120 deletions
+50 -1
View File
@@ -30,7 +30,7 @@ class LoggerTests(unittest.TestCase):
self.env_patchers = [
patch("fastdeploy.envs.FD_LOG_DIR", self.tmp_dir),
patch("fastdeploy.envs.FD_DEBUG", 0),
patch("fastdeploy.envs.FD_LOG_BACKUP_COUNT", "1"),
patch("fastdeploy.envs.FD_LOG_BACKUP_COUNT", 1),
]
for p in self.env_patchers:
p.start()
@@ -77,5 +77,54 @@ class LoggerTests(unittest.TestCase):
self.assertTrue(legacy_logger.propagate)
class LoggerExtraTests(unittest.TestCase):
def setUp(self):
self.logger = FastDeployLogger()
def tearDown(self):
if hasattr(FastDeployLogger, "_instance"):
FastDeployLogger._instance = None
if hasattr(FastDeployLogger, "_initialized"):
FastDeployLogger._initialized = False
def test_singleton_behavior(self):
"""Ensure multiple instances are same"""
a = FastDeployLogger()
b = FastDeployLogger()
self.assertIs(a, b)
def test_initialize_only_once(self):
"""Ensure _initialize won't re-run if already initialized"""
self.logger._initialized = True
with patch("fastdeploy.logger.logger.setup_logging") as mock_setup:
self.logger._initialize()
mock_setup.assert_not_called()
def test_get_logger_unified_path(self):
"""Directly test get_logger unified path"""
with patch("fastdeploy.logger.logger.setup_logging") as mock_setup:
log = self.logger.get_logger("utils")
self.assertTrue(log.name.startswith("fastdeploy."))
mock_setup.assert_called_once()
def test_get_logger_legacy_path(self):
"""Test legacy get_logger path"""
with patch("fastdeploy.logger.logger.FastDeployLogger._get_legacy_logger") as mock_legacy:
self.logger.get_logger("x", "y.log", False, False)
mock_legacy.assert_called_once()
def test_get_legacy_logger_debug_mode(self):
"""Ensure debug level is set when FD_DEBUG=1"""
with patch("fastdeploy.envs.FD_DEBUG", 1):
logger = self.logger._get_legacy_logger("debug_case", "d.log")
self.assertEqual(logger.level, logging.DEBUG)
def test_get_legacy_logger_without_formatter(self):
"""Test legacy logger without formatter"""
logger = self.logger._get_legacy_logger("nofmt", "n.log", without_formater=True)
for h in logger.handlers:
self.assertIsNone(h.formatter)
if __name__ == "__main__":
unittest.main(verbosity=2)