11import logging
22import typing
33from io import StringIO
4+ from unittest import mock
45
56import fastapi
67import litestar
8+ import pytest
79from fastapi .testclient import TestClient as FastAPITestClient
810from litestar .testing import TestClient as LitestarTestClient
911from opentelemetry import trace
@@ -51,7 +53,9 @@ def test_litestar_logging_bootstrap(minimal_logging_config: LoggingConfig) -> No
5153 assert len (bootsrap_result ["middleware" ]) == 1
5254
5355
54- def test_litestar_logging_bootstrap_working (minimal_logging_config : LoggingConfig ) -> None :
56+ def test_litestar_logging_bootstrap_working (
57+ monkeypatch : pytest .MonkeyPatch , minimal_logging_config : LoggingConfig
58+ ) -> None :
5559 logging_instrument : typing .Final = LitestarLoggingInstrument (minimal_logging_config )
5660
5761 @litestar .get ("/test-handler" )
@@ -63,11 +67,28 @@ async def error_handler() -> str:
6367 route_handlers = [error_handler ],
6468 ** logging_instrument .bootstrap_before (),
6569 )
70+ monkeypatch .setattr ("microbootstrap.middlewares.litestar.fill_log_message" , fill_log_mock := mock .Mock ())
6671
6772 with LitestarTestClient (app = litestar_application ) as test_client :
6873 test_client .get ("/test-handler?test-query=1" )
6974 test_client .get ("/test-handler" )
7075
76+ assert fill_log_mock .call_count == 2 # noqa: PLR2004
77+
78+
79+ def test_litestar_logging_bootstrap_ignores_health (
80+ monkeypatch : pytest .MonkeyPatch , minimal_logging_config : LoggingConfig
81+ ) -> None :
82+ logging_instrument : typing .Final = LitestarLoggingInstrument (minimal_logging_config )
83+ logging_instrument .bootstrap ()
84+ litestar_application : typing .Final = litestar .Litestar (** logging_instrument .bootstrap_before ())
85+ monkeypatch .setattr ("microbootstrap.middlewares.litestar.fill_log_message" , fill_log_mock := mock .Mock ())
86+
87+ with LitestarTestClient (app = litestar_application ) as test_client :
88+ test_client .get ("/health" )
89+
90+ assert fill_log_mock .call_count == 0
91+
7192
7293def test_litestar_logging_bootstrap_tracer_injection (minimal_logging_config : LoggingConfig ) -> None :
7394 trace .set_tracer_provider (TracerProvider ())
@@ -133,18 +154,37 @@ def test_memory_logger_factory_error() -> None:
133154 assert error_message in test_stream .getvalue ()
134155
135156
136- def test_fastapi_logging_bootstrap_working (minimal_logging_config : LoggingConfig ) -> None :
137- logging_instrument : typing . Final = FastApiLoggingInstrument ( minimal_logging_config )
138-
157+ def test_fastapi_logging_bootstrap_working (
158+ monkeypatch : pytest . MonkeyPatch , minimal_logging_config : LoggingConfig
159+ ) -> None :
139160 fastapi_application : typing .Final = fastapi .FastAPI ()
140161
141162 @fastapi_application .get ("/test-handler" )
142163 async def test_handler () -> str :
143164 return "Ok"
144165
166+ logging_instrument : typing .Final = FastApiLoggingInstrument (minimal_logging_config )
145167 logging_instrument .bootstrap ()
146168 logging_instrument .bootstrap_after (fastapi_application )
169+ monkeypatch .setattr ("microbootstrap.middlewares.fastapi.fill_log_message" , fill_log_mock := mock .Mock ())
147170
148171 with FastAPITestClient (app = fastapi_application ) as test_client :
149172 test_client .get ("/test-handler?test-query=1" )
150173 test_client .get ("/test-handler" )
174+
175+ assert fill_log_mock .call_count == 2 # noqa: PLR2004
176+
177+
178+ def test_fastapi_logging_bootstrap_ignores_health (
179+ monkeypatch : pytest .MonkeyPatch , minimal_logging_config : LoggingConfig
180+ ) -> None :
181+ fastapi_application : typing .Final = fastapi .FastAPI ()
182+ logging_instrument : typing .Final = FastApiLoggingInstrument (minimal_logging_config )
183+ logging_instrument .bootstrap ()
184+ logging_instrument .bootstrap_after (fastapi_application )
185+ monkeypatch .setattr ("microbootstrap.middlewares.fastapi.fill_log_message" , fill_log_mock := mock .Mock ())
186+
187+ with FastAPITestClient (app = fastapi_application ) as test_client :
188+ test_client .get ("/health" )
189+
190+ assert fill_log_mock .call_count == 0
0 commit comments