|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from collections.abc import Iterator |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from configdirector import ConfigDirectorClient, create_client |
| 9 | +from configdirector._logger import get_default_logger |
| 10 | + |
| 11 | +SDK_KEY = "test-server-sdk-key" |
| 12 | + |
| 13 | +# Spelled out rather than imported, because the name being exactly this is what is under test. |
| 14 | +LOGGER_NAME = "configdirector" |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(autouse=True) |
| 18 | +def restore_the_package_logger() -> Iterator[None]: |
| 19 | + """Loggers are process-wide, so a test that changes one has to hand it back.""" |
| 20 | + logger = logging.getLogger(LOGGER_NAME) |
| 21 | + level = logger.level |
| 22 | + yield |
| 23 | + logger.setLevel(level) |
| 24 | + |
| 25 | + |
| 26 | +def test_the_default_logger_is_named_for_the_package() -> None: |
| 27 | + assert get_default_logger().name == "configdirector" |
| 28 | + |
| 29 | + |
| 30 | +def test_no_level_is_imposed_when_none_is_given() -> None: |
| 31 | + assert get_default_logger().level == logging.NOTSET |
| 32 | + |
| 33 | + |
| 34 | +def test_the_application_keeps_control_of_the_level() -> None: |
| 35 | + # The configuration the README and the client docstring both tell users to write. |
| 36 | + logging.getLogger("configdirector").setLevel(logging.DEBUG) |
| 37 | + |
| 38 | + assert get_default_logger().isEnabledFor(logging.DEBUG) is True |
| 39 | + |
| 40 | + |
| 41 | +def test_warnings_reach_an_application_that_configured_nothing() -> None: |
| 42 | + # An unconfigured application inherits the root logger's WARNING, which logging.lastResort |
| 43 | + # then carries to stderr. That is what keeps an invalid SDK key from failing silently. |
| 44 | + assert get_default_logger().isEnabledFor(logging.WARNING) is True |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + ("log_level", "expected"), |
| 49 | + [(logging.DEBUG, logging.DEBUG), ("INFO", logging.INFO)], |
| 50 | +) |
| 51 | +def test_applies_an_explicit_log_level(log_level: int | str, expected: int) -> None: |
| 52 | + assert get_default_logger(log_level).level == expected |
| 53 | + |
| 54 | + |
| 55 | +def test_the_client_logs_through_the_package_logger() -> None: |
| 56 | + client = ConfigDirectorClient(SDK_KEY) |
| 57 | + |
| 58 | + assert client._logger is logging.getLogger("configdirector") |
| 59 | + |
| 60 | + |
| 61 | +def test_the_client_applies_an_explicit_log_level() -> None: |
| 62 | + client = ConfigDirectorClient(SDK_KEY, log_level="DEBUG") |
| 63 | + |
| 64 | + assert isinstance(client._logger, logging.Logger) |
| 65 | + assert client._logger.isEnabledFor(logging.DEBUG) is True |
| 66 | + |
| 67 | + |
| 68 | +def test_create_client_applies_an_explicit_log_level() -> None: |
| 69 | + client = create_client(SDK_KEY, log_level="DEBUG") |
| 70 | + |
| 71 | + assert isinstance(client._logger, logging.Logger) |
| 72 | + assert client._logger.isEnabledFor(logging.DEBUG) is True |
0 commit comments