mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-04 20:33:13 +03:00
Add implementation + tests
This commit is contained in:
parent
2fadbf0f81
commit
ef0f5f5ebe
File diff suppressed because it is too large
Load Diff
|
@ -43,6 +43,8 @@ class Container:
|
||||||
def unwire(self) -> None: ...
|
def unwire(self) -> None: ...
|
||||||
def init_resources(self) -> Optional[Awaitable]: ...
|
def init_resources(self) -> Optional[Awaitable]: ...
|
||||||
def shutdown_resources(self) -> Optional[Awaitable]: ...
|
def shutdown_resources(self) -> Optional[Awaitable]: ...
|
||||||
|
def apply_container_providers_overridings(self) -> None: ...
|
||||||
|
def reset_singletons(self) -> None: ...
|
||||||
@overload
|
@overload
|
||||||
def traverse(self, types: Optional[Sequence[Type]] = None) -> Iterator[Provider]: ...
|
def traverse(self, types: Optional[Sequence[Type]] = None) -> Iterator[Provider]: ...
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -271,6 +271,11 @@ class DynamicContainer(Container):
|
||||||
for provider in self.traverse(types=[providers.Container]):
|
for provider in self.traverse(types=[providers.Container]):
|
||||||
provider.apply_overridings()
|
provider.apply_overridings()
|
||||||
|
|
||||||
|
def reset_singletons(self):
|
||||||
|
"""Reset all container singletons."""
|
||||||
|
for provider in self.traverse(types=[providers.Singleton]):
|
||||||
|
provider.reset()
|
||||||
|
|
||||||
|
|
||||||
class DeclarativeContainerMetaClass(type):
|
class DeclarativeContainerMetaClass(type):
|
||||||
"""Declarative inversion of control container meta class."""
|
"""Declarative inversion of control container meta class."""
|
||||||
|
|
|
@ -287,3 +287,51 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
|
||||||
self.assertEqual(_init1.shutdown_counter, 2)
|
self.assertEqual(_init1.shutdown_counter, 2)
|
||||||
self.assertEqual(_init2.init_counter, 2)
|
self.assertEqual(_init2.init_counter, 2)
|
||||||
self.assertEqual(_init2.shutdown_counter, 2)
|
self.assertEqual(_init2.shutdown_counter, 2)
|
||||||
|
|
||||||
|
def reset_singletons(self):
|
||||||
|
class SubSubContainer(containers.DeclarativeContainer):
|
||||||
|
singleton = providers.Singleton(object)
|
||||||
|
|
||||||
|
class SubContainer(containers.DeclarativeContainer):
|
||||||
|
singleton = providers.Singleton(object)
|
||||||
|
sub_sub_container = providers.Container(SubSubContainer)
|
||||||
|
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
singleton = providers.Singleton(object)
|
||||||
|
sub_container = providers.Container(SubContainer)
|
||||||
|
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
obj11 = container.singleton()
|
||||||
|
obj12 = container.sub_container().singleton()
|
||||||
|
obj13 = container.sub_container().sub_sub_container().singleton()
|
||||||
|
|
||||||
|
obj21 = container.singleton()
|
||||||
|
obj22 = container.sub_container().singleton()
|
||||||
|
obj23 = container.sub_container().sub_sub_container().singleton()
|
||||||
|
|
||||||
|
self.assertIs(obj11, obj21)
|
||||||
|
self.assertIs(obj12, obj22)
|
||||||
|
self.assertIs(obj13, obj23)
|
||||||
|
|
||||||
|
container.reset_singletons()
|
||||||
|
|
||||||
|
obj31 = container.singleton()
|
||||||
|
obj32 = container.sub_container().singleton()
|
||||||
|
obj33 = container.sub_container().sub_sub_container().singleton()
|
||||||
|
|
||||||
|
obj41 = container.singleton()
|
||||||
|
obj42 = container.sub_container().singleton()
|
||||||
|
obj43 = container.sub_container().sub_sub_container().singleton()
|
||||||
|
|
||||||
|
self.assertIsNot(obj11, obj31)
|
||||||
|
self.assertIsNot(obj12, obj32)
|
||||||
|
self.assertIsNot(obj13, obj33)
|
||||||
|
|
||||||
|
self.assertIsNot(obj21, obj31)
|
||||||
|
self.assertIsNot(obj22, obj32)
|
||||||
|
self.assertIsNot(obj23, obj33)
|
||||||
|
|
||||||
|
self.assertIs(obj31, obj41)
|
||||||
|
self.assertIs(obj32, obj42)
|
||||||
|
self.assertIs(obj33, obj43)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user