Skip to content

Commit

Permalink
fix: additional test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Dec 15, 2024
1 parent 80f8e7d commit 343c29d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 33 deletions.
13 changes: 10 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ dependencies = [
"eval_type_backport; python_version <= \"3.9\"",
"importlib-metadata; python_version < \"3.10\"",
"importlib-resources>=5.12.0; python_version < \"3.9\"",
"litestar-msgspec; python_version >= \"3.9\"",
"msgspec>=0.18.2,<=0.18.6; python_version < \"3.9\"",
"litestar-msgspec; python_version > \"3.8\"",
"msgspec>=0.18.2,<0.19.0; python_version < \"3.9\"",
"multidict>=6.0.2",
"polyfactory>=2.6.3",
"pyyaml",
Expand Down Expand Up @@ -101,7 +101,7 @@ opentelemetry = ["opentelemetry-instrumentation-asgi"]
piccolo = ["piccolo"]
picologging = ["picologging"]
prometheus = ["prometheus-client"]
pydantic = ["pydantic", "email-validator", "pydantic-extra-types"]
pydantic = ["pydantic", "email-validator", "pydantic-extra-types<=2.9.0; python_version < \"3.9\"", "pydantic-extra-types>=2.9.0; python_version >= \"3.9\""]
redis = ["redis[hiredis]>=4.4.4"]
sqlalchemy = ["advanced-alchemy>=0.2.2"]
standard = ["jinja2", "jsbeautifier", "uvicorn[standard]", "uvloop>=0.18.0; sys_platform != 'win32'", "fast-query-parsers>=1.0.2"]
Expand Down Expand Up @@ -312,6 +312,13 @@ module = [
]
disable_error_code = "arg-type"

[[tool.mypy.overrides]]
warn_unused_ignores = false
module = [
"tests.unit.test_logging.test_logging_config",
]
disable_error_code = "assignment"

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
Expand Down
128 changes: 104 additions & 24 deletions tests/unit/test_logging/test_logging_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from importlib.util import find_spec
from logging.handlers import QueueHandler
from queue import Queue
from types import ModuleType
from typing import TYPE_CHECKING, Any, Dict, Generator, Optional, cast
from typing import TYPE_CHECKING, Any, Dict, Generator, Optional, Union, cast
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -125,25 +124,45 @@ def test_dictconfig_on_startup(logging_module: str, dict_config_not_called: str)
[
[
"logging",
"QueueHandler" if sys.version_info >= (3, 12, 0) else "StandardQueueListenerHandler",
"LoggingQueueListener",
"logging.QueueHandler"
if sys.version_info >= (3, 12, 0)
else "litestar.logging.standard.QueueListenerHandler",
"litestar.logging.standard.LoggingQueueListener",
],
[
"picologging",
"PicologgingQueueListenerHandler",
"litestar.logging.picologging.QueueListenerHandler",
"picologging.handlers.QueueListener", # pyright: ignore[reportGeneralTypeIssues,reportAttributeAccessIssue]
],
],
)
def test_default_queue_listener_handler(
logging_module_str: str,
expected_handler_class_str: str,
expected_handler_class_str: Union[str, Any],
expected_listener_class_str: str,
capsys: "CaptureFixture[str]",
) -> None:
logging_module = importlib.import_module(logging_module_str)
expected_handler_class = importlib.import_module(expected_handler_class_str)
expected_listener_class = importlib.import_module(expected_listener_class_str)
if expected_handler_class_str == "litestar.logging.standard.QueueListenerHandler":
from litestar.logging.standard import QueueListenerHandler

expected_handler_class = QueueListenerHandler
elif expected_handler_class_str == "litestar.logging.picologging.QueueListenerHandler":
from litestar.logging.picologging import QueueListenerHandler

expected_handler_class = QueueListenerHandler
else:
expected_handler_class = importlib.import_module(expected_handler_class_str)
if expected_listener_class_str == "litestar.logging.standard.LoggingQueueListener":
from litestar.logging.standard import LoggingQueueListener

expected_listener_class = LoggingQueueListener
elif expected_listener_class_str == "picologging.handlers.QueueListener":
from picologging.handlers import QueueListener

expected_listener_class = QueueListener
else:
expected_listener_class = importlib.import_module(expected_listener_class_str)

def wait_log_queue(queue: Any, sleep_time: float = 0.1, max_retries: int = 5) -> None:
retry = 0
Expand Down Expand Up @@ -200,13 +219,27 @@ def test_get_logger_without_logging_config() -> None:
@pytest.mark.parametrize(
"logging_module_str, expected_handler_class_str",
[
["logging", "QueueHandler" if sys.version_info >= (3, 12, 0) else "StandardQueueListenerHandler"],
["picologging", "PicologgingQueueListenerHandler"],
[
"logging",
"logging.QueueHandler"
if sys.version_info >= (3, 12, 0)
else "litestar.logging.standard.QueueListenerHandler",
],
["picologging", "litestar.logging.picologging.QueueListenerHandler"],
],
)
def test_default_loggers(logging_module_str: str, expected_handler_class_str: str) -> None:
logging_module = importlib.import_module(logging_module_str)
expected_handler_class = importlib.import_module(expected_handler_class_str)
if expected_handler_class_str == "litestar.logging.standard.QueueListenerHandler":
from litestar.logging.standard import QueueListenerHandler

expected_handler_class = QueueListenerHandler
elif expected_handler_class_str == "litestar.logging.picologging.QueueListenerHandler":
from litestar.logging.picologging import QueueListenerHandler

