|
| 1 | +import io |
| 2 | +import logging |
| 3 | +import sys |
| 4 | +import unittest |
| 5 | + |
| 6 | +from src.amplitude_experiment.remote.config import RemoteEvaluationConfig |
| 7 | + |
| 8 | + |
| 9 | +class RemoteEvaluationConfigLoggerTestCase(unittest.TestCase): |
| 10 | + """Tests for RemoteEvaluationConfig logger configuration""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + """Clear existing handlers from the Amplitude logger before each test""" |
| 14 | + logger = logging.getLogger("Amplitude") |
| 15 | + logger.handlers.clear() |
| 16 | + logger.setLevel(logging.NOTSET) |
| 17 | + |
| 18 | + def tearDown(self): |
| 19 | + """Clean up handlers after each test""" |
| 20 | + logger = logging.getLogger("Amplitude") |
| 21 | + logger.handlers.clear() |
| 22 | + |
| 23 | + def test_default_logger_has_warning_level(self): |
| 24 | + """Test that default logger has WARNING level and stderr handler when debug=False""" |
| 25 | + config = RemoteEvaluationConfig(debug=False) |
| 26 | + self.assertEqual(config.logger.level, logging.WARNING) |
| 27 | + self.assertEqual(len(config.logger.handlers), 1) |
| 28 | + handler = config.logger.handlers[0] |
| 29 | + self.assertIsInstance(handler, logging.StreamHandler) |
| 30 | + self.assertEqual(handler.stream, sys.stderr) |
| 31 | + |
| 32 | + def test_default_logger_has_debug_level_when_debug_true(self): |
| 33 | + """Test that default logger has DEBUG level when debug=True""" |
| 34 | + config = RemoteEvaluationConfig(debug=True) |
| 35 | + self.assertEqual(config.logger.level, logging.DEBUG) |
| 36 | + |
| 37 | + def test_custom_logger_is_used(self): |
| 38 | + """Test that provided custom logger is used""" |
| 39 | + custom_logger = logging.getLogger("CustomLogger") |
| 40 | + config = RemoteEvaluationConfig(logger=custom_logger) |
| 41 | + self.assertEqual(config.logger, custom_logger) |
| 42 | + self.assertEqual(config.logger.name, "CustomLogger") |
| 43 | + |
| 44 | + def test_custom_logger_level_not_modified_by_debug_flag(self): |
| 45 | + """Test that custom logger level is not modified by debug flag""" |
| 46 | + # Test with debug=True |
| 47 | + custom_logger = logging.getLogger("CustomLoggerDebug") |
| 48 | + custom_logger.setLevel(logging.ERROR) |
| 49 | + config = RemoteEvaluationConfig(debug=True, logger=custom_logger) |
| 50 | + # Logger level should remain unchanged (ERROR), not modified to DEBUG |
| 51 | + self.assertEqual(config.logger.level, logging.ERROR) |
| 52 | + |
| 53 | + # Test with debug=False |
| 54 | + custom_logger2 = logging.getLogger("CustomLoggerWarning") |
| 55 | + custom_logger2.setLevel(logging.INFO) |
| 56 | + config2 = RemoteEvaluationConfig(debug=False, logger=custom_logger2) |
| 57 | + # Logger level should remain unchanged (INFO), not modified to WARNING |
| 58 | + self.assertEqual(config2.logger.level, logging.INFO) |
| 59 | + |
| 60 | + def test_default_logger_only_one_handler_added(self): |
| 61 | + """Test that only one handler is added to default logger""" |
| 62 | + config1 = RemoteEvaluationConfig() |
| 63 | + config2 = RemoteEvaluationConfig() |
| 64 | + # Both should use the same logger instance (singleton) |
| 65 | + logger = logging.getLogger("Amplitude") |
| 66 | + # Should only have one handler even after creating multiple configs |
| 67 | + self.assertEqual(len(logger.handlers), 1) |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + unittest.main() |
0 commit comments