Coverage for tests/utils/context/test_main.py: 99%
137 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
1from typing import Any
3import pytest
4from fast_depends import ValidationError
6from faststream import Context, ContextRepo
7from faststream._internal.utils import apply_types
10def test_context_getattr(context: ContextRepo) -> None:
11 a = 1000
12 context.set_global("key", a)
14 assert context.key is a
15 assert context.key2 is None
18@pytest.mark.asyncio()
19async def test_context_apply(context: ContextRepo) -> None:
20 a = 1000
21 context.set_global("key", a)
23 @apply_types(context__=context)
24 async def use(key: Any = Context()) -> Any:
25 return key is a
27 assert await use()
30@pytest.mark.asyncio()
31async def test_context_ignore(context: ContextRepo) -> None:
32 a = 3
33 context.set_global("key", a)
35 @apply_types(context__=context)
36 async def use() -> None:
37 return None
39 assert await use() is None
42@pytest.mark.asyncio()
43async def test_context_apply_multi(context: ContextRepo) -> None:
44 a = 1001
45 context.set_global("key_a", a)
47 b = 1000
48 context.set_global("key_b", b)
50 @apply_types(context__=context)
51 async def use1(key_a: Any = Context()) -> Any:
52 return key_a is a
54 assert await use1()
56 @apply_types(context__=context)
57 async def use2(key_b: Any = Context()) -> Any:
58 return key_b is b
60 assert await use2()
62 @apply_types(context__=context)
63 async def use3(key_a: Any = Context(), key_b: Any = Context()) -> Any:
64 return key_a is a and key_b is b
66 assert await use3()
69@pytest.mark.asyncio()
70async def test_context_overrides(context: ContextRepo) -> None:
71 a = 1001
72 context.set_global("test", a)
74 b = 1000
75 context.set_global("test", b)
77 @apply_types(context__=context)
78 async def use(test: Any = Context()) -> Any:
79 return test is b
81 assert await use()
84@pytest.mark.asyncio()
85async def test_context_nested_apply(context: ContextRepo) -> None:
86 a = 1000
87 context.set_global("key", a)
89 @apply_types(context__=context)
90 def use_nested(key: Any = Context()) -> Any:
91 return key
93 @apply_types(context__=context)
94 async def use(key: Any = Context()) -> Any:
95 return key is use_nested() is a
97 assert await use()
100@pytest.mark.asyncio()
101async def test_reset_global(context: ContextRepo) -> None:
102 a = 1000
103 context.set_global("key", a)
104 context.reset_global("key")
106 @apply_types(context__=context)
107 async def use(key: Any = Context()) -> None: ...
109 with pytest.raises(ValidationError):
110 await use()
113@pytest.mark.asyncio()
114async def test_clear_context(context: ContextRepo) -> None:
115 a = 1000
116 context.set_global("key", a)
117 context.clear()
119 @apply_types(context__=context)
120 async def use(key: Any = Context(default=None)) -> Any:
121 return key is None
123 assert await use()
126def test_scope(context: ContextRepo) -> None:
127 @apply_types(context__=context)
128 def use(key: Any = Context(), key2: Any = Context()) -> None:
129 assert key == 1
130 assert key2 == 1
132 with context.scope("key", 1), context.scope("key2", 1):
133 use()
135 assert context.get("key") is None
136 assert context.get("key2") is None
139def test_scopes(context: ContextRepo) -> None:
140 @apply_types(context__=context)
141 def use(key: Any = Context(), key2: Any = Context()) -> None:
142 assert key == 1
143 assert key2 == 2
145 with context.scopes((("key", 1), ("key2", 2))):
146 use()
148 assert context.get("key") is None
149 assert context.get("key2") is None
152def test_scopes_restores_a_repeated_key(context: ContextRepo) -> None:
153 with context.scopes((("key", "first"), ("key", "second"))):
154 assert context.get("key") == "second"
156 assert context.get("key") is None
159def test_default(context: ContextRepo) -> None:
160 @apply_types(context__=context)
161 def use(
162 key: Any = Context(),
163 key2: Any = Context(),
164 key3: Any = Context(default=1),
165 key4: Any = Context("key.key4", default=1),
166 key5: Any = Context("key5.key6"),
167 ) -> None:
168 assert key == 0
169 assert key2 is True
170 assert key3 == 1
171 assert key4 == 1
172 assert key5 is False
174 with (
175 context.scope("key", 0),
176 context.scope("key2", True),
177 context.scope(
178 "key5",
179 {"key6": False},
180 ),
181 ):
182 use()
185def test_local_default(context: ContextRepo) -> None:
186 key = "some-key"
188 tag = context.set_local(key, "useless")
189 context.reset_local(key, tag)
191 assert context.get_local(key, 1) == 1
194def test_initial(context: ContextRepo) -> None:
195 @apply_types(context__=context)
196 def use(
197 a: Any,
198 key: Any = Context(initial=list),
199 ) -> Any:
200 key.append(a)
201 return key
203 assert use(1) == [1]
204 assert use(2) == [1, 2]
207@pytest.mark.asyncio()
208async def test_context_with_custom_object_implementing_comparison(
209 context: ContextRepo,
210) -> None:
211 class User: # ruff: ignore[eq-without-hash] - a user object that defines only `__eq__`
212 def __init__(self, user_id: int) -> None:
213 self.user_id = user_id
215 def __eq__(self, other: object) -> Any:
216 if not isinstance(other, User):
217 return NotImplemented
218 return self.user_id == other.user_id
220 def __ne__(self, other: object) -> Any:
221 return not self.__eq__(other)
223 user2 = User(user_id=2)
224 user3 = User(user_id=3)
226 @apply_types(context__=context)
227 async def use(
228 key1: Any = Context("user1"),
229 key2: Any = Context("user2", default=user2),
230 key3: Any = Context("user3", default=user3),
231 ) -> Any:
232 return (
233 key1 == User(user_id=1)
234 and key2 == User(user_id=2)
235 and key3 == User(user_id=4)
236 )
238 with (
239 context.scope("user1", User(user_id=1)),
240 context.scope("user3", User(user_id=4)),
241 ):
242 assert await use()