Skip to content

Commit 3e4c184

Browse files
committed
test: Split and extend get_bindings() tests
1 parent 13cf0d9 commit 3e4c184

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

injector_test.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,59 +1589,82 @@ def test_binder_has_implicit_binding_for_implicitly_bound_type():
15891589
assert not injector.binder.has_explicit_binding_for(int)
15901590

15911591

1592-
def test_get_bindings():
1592+
def test_gets_no_bindings_without_injection() -> None:
15931593
def function1(a: int) -> None:
15941594
pass
15951595

15961596
assert get_bindings(function1) == {}
15971597

1598+
1599+
def test_gets_bindings_with_inject_decorator() -> None:
15981600
@inject
15991601
def function2(a: int) -> None:
16001602
pass
16011603

16021604
assert get_bindings(function2) == {'a': int}
16031605

1606+
1607+
def test_gets_multiple_bindings_with_inject_decorator() -> None:
1608+
@inject
1609+
def function2(a: int, b: str) -> None:
1610+
pass
1611+
1612+
assert get_bindings(function2) == {'a': int, 'b': str}
1613+
1614+
1615+
def test_only_gets_injectable_bindings_without_noninjectable_decorator() -> None:
16041616
@inject
16051617
@noninjectable('b')
16061618
def function3(a: int, b: str) -> None:
16071619
pass
16081620

1609-
assert get_bindings(function3) == {'a': int}
1610-
16111621
# Let's verify that the inject/noninjectable ordering doesn't matter
16121622
@noninjectable('b')
16131623
@inject
16141624
def function3b(a: int, b: str) -> None:
16151625
pass
16161626

1617-
assert get_bindings(function3b) == {'a': int}
1627+
assert get_bindings(function3) == {'a': int} == get_bindings(function3b)
16181628

1619-
# The simple case of no @inject but injection requested with Inject[...]
1629+
1630+
def test_gets_bindings_with_inject_annotation() -> None:
16201631
def function4(a: Inject[int], b: str) -> None:
16211632
pass
16221633

16231634
assert get_bindings(function4) == {'a': int}
16241635

1625-
# Using @inject with Inject is redundant but it should not break anything
1636+
1637+
def test_gets_multiple_bindings_with_inject_annotation() -> None:
1638+
def function4(a: Inject[int], b: Inject[str]) -> None:
1639+
pass
1640+
1641+
assert get_bindings(function4) == {'a': int, 'b': str}
1642+
1643+
1644+
def test_gets_bindings_inject_with_redundant_inject_annotation() -> None:
16261645
@inject
16271646
def function5(a: Inject[int], b: str) -> None:
16281647
pass
16291648

16301649
assert get_bindings(function5) == {'a': int, 'b': str}
16311650

1632-
# We need to be able to exclude a parameter from injection with NoInject
1651+
1652+
def test_only_gets_bindings_without_noinject_annotation() -> None:
16331653
@inject
16341654
def function6(a: int, b: NoInject[str]) -> None:
16351655
pass
16361656

16371657
assert get_bindings(function6) == {'a': int}
16381658

1639-
# The presence of NoInject should not trigger anything on its own
1659+
1660+
def test_gets_no_bindings_for_noinject_annotation_only() -> None:
16401661
def function7(a: int, b: NoInject[str]) -> None:
16411662
pass
16421663

16431664
assert get_bindings(function7) == {}
16441665

1666+
1667+
def test_gets_no_bindings_for_multiple_noinject_annotations() -> None:
16451668
# There was a bug where in case of multiple NoInject-decorated parameters only the first one was
16461669
# actually made noninjectable and we tried to inject something we couldn't possibly provide
16471670
# into the second one.
@@ -1651,19 +1674,22 @@ def function8(a: NoInject[int], b: NoInject[int]) -> None:
16511674

16521675
assert get_bindings(function8) == {}
16531676

1654-
# Default arguments to NoInject annotations should behave the same as noninjectable decorator w.r.t 'None'
1677+
1678+
def test_get_bindings_noinject_with_default_should_behave_identically() -> None:
16551679
@inject
16561680
@noninjectable('b')
1657-
def function9(self, a: int, b: Optional[str] = None):
1681+
def function9(self, a: int, b: Optional[str] = None) -> None:
16581682
pass
16591683

16601684
@inject
1661-
def function10(self, a: int, b: NoInject[Optional[str]] = None):
1662-
# b:s type is Union[NoInject[Union[str, None]], None]
1685+
def function10(self, a: int, b: NoInject[Optional[str]] = None) -> None:
1686+
# b's type is Union[NoInject[Union[str, None]], None]
16631687
pass
16641688

16651689
assert get_bindings(function9) == {'a': int} == get_bindings(function10)
16661690

1691+
1692+
def test_get_bindings_with_an_invalid_forward_reference_return_type() -> None:
16671693
# If there's a return type annottion that contains an a forward reference that can't be
16681694
# resolved (for whatever reason) we don't want that to break things for us – return types
16691695
# don't matter for the purpose of dependency injection.

0 commit comments

Comments
 (0)