87 lines
2.0 KiB
Python
87 lines
2.0 KiB
Python
from contextlib import contextmanager, asynccontextmanager
|
|
from dataclasses import dataclass
|
|
from typing import Any, Generator, AsyncGenerator
|
|
|
|
import pytest
|
|
|
|
from src.breakshaft.convertor import ConvRepo
|
|
|
|
pytest_plugins = ('pytest_asyncio',)
|
|
|
|
|
|
@dataclass
|
|
class A:
|
|
a: int
|
|
|
|
|
|
@dataclass
|
|
class B:
|
|
b: float
|
|
|
|
|
|
def test_sync_ctxmanager():
|
|
repo = ConvRepo()
|
|
|
|
@repo.mark_injector()
|
|
def b_to_a(b: B) -> A:
|
|
return A(int(b.b))
|
|
|
|
@repo.mark_injector()
|
|
def a_to_b(a: A) -> B:
|
|
return B(float(a.a))
|
|
|
|
int_to_a_finalized = [False]
|
|
|
|
@repo.mark_injector()
|
|
@contextmanager
|
|
def int_to_a(i: int) -> Generator[A, Any, None]:
|
|
yield A(i)
|
|
int_to_a_finalized[0] = True
|
|
|
|
def consumer(dep: A) -> int:
|
|
return dep.a
|
|
|
|
fn1 = repo.get_conversion((B,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
|
dep = fn1(B(42.1))
|
|
assert dep == 42
|
|
assert not int_to_a_finalized[0]
|
|
|
|
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
|
dep = fn2(123)
|
|
assert dep == 123
|
|
assert int_to_a_finalized[0]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_ctxmanager():
|
|
repo = ConvRepo()
|
|
|
|
@repo.mark_injector()
|
|
def b_to_a(b: B) -> A:
|
|
return A(int(b.b))
|
|
|
|
@repo.mark_injector()
|
|
def a_to_b(a: A) -> B:
|
|
return B(float(a.a))
|
|
|
|
int_to_a_finalized = [False]
|
|
|
|
@repo.mark_injector()
|
|
@asynccontextmanager
|
|
async def int_to_a(i: int) -> AsyncGenerator[A, Any]:
|
|
yield A(i)
|
|
int_to_a_finalized[0] = True
|
|
|
|
def consumer(dep: A) -> int:
|
|
return dep.a
|
|
|
|
fn1 = repo.get_conversion((B,), consumer, force_commutative=True, force_async=False, allow_async=True)
|
|
dep = fn1(B(42.1))
|
|
assert dep == 42
|
|
assert not int_to_a_finalized[0]
|
|
|
|
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=True)
|
|
dep = await fn2(123)
|
|
assert dep == 123
|
|
assert int_to_a_finalized[0]
|