mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Clean up Container provider and add tests
This commit is contained in:
parent
351bdd282e
commit
cb7c13f1ba
File diff suppressed because it is too large
Load Diff
|
@ -370,10 +370,11 @@ class Container(Provider[T]):
|
|||
def __getattr__(self, name: str) -> Provider: ...
|
||||
@property
|
||||
def container(self) -> T: ...
|
||||
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: ...
|
||||
|
||||
|
||||
|
|
|
@ -3553,13 +3553,7 @@ cdef class Container(Provider):
|
|||
copied.__overriding_providers = deepcopy(self.__overriding_providers, memo)
|
||||
copied.apply_overridings()
|
||||
|
||||
# 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)
|
||||
|
||||
return copied
|
||||
|
||||
|
@ -3596,24 +3590,43 @@ cdef class Container(Provider):
|
|||
self.__container.override_providers(**self.__overriding_providers)
|
||||
|
||||
@property
|
||||
def parent_name(self):
|
||||
"""Return parent name."""
|
||||
return f'{self.parent.parent_name}.{self.parent.resolve_provider_name(self)}'
|
||||
def related(self):
|
||||
"""Return related providers generator."""
|
||||
yield from self.providers.values()
|
||||
yield from super().related
|
||||
|
||||
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 Error(f'Can not resolve name for provider "{provider}"')
|
||||
|
||||
@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
|
||||
|
||||
@property
|
||||
def related(self):
|
||||
"""Return related providers generator."""
|
||||
yield from self.providers.values()
|
||||
yield from super().related
|
||||
def _copy_parent(self, copied, memo):
|
||||
_copy_parent(self, copied, memo)
|
||||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return single instance."""
|
||||
|
|
|
@ -698,8 +698,6 @@ class DependenciesContainerTests(unittest.TestCase):
|
|||
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)
|
||||
container.resolve_provider_name(providers.Provider())
|
||||
|
|
|
@ -175,3 +175,50 @@ class ContainerTests(unittest.TestCase):
|
|||
b = B(d=D())
|
||||
result = b.a().c().bar()
|
||||
self.assertEqual(result, 'foo++')
|
||||
|
||||
def test_assign_parent(self):
|
||||
parent = providers.DependenciesContainer()
|
||||
provider = providers.Container(TestCore)
|
||||
|
||||
provider.assign_parent(parent)
|
||||
|
||||
self.assertIs(provider.parent, parent)
|
||||
|
||||
def test_parent_name(self):
|
||||
container = containers.DynamicContainer()
|
||||
provider = providers.Container(TestCore)
|
||||
container.name = provider
|
||||
self.assertEqual(provider.parent_name, 'name')
|
||||
|
||||
def test_parent_name_with_deep_parenting(self):
|
||||
provider = providers.Container(TestCore)
|
||||
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.Container(TestCore)
|
||||
self.assertIsNone(provider.parent_name)
|
||||
|
||||
def test_parent_deepcopy(self):
|
||||
container = containers.DynamicContainer()
|
||||
provider = providers.Container(TestCore)
|
||||
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_resolve_provider_name(self):
|
||||
container = providers.Container(TestCore)
|
||||
self.assertEqual(container.resolve_provider_name(container.value_getter), 'value_getter')
|
||||
|
||||
def test_resolve_provider_name_no_provider(self):
|
||||
container = providers.Container(TestCore)
|
||||
with self.assertRaises(errors.Error):
|
||||
container.resolve_provider_name(providers.Provider())
|
||||
|
|
Loading…
Reference in New Issue
Block a user