expected_handler_class = QueueListenerHandler
else:
expected_handler_class = importlib.import_module(expected_handler_class_str)
with create_test_client(logging_config=LoggingConfig(logging_module=logging_module_str)) as client:
root_logger = client.app.get_logger()
assert isinstance(root_logger, logging_module.Logger)
Expand All @@ -225,21 +258,35 @@ def test_default_loggers(logging_module_str: str, expected_handler_class_str: st
@pytest.mark.parametrize(
"logging_module_str, expected_handler_class_str",
[
["logging", "QueueHandler" if sys.version_info >= (3, 12, 0) else "StandardQueueListenerHandler"],
["picologging", "PicologgingQueueListenerHandler"],
[
"logging",
"litestar.logging.standard.QueueListenerHandler"
if sys.version_info >= (3, 12, 0)
else "litestar.logging.standard.QueueListenerHandler",
],
["picologging", "litestar.logging.picologging.QueueListenerHandler"],
],
)
def test_connection_logger(logging_module_str: str, expected_handler_class_str: str) -> None:
logging_module = importlib.import_module(logging_module_str)
expected_handler_class = importlib.import_module(expected_handler_class_str)
if expected_handler_class_str == "litestar.logging.standard.QueueListenerHandler":
from litestar.logging.standard import QueueListenerHandler

expected_handler_class = QueueListenerHandler
elif expected_handler_class_str == "litestar.logging.picologging.QueueListenerHandler":
from litestar.logging.picologging import QueueListenerHandler

expected_handler_class = QueueListenerHandler
else:
expected_handler_class = importlib.import_module(expected_handler_class_str)

@get("/")
def handler(request: Request) -> Dict[str, bool]:
return {"isinstance": isinstance(request.logger.handlers[0], expected_handler_class)} # type: ignore[attr-defined]

with create_test_client(
route_handlers=[handler],
logging_config=LoggingConfig(logging_module=logging_module_str),
logging_config=LoggingConfig(logging_module=logging_module.__name__),
) as client:
response = client.get("/")
assert response.status_code == HTTP_200_OK
Expand Down Expand Up @@ -280,14 +327,29 @@ def test_validation(logging_module_str: Optional[str]) -> None:
@pytest.mark.parametrize(
"logging_module_str, expected_handler_class_str",
[
["logging", "QueueHandler" if sys.version_info >= (3, 12, 0) else "StandardQueueListenerHandler"],
["picologging", "PicologgingQueueListenerHandler"],
[
"logging",
"logging.QueueHandler"
if sys.version_info >= (3, 12, 0)
else "litestar.logging.standard.QueueListenerHandler",
],
["picologging", "litestar.logging.picologging.QueueListenerHandler"],
],
)
def test_root_logger(logging_module_str: str, expected_handler_class_str: str) -> None:
logging_module = importlib.import_module(logging_module_str)
expected_handler_class = importlib.import_module(expected_handler_class_str)
logging_config = LoggingConfig(logging_module=logging_module_str)
if expected_handler_class_str == "litestar.logging.standard.QueueListenerHandler":
from litestar.logging.standard import QueueListenerHandler

expected_handler_class = QueueListenerHandler
elif expected_handler_class_str == "litestar.logging.picologging.QueueListenerHandler":
from litestar.logging.picologging import QueueListenerHandler

expected_handler_class = QueueListenerHandler
else:
expected_handler_class = importlib.import_module(expected_handler_class_str)

logging_config = LoggingConfig(logging_module=logging_module.__name__)
get_logger = logging_config.configure()
root_logger = get_logger()
assert root_logger.name == "root" # type: ignore[attr-defined]
Expand Down Expand Up @@ -318,20 +380,37 @@ def test_root_logger_no_config(logging_module_str: str) -> None:
@pytest.mark.parametrize(
"logging_module_str, configure_root_logger, expected_root_logger_handler_class_str",
[
["logging", True, "QueueHandler" if sys.version_info >= (3, 12, 0) else "StandardQueueListenerHandler"],
[
"logging",
True,
"logging.QueueHandler"
if sys.version_info >= (3, 12, 0)
else "litestar.logging.standard.QueueListenerHandler",
],
["logging", False, None],
["picologging", True, "PicologgingQueueListenerHandler"],
["picologging", True, "litestar.logging.picologging.QueueListenerHandler"],
["picologging", False, None],
],
)
def test_customizing_handler(
logging_module_str: str,
configure_root_logger: bool,
expected_root_logger_handler_class_str: str,
expected_root_logger_handler_class_str: "Optional[str]",
capsys: "CaptureFixture[str]",
) -> None:
logging_module = importlib.import_module(logging_module_str)
expected_root_logger_handler_class = importlib.import_module(expected_root_logger_handler_class_str)
if expected_root_logger_handler_class_str is None:
expected_root_logger_handler_class = None
elif expected_root_logger_handler_class_str == "litestar.logging.standard.QueueListenerHandler":
from litestar.logging.standard import QueueListenerHandler

expected_root_logger_handler_class = QueueListenerHandler
elif expected_root_logger_handler_class_str == "litestar.logging.picologging.QueueListenerHandler":
from litestar.logging.picologging import QueueListenerHandler

expected_root_logger_handler_class = QueueListenerHandler
else:
expected_root_logger_handler_class = importlib.import_module(expected_root_logger_handler_class_str)

log_format = "%(levelname)s :: %(name)s :: %(message)s"

Expand Down Expand Up @@ -383,7 +462,8 @@ def test_customizing_handler(
formatter = root_logger_handler.listener.handlers[0].formatter # type: ignore[attr-defined]
else:
formatter = root_logger_handler.formatter
assert formatter._fmt == log_format
if formatter is not None:
assert formatter._fmt == log_format
else:
# Root logger shouldn't be configured but pytest adds some handlers (for the standard `logging` module)
for handler in root_logger.handlers: # type: ignore[attr-defined]
Expand Down
39 changes: 33 additions & 6 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 343c29d

Please sign in to comment.