python-dependency-injector/tests/test_utils.py

71 lines
2.2 KiB
Python
Raw Normal View History

2015-08-31 13:31:38 +00:00
"""Dependency injector utils unittests."""
import unittest2 as unittest
2015-11-23 19:45:58 +00:00
from dependency_injector import utils
from dependency_injector import providers
from dependency_injector import errors
class IsProviderTests(unittest.TestCase):
"""`is_provider()` test cases."""
def test_with_instance(self):
"""Test with instance."""
2015-11-23 19:45:58 +00:00
self.assertTrue(utils.is_provider(providers.Provider()))
def test_with_class(self):
"""Test with class."""
2015-11-23 19:45:58 +00:00
self.assertFalse(utils.is_provider(providers.Provider))
def test_with_string(self):
"""Test with string."""
2015-11-23 19:45:58 +00:00
self.assertFalse(utils.is_provider('some_string'))
def test_with_object(self):
"""Test with object."""
2015-11-23 19:45:58 +00:00
self.assertFalse(utils.is_provider(object()))
2015-07-22 07:53:16 +00:00
def test_with_subclass_instance(self):
"""Test with subclass of provider instance."""
2015-11-23 19:45:58 +00:00
class SomeProvider(providers.Provider):
2015-07-22 07:53:16 +00:00
"""Some provider for test."""
2015-11-23 19:45:58 +00:00
self.assertTrue(utils.is_provider(SomeProvider()))
2015-07-22 07:53:16 +00: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-11-23 19:45:58 +00:00
self.assertFalse(utils.is_provider(SomeClass()))
2015-07-22 07:53:16 +00:00
class EnsureIsProviderTests(unittest.TestCase):
"""`ensure_is_provider` test cases."""
def test_with_instance(self):
"""Test with instance."""
2015-11-23 19:45:58 +00:00
provider = providers.Provider()
self.assertIs(utils.ensure_is_provider(provider), provider)
def test_with_class(self):
"""Test with class."""
2015-11-23 19:45:58 +00:00
self.assertRaises(errors.Error,
utils.ensure_is_provider,
providers.Provider)
def test_with_string(self):
"""Test with string."""
2015-11-23 19:45:58 +00:00
self.assertRaises(errors.Error,
utils.ensure_is_provider,
'some_string')
def test_with_object(self):
"""Test with object."""
2015-11-23 19:45:58 +00:00
self.assertRaises(errors.Error, utils.ensure_is_provider, object())