mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-26 03:23:58 +03:00
Adding tests for inject() decorator
This commit is contained in:
parent
1032143f5b
commit
b0c2005288
|
@ -1,5 +1,7 @@
|
||||||
"""Injections module."""
|
"""Injections module."""
|
||||||
|
|
||||||
|
from six import wraps
|
||||||
|
|
||||||
from .utils import is_provider
|
from .utils import is_provider
|
||||||
from .utils import ensure_is_injection
|
from .utils import ensure_is_injection
|
||||||
|
|
||||||
|
@ -58,6 +60,7 @@ def inject(injection):
|
||||||
|
|
||||||
def decorator(callback):
|
def decorator(callback):
|
||||||
"""Decorator."""
|
"""Decorator."""
|
||||||
|
@wraps(callback)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
"""Decorated."""
|
"""Decorated."""
|
||||||
if injection.name not in kwargs:
|
if injection.name not in kwargs:
|
||||||
|
|
|
@ -6,6 +6,7 @@ from objects.injections import Injection
|
||||||
from objects.injections import KwArg
|
from objects.injections import KwArg
|
||||||
from objects.injections import Attribute
|
from objects.injections import Attribute
|
||||||
from objects.injections import Method
|
from objects.injections import Method
|
||||||
|
from objects.injections import inject
|
||||||
|
|
||||||
from objects.providers import NewInstance
|
from objects.providers import NewInstance
|
||||||
|
|
||||||
|
@ -31,9 +32,9 @@ class InjectionTests(unittest.TestCase):
|
||||||
self.assertIsInstance(injection.value, object)
|
self.assertIsInstance(injection.value, object)
|
||||||
|
|
||||||
|
|
||||||
class InitArgTests(unittest.TestCase):
|
class KwArgTests(unittest.TestCase):
|
||||||
|
|
||||||
"""Init arg injection test cases."""
|
"""Keyword arg injection test cases."""
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
"""Test KwArg creation and initialization."""
|
"""Test KwArg creation and initialization."""
|
||||||
|
@ -62,3 +63,73 @@ class MethodTests(unittest.TestCase):
|
||||||
injection = Method('some_arg_name', 'some_value')
|
injection = Method('some_arg_name', 'some_value')
|
||||||
self.assertEqual(injection.name, 'some_arg_name')
|
self.assertEqual(injection.name, 'some_arg_name')
|
||||||
self.assertEqual(injection.injectable, 'some_value')
|
self.assertEqual(injection.injectable, 'some_value')
|
||||||
|
|
||||||
|
|
||||||
|
class InjectTests(unittest.TestCase):
|
||||||
|
|
||||||
|
"""Inject decorator test cases."""
|
||||||
|
|
||||||
|
def test_decorated(self):
|
||||||
|
"""Test `inject()` decorated callback."""
|
||||||
|
provider1 = NewInstance(object)
|
||||||
|
provider2 = NewInstance(list)
|
||||||
|
|
||||||
|
@inject(KwArg('a', provider1))
|
||||||
|
@inject(KwArg('b', 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_kwargs_priority(self):
|
||||||
|
"""Test `inject()` decorated callback kwargs priority."""
|
||||||
|
provider1 = NewInstance(object)
|
||||||
|
provider2 = NewInstance(list)
|
||||||
|
object_a = object()
|
||||||
|
|
||||||
|
@inject(KwArg('a', provider1))
|
||||||
|
@inject(KwArg('b', provider2))
|
||||||
|
def test(a, b):
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
a1, b1 = test(a=object_a)
|
||||||
|
a2, b2 = test(a=object_a)
|
||||||
|
|
||||||
|
self.assertIsInstance(a1, object)
|
||||||
|
self.assertIsInstance(a2, object)
|
||||||
|
self.assertIs(a1, object_a)
|
||||||
|
self.assertIs(a2, object_a)
|
||||||
|
|
||||||
|
self.assertIsInstance(b1, list)
|
||||||
|
self.assertIsInstance(b2, list)
|
||||||
|
self.assertIsNot(b1, b2)
|
||||||
|
|
||||||
|
def test_decorated_with_args(self):
|
||||||
|
"""Test `inject()` decorated callback with args."""
|
||||||
|
provider = NewInstance(list)
|
||||||
|
object_a = object()
|
||||||
|
|
||||||
|
@inject(KwArg('b', provider))
|
||||||
|
def test(a, b):
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
a1, b1 = test(object_a)
|
||||||
|
a2, b2 = test(object_a)
|
||||||
|
|
||||||
|
self.assertIsInstance(a1, object)
|
||||||
|
self.assertIsInstance(a2, object)
|
||||||
|
self.assertIs(a1, object_a)
|
||||||
|
self.assertIs(a2, object_a)
|
||||||
|
|
||||||
|
self.assertIsInstance(b1, list)
|
||||||
|
self.assertIsInstance(b2, list)
|
||||||
|
self.assertIsNot(b1, b2)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user