|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
7 | | -from taskiq_dependencies import DependencyGraph, Depends |
| 7 | +from taskiq_dependencies import DependencyGraph, Depends, ParamInfo |
8 | 8 |
|
9 | 9 |
|
10 | 10 | @pytest.mark.anyio |
@@ -258,3 +258,55 @@ def __init__(self, dep=Depends()) -> None: # type: ignore |
258 | 258 |
|
259 | 259 | with pytest.raises(ValueError): |
260 | 260 | DependencyGraph(Target) |
| 261 | + |
| 262 | + |
| 263 | +def test_get_param_info() -> None: |
| 264 | + """Tests that param info resolved correctly.""" |
| 265 | + |
| 266 | + def dep(info: ParamInfo = Depends()) -> ParamInfo: |
| 267 | + return info |
| 268 | + |
| 269 | + def target(my_test_param: ParamInfo = Depends(dep)) -> None: |
| 270 | + return None |
| 271 | + |
| 272 | + with DependencyGraph(target=target).sync_ctx() as g: |
| 273 | + kwargs = g.resolve_kwargs() |
| 274 | + |
| 275 | + info: ParamInfo = kwargs["my_test_param"] |
| 276 | + assert info.name == "my_test_param" |
| 277 | + assert info.definition |
| 278 | + assert info.definition.annotation == ParamInfo |
| 279 | + |
| 280 | + |
| 281 | +def test_param_info_no_dependant() -> None: |
| 282 | + """Tests that if ParamInfo is used on the target, no error is raised.""" |
| 283 | + |
| 284 | + def target(info: ParamInfo = Depends()) -> None: |
| 285 | + return None |
| 286 | + |
| 287 | + with DependencyGraph(target=target).sync_ctx() as g: |
| 288 | + kwargs = g.resolve_kwargs() |
| 289 | + |
| 290 | + info: ParamInfo = kwargs["info"] |
| 291 | + assert info.name == "" |
| 292 | + assert info.definition is None |
| 293 | + |
| 294 | + |
| 295 | +def test_class_based_dependencies() -> None: |
| 296 | + """Tests that if ParamInfo is used on the target, no error is raised.""" |
| 297 | + |
| 298 | + class TeClass: |
| 299 | + def __init__(self, return_val: str) -> None: |
| 300 | + self.return_val = return_val |
| 301 | + |
| 302 | + def __call__(self) -> str: |
| 303 | + return self.return_val |
| 304 | + |
| 305 | + def target(class_val: str = Depends(TeClass("tval"))) -> None: |
| 306 | + return None |
| 307 | + |
| 308 | + with DependencyGraph(target=target).sync_ctx() as g: |
| 309 | + kwargs = g.resolve_kwargs() |
| 310 | + |
| 311 | + info: str = kwargs["class_val"] |
| 312 | + assert info == "tval" |
0 commit comments