2022-05-28 07:28:58 +00:00
|
|
|
import functools
|
2024-02-04 16:50:59 +00:00
|
|
|
from typing import Any
|
2022-05-28 07:28:58 +00:00
|
|
|
|
2024-09-01 15:12:43 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from starlette._utils import get_route_path, is_async_callable
|
|
|
|
from starlette.types import Scope
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
def test_async_func() -> None:
|
2024-07-27 09:03:46 +00:00
|
|
|
async def async_func() -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
2024-07-27 09:03:46 +00:00
|
|
|
def func() -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
assert is_async_callable(async_func)
|
|
|
|
assert not is_async_callable(func)
|
|
|
|
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
def test_async_partial() -> None:
|
2024-07-27 09:03:46 +00:00
|
|
|
async def async_func(a: Any, b: Any) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
2024-07-27 09:03:46 +00:00
|
|
|
def func(a: Any, b: Any) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
partial = functools.partial(async_func, 1)
|
|
|
|
assert is_async_callable(partial)
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
partial = functools.partial(func, 1) # type: ignore
|
2022-05-28 07:28:58 +00:00
|
|
|
assert not is_async_callable(partial)
|
|
|
|
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
def test_async_method() -> None:
|
2022-05-28 07:28:58 +00:00
|
|
|
class Async:
|
2024-07-27 09:03:46 +00:00
|
|
|
async def method(self) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
class Sync:
|
2024-07-27 09:03:46 +00:00
|
|
|
def method(self) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
assert is_async_callable(Async().method)
|
|
|
|
assert not is_async_callable(Sync().method)
|
|
|
|
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
def test_async_object_call() -> None:
|
2022-05-28 07:28:58 +00:00
|
|
|
class Async:
|
2024-07-27 09:03:46 +00:00
|
|
|
async def __call__(self) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
class Sync:
|
2024-07-27 09:03:46 +00:00
|
|
|
def __call__(self) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
assert is_async_callable(Async())
|
|
|
|
assert not is_async_callable(Sync())
|
|
|
|
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
def test_async_partial_object_call() -> None:
|
2022-05-28 07:28:58 +00:00
|
|
|
class Async:
|
2024-02-04 16:50:59 +00:00
|
|
|
async def __call__(
|
|
|
|
self,
|
|
|
|
a: Any,
|
|
|
|
b: Any,
|
2024-07-27 09:03:46 +00:00
|
|
|
) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
class Sync:
|
2024-02-04 16:50:59 +00:00
|
|
|
def __call__(
|
|
|
|
self,
|
|
|
|
a: Any,
|
|
|
|
b: Any,
|
2024-07-27 09:03:46 +00:00
|
|
|
) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
partial = functools.partial(Async(), 1)
|
|
|
|
assert is_async_callable(partial)
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
partial = functools.partial(Sync(), 1) # type: ignore
|
2022-05-28 07:28:58 +00:00
|
|
|
assert not is_async_callable(partial)
|
|
|
|
|
|
|
|
|
2024-02-04 16:50:59 +00:00
|
|
|
def test_async_nested_partial() -> None:
|
|
|
|
async def async_func(
|
|
|
|
a: Any,
|
|
|
|
b: Any,
|
2024-07-27 09:03:46 +00:00
|
|
|
) -> None: ... # pragma: no cover
|
2022-05-28 07:28:58 +00:00
|
|
|
|
|
|
|
partial = functools.partial(async_func, b=2)
|
|
|
|
nested_partial = functools.partial(partial, a=1)
|
|
|
|
assert is_async_callable(nested_partial)
|
2024-09-01 15:12:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"scope, expected_result",
|
|
|
|
[
|
|
|
|
({"path": "/foo-123/bar", "root_path": "/foo"}, "/foo-123/bar"),
|
|
|
|
({"path": "/foo/bar", "root_path": "/foo"}, "/bar"),
|
|
|
|
({"path": "/foo", "root_path": "/foo"}, ""),
|
2024-09-23 18:23:34 +00:00
|
|
|
({"path": "/foo/bar", "root_path": "/bar"}, "/foo/bar"),
|
2024-09-01 15:12:43 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_route_path(scope: Scope, expected_result: str) -> None:
|
|
|
|
assert get_route_path(scope) == expected_result
|