Add container tests

This commit is contained in:
Roman Mogylatov 2021-02-06 22:50:19 -05:00
parent beded8668d
commit ce88d50dd2
2 changed files with 93 additions and 2 deletions

View File

@ -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: ...

View File

@ -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, {})