mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-12-28 04:22:59 +03:00
Add ServiceWithCallable and related tests for method calls with args and kwargs
This commit is contained in:
parent
4f8c4bf08d
commit
06b1bd023b
|
|
@ -1,6 +1,6 @@
|
|||
from dependency_injector import containers, providers
|
||||
|
||||
from .service import Service
|
||||
from .service import Service, ServiceWithCallable
|
||||
|
||||
|
||||
class SubContainer(containers.DeclarativeContainer):
|
||||
|
|
@ -14,4 +14,6 @@ class Container(containers.DeclarativeContainer):
|
|||
|
||||
service = providers.Factory(Service)
|
||||
|
||||
service_with_callable = providers.Factory(ServiceWithCallable)
|
||||
|
||||
sub = providers.Container(SubContainer)
|
||||
|
|
|
|||
|
|
@ -100,6 +100,27 @@ def test_provided_instance(some_value: int = Provide[Container.service.provided.
|
|||
return some_value
|
||||
|
||||
|
||||
@inject
|
||||
def test_provided_instance_call_with_args(
|
||||
some_value: int = Provide[Container.service_with_callable.provided.method_with_args.call(1, 2)]
|
||||
):
|
||||
return some_value
|
||||
|
||||
|
||||
@inject
|
||||
def test_provided_instance_call_with_kwargs(
|
||||
some_value: dict = Provide[Container.service_with_callable.provided.method_with_kwargs.call(a=1, b=2)]
|
||||
):
|
||||
return some_value
|
||||
|
||||
|
||||
@inject
|
||||
def test_provided_instance_call_with_args_and_kwargs(
|
||||
some_value: dict = Provide[Container.service_with_callable.provided.foo.process.call(1, 2, key="value")]
|
||||
):
|
||||
return some_value
|
||||
|
||||
|
||||
@inject
|
||||
def test_subcontainer_provider(some_value: int = Provide[Container.sub.int_object]):
|
||||
return some_value
|
||||
|
|
|
|||
|
|
@ -1,2 +1,15 @@
|
|||
class Service:
|
||||
service_attr: int
|
||||
|
||||
|
||||
class ServiceWithCallable:
|
||||
def __init__(self):
|
||||
self.foo = CallableDict({"bar": lambda: 10})
|
||||
self.method_with_args = lambda x, y: x + y
|
||||
self.method_with_kwargs = lambda **kwargs: kwargs
|
||||
|
||||
|
||||
class CallableDict(dict):
|
||||
def __init__(self, data):
|
||||
super().__init__(data)
|
||||
self.process = lambda *args, **kwargs: {"args": args, "kwargs": kwargs}
|
||||
|
|
|
|||
|
|
@ -188,6 +188,21 @@ def test_provided_instance(container: Container):
|
|||
assert some_value == 10
|
||||
|
||||
|
||||
def test_provided_instance_call_with_args():
|
||||
some_value = module.test_provided_instance_call_with_args()
|
||||
assert some_value == 3
|
||||
|
||||
|
||||
def test_provided_instance_call_with_kwargs():
|
||||
some_value = module.test_provided_instance_call_with_kwargs()
|
||||
assert some_value == {"a": 1, "b": 2}
|
||||
|
||||
|
||||
def test_provided_instance_call_with_args_and_kwargs():
|
||||
some_value = module.test_provided_instance_call_with_args_and_kwargs()
|
||||
assert some_value == {"args": (1, 2), "kwargs": {"key": "value"}}
|
||||
|
||||
|
||||
def test_subcontainer():
|
||||
some_value = module.test_subcontainer_provider()
|
||||
assert some_value == 1
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user