2015-08-31 16:31:38 +03:00
|
|
|
"""Dependency injector utils unittests."""
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
import unittest2 as unittest
|
2015-09-01 15:02:00 +03:00
|
|
|
import dependency_injector as di
|
2015-03-14 00:38:41 +03:00
|
|
|
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class IsProviderTests(unittest.TestCase):
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
"""`is_provider()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_provider(di.Provider()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_provider(di.Provider))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_provider('some_string'))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_provider(object()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
def test_with_subclass_instance(self):
|
|
|
|
"""Test with subclass of provider instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
class SomeProvider(di.Provider):
|
2015-07-22 10:53:16 +03:00
|
|
|
|
|
|
|
"""Some provider for test."""
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_provider(SomeProvider()))
|
2015-07-22 10:53:16 +03:00
|
|
|
|
|
|
|
def test_with_class_with_getattr(self):
|
|
|
|
"""Test with class that has __getattr__() method implementation."""
|
|
|
|
class SomeClass(object):
|
|
|
|
|
|
|
|
"""Some test class with __getattr__() method implementation."""
|
|
|
|
|
|
|
|
def __getattr__(self, _):
|
|
|
|
"""Test implementation that just returns False."""
|
|
|
|
return False
|
|
|
|
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_provider(SomeClass()))
|
2015-07-22 10:53:16 +03:00
|
|
|
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class EnsureIsProviderTests(unittest.TestCase):
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
"""`ensure_is_provider` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
provider = di.Provider()
|
|
|
|
self.assertIs(di.ensure_is_provider(provider), provider)
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.ensure_is_provider, di.Provider)
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.ensure_is_provider, 'some_string')
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.ensure_is_provider, object())
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class IsInjectionTests(unittest.TestCase):
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
"""`is_injection()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_injection(di.Injection('name', 'value')))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_subclass_instances(self):
|
|
|
|
"""Test with subclass instances."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_injection(di.KwArg('name', 'value')))
|
|
|
|
self.assertTrue(di.is_injection(di.Attribute('name', 'value')))
|
|
|
|
self.assertTrue(di.is_injection(di.Method('name', 'value')))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_injection(di.Injection))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_injection('some_string'))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_injection(object()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
class EnsureIsInjectionTests(unittest.TestCase):
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-03-23 02:04:18 +03:00
|
|
|
"""`ensure_is_injection` test cases."""
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
injection = di.Injection('name', 'value')
|
|
|
|
self.assertIs(di.ensure_is_injection(injection), injection)
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.ensure_is_injection, di.Injection)
|
2015-03-23 02:04:18 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.ensure_is_injection, 'some_string')
|
2015-03-23 02:04:18 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertRaises(di.Error, di.ensure_is_injection, object())
|
2015-03-23 02:04:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
class IsKwArgInjectionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""`is_kwarg_injection()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_kwarg_injection(di.KwArg('name', 'value')))
|
2015-03-23 02:04:18 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_kwarg_injection(di.KwArg))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_parent_class(self):
|
|
|
|
"""Test with parent class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_kwarg_injection(di.Injection))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_kwarg_injection('some_string'))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_kwarg_injection(object()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class IsAttributeInjectionTests(unittest.TestCase):
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
"""`is_attribute_injection()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_attribute_injection(di.Attribute('name',
|
|
|
|
'value')))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_attribute_injection(di.Attribute))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_parent_class(self):
|
|
|
|
"""Test with parent class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_attribute_injection(di.Injection))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_attribute_injection('some_string'))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_attribute_injection(object()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class IsMethodInjectionTests(unittest.TestCase):
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
"""`is_method_injection()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertTrue(di.is_method_injection(di.Method('name', 'value')))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_method_injection(di.Method))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_parent_class(self):
|
|
|
|
"""Test with parent class."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_method_injection(di.Injection))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_method_injection('some_string'))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-09-01 15:02:00 +03:00
|
|
|
self.assertFalse(di.is_method_injection(object()))
|