From ce88d50dd2fc85c9e35cddd9e980943ac40c91cb Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Sat, 6 Feb 2021 22:50:19 -0500 Subject: [PATCH] Add container tests --- src/dependency_injector/containers.pyi | 4 +- tests/unit/containers/test_dynamic_py2_py3.py | 91 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/dependency_injector/containers.pyi b/src/dependency_injector/containers.pyi index 1dedc056..2575d9d0 100644 --- a/src/dependency_injector/containers.pyi +++ b/src/dependency_injector/containers.pyi @@ -15,7 +15,7 @@ from typing import ( overload, ) -from .providers import Provider +from .providers import Provider, Self C_Base = TypeVar('C_Base', bound='Container') @@ -29,7 +29,7 @@ class Container: providers: Dict[str, Provider] dependencies: Dict[str, Provider] overridden: Tuple[Provider] - __self__: Provider + __self__: Self def __init__(self) -> None: ... def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ... def __setattr__(self, name: str, value: Union[Provider, Any]) -> None: ... diff --git a/tests/unit/containers/test_dynamic_py2_py3.py b/tests/unit/containers/test_dynamic_py2_py3.py index f0ca2a55..da68dfd5 100644 --- a/tests/unit/containers/test_dynamic_py2_py3.py +++ b/tests/unit/containers/test_dynamic_py2_py3.py @@ -335,3 +335,94 @@ class DeclarativeContainerInstanceTests(unittest.TestCase): self.assertIs(obj31, obj41) self.assertIs(obj32, obj42) 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, {})