|
1 | 1 | import asyncio |
| 2 | +import re |
2 | 3 | import uuid |
3 | 4 | from typing import Any, AsyncGenerator, Generator, Generic, Tuple, TypeVar |
4 | 5 |
|
@@ -36,8 +37,9 @@ def testfunc(a: int = Depends(dep1)) -> int: |
36 | 37 | return a |
37 | 38 |
|
38 | 39 | with DependencyGraph(testfunc).sync_ctx({}) as sctx: |
39 | | - with pytest.raises(RuntimeError): |
40 | | - assert sctx.resolve_kwargs() == {"a": 1} |
| 40 | + with pytest.warns(match=re.compile(".*was never awaited.*")): |
| 41 | + with pytest.raises(RuntimeError): |
| 42 | + assert sctx.resolve_kwargs() == {"a": 1} |
41 | 43 |
|
42 | 44 | async with DependencyGraph(testfunc).async_ctx({}) as actx: |
43 | 45 | assert await actx.resolve_kwargs() == {"a": 1} |
@@ -611,3 +613,94 @@ def target( |
611 | 613 | assert dep_obj.dependency == GenericClass[Tuple[str, int]] |
612 | 614 | assert dep_obj.signature.name == "class_val" |
613 | 615 | assert dep_obj.signature.annotation == GenericClass[Tuple[str, int]] |
| 616 | + |
| 617 | + |
| 618 | +@pytest.mark.anyio |
| 619 | +async def test_replaced_dep_simple() -> None: |
| 620 | + def replaced() -> int: |
| 621 | + return 321 |
| 622 | + |
| 623 | + def dep() -> int: |
| 624 | + return 123 |
| 625 | + |
| 626 | + def target(val: int = Depends(dep)) -> None: |
| 627 | + return None |
| 628 | + |
| 629 | + graph = DependencyGraph(target=target) |
| 630 | + async with graph.async_ctx(replaced_deps={dep: replaced}) as ctx: |
| 631 | + kwargs = await ctx.resolve_kwargs() |
| 632 | + assert kwargs["val"] == 321 |
| 633 | + |
| 634 | + |
| 635 | +@pytest.mark.anyio |
| 636 | +async def test_replaced_dep_generators() -> None: |
| 637 | + call_count = 0 |
| 638 | + |
| 639 | + def replaced() -> Generator[int, None, None]: |
| 640 | + nonlocal call_count |
| 641 | + yield 321 |
| 642 | + call_count += 1 |
| 643 | + |
| 644 | + def dep() -> int: |
| 645 | + return 123 |
| 646 | + |
| 647 | + def target(val: int = Depends(dep)) -> None: |
| 648 | + return None |
| 649 | + |
| 650 | + graph = DependencyGraph(target=target) |
| 651 | + async with graph.async_ctx(replaced_deps={dep: replaced}) as ctx: |
| 652 | + kwargs = await ctx.resolve_kwargs() |
| 653 | + assert kwargs["val"] == 321 |
| 654 | + assert call_count == 1 |
| 655 | + |
| 656 | + |
| 657 | +@pytest.mark.anyio |
| 658 | +async def test_replaced_dep_exception_propogation() -> None: |
| 659 | + exc_count = 0 |
| 660 | + |
| 661 | + def replaced() -> Generator[int, None, None]: |
| 662 | + nonlocal exc_count |
| 663 | + try: |
| 664 | + yield 321 |
| 665 | + except ValueError: |
| 666 | + exc_count += 1 |
| 667 | + |
| 668 | + def dep() -> int: |
| 669 | + return 123 |
| 670 | + |
| 671 | + def target(val: int = Depends(dep)) -> None: |
| 672 | + raise ValueError("lol") |
| 673 | + |
| 674 | + graph = DependencyGraph(target=target) |
| 675 | + with pytest.raises(ValueError): |
| 676 | + async with graph.async_ctx( |
| 677 | + replaced_deps={dep: replaced}, |
| 678 | + exception_propagation=True, |
| 679 | + ) as ctx: |
| 680 | + kwargs = await ctx.resolve_kwargs() |
| 681 | + assert kwargs["val"] == 321 |
| 682 | + target(**kwargs) |
| 683 | + assert exc_count == 1 |
| 684 | + |
| 685 | + |
| 686 | +@pytest.mark.anyio |
| 687 | +async def test_replaced_dep_subdependencies() -> None: |
| 688 | + def subdep() -> int: |
| 689 | + return 321 |
| 690 | + |
| 691 | + def replaced(ret_val: int = Depends(subdep)) -> int: |
| 692 | + return ret_val |
| 693 | + |
| 694 | + def dep() -> int: |
| 695 | + return 123 |
| 696 | + |
| 697 | + def target(val: int = Depends(dep)) -> None: |
| 698 | + """Stub function.""" |
| 699 | + |
| 700 | + graph = DependencyGraph(target=target) |
| 701 | + async with graph.async_ctx( |
| 702 | + replaced_deps={dep: replaced}, |
| 703 | + exception_propagation=True, |
| 704 | + ) as ctx: |
| 705 | + kwargs = await ctx.resolve_kwargs() |
| 706 | + assert kwargs["val"] == 321 |
0 commit comments