Skip to content

Commit

Permalink
Add test toi include errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil committed Jul 21, 2023
1 parent 15c8212 commit 90128b7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion esmerald/middleware/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Request = TypeVar("Request", _Request, StarletteRequest)


class ServerErrorMiddleware(StarletteServerErrorMiddleware):
class ServerErrorMiddleware(StarletteServerErrorMiddleware): # pragma: no cover
"""
Handles returning 500 responses when a server error occurs.
Expand Down
33 changes: 15 additions & 18 deletions esmerald/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
from esmerald.utils.url import clean_path
from esmerald.websockets import WebSocket, WebSocketClose

if TYPE_CHECKING:
if TYPE_CHECKING: # pragma: no cover
from openapi_schemas_pydantic.v3_1_0.security_scheme import SecurityScheme

from esmerald.applications import Esmerald
Expand Down Expand Up @@ -102,7 +102,7 @@ def create_signature_models(self, route: "RouteParent") -> None:

if isinstance(route, Gateway):
if not route.handler.parent:
route.handler.parent = route
route.handler.parent = route # pragma: no cover

if not is_class_and_subclass(route.handler, APIView) and not isinstance(
route.handler, APIView
Expand Down Expand Up @@ -134,7 +134,7 @@ def validate_root_route_parent(
if not value.handler.parent:
value.handler.parent = value
else:
if not value.handler.parent:
if not value.handler.parent: # pragma: no cover
value(parent=self) # type: ignore

handler: APIView = cast("APIView", value.handler)
Expand Down Expand Up @@ -233,7 +233,9 @@ def __init__(
Router,
),
):
raise ImproperlyConfigured(f"The route {route} must be of type Gateway or Include")
raise ImproperlyConfigured(
f"The route {route} must be of type Gateway or Include"
) # pragma: no cover

assert lifespan is None or (
on_startup is None and on_shutdown is None
Expand Down Expand Up @@ -292,7 +294,7 @@ def add_apiview(self, value: Union["Gateway", "WebSocketGateway"]) -> None:
Generates the signature model for it and sorts the routing list.
"""
routes = []
if not value.handler.parent:
if not value.handler.parent: # pragma: no cover
value.handler(parent=self) # type: ignore

route_handlers: List[Union[HTTPHandler, WebSocketHandler]] = value.handler.get_route_handlers() # type: ignore
Expand Down Expand Up @@ -383,7 +385,9 @@ def add_websocket_route(
self.create_signature_models(websocket_gateway)
self.routes.append(websocket_gateway)

async def not_found(self, scope: "Scope", receive: "Receive", send: "Send") -> None:
async def not_found(
self, scope: "Scope", receive: "Receive", send: "Send"
) -> None: # pragma: no cover
"""Esmerald version of a not found handler when a resource is
called and cannot be dealt with properly.
Expand Down Expand Up @@ -530,7 +534,7 @@ def __init__(

self.methods: Set[str] = {HttpMethod[method].value for method in methods}

if isinstance(status_code, IntEnum):
if isinstance(status_code, IntEnum): # pragma: no cover
status_code = int(status_code)
self.status_code = status_code

Expand Down Expand Up @@ -600,13 +604,6 @@ def allow_header(self) -> Mapping[str, str]:
"""
return {"allow": str(self.methods)}

@property
def permission_names(self) -> Sequence[str]:
"""
List of permissions for the route. This is used for OpenAPI Spec purposes only.
"""
return [permission.__name__ for permission in self.permissions]

def get_response_class(self) -> Type["Response"]:
"""
Returns the closest custom Response class in the parent graph or the
Expand Down Expand Up @@ -697,14 +694,14 @@ async def index(request: Request) -> Response:
"Cannot call check_handler_function without first setting self.fn"
)

if not settings.enable_sync_handlers:
if not settings.enable_sync_handlers: # pragma: no cover
fn = cast("AnyCallable", self.fn)
if not is_async_callable(fn):
raise ImproperlyConfigured(
"Functions decorated with 'route, websocket, get, patch, put, post and delete' must be async functions"
)

def validate_annotations(self) -> None:
def validate_annotations(self) -> None: # pragma: no cover
"""
Validate annotations of the handlers.
"""
Expand Down Expand Up @@ -737,7 +734,7 @@ def validate_annotations(self) -> None:
]:
self.media_type = MediaType.TEXT

def validate_reserved_kwargs(self) -> None:
def validate_reserved_kwargs(self) -> None: # pragma: no cover
"""
Validates if special words are in the signature.
"""
Expand Down Expand Up @@ -822,7 +819,7 @@ def validate_reserved_words(self, signature: "Signature") -> None:
f"The '{kwarg}'is not supported with websocket handlers."
)

def validate_websocket_handler_function(self) -> None:
def validate_websocket_handler_function(self) -> None: # pragma: no cover
"""
Validates the route handler function once it is set by inspecting its
return annotations.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ context = '${CONTEXT}'
source = ["tests", "esmerald"]
omit = [
"esmerald/conf/__init__.py",
"esmerald/middleware/errors.py",
"esmerald/core/directives/*",
"esmerald/core/terminal/*",
"esmerald/datastructures/types.py",
Expand Down
42 changes: 42 additions & 0 deletions tests/routing/test_include_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from esmerald import Gateway, ImproperlyConfigured, Include, get


@get()
async def home() -> None:
""""""


gateway = Gateway(handler=home)


route_patterns = [Gateway(handler=home)]


def test_raise_error_namespace_and_routes():
with pytest.raises(ImproperlyConfigured):
Include(namespace="test", routes=[gateway])


@pytest.mark.parametrize("arg", [gateway, 2, get])
def test_raise_error_namespace(arg):
with pytest.raises(ImproperlyConfigured):
Include(namespace=arg)


@pytest.mark.parametrize("arg", [gateway, 2, get])
def test_raise_error_pattern(arg):
with pytest.raises(ImproperlyConfigured):
Include(pattern=arg)


def test_raise_error_pattern_and_routes():
with pytest.raises(ImproperlyConfigured):
Include(pattern="test", routes=[gateway])


def test_namespace_include_routes():
include = Include(namespace="tests.routing.test_include_errors")

assert len(include.routes) == 1

0 comments on commit 90128b7

Please sign in to comment.