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: ... def __getattr__(self, name: str) -> Provider: ...
@property @property
def container(self) -> T: ... def container(self) -> T: ...
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: ...

View File

@ -3553,13 +3553,7 @@ cdef class Container(Provider):
copied.__overriding_providers = deepcopy(self.__overriding_providers, memo) copied.__overriding_providers = deepcopy(self.__overriding_providers, memo)
copied.apply_overridings() copied.apply_overridings()
# TODO: remove duplication self._copy_parent(copied, memo)
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)
return copied return copied
@ -3596,24 +3590,43 @@ cdef class Container(Provider):
self.__container.override_providers(**self.__overriding_providers) self.__container.override_providers(**self.__overriding_providers)
@property @property
def parent_name(self): def related(self):
"""Return parent name.""" """Return related providers generator."""
return f'{self.parent.parent_name}.{self.parent.resolve_provider_name(self)}' 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 @property
def parent(self): def parent(self):
"""Return parent.""" """Return parent."""
return self.__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): def assign_parent(self, parent):
"""Assign parent.""" """Assign parent."""
self.__parent = parent self.__parent = parent
@property def _copy_parent(self, copied, memo):
def related(self): _copy_parent(self, copied, memo)
"""Return related providers generator."""
yield from self.providers.values()
yield from super().related
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance.""" """Return single instance."""

View File

@ -698,8 +698,6 @@ class DependenciesContainerTests(unittest.TestCase):
self.assertEqual(container.resolve_provider_name(container.name), 'name') self.assertEqual(container.resolve_provider_name(container.name), 'name')
def test_resolve_provider_name_no_provider(self): def test_resolve_provider_name_no_provider(self):
provider = providers.Dependency()
container = providers.DependenciesContainer() container = providers.DependenciesContainer()
with self.assertRaises(errors.Error): 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()) b = B(d=D())
result = b.a().c().bar() result = b.a().c().bar()
self.assertEqual(result, 'foo++') 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())