mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Clean up DependenciesContainer provider and add tests
This commit is contained in:
parent
9677701626
commit
351bdd282e
File diff suppressed because it is too large
Load Diff
|
@ -124,9 +124,9 @@ class DependenciesContainer(Object):
|
|||
def providers(self) -> _Dict[str, Provider]: ...
|
||||
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: ...
|
||||
|
||||
|
||||
|
|
|
@ -833,12 +833,11 @@ cdef class DependenciesContainer(Object):
|
|||
|
||||
def __init__(self, **dependencies):
|
||||
"""Initializer."""
|
||||
self.__providers = dependencies
|
||||
|
||||
for provider in dependencies.items():
|
||||
for provider in dependencies.values():
|
||||
if isinstance(provider, (Dependency, DependenciesContainer, Container)):
|
||||
provider.assign_parent(self)
|
||||
|
||||
self.__providers = dependencies
|
||||
self.__parent = None
|
||||
|
||||
super(DependenciesContainer, self).__init__(None)
|
||||
|
@ -857,14 +856,7 @@ cdef class DependenciesContainer(Object):
|
|||
copied.__provides = deepcopy(self.__provides, memo)
|
||||
copied.__providers = deepcopy(self.__providers, memo)
|
||||
|
||||
# TODO: remove duplication
|
||||
copied_parent = (
|
||||
deepcopy(self.__parent, memo)
|
||||
if is_provider(self.parent) or is_container_instance(self.parent)
|
||||
else self.parent
|
||||
)
|
||||
copied.assign_parent(copied_parent)
|
||||
|
||||
self._copy_parent(copied, memo)
|
||||
self._copy_overridings(copied, memo)
|
||||
|
||||
return copied
|
||||
|
@ -943,27 +935,37 @@ cdef class DependenciesContainer(Object):
|
|||
|
||||
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 Error(f'Can not resolve name for provider "{provider}"')
|
||||
|
||||
@property
|
||||
def parent_name(self):
|
||||
"""Return parent name."""
|
||||
return f'{self.parent.parent_name}.{self.parent.resolve_provider_name(self)}'
|
||||
|
||||
@property
|
||||
def parent(self):
|
||||
"""Return parent."""
|
||||
return self.__parent
|
||||
|
||||
@property
|
||||
def parent_name(self):
|
||||
"""Return parent name."""
|
||||
if not self.__parent:
|
||||
return None
|
||||
|
||||
name = ''
|
||||
if self.__parent.parent_name:
|
||||
name += f'{self.__parent.parent_name}.'
|
||||
name += f'{self.__parent.resolve_provider_name(self)}'
|
||||
|
||||
return name
|
||||
|
||||
def assign_parent(self, parent):
|
||||
"""Assign parent."""
|
||||
self.__parent = parent
|
||||
|
||||
def _copy_parent(self, copied, memo):
|
||||
_copy_parent(self, copied, memo)
|
||||
|
||||
cpdef object _override_providers(self, object container):
|
||||
"""Override providers with providers from provided container."""
|
||||
for name, dependency_provider in container.providers.items():
|
||||
|
|
|
@ -459,7 +459,17 @@ class DependencyTests(unittest.TestCase):
|
|||
container.name = provider
|
||||
self.assertEqual(provider.parent_name, 'name')
|
||||
|
||||
def test_deepcopy_parent(self):
|
||||
def test_parent_name_with_deep_parenting(self):
|
||||
provider = providers.Dependency()
|
||||
container = providers.DependenciesContainer(name=provider)
|
||||
_ = providers.DependenciesContainer(container=container)
|
||||
self.assertEqual(provider.parent_name, 'container.name')
|
||||
|
||||
def test_parent_name_is_none(self):
|
||||
provider = providers.DependenciesContainer()
|
||||
self.assertIsNone(provider.parent_name)
|
||||
|
||||
def test_parent_deepcopy(self):
|
||||
container = containers.DynamicContainer()
|
||||
provider = providers.Dependency()
|
||||
container.name = provider
|
||||
|
@ -633,3 +643,63 @@ class DependenciesContainerTests(unittest.TestCase):
|
|||
|
||||
self.assertFalse(dependency.overridden)
|
||||
self.assertFalse(dependency.overridden)
|
||||
|
||||
def test_assign_parent(self):
|
||||
parent = providers.DependenciesContainer()
|
||||
provider = providers.DependenciesContainer()
|
||||
|
||||
provider.assign_parent(parent)
|
||||
|
||||
self.assertIs(provider.parent, parent)
|
||||
|
||||
def test_parent_name(self):
|
||||
container = containers.DynamicContainer()
|
||||
provider = providers.DependenciesContainer()
|
||||
container.name = provider
|
||||
self.assertEqual(provider.parent_name, 'name')
|
||||
|
||||
def test_parent_name_with_deep_parenting(self):
|
||||
provider = providers.DependenciesContainer()
|
||||
container = providers.DependenciesContainer(name=provider)
|
||||
_ = providers.DependenciesContainer(container=container)
|
||||
self.assertEqual(provider.parent_name, 'container.name')
|
||||
|
||||
def test_parent_name_is_none(self):
|
||||
provider = providers.DependenciesContainer()
|
||||
self.assertIsNone(provider.parent_name)
|
||||
|
||||
def test_parent_deepcopy(self):
|
||||
container = containers.DynamicContainer()
|
||||
provider = providers.DependenciesContainer()
|
||||
container.name = provider
|
||||
|
||||
copied = providers.deepcopy(container)
|
||||
|
||||
self.assertIs(container.name.parent, container)
|
||||
self.assertIs(copied.name.parent, copied)
|
||||
|
||||
self.assertIsNot(container, copied)
|
||||
self.assertIsNot(container.name, copied.name)
|
||||
self.assertIsNot(container.name.parent, copied.name.parent)
|
||||
|
||||
def test_parent_set_on__getattr__(self):
|
||||
provider = providers.DependenciesContainer()
|
||||
self.assertIsInstance(provider.name, providers.Dependency)
|
||||
self.assertIs(provider.name.parent, provider)
|
||||
|
||||
def test_parent_set_on__init__(self):
|
||||
provider = providers.Dependency()
|
||||
container = providers.DependenciesContainer(name=provider)
|
||||
self.assertIs(container.name, provider)
|
||||
self.assertIs(container.name.parent, container)
|
||||
|
||||
def test_resolve_provider_name(self):
|
||||
container = providers.DependenciesContainer()
|
||||
self.assertEqual(container.resolve_provider_name(container.name), 'name')
|
||||
|
||||
def test_resolve_provider_name_no_provider(self):
|
||||
provider = providers.Dependency()
|
||||
container = providers.DependenciesContainer()
|
||||
|
||||
with self.assertRaises(errors.Error):
|
||||
container.resolve_provider_name(provider)
|
||||
|
|
Loading…
Reference in New Issue
Block a user