Add functionality for using positional argument injections with @inject decorator

This commit is contained in:
Roman Mogilatov 2015-10-23 09:53:53 +03:00
parent a37f1fc256
commit 6dc007c8df
3 changed files with 94 additions and 15 deletions

View File

@ -77,10 +77,10 @@ class Method(NamedInjection):
def inject(*args, **kwargs):
"""Dependency injection decorator.
:type injection: Injection
:return: (callable) -> (callable)
"""
injections = _parse_kwargs_injections(args, kwargs)
arg_injections = _parse_args_injections(args)
kwarg_injections = _parse_kwargs_injections(args, kwargs)
def decorator(callback_or_cls):
"""Dependency injection decorator."""
@ -99,17 +99,20 @@ def inject(*args, **kwargs):
callback = callback_or_cls
if hasattr(callback, 'injections'):
callback.injections += injections
callback.args += arg_injections
callback.kwargs += kwarg_injections
callback.injections += arg_injections + kwarg_injections
return callback
@six.wraps(callback)
def decorated(*args, **kwargs):
"""Decorated with dependency injection callback."""
return callback(*args,
**_get_injectable_kwargs(kwargs,
decorated.injections))
return callback(*_get_injectable_args(args, decorated.args),
**_get_injectable_kwargs(kwargs, decorated.kwargs))
decorated.injections = injections
decorated.args = arg_injections
decorated.kwargs = kwarg_injections
decorated.injections = arg_injections + kwarg_injections
return decorated
return decorator

View File

@ -75,8 +75,89 @@ class MethodTests(unittest.TestCase):
class InjectTests(unittest.TestCase):
"""Inject decorator test cases."""
def test_decorated(self):
"""Test `inject()` decorated callback."""
def test_decorated_args(self):
"""Test `inject()` decoration with args."""
provider1 = di.Factory(object)
provider2 = di.Factory(list)
@di.inject(provider1, provider2)
def test(a, b):
return a, b
a1, b1 = test()
a2, b2 = test()
self.assertIsInstance(a1, object)
self.assertIsInstance(a2, object)
self.assertIsNot(a1, a2)
self.assertIsInstance(b1, list)
self.assertIsInstance(b2, list)
self.assertIsNot(b1, b2)
def test_decorated_args_extended_syntax(self):
"""Test `inject()` decoration with args."""
provider1 = di.Factory(object)
provider2 = di.Factory(list)
@di.inject(di.Arg(provider1), di.Arg(provider2))
def test(a, b):
return a, b
a1, b1 = test()
a2, b2 = test()
self.assertIsInstance(a1, object)
self.assertIsInstance(a2, object)
self.assertIsNot(a1, a2)
self.assertIsInstance(b1, list)
self.assertIsInstance(b2, list)
self.assertIsNot(b1, b2)
def test_decorated_args_several_times(self):
"""Test `inject()` decoration with args several times."""
provider1 = di.Factory(object)
provider2 = di.Factory(list)
@di.inject(provider2)
@di.inject(provider1)
def test(a, b):
return a, b
a1, b1 = test()
a2, b2 = test()
self.assertIsInstance(a1, object)
self.assertIsInstance(a2, object)
self.assertIsNot(a1, a2)
self.assertIsInstance(b1, list)
self.assertIsInstance(b2, list)
self.assertIsNot(b1, b2)
def test_decorated_context_args(self):
"""Test `inject()` decoration with context args."""
provider1 = di.Factory(object)
provider2 = di.Factory(list)
@di.inject(provider1)
def test(a, b):
return a, b
a1, b1 = test(provider2())
a2, b2 = test(provider2())
self.assertIsInstance(a1, object)
self.assertIsInstance(a2, object)
self.assertIsNot(a1, a2)
self.assertIsInstance(b1, list)
self.assertIsInstance(b2, list)
self.assertIsNot(b1, b2)
def test_decorated_kwargs(self):
"""Test `inject()` decoration with kwargs."""
provider1 = di.Factory(object)
provider2 = di.Factory(list)

View File

@ -9,12 +9,7 @@ class Example(object):
def __init__(self, init_arg1=None, init_arg2=None, init_arg3=None,
init_arg4=None):
"""Initializer.
:param init_arg1:
:param init_arg2:
:return:
"""
"""Initializer."""
self.init_arg1 = init_arg1
self.init_arg2 = init_arg2
self.init_arg3 = init_arg3