mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Clean up container instance and add tests
This commit is contained in:
parent
cb7c13f1ba
commit
3ba65da1ad
File diff suppressed because it is too large
Load Diff
|
@ -47,9 +47,9 @@ class Container:
|
|||
def reset_singletons(self) -> None: ...
|
||||
def resolve_provider_name(self, provider: Provider) -> str: ...
|
||||
@property
|
||||
def parent_name(self) -> str: ...
|
||||
def parent(self) -> Optional[ProviderParent]: ...
|
||||
@property
|
||||
def parent(self) -> ProviderParent: ...
|
||||
def parent_name(self) -> Optional[str]: ...
|
||||
def assign_parent(self, parent: ProviderParent) -> None: ...
|
||||
@overload
|
||||
def traverse(self, types: Optional[Iterable[Type[TT]]] = None) -> Iterator[TT]: ...
|
||||
|
|
|
@ -305,6 +305,14 @@ class DynamicContainer(Container):
|
|||
for provider in self.traverse(types=[providers.BaseSingleton]):
|
||||
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
|
||||
def parent_name(self):
|
||||
"""Return parent name."""
|
||||
|
@ -320,15 +328,6 @@ class DynamicContainer(Container):
|
|||
"""Assign 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):
|
||||
"""Declarative inversion of control container meta class."""
|
||||
|
|
|
@ -336,6 +336,62 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
|||
self.assertIs(obj32, obj42)
|
||||
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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user