mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-14 10:30:51 +03:00
Add container tests
This commit is contained in:
parent
beded8668d
commit
ce88d50dd2
|
@ -15,7 +15,7 @@ from typing import (
|
||||||
overload,
|
overload,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .providers import Provider
|
from .providers import Provider, Self
|
||||||
|
|
||||||
|
|
||||||
C_Base = TypeVar('C_Base', bound='Container')
|
C_Base = TypeVar('C_Base', bound='Container')
|
||||||
|
@ -29,7 +29,7 @@ class Container:
|
||||||
providers: Dict[str, Provider]
|
providers: Dict[str, Provider]
|
||||||
dependencies: Dict[str, Provider]
|
dependencies: Dict[str, Provider]
|
||||||
overridden: Tuple[Provider]
|
overridden: Tuple[Provider]
|
||||||
__self__: Provider
|
__self__: Self
|
||||||
def __init__(self) -> None: ...
|
def __init__(self) -> None: ...
|
||||||
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
|
def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
|
||||||
def __setattr__(self, name: str, value: Union[Provider, Any]) -> None: ...
|
def __setattr__(self, name: str, value: Union[Provider, Any]) -> None: ...
|
||||||
|
|
|
@ -335,3 +335,94 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
self.assertIs(obj31, obj41)
|
self.assertIs(obj31, obj41)
|
||||||
self.assertIs(obj32, obj42)
|
self.assertIs(obj32, obj42)
|
||||||
self.assertIs(obj33, obj43)
|
self.assertIs(obj33, obj43)
|
||||||
|
|
||||||
|
|
||||||
|
class SelfTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_self(self):
|
||||||
|
def call_bar(container):
|
||||||
|
return container.bar()
|
||||||
|
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
__self__ = providers.Self()
|
||||||
|
foo = providers.Callable(call_bar, __self__)
|
||||||
|
bar = providers.Object('hello')
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertIs(container.foo(), 'hello')
|
||||||
|
|
||||||
|
def test_self_attribute_implicit(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
...
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertIs(container.__self__(), container)
|
||||||
|
|
||||||
|
def test_self_attribute_explicit(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
__self__ = providers.Self()
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertIs(container.__self__(), container)
|
||||||
|
|
||||||
|
def test_single_self(self):
|
||||||
|
with self.assertRaises(errors.Error):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
self1 = providers.Self()
|
||||||
|
self2 = providers.Self()
|
||||||
|
|
||||||
|
def test_self_attribute_alt_name_implicit(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
foo = providers.Self()
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertIs(container.__self__, container.foo)
|
||||||
|
self.assertEqual(set(container.__self__.alt_names), {'foo'})
|
||||||
|
|
||||||
|
def test_self_attribute_alt_name_explicit_1(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
__self__ = providers.Self()
|
||||||
|
foo = __self__
|
||||||
|
bar = __self__
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertIs(container.__self__, container.foo)
|
||||||
|
self.assertIs(container.__self__, container.bar)
|
||||||
|
self.assertEqual(set(container.__self__.alt_names), {'foo', 'bar'})
|
||||||
|
|
||||||
|
def test_self_attribute_alt_name_explicit_2(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
foo = providers.Self()
|
||||||
|
bar = foo
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertIs(container.__self__, container.foo)
|
||||||
|
self.assertIs(container.__self__, container.bar)
|
||||||
|
self.assertEqual(set(container.__self__.alt_names), {'foo', 'bar'})
|
||||||
|
|
||||||
|
def test_providers_attribute_1(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
__self__ = providers.Self()
|
||||||
|
foo = __self__
|
||||||
|
bar = __self__
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertEqual(container.providers, {})
|
||||||
|
self.assertEqual(Container.providers, {})
|
||||||
|
|
||||||
|
def test_providers_attribute_2(self):
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
foo = providers.Self()
|
||||||
|
bar = foo
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
self.assertEqual(container.providers, {})
|
||||||
|
self.assertEqual(Container.providers, {})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user