|
| 1 | +import sys |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +if sys.version_info < (3, 10): |
| 6 | + pytest.skip("Annotated is available only for python 3.10+", allow_module_level=True) |
| 7 | + |
| 8 | +from typing import Annotated, AsyncGenerator, Generic, Tuple, TypeVar |
| 9 | + |
| 10 | +from taskiq_dependencies import DependencyGraph, Depends |
| 11 | + |
| 12 | + |
| 13 | +def test_annotated_func() -> None: |
| 14 | + def get_int() -> int: |
| 15 | + return 1 |
| 16 | + |
| 17 | + def target_func(dep: Annotated[int, Depends(get_int)]) -> int: |
| 18 | + return dep |
| 19 | + |
| 20 | + with DependencyGraph(target_func).sync_ctx() as ctx: |
| 21 | + res = target_func(**ctx.resolve_kwargs()) |
| 22 | + assert res == 1 |
| 23 | + |
| 24 | + |
| 25 | +def test_annotated_class() -> None: |
| 26 | + class TestClass: |
| 27 | + pass |
| 28 | + |
| 29 | + def target_func(dep: Annotated[TestClass, Depends()]) -> TestClass: |
| 30 | + return dep |
| 31 | + |
| 32 | + with DependencyGraph(target_func).sync_ctx() as ctx: |
| 33 | + res = target_func(**ctx.resolve_kwargs()) |
| 34 | + assert isinstance(res, TestClass) |
| 35 | + |
| 36 | + |
| 37 | +def test_annotated_generic() -> None: |
| 38 | + _T = TypeVar("_T") |
| 39 | + |
| 40 | + class MyClass: |
| 41 | + pass |
| 42 | + |
| 43 | + class MainClass(Generic[_T]): |
| 44 | + def __init__(self, val: _T = Depends()) -> None: |
| 45 | + self.val = val |
| 46 | + |
| 47 | + def test_func(a: Annotated[MainClass[MyClass], Depends()]) -> MyClass: |
| 48 | + return a.val |
| 49 | + |
| 50 | + with DependencyGraph(target=test_func).sync_ctx(exception_propagation=False) as g: |
| 51 | + value = test_func(**(g.resolve_kwargs())) |
| 52 | + |
| 53 | + assert isinstance(value, MyClass) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.anyio |
| 57 | +async def test_annotated_asyncgen() -> None: |
| 58 | + opened = False |
| 59 | + closed = False |
| 60 | + |
| 61 | + async def my_gen() -> AsyncGenerator[int, None]: |
| 62 | + nonlocal opened, closed |
| 63 | + opened = True |
| 64 | + |
| 65 | + yield 1 |
| 66 | + |
| 67 | + closed = True |
| 68 | + |
| 69 | + def test_func(dep: Annotated[int, Depends(my_gen)]) -> int: |
| 70 | + return dep |
| 71 | + |
| 72 | + async with DependencyGraph(target=test_func).async_ctx() as g: |
| 73 | + value = test_func(**(await g.resolve_kwargs())) |
| 74 | + assert value == 1 |
| 75 | + |
| 76 | + assert opened and closed |
| 77 | + |
| 78 | + |
| 79 | +def test_multiple() -> None: |
| 80 | + class TestClass: |
| 81 | + pass |
| 82 | + |
| 83 | + MyType = Annotated[TestClass, Depends(use_cache=False)] |
| 84 | + |
| 85 | + def test_func(dep: MyType, dep2: MyType) -> Tuple[MyType, MyType]: |
| 86 | + return dep, dep2 |
| 87 | + |
| 88 | + with DependencyGraph(target=test_func).sync_ctx(exception_propagation=False) as g: |
| 89 | + value = test_func(**(g.resolve_kwargs())) |
| 90 | + assert value[0] != value[1] |
| 91 | + assert isinstance(value[0], TestClass) |
| 92 | + assert isinstance(value[1], TestClass) |
| 93 | + |
| 94 | + |
| 95 | +def test_multiple_with_cache() -> None: |
| 96 | + class TestClass: |
| 97 | + pass |
| 98 | + |
| 99 | + MyType = Annotated[TestClass, Depends()] |
| 100 | + |
| 101 | + def test_func(dep: MyType, dep2: MyType) -> Tuple[MyType, MyType]: |
| 102 | + return dep, dep2 |
| 103 | + |
| 104 | + with DependencyGraph(target=test_func).sync_ctx(exception_propagation=False) as g: |
| 105 | + value = test_func(**(g.resolve_kwargs())) |
| 106 | + assert id(value[0]) == id(value[1]) |
| 107 | + assert isinstance(value[0], TestClass) |
| 108 | + |
| 109 | + |
| 110 | +def test_override() -> None: |
| 111 | + class TestClass: |
| 112 | + pass |
| 113 | + |
| 114 | + MyType = Annotated[TestClass, Depends()] |
| 115 | + |
| 116 | + def test_func( |
| 117 | + dep: MyType, |
| 118 | + dep2: Annotated[MyType, Depends(use_cache=False)], |
| 119 | + ) -> Tuple[MyType, MyType]: |
| 120 | + return dep, dep2 |
| 121 | + |
| 122 | + with DependencyGraph(target=test_func).sync_ctx(exception_propagation=False) as g: |
| 123 | + value = test_func(**(g.resolve_kwargs())) |
| 124 | + assert id(value[0]) != id(value[1]) |
| 125 | + assert isinstance(value[0], TestClass) |
| 126 | + assert isinstance(value[1], TestClass) |
0 commit comments