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-09-01 15:02:00 +03:00
|
|
|
import dependency_injector as di
|
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-09-01 15:02:00 +03:00
|
|
|
injection = di.Injection('some_arg_name', 'some_value')
|
2015-03-10 17:12:42 +03:00
|
|
|
self.assertEqual(injection.name, 'some_arg_name')
|
|
|
|
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-09-01 15:02:00 +03:00
|
|
|
injection = di.Injection('some_arg_name', '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-09-01 15:02:00 +03:00
|
|
|
injection = di.Injection('some_arg_name', di.Factory(object))
|
2015-03-12 01:10:51 +03:00
|
|
|
self.assertIsInstance(injection.value, object)
|
|
|
|
|
|
|
|
|
2015-03-23 13:19:58 +03:00
|
|
|
class KwArgTests(unittest.TestCase):
|
2015-03-12 01:10:51 +03:00
|
|
|
|
2015-03-23 13:19:58 +03:00
|
|
|
"""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-09-01 15:02:00 +03:00
|
|
|
injection = di.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-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-09-01 15:02:00 +03:00
|
|
|
injection = di.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-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-09-01 15:02:00 +03:00
|
|
|
injection = di.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
|
|
|
|
|
|
|
|
|
|
|
class InjectTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Inject decorator test cases."""
|
|
|
|
|
|
|
|
def test_decorated(self):
|
|
|
|
"""Test `inject()` decorated callback."""
|
2015-09-01 15:02:00 +03:00
|
|
|
provider1 = di.Factory(object)
|
|
|
|
provider2 = di.Factory(list)
|
2015-08-03 12:57:42 +03:00
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
@di.inject(a=provider1)
|
|
|
|
@di.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-09-01 15:02:00 +03:00
|
|
|
provider1 = di.Factory(object)
|
|
|
|
provider2 = di.Factory(list)
|
2015-08-03 12:57:42 +03:00
|
|
|
object_a = object()
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
@di.inject(a=provider1)
|
|
|
|
@di.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-09-01 15:02:00 +03:00
|
|
|
provider = di.Factory(list)
|
2015-08-03 12:57:42 +03:00
|
|
|
object_a = object()
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
@di.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-09-01 15:02:00 +03:00
|
|
|
provider = di.Factory(list)
|
2015-09-01 00:30:38 +03:00
|
|
|
object_a = object()
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
@di.inject(di.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)
|
|
|
|
|
|
|
|
def test_decorate_with_not_injection(self):
|
|
|
|
"""Test `inject()` decorator with not an injection instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.inject, object)
|