Refactoring of utils checks

This commit is contained in:
Roman Mogilatov 2015-07-22 10:53:16 +03:00
parent 1b4c6dbf8b
commit 541d3c0e4b
4 changed files with 30 additions and 10 deletions

View File

@ -7,7 +7,7 @@ class Injection(object):
"""Base injection class."""
__IS_OBJECTS_INJECTION__ = True
__IS_INJECTION__ = True
__slots__ = ('name', 'injectable')
def __init__(self, name, injectable):
@ -27,18 +27,18 @@ class KwArg(Injection):
"""Keyword argument injection."""
__IS_OBJECTS_KWARG_INJECTION__ = True
__IS_KWARG_INJECTION__ = True
class Attribute(Injection):
"""Attribute injection."""
__IS_OBJECTS_ATTRIBUTE_INJECTION__ = True
__IS_ATTRIBUTE_INJECTION__ = True
class Method(Injection):
"""Method injection."""
__IS_OBJECTS_METHOD_INJECTION__ = True
__IS_METHOD_INJECTION__ = True

View File

@ -14,7 +14,7 @@ class Provider(object):
"""Base provider class."""
__IS_OBJECTS_PROVIDER__ = True
__IS_PROVIDER__ = True
__slots__ = ('overridden',)
def __init__(self):

View File

@ -8,7 +8,7 @@ from .errors import Error
def is_provider(instance):
"""Check if instance is provider instance."""
return (not isinstance(instance, class_types) and
hasattr(instance, '__IS_OBJECTS_PROVIDER__'))
getattr(instance, '__IS_PROVIDER__', False) is True)
def ensure_is_provider(instance):
@ -22,7 +22,7 @@ def ensure_is_provider(instance):
def is_injection(instance):
"""Check if instance is injection instance."""
return (not isinstance(instance, class_types) and
hasattr(instance, '__IS_OBJECTS_INJECTION__'))
getattr(instance, '__IS_INJECTION__', False) is True)
def ensure_is_injection(instance):
@ -36,16 +36,16 @@ def ensure_is_injection(instance):
def is_kwarg_injection(instance):
"""Check if instance is keyword argument injection instance."""
return (not isinstance(instance, class_types) and
hasattr(instance, '__IS_OBJECTS_KWARG_INJECTION__'))
getattr(instance, '__IS_KWARG_INJECTION__', False) is True)
def is_attribute_injection(instance):
"""Check if instance is attribute injection instance."""
return (not isinstance(instance, class_types) and
hasattr(instance, '__IS_OBJECTS_ATTRIBUTE_INJECTION__'))
getattr(instance, '__IS_ATTRIBUTE_INJECTION__', False) is True)
def is_method_injection(instance):
"""Check if instance is method injection instance."""
return (not isinstance(instance, class_types) and
hasattr(instance, '__IS_OBJECTS_METHOD_INJECTION__'))
getattr(instance, '__IS_METHOD_INJECTION__', False) is True)

View File

@ -40,6 +40,26 @@ class IsProviderTests(unittest.TestCase):
"""Test with object."""
self.assertFalse(is_provider(object()))
def test_with_subclass_instance(self):
"""Test with subclass of provider instance."""
class SomeProvider(Provider):
"""Some provider for test."""
self.assertTrue(is_provider(SomeProvider()))
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
self.assertFalse(is_provider(SomeClass()))
class EnsureIsProviderTests(unittest.TestCase):