Coverage for tests/utils/test_body_binding.py: 97%
36 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import json
2from typing import TYPE_CHECKING, Any
3from unittest.mock import MagicMock
5import pytest
6from fast_depends import Depends, Provider
7from pydantic import Field
9from faststream._internal.di import FastDependsConfig
10from faststream._internal.di.config import _unwrap_message_to_fast_depends_decorator
11from faststream.message import StreamMessage, decode_message
13if TYPE_CHECKING:
14 from collections.abc import Callable
17async def decode(message: StreamMessage[Any]) -> Any:
18 return decode_message(message)
21def message(value: Any) -> StreamMessage[Any]:
22 result = StreamMessage(
23 None, json.dumps(value).encode(), content_type="application/json"
24 )
25 result.set_decoder(decode)
26 return result
29@pytest.mark.asyncio()
30class TestBodyShapes:
31 @pytest.mark.parametrize(
32 "payload",
33 (
34 pytest.param(None, id="null"),
35 pytest.param({}, id="empty-mapping"),
36 pytest.param([], id="empty-sequence"),
37 ),
38 )
39 async def test_whole_body_is_preserved(self, payload: Any) -> None:
40 async def handler(value: Any) -> Any:
41 return value
43 built = FastDependsConfig().build_call(handler)
45 assert await built.wrapped_call(message(payload)) == payload
47 async def test_multiple_parameters_preserve_aliases_and_defaults(self) -> None:
48 async def handler(
49 a: int = Field(..., alias="external"), b: int = 7
50 ) -> tuple[int, int]:
51 return a, b
53 built = FastDependsConfig().build_call(handler)
54 assert await built.wrapped_call(message({"external": 1})) == (1, 7)
57@pytest.mark.asyncio()
58class TestDependencyBodyBinding:
59 async def test_provider_override_inputs_define_body(self) -> None:
60 def dependency(value: int) -> int:
61 return value
63 def replacement(a: int, b: int) -> int:
64 return a * 2 + b
66 async def handler(result: int = Depends(dependency)) -> int:
67 return result
69 provider = Provider()
70 provider.override(dependency, replacement)
71 built = FastDependsConfig(provider=provider).build_call(handler)
73 assert await built.wrapped_call(message({"a": 10, "b": 22})) == 42
76@pytest.mark.asyncio()
77class TestBodyUnpacking:
78 @pytest.mark.parametrize(
79 ("signature", "payload", "expected_args", "expected_kwargs"),
80 (
81 pytest.param("positional", 42, (42,), {}, id="positional-only"),
82 pytest.param("star", [1, 2], (1, 2), {}, id="var-positional"),
83 pytest.param("double_star", {"a": 1}, ({"a": 1},), {}, id="var-keyword"),
84 pytest.param("mixed", {"a": 1, "b": 2}, (), {"a": 1, "b": 2}, id="mixed"),
85 ),
86 )
87 async def test_argument_kinds_preserve_unwrapping(
88 self,
89 signature: str,
90 payload: Any,
91 expected_args: tuple[Any, ...],
92 expected_kwargs: dict[str, Any],
93 mock: MagicMock,
94 ) -> None:
95 async def positional(value: int, /) -> None: ...
97 async def star(*items: tuple[int, ...]) -> None: ...
99 async def double_star(**values: Any) -> None: ...
101 async def mixed(a: int, **values: Any) -> None: ...
103 async def receive(*args: Any, **kwargs: Any) -> None:
104 mock(*args, **kwargs)
106 handlers: dict[str, Callable[..., Any]] = {
107 "positional": positional,
108 "star": star,
109 "double_star": double_star,
110 "mixed": mixed,
111 }
112 built = FastDependsConfig().build_call(handlers[signature])
113 wrapper = _unwrap_message_to_fast_depends_decorator(receive, built.dependent)
114 await wrapper(message(payload))
116 mock.assert_called_once_with(*expected_args, **expected_kwargs)
118 async def test_non_container_body_keeps_unpacking_error(self) -> None:
119 async def handler(a: int, b: int) -> None: ...
121 built = FastDependsConfig().build_call(handler)
123 with pytest.raises(ValueError, match="Couldn't unpack `42` to multiple values"):
124 await built.wrapped_call(message(42))