Skip to content

Commit

Permalink
Add tests for deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Nov 28, 2022
1 parent bb2f0a1 commit d9db160
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
20 changes: 15 additions & 5 deletions starlette/applications.py
Expand Up @@ -175,9 +175,9 @@ def add_websocket_route(

def exception_handler(
self, exc_class_or_status_code: typing.Union[int, typing.Type[Exception]]
) -> typing.Callable: # pragma: nocover
) -> typing.Callable:
warnings.warn(
"The `exception_handler` decorator is deprecated, and will be removed in version 1.0.0." # noqa: E501
"The `exception_handler` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501
"Refer to https://www.starlette.io/exceptions/ for the recommended approach.", # noqa: E501
DeprecationWarning,
)
Expand All @@ -194,14 +194,19 @@ def route(
methods: typing.Optional[typing.List[str]] = None,
name: typing.Optional[str] = None,
include_in_schema: bool = True,
) -> typing.Callable: # pragma: nocover
) -> typing.Callable:
"""
We no longer document this decorator style API, and its usage is discouraged.
Instead you should use the following approach:
>>> routes = [Route(path, endpoint=...), ...]
>>> app = Starlette(routes=routes)
"""
warnings.warn(
"The `route` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501
"Refer to https://www.starlette.io/routing/ for the recommended approach.", # noqa: E501
DeprecationWarning,
)

def decorator(func: typing.Callable) -> typing.Callable:
self.router.add_route(
Expand All @@ -217,22 +222,27 @@ def decorator(func: typing.Callable) -> typing.Callable:

def websocket_route(
self, path: str, name: typing.Optional[str] = None
) -> typing.Callable: # pragma: nocover
) -> typing.Callable:
"""
We no longer document this decorator style API, and its usage is discouraged.
Instead you should use the following approach:
>>> routes = [WebSocketRoute(path, endpoint=...), ...]
>>> app = Starlette(routes=routes)
"""
warnings.warn(
"The `websocket_route` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501
"Refer to https://www.starlette.io/routing/#websocket-routing for the recommended approach.", # noqa: E501
DeprecationWarning,
)

def decorator(func: typing.Callable) -> typing.Callable:
self.router.add_websocket_route(path, func, name=name)
return func

return decorator

def middleware(self, middleware_type: str) -> typing.Callable: # pragma: nocover
def middleware(self, middleware_type: str) -> typing.Callable:
"""
We no longer document this decorator style API, and its usage is discouraged.
Instead you should use the following approach:
Expand Down
6 changes: 3 additions & 3 deletions starlette/routing.py
Expand Up @@ -778,7 +778,7 @@ def route(
methods: typing.Optional[typing.List[str]] = None,
name: typing.Optional[str] = None,
include_in_schema: bool = True,
) -> typing.Callable: # pragma: nocover
) -> typing.Callable:
"""
We no longer document this decorator style API, and its usage is discouraged.
Instead you should use the following approach:
Expand Down Expand Up @@ -806,7 +806,7 @@ def decorator(func: typing.Callable) -> typing.Callable:

def websocket_route(
self, path: str, name: typing.Optional[str] = None
) -> typing.Callable: # pragma: nocover
) -> typing.Callable:
"""
We no longer document this decorator style API, and its usage is discouraged.
Instead you should use the following approach:
Expand Down Expand Up @@ -836,7 +836,7 @@ def add_event_handler(
else:
self.on_shutdown.append(func)

def on_event(self, event_type: str) -> typing.Callable: # pragma: nocover
def on_event(self, event_type: str) -> typing.Callable:
warnings.warn(
"The `on_event` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501
"Refer to https://www.starlette.io/events/#registering-events for recommended approach.", # noqa: E501
Expand Down
57 changes: 57 additions & 0 deletions tests/test_applications.py
Expand Up @@ -429,3 +429,60 @@ def lifespan(app):
assert not cleanup_complete
assert startup_complete
assert cleanup_complete


def test_decorator_deprecations() -> None:
app = Starlette()

with pytest.deprecated_call(
match=(
"The `exception_handler` decorator is deprecated, "
"and will be removed in version 1.0.0."
)
) as record:
app.exception_handler(500)(http_exception)
assert len(record) == 1

with pytest.deprecated_call(
match=(
"The `middleware` decorator is deprecated, "
"and will be removed in version 1.0.0."
)
) as record:

async def middleware(request, call_next):
... # pragma: no cover

app.middleware("http")(middleware)
assert len(record) == 1

with pytest.deprecated_call(
match=(
"The `route` decorator is deprecated, "
"and will be removed in version 1.0.0."
)
) as record:
app.route("/")(async_homepage)
assert len(record) == 1

with pytest.deprecated_call(
match=(
"The `websocket_route` decorator is deprecated, "
"and will be removed in version 1.0.0."
)
) as record:
app.websocket_route("/ws")(websocket_endpoint)
assert len(record) == 1

with pytest.deprecated_call(
match=(
"The `on_event` decorator is deprecated, "
"and will be removed in version 1.0.0."
)
) as record:

async def startup():
... # pragma: no cover

app.on_event("startup")(startup)
assert len(record) == 1
17 changes: 17 additions & 0 deletions tests/test_routing.py
Expand Up @@ -1020,3 +1020,20 @@ def test_host_named_repr() -> None:
)
# test for substring because repr(Router) returns unique object ID
assert repr(route).startswith("Host(host='example.com', name='app', app=")


def test_decorator_deprecations() -> None:
router = Router()

with pytest.deprecated_call():
router.route("/")(homepage)

with pytest.deprecated_call():
router.websocket_route("/ws")(websocket_endpoint)

with pytest.deprecated_call():

async def startup() -> None:
... # pragma: nocover

router.on_event("startup")(startup)

0 comments on commit d9db160

Please sign in to comment.