Improve DependenciesContainer overriding logic

This commit is contained in:
Roman Mogylatov 2018-01-24 20:11:36 +02:00
parent b94b424f3a
commit 39b62e37d5
3 changed files with 2558 additions and 2051 deletions

File diff suppressed because it is too large Load Diff

View File

@ -427,7 +427,6 @@ cdef class DependenciesContainer(Object):
Dependencies container provider is used to implement late static binding Dependencies container provider is used to implement late static binding
for a set of providers of a particular container. for a set of providers of a particular container.
Example code: Example code:
.. code-block:: python .. code-block:: python
@ -520,6 +519,29 @@ cdef class DependenciesContainer(Object):
self._override_providers(container=provider) self._override_providers(container=provider)
return super(DependenciesContainer, self).override(provider) return super(DependenciesContainer, self).override(provider)
def reset_last_overriding(self):
"""Reset last overriding provider.
:raise: :py:exc:`dependency_injector.errors.Error` if provider is not
overridden.
:rtype: None
"""
for child in self.__providers.values():
try:
child.reset_last_overriding()
except Error:
pass
super(DependenciesContainer, self).reset_last_overriding()
def reset_override(self):
"""Reset all overriding providers.
:rtype: None
"""
for child in self.__providers.values():
child.reset_override()
super(DependenciesContainer, self).reset_override()
cpdef object _override_providers(self, object container): cpdef object _override_providers(self, object container):
"""Override providers with providers from provided container.""" """Override providers with providers from provided container."""

View File

@ -341,6 +341,22 @@ class DependenciesContainerTests(unittest.TestCase):
self.assertTrue(dependency.overridden) self.assertTrue(dependency.overridden)
self.assertIs(dependency.last_overriding, self.container.dependency) self.assertIs(dependency.last_overriding, self.container.dependency)
def test_reset_last_overriding(self):
dependency = self.provider.dependency
self.provider.override(self.container)
self.provider.reset_last_overriding()
self.assertIsNone(dependency.last_overriding)
self.assertIsNone(dependency.last_overriding)
def test_reset_override(self):
dependency = self.provider.dependency
self.provider.override(self.container)
self.provider.reset_override()
self.assertFalse(dependency.overridden)
self.assertFalse(dependency.overridden)
def test_init_with_container_and_providers(self): def test_init_with_container_and_providers(self):
provider = providers.DependenciesContainer( provider = providers.DependenciesContainer(
self.container, dependency=providers.Dependency()) self.container, dependency=providers.Dependency())