Adding tests for inject() decorator

This commit is contained in:
Roman Mogilatov 2015-03-23 12:19:58 +02:00
parent 1032143f5b
commit b0c2005288
2 changed files with 76 additions and 2 deletions

View File

@ -1,5 +1,7 @@
"""Injections module."""
from six import wraps
from .utils import is_provider
from .utils import ensure_is_injection
@ -58,6 +60,7 @@ def inject(injection):
def decorator(callback):
"""Decorator."""
@wraps(callback)
def decorated(*args, **kwargs):
"""Decorated."""
if injection.name not in kwargs:

View File

@ -6,6 +6,7 @@ from objects.injections import Injection
from objects.injections import KwArg
from objects.injections import Attribute
from objects.injections import Method
from objects.injections import inject
from objects.providers import NewInstance
@ -31,9 +32,9 @@ class InjectionTests(unittest.TestCase):
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):
"""Test KwArg creation and initialization."""
@ -62,3 +63,73 @@ class MethodTests(unittest.TestCase):
injection = Method('some_arg_name', 'some_value')
self.assertEqual(injection.name, 'some_arg_name')
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)