Clean up container instance and add tests

This commit is contained in:
Roman Mogylatov 2021-02-12 18:55:20 -05:00
parent cb7c13f1ba
commit 3ba65da1ad
4 changed files with 1189 additions and 1134 deletions

File diff suppressed because it is too large Load Diff

View File

@ -47,9 +47,9 @@ class Container:
def reset_singletons(self) -> None: ... def reset_singletons(self) -> None: ...
def resolve_provider_name(self, provider: Provider) -> str: ... def resolve_provider_name(self, provider: Provider) -> str: ...
@property @property
def parent_name(self) -> str: ... def parent(self) -> Optional[ProviderParent]: ...
@property @property
def parent(self) -> ProviderParent: ... def parent_name(self) -> Optional[str]: ...
def assign_parent(self, parent: ProviderParent) -> None: ... def assign_parent(self, parent: ProviderParent) -> None: ...
@overload @overload
def traverse(self, types: Optional[Iterable[Type[TT]]] = None) -> Iterator[TT]: ... def traverse(self, types: Optional[Iterable[Type[TT]]] = None) -> Iterator[TT]: ...

View File

@ -305,6 +305,14 @@ class DynamicContainer(Container):
for provider in self.traverse(types=[providers.BaseSingleton]): for provider in self.traverse(types=[providers.BaseSingleton]):
provider.reset() provider.reset()
def resolve_provider_name(self, provider):
"""Try to resolve provider name."""
for provider_name, container_provider in self.providers.items():
if container_provider is provider:
return provider_name
else:
raise errors.Error(f'Can not resolve name for provider "{provider}"')
@property @property
def parent_name(self): def parent_name(self):
"""Return parent name.""" """Return parent name."""
@ -320,15 +328,6 @@ class DynamicContainer(Container):
"""Assign parent.""" """Assign parent."""
self.parent = parent self.parent = parent
def resolve_provider_name(self, provider):
"""Try to resolve provider name."""
# TODO: add tests
for provider_name, container_provider in self.providers.items():
if container_provider is provider:
return provider_name
else:
raise errors.Error(f'Can not resolve name for provider "{provider}"')
class DeclarativeContainerMetaClass(type): class DeclarativeContainerMetaClass(type):
"""Declarative inversion of control container meta class.""" """Declarative inversion of control container meta class."""

View File

@ -336,6 +336,62 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
self.assertIs(obj32, obj42) self.assertIs(obj32, obj42)
self.assertIs(obj33, obj43) self.assertIs(obj33, obj43)
def test_assign_parent(self):
parent = providers.DependenciesContainer()
container = ContainerA()
container.assign_parent(parent)
self.assertIs(container.parent, parent)
def test_parent_name_declarative_parent(self):
container = ContainerA()
self.assertEqual(container.parent_name, 'ContainerA')
def test_parent_name(self):
container = ContainerA()
self.assertEqual(container.parent_name, 'ContainerA')
def test_parent_name_with_deep_parenting(self):
class Container2(containers.DeclarativeContainer):
name = providers.Container(ContainerA)
class Container1(containers.DeclarativeContainer):
container = providers.Container(Container2)
container = Container1()
self.assertEqual(container.container().name.parent_name, 'Container1.container.name')
def test_parent_name_is_none(self):
container = containers.DynamicContainer()
self.assertIsNone(container.parent_name)
def test_parent_deepcopy(self):
class Container(containers.DeclarativeContainer):
container = providers.Container(ContainerA)
container = Container()
copied = providers.deepcopy(container)
self.assertIs(container.container.parent, container)
self.assertIs(copied.container.parent, copied)
self.assertIsNot(container, copied)
self.assertIsNot(container.container, copied.container)
self.assertIsNot(container.container.parent, copied.container.parent)
def test_resolve_provider_name(self):
container = ContainerA()
self.assertEqual(container.resolve_provider_name(container.p11), 'p11')
def test_resolve_provider_name_no_provider(self):
container = ContainerA()
with self.assertRaises(errors.Error):
container.resolve_provider_name(providers.Provider())
class SelfTests(unittest.TestCase): class SelfTests(unittest.TestCase):