Debian Codegraph @

tests/test_dependency_models.py

raw
1 from collections.abc import AsyncGenerator, Callable, Generator
2 from typing import Any
3
4 from fastapi.dependencies.models import (
5 Dependant,
6 _get_cache_key,
7 _get_computed_scope,
8 _get_oauth_scopes,
9 _get_security_scheme,
10 _is_async_gen_callable,
11 _is_async_gen_callable_cached,
12 _is_coroutine_callable,
13 _is_coroutine_callable_cached,
14 _is_gen_callable,
15 _is_gen_callable_cached,
16 _is_security_scheme,
17 _uses_scopes,
18 )
19 from fastapi.security import APIKeyHeader
20
21
22 def sync_dependency() -> None:
23 pass # pragma: no cover
24
25
26 async def async_dependency() -> None:
27 pass # pragma: no cover
28
29
30 def generator_dependency() -> Generator[None, None, None]:
31 yield # pragma: no cover
32
33
34 async def async_generator_dependency() -> AsyncGenerator[None, None]:
35 yield # pragma: no cover
36
37
38 class UnhashableCallable:
39 __hash__ = None
40
41 async def __call__(self) -> None:
42 pass # pragma: no cover
43
44
45 class UnhashableGeneratorCallable:
46 __hash__ = None
47
48 def __call__(self) -> Generator[None, None, None]:
49 yield # pragma: no cover
50
51
52 class UnhashableAsyncGeneratorCallable:
53 __hash__ = None
54
55 async def __call__(self) -> AsyncGenerator[None, None]:
56 yield # pragma: no cover
57
58
59 class EqualCallable:
60 def __eq__(self, other: object) -> bool:
61 return isinstance(other, EqualCallable)
62
63 def __hash__(self) -> int:
64 return 1 # pragma: no cover
65
66
67 class EqualAsyncCallable(EqualCallable):
68 async def __call__(self) -> None:
69 pass # pragma: no cover
70
71
72 class EqualSyncCallable(EqualCallable):
73 def __call__(self) -> None:
74 pass # pragma: no cover
75
76
77 def test_callable_classification_is_shared_by_call() -> None:
78 _is_gen_callable_cached.cache_clear()
79 _is_async_gen_callable_cached.cache_clear()
80 _is_coroutine_callable_cached.cache_clear()
81
82 for _ in range(2):
83 assert not _is_gen_callable(async_dependency)
84 assert not _is_async_gen_callable(async_dependency)
85 assert _is_coroutine_callable(async_dependency)
86
87 for cached_function in (
88 _is_gen_callable_cached,
89 _is_async_gen_callable_cached,
90 _is_coroutine_callable_cached,
91 ):
92 cache_info = cached_function.cache_info()
93 assert cache_info.hits == 1
94 assert cache_info.misses == 1
95 assert cache_info.maxsize == 4096
96
97
98 def test_callable_classification_cache_supports_large_apps() -> None:
99 callables: list[Callable[[], None]] = [lambda: None for _ in range(3000)]
100
101 for classifier, cached_classifier in (
102 (_is_gen_callable, _is_gen_callable_cached),
103 (_is_async_gen_callable, _is_async_gen_callable_cached),
104 (_is_coroutine_callable, _is_coroutine_callable_cached),
105 ):
106 cached_classifier.cache_clear()
107
108 for _ in range(2):
109 assert all(not classifier(call) for call in callables)
110
111 cache_info = cached_classifier.cache_info()
112 assert cache_info.hits == len(callables)
113 assert cache_info.misses == len(callables)
114 assert cache_info.maxsize == 4096
115 cached_classifier.cache_clear()
116
117
118 def test_unhashable_callable_classification() -> None:
119 assert _is_coroutine_callable(UnhashableCallable())
120 assert _is_gen_callable(UnhashableGeneratorCallable())
121 assert _is_async_gen_callable(UnhashableAsyncGeneratorCallable())
122
123
124 def test_equal_callable_instances_are_cached_by_identity() -> None:
125 async_callable = EqualAsyncCallable()
126 sync_callable = EqualSyncCallable()
127
128 assert async_callable == sync_callable
129 assert _is_coroutine_callable(async_callable)
130 assert not _is_coroutine_callable(sync_callable)
131
132
133 def test_callable_classification() -> None:
134 assert not _is_gen_callable(sync_dependency)
135 assert not _is_async_gen_callable(sync_dependency)
136 assert not _is_coroutine_callable(sync_dependency)
137 assert _is_gen_callable(generator_dependency)
138 assert _is_async_gen_callable(async_generator_dependency)
139
140
141 def test_derived_values_are_not_stored_on_dependant() -> None:
142 dependant = Dependant(call=async_dependency)
143 uses_scopes_cache = {}
144
145 assert _get_oauth_scopes(dependant=dependant) == []
146 assert not _uses_scopes(dependant=dependant, cache=uses_scopes_cache)
147 assert not _uses_scopes(dependant=dependant, cache=uses_scopes_cache)
148 assert _get_computed_scope(dependant=dependant) is None
149 assert _get_cache_key(dependant=dependant) == (async_dependency, (), "")
150
151 assert not hasattr(dependant, "__dict__")
152
153
154 def test_security_scheme_helpers() -> None:
155 security_scheme = APIKeyHeader(name="key")
156 security_dependant = Dependant(call=security_scheme)
157 dependant = Dependant(dependencies=[security_dependant])
158
159 assert _is_security_scheme(dependant=security_dependant)
160 assert _get_security_scheme(dependant=security_dependant) is security_scheme
161 assert _uses_scopes(dependant=dependant)
162
163
164 def test_derived_values_follow_dependency_state() -> None:
165 child = Dependant(call=sync_dependency)
166 dependant = Dependant(
167 call=sync_dependency,
168 dependencies=[child],
169 own_oauth_scopes=[],
170 parent_oauth_scopes=["parent"],
171 )
172
173 assert _get_cache_key(dependant=dependant) == (sync_dependency, (), "")
174
175 child.security_scopes_param_name = "scopes"
176 dependant.own_oauth_scopes = ["own", "parent"]
177
178 assert _uses_scopes(dependant=dependant)
179 assert _get_oauth_scopes(dependant=dependant) == ["parent", "own"]
180 assert _get_cache_key(dependant=dependant) == (
181 sync_dependency,
182 ("own", "parent"),
183 "",
184 )
185
186
187 def test_explicit_and_generator_scopes() -> None:
188 assert (
189 _get_computed_scope(dependant=Dependant(call=sync_dependency, scope="function"))
190 == "function"
191 )
192 assert (
193 _get_computed_scope(dependant=Dependant(call=generator_dependency)) == "request"
194 )
195
196
197 def test_callable_return_annotations_are_not_used() -> None:
198 class CallableWithUnhashableReturn:
199 def __call__(self) -> Any:
200 return None # pragma: no cover
201
202 __hash__ = None
203
204 assert not _is_coroutine_callable(CallableWithUnhashableReturn())