|
| 1 | +import io |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from sed.core.logging import call_logger |
| 9 | +from sed.core.logging import set_verbosity |
| 10 | +from sed.core.logging import setup_logging |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def logger_(): |
| 15 | + logger = setup_logging("test_logger") |
| 16 | + log_capture_string = io.StringIO() |
| 17 | + ch = logging.StreamHandler(log_capture_string) |
| 18 | + ch.setLevel(logging.DEBUG) |
| 19 | + logger.addHandler(ch) |
| 20 | + yield logger, log_capture_string |
| 21 | + |
| 22 | + |
| 23 | +def test_debug_logging(logger_): |
| 24 | + logger, log_capture_string = logger_ |
| 25 | + logger.debug("This is a debug message") |
| 26 | + assert "This is a debug message" in log_capture_string.getvalue() |
| 27 | + |
| 28 | + |
| 29 | +def test_info_logging(logger_): |
| 30 | + logger, log_capture_string = logger_ |
| 31 | + logger.info("This is an info message") |
| 32 | + assert "This is an info message" in log_capture_string.getvalue() |
| 33 | + |
| 34 | + |
| 35 | +def test_warning_logging(logger_): |
| 36 | + logger, log_capture_string = logger_ |
| 37 | + logger.warning("This is a warning message") |
| 38 | + assert "This is a warning message" in log_capture_string.getvalue() |
| 39 | + |
| 40 | + |
| 41 | +def test_error_logging(logger_): |
| 42 | + logger, log_capture_string = logger_ |
| 43 | + logger.error("This is an error message") |
| 44 | + assert "This is an error message" in log_capture_string.getvalue() |
| 45 | + |
| 46 | + |
| 47 | +def test_critical_logging(logger_): |
| 48 | + logger, log_capture_string = logger_ |
| 49 | + logger.critical("This is a critical message") |
| 50 | + assert "This is a critical message" in log_capture_string.getvalue() |
| 51 | + |
| 52 | + |
| 53 | +def test_set_verbosity(logger_): |
| 54 | + logger, log_capture_string = logger_ |
| 55 | + set_verbosity(logger, verbose=True) |
| 56 | + assert logger.handlers[0].level == logging.INFO |
| 57 | + set_verbosity(logger, verbose=False) |
| 58 | + assert logger.handlers[0].level == logging.WARNING |
| 59 | + |
| 60 | + |
| 61 | +def test_logger_has_base_logger(logger_): |
| 62 | + logger, log_capture_string = logger_ |
| 63 | + assert logger.name == "sed.test_logger" |
| 64 | + assert logger.parent.name == "sed" |
| 65 | + assert logger.parent.parent.name == "root" |
| 66 | + assert logger.parent.level == logging.DEBUG |
| 67 | + assert isinstance(logger.parent.handlers[0], logging.FileHandler) |
| 68 | + |
| 69 | + |
| 70 | +def test_logger_creates_logfile(tmp_path): |
| 71 | + logger = setup_logging("test_logger", set_base_handler=True, user_log_path=tmp_path) |
| 72 | + log_file = os.path.join(tmp_path, f"sed_{datetime.now().strftime('%Y-%m-%d')}.log") |
| 73 | + assert os.path.exists(log_file) |
| 74 | + with open(log_file) as f: |
| 75 | + assert f.read() == "" |
| 76 | + logger.debug("This is a debug message") |
| 77 | + with open(log_file) as f: |
| 78 | + assert "This is a debug message" in f.read() |
| 79 | + |
| 80 | + |
| 81 | +def test_readonly_path(tmp_path, caplog): |
| 82 | + os.chmod(tmp_path, 0o444) |
| 83 | + with caplog.at_level(logging.WARNING): |
| 84 | + setup_logging("test_logger", set_base_handler=True, user_log_path=tmp_path) |
| 85 | + assert f"Cannot create logfile in Folder {tmp_path}, disabling logfile." in caplog.messages[0] |
| 86 | + log_file = os.path.join(tmp_path, f"sed_{datetime.now().strftime('%Y-%m-%d')}.log") |
| 87 | + assert not os.path.exists(log_file) |
| 88 | + |
| 89 | + |
| 90 | +def test_call_logger(logger_): |
| 91 | + logger, log_capture_string = logger_ |
| 92 | + |
| 93 | + @call_logger(logger) |
| 94 | + def test_function(test_param=None): # noqa: ARG001 |
| 95 | + return |
| 96 | + |
| 97 | + test_function(test_param=[1, 3, 5]) |
| 98 | + assert "test_function(test_param=[1, 3, 5])" in log_capture_string.getvalue() |
| 99 | + |
| 100 | + test_function([1, 3, 5]) |
| 101 | + assert "test_function([1, 3, 5])" in log_capture_string.getvalue() |
| 102 | + |
| 103 | + test_function() |
| 104 | + assert "test_function()" in log_capture_string.getvalue() |
| 105 | + |
| 106 | + class TestClass: |
| 107 | + @call_logger(logger) |
| 108 | + def test_method(self, test_param=None): # noqa: ARG002 |
| 109 | + return |
| 110 | + |
| 111 | + test_instance = TestClass() |
| 112 | + test_instance.test_method(test_param=[1, 3, 5]) |
| 113 | + assert "test_method(test_param=[1, 3, 5])" in log_capture_string.getvalue() |
0 commit comments