2015-03-12 10:01:56 +00:00
|
|
|
"""Objects utils unittests."""
|
|
|
|
|
|
|
|
import unittest2 as unittest
|
|
|
|
|
|
|
|
from objects.utils import is_provider
|
2015-03-13 21:38:41 +00:00
|
|
|
from objects.utils import ensure_is_provider
|
2015-03-12 10:01:56 +00:00
|
|
|
from objects.utils import is_injection
|
2015-03-22 23:04:18 +00:00
|
|
|
from objects.utils import ensure_is_injection
|
|
|
|
from objects.utils import is_kwarg_injection
|
2015-03-12 10:01:56 +00:00
|
|
|
from objects.utils import is_attribute_injection
|
|
|
|
from objects.utils import is_method_injection
|
|
|
|
|
|
|
|
from objects.providers import Provider
|
|
|
|
|
|
|
|
from objects.injections import Injection
|
2015-03-22 23:04:18 +00:00
|
|
|
from objects.injections import KwArg
|
2015-03-12 10:01:56 +00:00
|
|
|
from objects.injections import Attribute
|
|
|
|
from objects.injections import Method
|
|
|
|
|
2015-03-13 21:38:41 +00:00
|
|
|
from objects.errors import Error
|
|
|
|
|
2015-03-12 10:01:56 +00:00
|
|
|
|
2015-03-15 13:31:05 +00:00
|
|
|
class IsProviderTests(unittest.TestCase):
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
"""`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()))
|
|
|
|
|
|
|
|
|
2015-03-15 13:31:05 +00:00
|
|
|
class EnsureIsProviderTests(unittest.TestCase):
|
2015-03-13 21:38:41 +00:00
|
|
|
|
|
|
|
"""`ensure_is_provider` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
|
|
|
provider = Provider()
|
|
|
|
self.assertIs(ensure_is_provider(provider), provider)
|
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
|
|
|
self.assertRaises(Error, ensure_is_provider, Provider)
|
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
|
|
|
self.assertRaises(Error, ensure_is_provider, 'some_string')
|
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
|
|
|
self.assertRaises(Error, ensure_is_provider, object())
|
|
|
|
|
|
|
|
|
2015-03-15 13:31:05 +00:00
|
|
|
class IsInjectionTests(unittest.TestCase):
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
"""`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."""
|
2015-03-22 23:04:18 +00:00
|
|
|
self.assertTrue(is_injection(KwArg('name', 'value')))
|
2015-03-12 10:01:56 +00:00
|
|
|
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()))
|
|
|
|
|
|
|
|
|
2015-03-22 23:04:18 +00:00
|
|
|
class EnsureIsInjectionTests(unittest.TestCase):
|
2015-03-12 10:01:56 +00:00
|
|
|
|
2015-03-22 23:04:18 +00:00
|
|
|
"""`ensure_is_injection` test cases."""
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-03-22 23:04:18 +00:00
|
|
|
injection = Injection('name', 'value')
|
|
|
|
self.assertIs(ensure_is_injection(injection), injection)
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-03-22 23:04:18 +00:00
|
|
|
self.assertRaises(Error, ensure_is_injection, Injection)
|
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
|
|
|
self.assertRaises(Error, ensure_is_injection, 'some_string')
|
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
|
|
|
self.assertRaises(Error, ensure_is_injection, object())
|
|
|
|
|
|
|
|
|
|
|
|
class IsKwArgInjectionTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""`is_kwarg_injection()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
|
|
|
self.assertTrue(is_kwarg_injection(KwArg('name', 'value')))
|
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
|
|
|
self.assertFalse(is_kwarg_injection(KwArg))
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
def test_with_parent_class(self):
|
|
|
|
"""Test with parent class."""
|
2015-03-22 23:04:18 +00:00
|
|
|
self.assertFalse(is_kwarg_injection(Injection))
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-03-22 23:04:18 +00:00
|
|
|
self.assertFalse(is_kwarg_injection('some_string'))
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-03-22 23:04:18 +00:00
|
|
|
self.assertFalse(is_kwarg_injection(object()))
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
|
2015-03-15 13:31:05 +00:00
|
|
|
class IsAttributeInjectionTests(unittest.TestCase):
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
"""`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()))
|
|
|
|
|
|
|
|
|
2015-03-15 13:31:05 +00:00
|
|
|
class IsMethodInjectionTests(unittest.TestCase):
|
2015-03-12 10:01:56 +00:00
|
|
|
|
|
|
|
"""`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()))
|