Clean up Container provider and add tests

This commit is contained in:
Roman Mogylatov 2021-02-12 16:58:23 -05:00
parent 351bdd282e
commit cb7c13f1ba
5 changed files with 2186 additions and 1683 deletions

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

@ -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())

View File

@ -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())