Skip to content

increase test coverage for helpers #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions microbootstrap/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@ def merge_dict_configs(

def is_valid_path(maybe_path: str) -> bool:
return bool(re.fullmatch(VALID_PATH_PATTERN, maybe_path))


def optimize_exclude_paths(
exclude_endpoints: typing.Iterable[str],
) -> typing.Collection[str]:
# `in` operator is faster for tuples than for lists
endpoints_to_ignore: typing.Collection[str] = tuple(exclude_endpoints)

# 10 is just an empirical value, based of measuring the performance
# iterating over a tuple of <10 elements is faster than hashing
if len(endpoints_to_ignore) >= 10: # noqa: PLR2004
endpoints_to_ignore = set(endpoints_to_ignore)

return endpoints_to_ignore
2 changes: 1 addition & 1 deletion microbootstrap/middlewares/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from fastapi import status
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint

from microbootstrap.helpers import optimize_exclude_paths
from microbootstrap.instruments.logging_instrument import fill_log_message
from .utils import optimize_exclude_paths


def build_fastapi_logging_middleware(
Expand Down
2 changes: 1 addition & 1 deletion microbootstrap/middlewares/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from litestar.middleware.base import MiddlewareProtocol
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR

from microbootstrap.helpers import optimize_exclude_paths
from microbootstrap.instruments.logging_instrument import fill_log_message
from .utils import optimize_exclude_paths


def build_litestar_logging_middleware(
Expand Down
15 changes: 0 additions & 15 deletions microbootstrap/middlewares/utils.py

This file was deleted.

12 changes: 12 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest

from microbootstrap import exceptions, helpers
from microbootstrap.helpers import optimize_exclude_paths


@pytest.mark.parametrize(
Expand Down Expand Up @@ -146,3 +147,14 @@ def test_merge_dataclasses_configs(
result: DataclassConfig,
) -> None:
assert result == helpers.merge_dataclasses_configs(first_class, second_class)


@pytest.mark.parametrize(
"exclude_paths",
[
["path"],
["path"] * 11,
],
)
def test_optimize_exclude_paths(exclude_paths: list[str]) -> None:
optimize_exclude_paths(exclude_paths)