python-dependency-injector/tests/test_utils.py

193 lines
5.8 KiB
Python
Raw Normal View History

2015-08-31 16:31:38 +03:00
"""Dependency injector utils unittests."""
import unittest2 as unittest
2015-09-01 15:02:00 +03:00
import dependency_injector as di
class IsProviderTests(unittest.TestCase):
"""`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()))
def test_with_class(self):
"""Test with class."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_provider(di.Provider))
def test_with_string(self):
"""Test with string."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_provider('some_string'))
def test_with_object(self):
"""Test with object."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_provider(object()))
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
class EnsureIsProviderTests(unittest.TestCase):
"""`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)
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)
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')
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())
class IsInjectionTests(unittest.TestCase):
"""`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')))
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')))
def test_with_class(self):
"""Test with class."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_injection(di.Injection))
def test_with_string(self):
"""Test with string."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_injection('some_string'))
def test_with_object(self):
"""Test with object."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_injection(object()))
class EnsureIsInjectionTests(unittest.TestCase):
"""`ensure_is_injection` test cases."""
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)
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)
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')
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())
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')))
def test_with_class(self):
"""Test with class."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_kwarg_injection(di.KwArg))
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))
def test_with_string(self):
"""Test with string."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_kwarg_injection('some_string'))
def test_with_object(self):
"""Test with object."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_kwarg_injection(object()))
class IsAttributeInjectionTests(unittest.TestCase):
"""`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')))
def test_with_class(self):
"""Test with class."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_attribute_injection(di.Attribute))
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))
def test_with_string(self):
"""Test with string."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_attribute_injection('some_string'))
def test_with_object(self):
"""Test with object."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_attribute_injection(object()))
class IsMethodInjectionTests(unittest.TestCase):
"""`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')))
def test_with_class(self):
"""Test with class."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_method_injection(di.Method))
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))
def test_with_string(self):
"""Test with string."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_method_injection('some_string'))
def test_with_object(self):
"""Test with object."""
2015-09-01 15:02:00 +03:00
self.assertFalse(di.is_method_injection(object()))