Skip to content

Commit

Permalink
Add "clear" method.
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Dec 28, 2023
1 parent 7ebae4e commit 631647d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions test/test_container_override.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from typing_extensions import Annotated
from wireup import DependencyContainer, ParameterBag, Wire
from wireup.ioc.override_manager import OverrideManager
from wireup.ioc.types import ServiceOverride


Expand Down Expand Up @@ -84,3 +85,13 @@ def target(foo: FooBase):
self.assertEqual(svc.foo, "mock")

target()

def test_clear_services_removes_all(self):
overrides = {}
mock1 = MagicMock()
override_mgr = OverrideManager(overrides)
override_mgr.set(RandomService, new=mock1)
self.assertEqual(overrides, {(RandomService, None): mock1})

override_mgr.clear()
self.assertEqual(overrides, {})
4 changes: 4 additions & 0 deletions wireup/ioc/override_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def delete(self, target: type, qualifier: ContainerProxyQualifierValue = None) -
if (target, qualifier) in self.__active_overrides:
del self.__active_overrides[target, qualifier]

def clear(self) -> None:
"""Clear all active service overrides."""
self.__active_overrides.clear()

@contextmanager
def service(self, target: type, new: Any, qualifier: ContainerProxyQualifierValue = None) -> Iterator[None]:
"""Override the target service with new for the duration of the context manager.
Expand Down

0 comments on commit 631647d

Please sign in to comment.