mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
adding utils tests, fixing of bug in utils with returning True for checked classes, not only instances
This commit is contained in:
parent
0aab7f5628
commit
ff4a092dab
|
@ -1,26 +1,33 @@
|
||||||
"""Utils module."""
|
"""Utils module."""
|
||||||
|
|
||||||
|
from inspect import isclass
|
||||||
|
|
||||||
|
|
||||||
def is_provider(instance):
|
def is_provider(instance):
|
||||||
"""Check if instance is provider instance."""
|
"""Check if instance is provider instance."""
|
||||||
return hasattr(instance, '__IS_OBJECTS_PROVIDER__')
|
return (not isclass(instance) and
|
||||||
|
hasattr(instance, '__IS_OBJECTS_PROVIDER__'))
|
||||||
|
|
||||||
|
|
||||||
def is_injection(instance):
|
def is_injection(instance):
|
||||||
"""Check if instance is injection instance."""
|
"""Check if instance is injection instance."""
|
||||||
return hasattr(instance, '__IS_OBJECTS_INJECTION__')
|
return (not isclass(instance) and
|
||||||
|
hasattr(instance, '__IS_OBJECTS_INJECTION__'))
|
||||||
|
|
||||||
|
|
||||||
def is_init_arg_injection(instance):
|
def is_init_arg_injection(instance):
|
||||||
"""Check if instance is init arg injection instance."""
|
"""Check if instance is init arg injection instance."""
|
||||||
return hasattr(instance, '__IS_OBJECTS_INIT_ARG_INJECTION__')
|
return (not isclass(instance) and
|
||||||
|
hasattr(instance, '__IS_OBJECTS_INIT_ARG_INJECTION__'))
|
||||||
|
|
||||||
|
|
||||||
def is_attribute_injection(instance):
|
def is_attribute_injection(instance):
|
||||||
"""Check if instance is attribute injection instance."""
|
"""Check if instance is attribute injection instance."""
|
||||||
return hasattr(instance, '__IS_OBJECTS_ATTRIBUTE_INJECTION__')
|
return (not isclass(instance) and
|
||||||
|
hasattr(instance, '__IS_OBJECTS_ATTRIBUTE_INJECTION__'))
|
||||||
|
|
||||||
|
|
||||||
def is_method_injection(instance):
|
def is_method_injection(instance):
|
||||||
"""Check if instance is method injection instance."""
|
"""Check if instance is method injection instance."""
|
||||||
return hasattr(instance, '__IS_OBJECTS_METHOD_INJECTION__')
|
return (not isclass(instance) and
|
||||||
|
hasattr(instance, '__IS_OBJECTS_METHOD_INJECTION__'))
|
||||||
|
|
139
tests/test_utils.py
Normal file
139
tests/test_utils.py
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
"""Objects utils unittests."""
|
||||||
|
|
||||||
|
import unittest2 as unittest
|
||||||
|
|
||||||
|
from objects.utils import is_provider
|
||||||
|
from objects.utils import is_injection
|
||||||
|
from objects.utils import is_init_arg_injection
|
||||||
|
from objects.utils import is_attribute_injection
|
||||||
|
from objects.utils import is_method_injection
|
||||||
|
|
||||||
|
from objects.providers import Provider
|
||||||
|
|
||||||
|
from objects.injections import Injection
|
||||||
|
from objects.injections import InitArg
|
||||||
|
from objects.injections import Attribute
|
||||||
|
from objects.injections import Method
|
||||||
|
|
||||||
|
|
||||||
|
class IsProviderTest(unittest.TestCase):
|
||||||
|
|
||||||
|
"""`is_provider()` test cases."""
|
||||||
|
|
||||||
|
def test_with_instance(self):
|
||||||
|
"""Test with instance."""
|
||||||
|
self.assertTrue(is_provider(Provider()))
|
||||||
|
|
||||||
|
def test_with_class(self):
|
||||||
|
"""Test with class."""
|
||||||
|
self.assertFalse(is_provider(Provider))
|
||||||
|
|
||||||
|
def test_with_string(self):
|
||||||
|
"""Test with string."""
|
||||||
|
self.assertFalse(is_provider('some_string'))
|
||||||
|
|
||||||
|
def test_with_object(self):
|
||||||
|
"""Test with object."""
|
||||||
|
self.assertFalse(is_provider(object()))
|
||||||
|
|
||||||
|
|
||||||
|
class IsInjectionTest(unittest.TestCase):
|
||||||
|
|
||||||
|
"""`is_injection()` test cases."""
|
||||||
|
|
||||||
|
def test_with_instance(self):
|
||||||
|
"""Test with instance."""
|
||||||
|
self.assertTrue(is_injection(Injection('name', 'value')))
|
||||||
|
|
||||||
|
def test_with_subclass_instances(self):
|
||||||
|
"""Test with subclass instances."""
|
||||||
|
self.assertTrue(is_injection(InitArg('name', 'value')))
|
||||||
|
self.assertTrue(is_injection(Attribute('name', 'value')))
|
||||||
|
self.assertTrue(is_injection(Method('name', 'value')))
|
||||||
|
|
||||||
|
def test_with_class(self):
|
||||||
|
"""Test with class."""
|
||||||
|
self.assertFalse(is_injection(Injection))
|
||||||
|
|
||||||
|
def test_with_string(self):
|
||||||
|
"""Test with string."""
|
||||||
|
self.assertFalse(is_injection('some_string'))
|
||||||
|
|
||||||
|
def test_with_object(self):
|
||||||
|
"""Test with object."""
|
||||||
|
self.assertFalse(is_injection(object()))
|
||||||
|
|
||||||
|
|
||||||
|
class IsInitArgInjectionTest(unittest.TestCase):
|
||||||
|
|
||||||
|
"""`is_init_arg_injection()` test cases."""
|
||||||
|
|
||||||
|
def test_with_instance(self):
|
||||||
|
"""Test with instance."""
|
||||||
|
self.assertTrue(is_init_arg_injection(InitArg('name', 'value')))
|
||||||
|
|
||||||
|
def test_with_class(self):
|
||||||
|
"""Test with class."""
|
||||||
|
self.assertFalse(is_init_arg_injection(InitArg))
|
||||||
|
|
||||||
|
def test_with_parent_class(self):
|
||||||
|
"""Test with parent class."""
|
||||||
|
self.assertFalse(is_init_arg_injection(Injection))
|
||||||
|
|
||||||
|
def test_with_string(self):
|
||||||
|
"""Test with string."""
|
||||||
|
self.assertFalse(is_init_arg_injection('some_string'))
|
||||||
|
|
||||||
|
def test_with_object(self):
|
||||||
|
"""Test with object."""
|
||||||
|
self.assertFalse(is_init_arg_injection(object()))
|
||||||
|
|
||||||
|
|
||||||
|
class IsAttributeInjectionTest(unittest.TestCase):
|
||||||
|
|
||||||
|
"""`is_attribute_injection()` test cases."""
|
||||||
|
|
||||||
|
def test_with_instance(self):
|
||||||
|
"""Test with instance."""
|
||||||
|
self.assertTrue(is_attribute_injection(Attribute('name', 'value')))
|
||||||
|
|
||||||
|
def test_with_class(self):
|
||||||
|
"""Test with class."""
|
||||||
|
self.assertFalse(is_attribute_injection(Attribute))
|
||||||
|
|
||||||
|
def test_with_parent_class(self):
|
||||||
|
"""Test with parent class."""
|
||||||
|
self.assertFalse(is_attribute_injection(Injection))
|
||||||
|
|
||||||
|
def test_with_string(self):
|
||||||
|
"""Test with string."""
|
||||||
|
self.assertFalse(is_attribute_injection('some_string'))
|
||||||
|
|
||||||
|
def test_with_object(self):
|
||||||
|
"""Test with object."""
|
||||||
|
self.assertFalse(is_attribute_injection(object()))
|
||||||
|
|
||||||
|
|
||||||
|
class IsMethodInjectionTest(unittest.TestCase):
|
||||||
|
|
||||||
|
"""`is_method_injection()` test cases."""
|
||||||
|
|
||||||
|
def test_with_instance(self):
|
||||||
|
"""Test with instance."""
|
||||||
|
self.assertTrue(is_method_injection(Method('name', 'value')))
|
||||||
|
|
||||||
|
def test_with_class(self):
|
||||||
|
"""Test with class."""
|
||||||
|
self.assertFalse(is_method_injection(Method))
|
||||||
|
|
||||||
|
def test_with_parent_class(self):
|
||||||
|
"""Test with parent class."""
|
||||||
|
self.assertFalse(is_method_injection(Injection))
|
||||||
|
|
||||||
|
def test_with_string(self):
|
||||||
|
"""Test with string."""
|
||||||
|
self.assertFalse(is_method_injection('some_string'))
|
||||||
|
|
||||||
|
def test_with_object(self):
|
||||||
|
"""Test with object."""
|
||||||
|
self.assertFalse(is_method_injection(object()))
|
Loading…
Reference in New Issue
Block a user