Skip to content

Commit 47e9f97

Browse files
authored
Split and extend get_bindings() tests (#278)
* test: Split and extend `get_bindings()` tests * refactor: Rename functions
1 parent 13cf0d9 commit 47e9f97

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

injector_test.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,89 +1589,115 @@ 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():
1593-
def function1(a: int) -> None:
1592+
def test_gets_no_bindings_without_injection() -> None:
1593+
def function(a: int) -> None:
15941594
pass
15951595

1596-
assert get_bindings(function1) == {}
1596+
assert get_bindings(function) == {}
15971597

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

1602-
assert get_bindings(function2) == {'a': int}
1604+
assert get_bindings(function) == {'a': int}
1605+
16031606

1607+
def test_gets_multiple_bindings_with_inject_decorator() -> None:
16041608
@inject
1605-
@noninjectable('b')
1606-
def function3(a: int, b: str) -> None:
1609+
def function(a: int, b: str) -> None:
16071610
pass
16081611

1609-
assert get_bindings(function3) == {'a': int}
1612+
assert get_bindings(function) == {'a': int, 'b': str}
1613+
1614+
1615+
def test_only_gets_injectable_bindings_without_noninjectable_decorator() -> None:
1616+
@inject
1617+
@noninjectable('b')
1618+
def function1(a: int, b: str) -> None:
1619+
pass
16101620

16111621
# Let's verify that the inject/noninjectable ordering doesn't matter
16121622
@noninjectable('b')
16131623
@inject
1614-
def function3b(a: int, b: str) -> None:
1624+
def function2(a: int, b: str) -> None:
16151625
pass
16161626

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

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

1623-
assert get_bindings(function4) == {'a': int}
1634+
assert get_bindings(function) == {'a': int}
1635+
16241636

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

1630-
assert get_bindings(function5) == {'a': int, 'b': str}
1649+
assert get_bindings(function) == {'a': int, 'b': str}
1650+
16311651

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

1637-
assert get_bindings(function6) == {'a': int}
1657+
assert get_bindings(function) == {'a': int}
16381658

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

1643-
assert get_bindings(function7) == {}
1664+
assert get_bindings(function) == {}
1665+
16441666

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.
16481671
@inject
1649-
def function8(a: NoInject[int], b: NoInject[int]) -> None:
1672+
def function(a: NoInject[int], b: NoInject[int]) -> None:
16501673
pass
16511674

1652-
assert get_bindings(function8) == {}
1675+
assert get_bindings(function) == {}
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 function1(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 function2(self, a: int, b: NoInject[Optional[str]] = None) -> None:
1686+
# b's type is Union[NoInject[Union[str, None]], None]
16631687
pass
16641688

1665-
assert get_bindings(function9) == {'a': int} == get_bindings(function10)
1689+
assert get_bindings(function1) == {'a': int} == get_bindings(function2)
1690+
16661691

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.
16701696
@inject
1671-
def function11(a: int) -> 'InvalidForwardReference':
1697+
def function(a: int) -> 'InvalidForwardReference':
16721698
pass
16731699

1674-
assert get_bindings(function11) == {'a': int}
1700+
assert get_bindings(function) == {'a': int}
16751701

16761702

16771703
# Tests https://github.com/alecthomas/injector/issues/202

0 commit comments

Comments
 (0)