2015-08-31 16:31:38 +03:00
|
|
|
"""Dependency injector utils unittests."""
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
import unittest2 as unittest
|
2015-11-23 22:45:58 +03:00
|
|
|
|
|
|
|
from dependency_injector import utils
|
|
|
|
from dependency_injector import providers
|
|
|
|
from dependency_injector import catalogs
|
|
|
|
from dependency_injector import errors
|
2015-03-14 00:38:41 +03:00
|
|
|
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class IsProviderTests(unittest.TestCase):
|
2015-03-12 13:01:56 +03:00
|
|
|
"""`is_provider()` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.is_provider(providers.Provider()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_provider(providers.Provider))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_provider('some_string'))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_provider(object()))
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-07-22 10:53:16 +03:00
|
|
|
def test_with_subclass_instance(self):
|
|
|
|
"""Test with subclass of provider instance."""
|
2015-11-23 22:45:58 +03:00
|
|
|
class SomeProvider(providers.Provider):
|
2015-07-22 10:53:16 +03:00
|
|
|
"""Some provider for test."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.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-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_provider(SomeClass()))
|
2015-07-22 10:53:16 +03:00
|
|
|
|
2015-03-12 13:01:56 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class EnsureIsProviderTests(unittest.TestCase):
|
2015-03-14 00:38:41 +03:00
|
|
|
"""`ensure_is_provider` test cases."""
|
|
|
|
|
|
|
|
def test_with_instance(self):
|
|
|
|
"""Test with instance."""
|
2015-11-23 22:45:58 +03:00
|
|
|
provider = providers.Provider()
|
|
|
|
self.assertIs(utils.ensure_is_provider(provider), provider)
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
def test_with_class(self):
|
|
|
|
"""Test with class."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error,
|
|
|
|
utils.ensure_is_provider,
|
|
|
|
providers.Provider)
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error,
|
|
|
|
utils.ensure_is_provider,
|
|
|
|
'some_string')
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertRaises(errors.Error, utils.ensure_is_provider, object())
|
2015-03-14 00:38:41 +03:00
|
|
|
|
|
|
|
|
2015-10-07 13:36:28 +03:00
|
|
|
class IsCatalogTests(unittest.TestCase):
|
|
|
|
"""`is_catalog()` test cases."""
|
|
|
|
|
2015-11-10 20:38:18 +03:00
|
|
|
def test_with_declarative_catalog(self):
|
2015-10-07 13:36:28 +03:00
|
|
|
"""Test with class."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.is_catalog(catalogs.DeclarativeCatalog))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
2015-11-10 20:38:18 +03:00
|
|
|
def test_with_dynamic_catalog(self):
|
2015-10-07 13:36:28 +03:00
|
|
|
"""Test with class."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.is_catalog(catalogs.DynamicCatalog()))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_with_child_class(self):
|
|
|
|
"""Test with parent class."""
|
2015-11-23 22:45:58 +03:00
|
|
|
class Catalog(catalogs.DeclarativeCatalog):
|
2015-10-07 13:36:28 +03:00
|
|
|
"""Example catalog child class."""
|
|
|
|
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.is_catalog(Catalog))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_with_string(self):
|
|
|
|
"""Test with string."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_catalog('some_string'))
|
2015-10-07 13:36:28 +03:00
|
|
|
|
|
|
|
def test_with_object(self):
|
|
|
|
"""Test with object."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_catalog(object()))
|
2015-10-19 12:12:38 +03:00
|
|
|
|
|
|
|
|
2015-11-10 20:38:18 +03:00
|
|
|
class IsDynamicCatalogTests(unittest.TestCase):
|
|
|
|
"""`is_dynamic_catalog()` test cases."""
|
|
|
|
|
|
|
|
def test_with_declarative_catalog(self):
|
|
|
|
"""Test with declarative catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_dynamic_catalog(catalogs.DeclarativeCatalog))
|
2015-11-10 20:38:18 +03:00
|
|
|
|
|
|
|
def test_with_dynamic_catalog(self):
|
|
|
|
"""Test with dynamic catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.is_dynamic_catalog(catalogs.DynamicCatalog()))
|
2015-11-10 20:38:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
class IsDeclarativeCatalogTests(unittest.TestCase):
|
|
|
|
"""`is_declarative_catalog()` test cases."""
|
|
|
|
|
|
|
|
def test_with_declarative_catalog(self):
|
|
|
|
"""Test with declarative catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertTrue(utils.is_declarative_catalog(
|
|
|
|
catalogs.DeclarativeCatalog))
|
2015-11-10 20:38:18 +03:00
|
|
|
|
|
|
|
def test_with_dynamic_catalog(self):
|
|
|
|
"""Test with dynamic catalog."""
|
2015-11-23 22:45:58 +03:00
|
|
|
self.assertFalse(utils.is_declarative_catalog(
|
|
|
|
catalogs.DynamicCatalog()))
|