Skip to content

Commit 6d55707

Browse files
authored
Merge pull request #79 from community-of-python/fix-coverage
increase test coverage for helpers
2 parents d51ce2c + c42668c commit 6d55707

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

microbootstrap/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,17 @@ def merge_dict_configs(
9595

9696
def is_valid_path(maybe_path: str) -> bool:
9797
return bool(re.fullmatch(VALID_PATH_PATTERN, maybe_path))
98+
99+
100+
def optimize_exclude_paths(
101+
exclude_endpoints: typing.Iterable[str],
102+
) -> typing.Collection[str]:
103+
# `in` operator is faster for tuples than for lists
104+
endpoints_to_ignore: typing.Collection[str] = tuple(exclude_endpoints)
105+
106+
# 10 is just an empirical value, based of measuring the performance
107+
# iterating over a tuple of <10 elements is faster than hashing
108+
if len(endpoints_to_ignore) >= 10: # noqa: PLR2004
109+
endpoints_to_ignore = set(endpoints_to_ignore)
110+
111+
return endpoints_to_ignore

microbootstrap/middlewares/fastapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from fastapi import status
66
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
77

8+
from microbootstrap.helpers import optimize_exclude_paths
89
from microbootstrap.instruments.logging_instrument import fill_log_message
9-
from .utils import optimize_exclude_paths
1010

1111

1212
def build_fastapi_logging_middleware(

microbootstrap/middlewares/litestar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from litestar.middleware.base import MiddlewareProtocol
88
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
99

10+
from microbootstrap.helpers import optimize_exclude_paths
1011
from microbootstrap.instruments.logging_instrument import fill_log_message
11-
from .utils import optimize_exclude_paths
1212

1313

1414
def build_litestar_logging_middleware(

microbootstrap/middlewares/utils.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/test_helpers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from microbootstrap import exceptions, helpers
8+
from microbootstrap.helpers import optimize_exclude_paths
89

910

1011
@pytest.mark.parametrize(
@@ -146,3 +147,14 @@ def test_merge_dataclasses_configs(
146147
result: DataclassConfig,
147148
) -> None:
148149
assert result == helpers.merge_dataclasses_configs(first_class, second_class)
150+
151+
152+
@pytest.mark.parametrize(
153+
"exclude_paths",
154+
[
155+
["path"],
156+
["path"] * 11,
157+
],
158+
)
159+
def test_optimize_exclude_paths(exclude_paths: list[str]) -> None:
160+
optimize_exclude_paths(exclude_paths)

0 commit comments

Comments
 (0)