2015-08-31 16:31:38 +03:00
|
|
|
"""Dependency injector injections unittests."""
|
2015-03-10 17:12:42 +03:00
|
|
|
|
|
|
|
import unittest2 as unittest
|
2015-11-23 22:45:58 +03:00
|
|
|
|
|
|
|
from dependency_injector import injections
|
|
|
|
from dependency_injector import catalogs
|
|
|
|
from dependency_injector import providers
|
|
|
|
from dependency_injector import errors
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-03-10 17:12:42 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class InjectionTests(unittest.TestCase):
|
2015-03-10 17:12:42 +03:00
|
|
|
"""Injection test cases."""
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""Test Injection creation and initialization."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.Injection('some_value')
|
2015-03-10 17:12:42 +03:00
|
|
|
self.assertEqual(injection.injectable, 'some_value')
|
2015-03-11 01:23:26 +03:00
|
|
|
|
|
|
|
def test_value_with_scalar_injectable(self):
|
2015-03-12 01:10:51 +03:00
|
|
|
"""Test Injection value property with scalar value."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.Injection('some_value')
|
2015-03-11 01:23:26 +03:00
|
|
|
self.assertEqual(injection.value, 'some_value')
|
2015-03-12 01:10:51 +03:00
|
|
|
|
|
|
|
def test_value_with_provider_injectable(self):
|
|
|
|
"""Test Injection value property with provider."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.Injection(providers.Factory(object))
|
2015-03-12 01:10:51 +03:00
|
|
|
self.assertIsInstance(injection.value, object)
|
|
|
|
|
2015-10-19 10:47:24 +03:00
|
|
|
def test_value_with_catalog_bundle_injectable(self):
|
|
|
|
"""Test Injection value property with catalog bundle."""
|
2015-11-23 22:45:58 +03:00
|
|
|
class TestCatalog(catalogs.DeclarativeCatalog):
|
2015-10-19 10:47:24 +03:00
|
|
|
"""Test catalog."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
provider = providers.Provider()
|
|
|
|
injection = injections.Injection(
|
|
|
|
TestCatalog.Bundle(TestCatalog.provider))
|
2015-10-19 10:47:24 +03:00
|
|
|
|
|
|
|
self.assertIsInstance(injection.value, TestCatalog.Bundle)
|
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def test_repr(self):
|
|
|
|
"""Test Injection representation."""
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = injections.Injection(provider)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(injection),
|
|
|
|
'<dependency_injector.injections.Injection({0}) at {1}>'.format(
|
|
|
|
repr(provider),
|
|
|
|
hex(id(injection))))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
2015-03-12 01:10:51 +03:00
|
|
|
|
2015-10-14 14:30:01 +03:00
|
|
|
class ArgTests(unittest.TestCase):
|
|
|
|
"""Positional arg injection test cases."""
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""Test Arg creation and initialization."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.Arg('some_value')
|
2015-10-14 14:30:01 +03:00
|
|
|
self.assertEqual(injection.injectable, 'some_value')
|
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def test_repr(self):
|
|
|
|
"""Test Arg representation."""
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = injections.Arg(provider)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(injection),
|
|
|
|
'<dependency_injector.injections.Arg({0}) at {1}>'.format(
|
|
|
|
repr(provider),
|
|
|
|
hex(id(injection))))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
2015-10-14 14:30:01 +03:00
|
|
|
|
2015-03-23 13:19:58 +03:00
|
|
|
class KwArgTests(unittest.TestCase):
|
|
|
|
"""Keyword arg injection test cases."""
|
2015-03-12 01:10:51 +03:00
|
|
|
|
|
|
|
def test_init(self):
|
2015-03-23 02:04:18 +03:00
|
|
|
"""Test KwArg creation and initialization."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.KwArg('some_arg_name', 'some_value')
|
2015-03-12 01:10:51 +03:00
|
|
|
self.assertEqual(injection.name, 'some_arg_name')
|
|
|
|
self.assertEqual(injection.injectable, 'some_value')
|
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def test_repr(self):
|
|
|
|
"""Test KwArg representation."""
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = injections.KwArg('name', provider)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(injection),
|
|
|
|
'<dependency_injector.injections.KwArg({0}, {1}) at {2}>'.format(
|
|
|
|
repr('name'),
|
|
|
|
repr(provider),
|
|
|
|
hex(id(injection))))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
2015-03-12 01:10:51 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class AttributeTests(unittest.TestCase):
|
2015-03-12 01:10:51 +03:00
|
|
|
"""Attribute injection test cases."""
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""Test Attribute creation and initialization."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.Attribute('some_arg_name', 'some_value')
|
2015-03-12 01:10:51 +03:00
|
|
|
self.assertEqual(injection.name, 'some_arg_name')
|
|
|
|
self.assertEqual(injection.injectable, 'some_value')
|
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def test_repr(self):
|
|
|
|
"""Test Attribute representation."""
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = injections.Attribute('name', provider)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(injection),
|
|
|
|
'<dependency_injector.injections.Attribute({0}, {1}) '
|
|
|
|
'at {2}>'.format(
|
|
|
|
repr('name'),
|
|
|
|
repr(provider),
|
|
|
|
hex(id(injection))))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
2015-03-12 01:10:51 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class MethodTests(unittest.TestCase):
|
2015-03-12 01:10:51 +03:00
|
|
|
"""Method injection test cases."""
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""Test Method creation and initialization."""
|
2015-11-23 22:45:58 +03:00
|
|
|
injection = injections.Method('some_arg_name', 'some_value')
|
2015-03-12 01:10:51 +03:00
|
|
|
self.assertEqual(injection.name, 'some_arg_name')
|
|
|
|
self.assertEqual(injection.injectable, 'some_value')
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-12-09 20:28:52 +03:00
|
|
|
def test_repr(self):
|
|
|
|
"""Test Method representation."""
|
|
|
|
provider = providers.Factory(object)
|
|
|
|
injection = injections.Method('name', provider)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(injection),
|
|
|
|
'<dependency_injector.injections.Method({0}, {1}) at {2}>'.format(
|
|
|
|
repr('name'),
|
|
|
|
repr(provider),
|
|
|
|
hex(id(injection))))
|
2015-12-07 15:31:51 +03:00
|
|
|
|
2015-08-03 12:57:42 +03:00
|
|
|
|
|
|
|
class InjectTests(unittest.TestCase):
|
|
|
|
"""Inject decorator test cases."""
|
|
|
|
|
2015-10-23 09:53:53 +03:00
|
|
|
def test_decorated_args(self):
|
|
|
|
"""Test `inject()` decoration with args."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider1 = providers.Factory(object)
|
|
|
|
provider2 = providers.Factory(list)
|
2015-10-23 09:53:53 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(provider1, provider2)
|
2015-10-23 09:53:53 +03:00
|
|
|
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."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider1 = providers.Factory(object)
|
|
|
|
provider2 = providers.Factory(list)
|
2015-10-23 09:53:53 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(injections.Arg(provider1),
|
|
|
|
injections.Arg(provider2))
|
2015-10-23 09:53:53 +03:00
|
|
|
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."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider1 = providers.Factory(object)
|
|
|
|
provider2 = providers.Factory(list)
|
2015-10-23 09:53:53 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(provider2)
|
|
|
|
@injections.inject(provider1)
|
2015-10-23 09:53:53 +03:00
|
|
|
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."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider1 = providers.Factory(object)
|
|
|
|
provider2 = providers.Factory(list)
|
2015-10-23 09:53:53 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(provider1)
|
2015-10-23 09:53:53 +03:00
|
|
|
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."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider1 = providers.Factory(object)
|
|
|
|
provider2 = providers.Factory(list)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(a=provider1)
|
|
|
|
@injections.inject(b=provider2)
|
2015-08-03 12:57:42 +03:00
|
|
|
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."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider1 = providers.Factory(object)
|
|
|
|
provider2 = providers.Factory(list)
|
2015-08-03 12:57:42 +03:00
|
|
|
object_a = object()
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(a=provider1)
|
|
|
|
@injections.inject(b=provider2)
|
2015-08-03 12:57:42 +03:00
|
|
|
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."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider = providers.Factory(list)
|
2015-08-03 12:57:42 +03:00
|
|
|
object_a = object()
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(b=provider)
|
2015-09-01 00:30:38 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
def test_injection_kwarg_syntax(self):
|
|
|
|
"""Test `inject()` decorated callback with "old" style using KwArg."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider = providers.Factory(list)
|
2015-09-01 00:30:38 +03:00
|
|
|
object_a = object()
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(injections.KwArg('b', provider))
|
2015-08-03 12:57:42 +03:00
|
|
|
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)
|
|
|
|
|
2015-09-28 14:19:22 +03:00
|
|
|
def test_decorate_class_method(self):
|
|
|
|
"""Test `inject()` decorator with class method."""
|
|
|
|
class Test(object):
|
|
|
|
"""Test class."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(arg1=123)
|
|
|
|
@injections.inject(arg2=456)
|
2015-09-28 14:19:22 +03:00
|
|
|
def some_method(self, arg1, arg2):
|
|
|
|
"""Some test method."""
|
|
|
|
return arg1, arg2
|
|
|
|
|
|
|
|
test_object = Test()
|
|
|
|
arg1, arg2 = test_object.some_method()
|
|
|
|
|
|
|
|
self.assertEquals(arg1, 123)
|
|
|
|
self.assertEquals(arg2, 456)
|
|
|
|
|
|
|
|
def test_decorate_class_with_init(self):
|
|
|
|
"""Test `inject()` decorator that decorate class with __init__."""
|
2015-11-23 22:45:58 +03:00
|
|
|
@injections.inject(arg1=123)
|
|
|
|
@injections.inject(arg2=456)
|
2015-09-28 14:19:22 +03:00
|
|
|
class Test(object):
|
|
|
|
"""Test class."""
|
|
|
|
|
|
|
|
def __init__(self, arg1, arg2):
|
|
|
|
"""Init."""
|
|
|
|
self.arg1 = arg1
|
|
|
|
self.arg2 = arg2
|
|
|
|
|
|
|
|
test_object = Test()
|
|
|
|
|
|
|
|
self.assertEquals(test_object.arg1, 123)
|
|
|
|
self.assertEquals(test_object.arg2, 456)
|
|
|
|
|
|
|
|
def test_decorate_class_without_init(self):
|
|
|
|
"""Test `inject()` decorator that decorate class without __init__."""
|
2015-11-23 22:45:58 +03:00
|
|
|
with self.assertRaises(errors.Error):
|
|
|
|
@injections.inject(arg1=123)
|
2015-09-28 14:19:22 +03:00
|
|
|
class Test(object):
|
|
|
|
"""Test class."""
|