Skip to content

Commit 155d68b

Browse files
committed
Additional test cases for logging_config.py:
- Testing the log directory creation - Testing the logger instantiation and return value - Testing that the expected log files are created - Testing that the logger is able to dynacmically change - Testing the MDAnalysis and command logger configuration
1 parent be99e00 commit 155d68b

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import logging
2+
import os
3+
import tempfile
4+
import unittest
5+
6+
from CodeEntropy.config.logging_config import LoggingConfig
7+
8+
9+
class TestLoggingConfig(unittest.TestCase):
10+
11+
def setUp(self):
12+
# Use a temporary directory for logs
13+
self.temp_dir = tempfile.TemporaryDirectory()
14+
self.log_dir = os.path.join(self.temp_dir.name, "logs")
15+
self.logging_config = LoggingConfig(folder=self.temp_dir.name)
16+
17+
def tearDown(self):
18+
self.temp_dir.cleanup()
19+
20+
def test_log_directory_created(self):
21+
"""Check if the log directory is created upon init"""
22+
self.assertTrue(os.path.exists(self.log_dir))
23+
self.assertTrue(os.path.isdir(self.log_dir))
24+
25+
def test_setup_logging_returns_logger(self):
26+
"""Ensure setup_logging returns a logger instance"""
27+
logger = self.logging_config.setup_logging()
28+
self.assertIsInstance(logger, logging.Logger)
29+
30+
def test_expected_log_files_created(self):
31+
"""Ensure log file paths are configured correctly in the logging config"""
32+
self.logging_config.setup_logging()
33+
34+
# Map actual output files to their corresponding handler keys
35+
expected_handlers = {
36+
"program.out": "stdout",
37+
"program.log": "logfile",
38+
"program.err": "errorfile",
39+
"program.com": "commandfile",
40+
"mdanalysis.log": "mdanalysis_log",
41+
}
42+
43+
for filename, handler_key in expected_handlers.items():
44+
expected_path = os.path.join(self.log_dir, filename)
45+
actual_path = self.logging_config.LOGGING["handlers"][handler_key][
46+
"filename"
47+
]
48+
self.assertEqual(actual_path, expected_path)
49+
50+
def test_update_logging_level(self):
51+
"""Ensure logging levels are updated correctly"""
52+
self.logging_config.setup_logging()
53+
54+
# Update to DEBUG
55+
self.logging_config.update_logging_level(logging.DEBUG)
56+
root_logger = logging.getLogger()
57+
self.assertEqual(root_logger.level, logging.DEBUG)
58+
59+
# Check that at least one handler is DEBUG
60+
handler_levels = [h.level for h in root_logger.handlers]
61+
self.assertIn(logging.DEBUG, handler_levels)
62+
63+
# Update to INFO
64+
self.logging_config.update_logging_level(logging.INFO)
65+
self.assertEqual(root_logger.level, logging.INFO)
66+
67+
def test_mdanalysis_and_command_loggers_exist(self):
68+
"""Ensure specialized loggers are set up"""
69+
self.logging_config.setup_logging()
70+
mda_logger = logging.getLogger("MDAnalysis")
71+
cmd_logger = logging.getLogger("commands")
72+
self.assertEqual(mda_logger.level, logging.DEBUG)
73+
self.assertEqual(cmd_logger.level, logging.INFO)
74+
self.assertFalse(mda_logger.propagate)
75+
self.assertFalse(cmd_logger.propagate)
76+
77+
78+
if __name__ == "__main__":
79+
unittest.main()

0 commit comments

Comments
 (0)