Skip to content

Commit

Permalink
fix: do not break user application on error in library; logging; upda…
Browse files Browse the repository at this point in the history
…te deps
  • Loading branch information
dantetemplar committed Feb 28, 2024
1 parent 531c0bc commit 075052e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 113 deletions.
12 changes: 7 additions & 5 deletions fastapi_mock/example_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ExampleProvider:
_providers: dict[type, PROVIDER_TYPE]

def __init__(
self,
providers: dict[type, PROVIDER_TYPE] = None,
self,
providers: dict[type, PROVIDER_TYPE] = None,
):
self._providers = dict()

Expand Down Expand Up @@ -105,8 +105,8 @@ def _call_provider(cls, provider: PROVIDER_TYPE, annotation: type) -> Any:
return provider

def _resolve_for_registered_type(
self,
annotation: type,
self,
annotation: type,
) -> Any:
for type_, provider in self._providers.items():
if issubclass(annotation, type_):
Expand All @@ -118,6 +118,8 @@ def _resolve_for_registered_type(
def _resolve_for_pydantic_model(self, model: type[BaseModel]) -> dict[str, Any]:
# Check for example in model_config
json_schema_extra = model.model_config.get("json_schema_extra")
if callable(json_schema_extra):
json_schema_extra = json_schema_extra()
if json_schema_extra:
example: dict | None = json_schema_extra.get("example")
examples: list[dict] | None = json_schema_extra.get("examples")
Expand All @@ -138,7 +140,7 @@ def _resolve_for_field_info(self, field_info: FieldInfo):
if field_info.examples:
return choice(field_info.examples)
elif (
default := field_info.get_default(call_default_factory=True)
default := field_info.get_default(call_default_factory=True)
) is not PydanticUndefined:
if default is not None:
return default
Expand Down
3 changes: 3 additions & 0 deletions fastapi_mock/logging_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging

logger = logging.getLogger("fastapi-mock")
26 changes: 15 additions & 11 deletions fastapi_mock/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)

from fastapi_mock.example_provider import ExampleProvider
from fastapi_mock.logging_ import logger


class MockUtilities:
Expand All @@ -30,10 +31,10 @@ class MockUtilities:
return_example_instead_of_501: bool

def __init__(
self,
fastapi_app: FastAPI,
example_provider: ExampleProvider = None,
return_example_instead_of_500: bool = False,
self,
fastapi_app: FastAPI,
example_provider: ExampleProvider = None,
return_example_instead_of_500: bool = False,
) -> None:
if example_provider is None:
example_provider = ExampleProvider()
Expand All @@ -50,14 +51,17 @@ def configure_app(self):
)

async def dispatch(self, request: Request, exc: Exception):
response = self.generate_mock_response(request)
if response is not None:
return response
try:
response = self.generate_mock_response(request)
if response is not None:
return response
except Exception as e:
logger.error(f"Error while mocking response: {e}")
raise exc

def generate_mock_response(
self,
request: Request,
self,
request: Request,
) -> JSONResponse:
# PRIORITIES:
resolved_by_example_from_route: tuple[bool, Any] = (False, None) # 1
Expand Down Expand Up @@ -87,8 +91,8 @@ def generate_mock_response(
status_code = route.status_code

for is_resolved, content in (
resolved_by_example_from_route,
resolved_by_response_model_from_route,
resolved_by_example_from_route,
resolved_by_response_model_from_route,
):
if is_resolved:
return JSONResponse(
Expand Down
Loading

0 comments on commit 075052e

Please sign in to comment.