mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 04:53:59 +03:00
Add implementation, tests, and typing stubs
This commit is contained in:
parent
4013225132
commit
d48e2ea972
File diff suppressed because it is too large
Load Diff
|
@ -267,6 +267,7 @@ class BaseSingleton(Provider[T]):
|
|||
def set_attributes(self, **kwargs: Injection) -> BaseSingleton[T]: ...
|
||||
def clear_attributes(self) -> BaseSingleton[T]: ...
|
||||
def reset(self) -> None: ...
|
||||
def full_reset(self) -> None: ...
|
||||
|
||||
|
||||
class Singleton(BaseSingleton[T]): ...
|
||||
|
|
|
@ -2468,6 +2468,15 @@ cdef class BaseSingleton(Provider):
|
|||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def full_reset(self):
|
||||
"""Reset cached instance in current and all underlying singletons, if any.
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
self.reset()
|
||||
for provider in self.traverse(types=[BaseSingleton]):
|
||||
provider.reset()
|
||||
|
||||
@property
|
||||
def related(self):
|
||||
"""Return related providers generator."""
|
||||
|
|
|
@ -355,6 +355,37 @@ class _BaseSingletonTestCase(object):
|
|||
|
||||
self.assertIsNot(instance1, instance2)
|
||||
|
||||
def test_reset_with_singleton(self):
|
||||
dependent_singleton = providers.Singleton(object)
|
||||
provider = self.singleton_cls(dict, dependency=dependent_singleton)
|
||||
|
||||
dependent_instance = dependent_singleton()
|
||||
instance1 = provider()
|
||||
self.assertIs(instance1['dependency'], dependent_instance)
|
||||
|
||||
provider.reset()
|
||||
|
||||
instance2 = provider()
|
||||
self.assertIs(instance1['dependency'], dependent_instance)
|
||||
|
||||
self.assertIsNot(instance1, instance2)
|
||||
|
||||
def test_full_reset(self):
|
||||
dependent_singleton = providers.Singleton(object)
|
||||
provider = self.singleton_cls(dict, dependency=dependent_singleton)
|
||||
|
||||
dependent_instance1 = dependent_singleton()
|
||||
instance1 = provider()
|
||||
self.assertIs(instance1['dependency'], dependent_instance1)
|
||||
|
||||
provider.full_reset()
|
||||
|
||||
dependent_instance2 = dependent_singleton()
|
||||
instance2 = provider()
|
||||
self.assertIsNot(instance2['dependency'], dependent_instance1)
|
||||
self.assertIsNot(dependent_instance1, dependent_instance2)
|
||||
self.assertIsNot(instance1, instance2)
|
||||
|
||||
|
||||
class SingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user