Compare commits
2 Commits
a2cf1bb6e6
...
b04ea2c16a
| Author | SHA1 | Date | |
|---|---|---|---|
| b04ea2c16a | |||
| fe53cf9270 |
70
tests/test_default_args.py
Normal file
70
tests/test_default_args.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from src.breakshaft.convertor import ConvRepo
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class A:
|
||||||
|
a: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class B:
|
||||||
|
b: float
|
||||||
|
|
||||||
|
|
||||||
|
type optC = str
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_consumer_args():
|
||||||
|
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))
|
||||||
|
|
||||||
|
@repo.mark_injector()
|
||||||
|
def int_to_a(i: int) -> A:
|
||||||
|
return A(i)
|
||||||
|
|
||||||
|
def consumer(dep: A, opt_dep: optC = '42') -> tuple[int, str]:
|
||||||
|
return dep.a, opt_dep
|
||||||
|
|
||||||
|
fn1 = repo.get_conversion((B,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
||||||
|
dep = fn1(B(42.1))
|
||||||
|
assert dep == (42, '42')
|
||||||
|
|
||||||
|
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
||||||
|
dep = fn2(123)
|
||||||
|
assert dep == (123, '42')
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_inj_args():
|
||||||
|
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))
|
||||||
|
|
||||||
|
@repo.mark_injector()
|
||||||
|
def int_to_a(i: int, opt_dep: optC = '42') -> A:
|
||||||
|
return A(i + int(opt_dep))
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
||||||
|
dep = fn2(123)
|
||||||
|
assert dep == 123 + 42
|
||||||
Reference in New Issue
Block a user