Update test imports

This commit is contained in:
Roman Mogilatov 2015-09-01 15:02:00 +03:00
parent 4fb3a12770
commit ad2072688b
4 changed files with 171 additions and 221 deletions

View File

@ -1,26 +1,19 @@
"""Dependency injector catalog unittests.""" """Dependency injector catalog unittests."""
import unittest2 as unittest import unittest2 as unittest
import dependency_injector as di
from dependency_injector.catalog import AbstractCatalog
from dependency_injector.catalog import override
from dependency_injector.providers import Object
from dependency_injector.providers import Value
from dependency_injector.errors import Error
class CatalogTests(unittest.TestCase): class CatalogTests(unittest.TestCase):
"""Catalog test cases.""" """Catalog test cases."""
class Catalog(AbstractCatalog): class Catalog(di.AbstractCatalog):
"""Test catalog.""" """Test catalog."""
obj = Object(object()) obj = di.Object(object())
another_obj = Object(object()) another_obj = di.Object(object())
def test_get_used(self): def test_get_used(self):
"""Test retrieving used provider.""" """Test retrieving used provider."""
@ -30,7 +23,7 @@ class CatalogTests(unittest.TestCase):
def test_get_unused(self): def test_get_unused(self):
"""Test retrieving unused provider.""" """Test retrieving unused provider."""
catalog = self.Catalog() catalog = self.Catalog()
self.assertRaises(Error, getattr, catalog, 'obj') self.assertRaises(di.Error, getattr, catalog, 'obj')
def test_all_providers(self): def test_all_providers(self):
"""Test getting of all catalog providers.""" """Test getting of all catalog providers."""
@ -45,22 +38,22 @@ class CatalogTests(unittest.TestCase):
def test_all_providers_by_type(self): def test_all_providers_by_type(self):
"""Test getting of all catalog providers of specific type.""" """Test getting of all catalog providers of specific type."""
self.assertTrue(len(self.Catalog.filter(Object)) == 2) self.assertTrue(len(self.Catalog.filter(di.Object)) == 2)
self.assertTrue(len(self.Catalog.filter(Value)) == 0) self.assertTrue(len(self.Catalog.filter(di.Value)) == 0)
def test_metaclass_with_several_catalogs(self): def test_metaclass_with_several_catalogs(self):
"""Test that metaclass work well with several catalogs.""" """Test that metaclass work well with several catalogs."""
class Catalog1(AbstractCatalog): class Catalog1(di.AbstractCatalog):
"""Catalog1.""" """Catalog1."""
provider = Object(object()) provider = di.Object(object())
class Catalog2(AbstractCatalog): class Catalog2(di.AbstractCatalog):
"""Catalog2.""" """Catalog2."""
provider = Object(object()) provider = di.Object(object())
self.assertTrue(len(Catalog1.providers) == 1) self.assertTrue(len(Catalog1.providers) == 1)
self.assertIs(Catalog1.provider, Catalog1.providers['provider']) self.assertIs(Catalog1.provider, Catalog1.providers['provider'])
@ -75,22 +68,22 @@ class OverrideTests(unittest.TestCase):
"""Override decorator test cases.""" """Override decorator test cases."""
class Catalog(AbstractCatalog): class Catalog(di.AbstractCatalog):
"""Test catalog.""" """Test catalog."""
obj = Object(object()) obj = di.Object(object())
another_obj = Object(object()) another_obj = di.Object(object())
def test_overriding(self): def test_overriding(self):
"""Test catalog overriding with another catalog.""" """Test catalog overriding with another catalog."""
@override(self.Catalog) @di.override(self.Catalog)
class OverridingCatalog(self.Catalog): class OverridingCatalog(self.Catalog):
"""Overriding catalog.""" """Overriding catalog."""
obj = Value(1) obj = di.Value(1)
another_obj = Value(2) another_obj = di.Value(2)
self.assertEqual(self.Catalog.obj(), 1) self.assertEqual(self.Catalog.obj(), 1)
self.assertEqual(self.Catalog.another_obj(), 2) self.assertEqual(self.Catalog.another_obj(), 2)

View File

@ -1,16 +1,7 @@
"""Dependency injector injections unittests.""" """Dependency injector injections unittests."""
import unittest2 as unittest import unittest2 as unittest
import dependency_injector as di
from dependency_injector.injections import Injection
from dependency_injector.injections import KwArg
from dependency_injector.injections import Attribute
from dependency_injector.injections import Method
from dependency_injector.injections import inject
from dependency_injector.providers import Factory
from dependency_injector.errors import Error
class InjectionTests(unittest.TestCase): class InjectionTests(unittest.TestCase):
@ -19,18 +10,18 @@ class InjectionTests(unittest.TestCase):
def test_init(self): def test_init(self):
"""Test Injection creation and initialization.""" """Test Injection creation and initialization."""
injection = Injection('some_arg_name', 'some_value') injection = di.Injection('some_arg_name', 'some_value')
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
def test_value_with_scalar_injectable(self): def test_value_with_scalar_injectable(self):
"""Test Injection value property with scalar value.""" """Test Injection value property with scalar value."""
injection = Injection('some_arg_name', 'some_value') injection = di.Injection('some_arg_name', 'some_value')
self.assertEqual(injection.value, 'some_value') self.assertEqual(injection.value, 'some_value')
def test_value_with_provider_injectable(self): def test_value_with_provider_injectable(self):
"""Test Injection value property with provider.""" """Test Injection value property with provider."""
injection = Injection('some_arg_name', Factory(object)) injection = di.Injection('some_arg_name', di.Factory(object))
self.assertIsInstance(injection.value, object) self.assertIsInstance(injection.value, object)
@ -40,7 +31,7 @@ class KwArgTests(unittest.TestCase):
def test_init(self): def test_init(self):
"""Test KwArg creation and initialization.""" """Test KwArg creation and initialization."""
injection = KwArg('some_arg_name', 'some_value') injection = di.KwArg('some_arg_name', 'some_value')
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
@ -51,7 +42,7 @@ class AttributeTests(unittest.TestCase):
def test_init(self): def test_init(self):
"""Test Attribute creation and initialization.""" """Test Attribute creation and initialization."""
injection = Attribute('some_arg_name', 'some_value') injection = di.Attribute('some_arg_name', 'some_value')
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
@ -62,7 +53,7 @@ class MethodTests(unittest.TestCase):
def test_init(self): def test_init(self):
"""Test Method creation and initialization.""" """Test Method creation and initialization."""
injection = Method('some_arg_name', 'some_value') injection = di.Method('some_arg_name', 'some_value')
self.assertEqual(injection.name, 'some_arg_name') self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value') self.assertEqual(injection.injectable, 'some_value')
@ -73,11 +64,11 @@ class InjectTests(unittest.TestCase):
def test_decorated(self): def test_decorated(self):
"""Test `inject()` decorated callback.""" """Test `inject()` decorated callback."""
provider1 = Factory(object) provider1 = di.Factory(object)
provider2 = Factory(list) provider2 = di.Factory(list)
@inject(a=provider1) @di.inject(a=provider1)
@inject(b=provider2) @di.inject(b=provider2)
def test(a, b): def test(a, b):
return a, b return a, b
@ -94,12 +85,12 @@ class InjectTests(unittest.TestCase):
def test_decorated_kwargs_priority(self): def test_decorated_kwargs_priority(self):
"""Test `inject()` decorated callback kwargs priority.""" """Test `inject()` decorated callback kwargs priority."""
provider1 = Factory(object) provider1 = di.Factory(object)
provider2 = Factory(list) provider2 = di.Factory(list)
object_a = object() object_a = object()
@inject(a=provider1) @di.inject(a=provider1)
@inject(b=provider2) @di.inject(b=provider2)
def test(a, b): def test(a, b):
return a, b return a, b
@ -117,10 +108,10 @@ class InjectTests(unittest.TestCase):
def test_decorated_with_args(self): def test_decorated_with_args(self):
"""Test `inject()` decorated callback with args.""" """Test `inject()` decorated callback with args."""
provider = Factory(list) provider = di.Factory(list)
object_a = object() object_a = object()
@inject(b=provider) @di.inject(b=provider)
def test(a, b): def test(a, b):
return a, b return a, b
@ -138,10 +129,10 @@ class InjectTests(unittest.TestCase):
def test_injection_kwarg_syntax(self): def test_injection_kwarg_syntax(self):
"""Test `inject()` decorated callback with "old" style using KwArg.""" """Test `inject()` decorated callback with "old" style using KwArg."""
provider = Factory(list) provider = di.Factory(list)
object_a = object() object_a = object()
@inject(KwArg('b', provider)) @di.inject(di.KwArg('b', provider))
def test(a, b): def test(a, b):
return a, b return a, b
@ -159,4 +150,4 @@ class InjectTests(unittest.TestCase):
def test_decorate_with_not_injection(self): def test_decorate_with_not_injection(self):
"""Test `inject()` decorator with not an injection instance.""" """Test `inject()` decorator with not an injection instance."""
self.assertRaises(Error, inject, object) self.assertRaises(di.Error, di.inject, object)

View File

@ -1,26 +1,7 @@
"""Dependency injector providers unittests.""" """Dependency injector providers unittests."""
import unittest2 as unittest import unittest2 as unittest
import dependency_injector as di
from dependency_injector.providers import Provider
from dependency_injector.providers import Delegate
from dependency_injector.providers import Factory
from dependency_injector.providers import Singleton
from dependency_injector.providers import ExternalDependency
from dependency_injector.providers import Class
from dependency_injector.providers import Object
from dependency_injector.providers import Function
from dependency_injector.providers import Value
from dependency_injector.providers import Callable
from dependency_injector.providers import Config
from dependency_injector.injections import KwArg
from dependency_injector.injections import Attribute
from dependency_injector.injections import Method
from dependency_injector.utils import is_provider
from dependency_injector.errors import Error
class ProviderTests(unittest.TestCase): class ProviderTests(unittest.TestCase):
@ -29,11 +10,11 @@ class ProviderTests(unittest.TestCase):
def setUp(self): def setUp(self):
"""Set test cases environment up.""" """Set test cases environment up."""
self.provider = Provider() self.provider = di.Provider()
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(self.provider)) self.assertTrue(di.is_provider(self.provider))
def test_call(self): def test_call(self):
"""Test call.""" """Test call."""
@ -43,30 +24,30 @@ class ProviderTests(unittest.TestCase):
"""Test creating of provider delegation.""" """Test creating of provider delegation."""
delegate1 = self.provider.delegate() delegate1 = self.provider.delegate()
self.assertIsInstance(delegate1, Delegate) self.assertIsInstance(delegate1, di.Delegate)
self.assertIs(delegate1(), self.provider) self.assertIs(delegate1(), self.provider)
delegate2 = self.provider.delegate() delegate2 = self.provider.delegate()
self.assertIsInstance(delegate2, Delegate) self.assertIsInstance(delegate2, di.Delegate)
self.assertIs(delegate2(), self.provider) self.assertIs(delegate2(), self.provider)
self.assertIsNot(delegate1, delegate2) self.assertIsNot(delegate1, delegate2)
def test_override(self): def test_override(self):
"""Test provider overriding.""" """Test provider overriding."""
overriding_provider = Provider() overriding_provider = di.Provider()
self.provider.override(overriding_provider) self.provider.override(overriding_provider)
self.assertTrue(self.provider.is_overridden) self.assertTrue(self.provider.is_overridden)
def test_override_with_not_provider(self): def test_override_with_not_provider(self):
"""Test provider overriding with not provider instance.""" """Test provider overriding with not provider instance."""
self.assertRaises(Error, self.provider.override, object()) self.assertRaises(di.Error, self.provider.override, object())
def test_last_overriding(self): def test_last_overriding(self):
"""Test getting last overriding provider.""" """Test getting last overriding provider."""
overriding_provider1 = Provider() overriding_provider1 = di.Provider()
overriding_provider2 = Provider() overriding_provider2 = di.Provider()
self.provider.override(overriding_provider1) self.provider.override(overriding_provider1)
self.assertIs(self.provider.last_overriding, overriding_provider1) self.assertIs(self.provider.last_overriding, overriding_provider1)
@ -78,7 +59,7 @@ class ProviderTests(unittest.TestCase):
"""Test getting last overriding from not overridden provider.""" """Test getting last overriding from not overridden provider."""
try: try:
self.provider.last_overriding self.provider.last_overriding
except Error: except di.Error:
pass pass
else: else:
self.fail('Got en error in {}'.format( self.fail('Got en error in {}'.format(
@ -86,8 +67,8 @@ class ProviderTests(unittest.TestCase):
def test_reset_last_overriding(self): def test_reset_last_overriding(self):
"""Test reseting of last overriding provider.""" """Test reseting of last overriding provider."""
overriding_provider1 = Provider() overriding_provider1 = di.Provider()
overriding_provider2 = Provider() overriding_provider2 = di.Provider()
self.provider.override(overriding_provider1) self.provider.override(overriding_provider1)
self.provider.override(overriding_provider2) self.provider.override(overriding_provider2)
@ -102,11 +83,11 @@ class ProviderTests(unittest.TestCase):
def test_reset_last_overriding_of_not_overridden_provider(self): def test_reset_last_overriding_of_not_overridden_provider(self):
"""Test resetting of last overriding on not overridden provier.""" """Test resetting of last overriding on not overridden provier."""
self.assertRaises(Error, self.provider.reset_last_overriding) self.assertRaises(di.Error, self.provider.reset_last_overriding)
def test_reset_override(self): def test_reset_override(self):
"""Test reset of provider's override.""" """Test reset of provider's override."""
overriding_provider = Provider() overriding_provider = di.Provider()
self.provider.override(overriding_provider) self.provider.override(overriding_provider)
self.assertTrue(self.provider.is_overridden) self.assertTrue(self.provider.is_overridden)
@ -117,7 +98,7 @@ class ProviderTests(unittest.TestCase):
self.assertFalse(self.provider.is_overridden) self.assertFalse(self.provider.is_overridden)
try: try:
self.provider.last_overriding self.provider.last_overriding
except Error: except di.Error:
pass pass
else: else:
self.fail('Got en error in {}'.format( self.fail('Got en error in {}'.format(
@ -130,16 +111,16 @@ class DelegateTests(unittest.TestCase):
def setUp(self): def setUp(self):
"""Set test cases environment up.""" """Set test cases environment up."""
self.delegated = Provider() self.delegated = di.Provider()
self.delegate = Delegate(delegated=self.delegated) self.delegate = di.Delegate(delegated=self.delegated)
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(self.delegate)) self.assertTrue(di.is_provider(self.delegate))
def test_init_with_not_provider(self): def test_init_with_not_provider(self):
"""Test that delegate accepts only another provider as delegated.""" """Test that delegate accepts only another provider as delegated."""
self.assertRaises(Error, Delegate, delegated=object()) self.assertRaises(di.Error, di.Delegate, delegated=object())
def test_call(self): def test_call(self):
"""Test returning of delegated provider.""" """Test returning of delegated provider."""
@ -184,19 +165,19 @@ class FactoryTests(unittest.TestCase):
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(Factory(self.Example))) self.assertTrue(di.is_provider(di.Factory(self.Example)))
def test_init_with_callable(self): def test_init_with_callable(self):
"""Test creation of provider with a callable.""" """Test creation of provider with a callable."""
self.assertTrue(Factory(credits)) self.assertTrue(di.Factory(credits))
def test_init_with_not_callable(self): def test_init_with_not_callable(self):
"""Test creation of provider with not a callable.""" """Test creation of provider with not a callable."""
self.assertRaises(Error, Factory, 123) self.assertRaises(di.Error, di.Factory, 123)
def test_call(self): def test_call(self):
"""Test creation of new instances.""" """Test creation of new instances."""
provider = Factory(self.Example) provider = di.Factory(self.Example)
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -204,11 +185,14 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance1, self.Example) self.assertIsInstance(instance1, self.Example)
self.assertIsInstance(instance2, self.Example) self.assertIsInstance(instance2, self.Example)
def test_call_with_init_args(self): def test_call_with_init_args_simplified_syntax(self):
"""Test creation of new instances with init args injections.""" """Test creation of new instances with init args injections.
provider = Factory(self.Example,
KwArg('init_arg1', 'i1'), New simplified syntax.
KwArg('init_arg2', 'i2')) """
provider = di.Factory(self.Example,
init_arg1='i1',
init_arg2='i2')
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -223,14 +207,11 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance1, self.Example) self.assertIsInstance(instance1, self.Example)
self.assertIsInstance(instance2, self.Example) self.assertIsInstance(instance2, self.Example)
def test_call_with_init_args_simplified_syntax(self): def test_call_with_init_args_old_syntax(self):
"""Test creation of new instances with init args injections. """Test creation of new instances with init args injections."""
provider = di.Factory(self.Example,
Simplified syntax. di.KwArg('init_arg1', 'i1'),
""" di.KwArg('init_arg2', 'i2'))
provider = Factory(self.Example,
init_arg1='i1',
init_arg2='i2')
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -247,9 +228,9 @@ class FactoryTests(unittest.TestCase):
def test_call_with_attributes(self): def test_call_with_attributes(self):
"""Test creation of new instances with attribute injections.""" """Test creation of new instances with attribute injections."""
provider = Factory(self.Example, provider = di.Factory(self.Example,
Attribute('attribute1', 'a1'), di.Attribute('attribute1', 'a1'),
Attribute('attribute2', 'a2')) di.Attribute('attribute2', 'a2'))
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -266,9 +247,9 @@ class FactoryTests(unittest.TestCase):
def test_call_with_methods(self): def test_call_with_methods(self):
"""Test creation of new instances with method injections.""" """Test creation of new instances with method injections."""
provider = Factory(self.Example, provider = di.Factory(self.Example,
Method('method1', 'm1'), di.Method('method1', 'm1'),
Method('method2', 'm2')) di.Method('method2', 'm2'))
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -285,7 +266,7 @@ class FactoryTests(unittest.TestCase):
def test_call_with_context_args(self): def test_call_with_context_args(self):
"""Test creation of new instances with context args.""" """Test creation of new instances with context args."""
provider = Factory(self.Example) provider = di.Factory(self.Example)
instance = provider(11, 22) instance = provider(11, 22)
self.assertEqual(instance.init_arg1, 11) self.assertEqual(instance.init_arg1, 11)
@ -293,8 +274,8 @@ class FactoryTests(unittest.TestCase):
def test_call_with_context_kwargs(self): def test_call_with_context_kwargs(self):
"""Test creation of new instances with context kwargs.""" """Test creation of new instances with context kwargs."""
provider = Factory(self.Example, provider = di.Factory(self.Example,
KwArg('init_arg1', 1)) di.KwArg('init_arg1', 1))
instance1 = provider(init_arg2=22) instance1 = provider(init_arg2=22)
self.assertEqual(instance1.init_arg1, 1) self.assertEqual(instance1.init_arg1, 1)
@ -306,9 +287,9 @@ class FactoryTests(unittest.TestCase):
def test_call_overridden(self): def test_call_overridden(self):
"""Test creation of new instances on overridden provider.""" """Test creation of new instances on overridden provider."""
provider = Factory(self.Example) provider = di.Factory(self.Example)
overriding_provider1 = Factory(dict) overriding_provider1 = di.Factory(dict)
overriding_provider2 = Factory(list) overriding_provider2 = di.Factory(list)
provider.override(overriding_provider1) provider.override(overriding_provider1)
provider.override(overriding_provider2) provider.override(overriding_provider2)
@ -327,7 +308,7 @@ class SingletonTests(unittest.TestCase):
def test_call(self): def test_call(self):
"""Test creation and returning of single object.""" """Test creation and returning of single object."""
provider = Singleton(object) provider = di.Singleton(object)
instance1 = provider() instance1 = provider()
instance2 = provider() instance2 = provider()
@ -338,7 +319,7 @@ class SingletonTests(unittest.TestCase):
def test_reset(self): def test_reset(self):
"""Test creation and reset of single object.""" """Test creation and reset of single object."""
provider = Singleton(object) provider = di.Singleton(object)
instance1 = provider() instance1 = provider()
self.assertIsInstance(instance1, object) self.assertIsInstance(instance1, object)
@ -357,29 +338,29 @@ class ExternalDependencyTests(unittest.TestCase):
def setUp(self): def setUp(self):
"""Set test cases environment up.""" """Set test cases environment up."""
self.provider = ExternalDependency(instance_of=list) self.provider = di.ExternalDependency(instance_of=list)
def test_init_with_not_class(self): def test_init_with_not_class(self):
"""Test creation with not a class.""" """Test creation with not a class."""
self.assertRaises(Error, ExternalDependency, object()) self.assertRaises(di.Error, di.ExternalDependency, object())
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(self.provider)) self.assertTrue(di.is_provider(self.provider))
def test_call_overridden(self): def test_call_overridden(self):
"""Test call of overridden external dependency.""" """Test call of overridden external dependency."""
self.provider.provided_by(Factory(list)) self.provider.provided_by(di.Factory(list))
self.assertIsInstance(self.provider(), list) self.assertIsInstance(self.provider(), list)
def test_call_overridden_but_not_instance_of(self): def test_call_overridden_but_not_instance_of(self):
"""Test call of overridden external dependency, but not instance of.""" """Test call of overridden external dependency, but not instance of."""
self.provider.provided_by(Factory(dict)) self.provider.provided_by(di.Factory(dict))
self.assertRaises(Error, self.provider) self.assertRaises(di.Error, self.provider)
def test_call_not_overridden(self): def test_call_not_overridden(self):
"""Test call of not satisfied external dependency.""" """Test call of not satisfied external dependency."""
self.assertRaises(Error, self.provider) self.assertRaises(di.Error, self.provider)
class StaticProvidersTests(unittest.TestCase): class StaticProvidersTests(unittest.TestCase):
@ -388,52 +369,52 @@ class StaticProvidersTests(unittest.TestCase):
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(Class(object))) self.assertTrue(di.is_provider(di.Class(object)))
self.assertTrue(is_provider(Object(object()))) self.assertTrue(di.is_provider(di.Object(object())))
self.assertTrue(is_provider(Function(map))) self.assertTrue(di.is_provider(di.Function(map)))
self.assertTrue(is_provider(Value(123))) self.assertTrue(di.is_provider(di.Value(123)))
def test_call_class_provider(self): def test_call_class_provider(self):
"""Test Class provider call.""" """Test Class provider call."""
self.assertIs(Class(dict)(), dict) self.assertIs(di.Class(dict)(), dict)
def test_call_object_provider(self): def test_call_object_provider(self):
"""Test Object provider call.""" """Test Object provider call."""
obj = object() obj = object()
self.assertIs(Object(obj)(), obj) self.assertIs(di.Object(obj)(), obj)
def test_call_function_provider(self): def test_call_function_provider(self):
"""Test Function provider call.""" """Test Function provider call."""
self.assertIs(Function(map)(), map) self.assertIs(di.Function(map)(), map)
def test_call_value_provider(self): def test_call_value_provider(self):
"""Test Value provider call.""" """Test Value provider call."""
self.assertEqual(Value(123)(), 123) self.assertEqual(di.Value(123)(), 123)
def test_call_overridden_class_provider(self): def test_call_overridden_class_provider(self):
"""Test overridden Class provider call.""" """Test overridden Class provider call."""
cls_provider = Class(dict) cls_provider = di.Class(dict)
cls_provider.override(Object(list)) cls_provider.override(di.Object(list))
self.assertIs(cls_provider(), list) self.assertIs(cls_provider(), list)
def test_call_overridden_object_provider(self): def test_call_overridden_object_provider(self):
"""Test overridden Object provider call.""" """Test overridden Object provider call."""
obj1 = object() obj1 = object()
obj2 = object() obj2 = object()
obj_provider = Object(obj1) obj_provider = di.Object(obj1)
obj_provider.override(Object(obj2)) obj_provider.override(di.Object(obj2))
self.assertIs(obj_provider(), obj2) self.assertIs(obj_provider(), obj2)
def test_call_overridden_function_provider(self): def test_call_overridden_function_provider(self):
"""Test overridden Function provider call.""" """Test overridden Function provider call."""
function_provider = Function(len) function_provider = di.Function(len)
function_provider.override(Function(sum)) function_provider.override(di.Function(sum))
self.assertIs(function_provider(), sum) self.assertIs(function_provider(), sum)
def test_call_overridden_value_provider(self): def test_call_overridden_value_provider(self):
"""Test overridden Value provider call.""" """Test overridden Value provider call."""
value_provider = Value(123) value_provider = di.Value(123)
value_provider.override(Value(321)) value_provider.override(di.Value(321))
self.assertEqual(value_provider(), 321) self.assertEqual(value_provider(), 321)
@ -447,18 +428,18 @@ class CallableTests(unittest.TestCase):
def setUp(self): def setUp(self):
"""Set test cases environment up.""" """Set test cases environment up."""
self.provider = Callable(self.example, self.provider = di.Callable(self.example,
KwArg('arg1', 'a1'), di.KwArg('arg1', 'a1'),
KwArg('arg2', 'a2'), di.KwArg('arg2', 'a2'),
KwArg('arg3', 'a3')) di.KwArg('arg3', 'a3'))
def test_init_with_not_callable(self): def test_init_with_not_callable(self):
"""Test creation of provider with not callable.""" """Test creation of provider with not callable."""
self.assertRaises(Error, Callable, 123) self.assertRaises(di.Error, di.Callable, 123)
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(self.provider)) self.assertTrue(di.is_provider(self.provider))
def test_call(self): def test_call(self):
"""Test provider call.""" """Test provider call."""
@ -466,8 +447,8 @@ class CallableTests(unittest.TestCase):
def test_call_with_args(self): def test_call_with_args(self):
"""Test provider call with kwargs priority.""" """Test provider call with kwargs priority."""
provider = Callable(self.example, provider = di.Callable(self.example,
KwArg('arg3', 'a3')) di.KwArg('arg3', 'a3'))
self.assertEqual(provider(1, 2), (1, 2, 'a3')) self.assertEqual(provider(1, 2), (1, 2, 'a3'))
def test_call_with_kwargs_priority(self): def test_call_with_kwargs_priority(self):
@ -476,8 +457,8 @@ class CallableTests(unittest.TestCase):
def test_call_overridden(self): def test_call_overridden(self):
"""Test overridden provider call.""" """Test overridden provider call."""
overriding_provider1 = Value((1, 2, 3)) overriding_provider1 = di.Value((1, 2, 3))
overriding_provider2 = Value((3, 2, 1)) overriding_provider2 = di.Value((3, 2, 1))
self.provider.override(overriding_provider1) self.provider.override(overriding_provider1)
self.provider.override(overriding_provider2) self.provider.override(overriding_provider2)
@ -497,15 +478,15 @@ class ConfigTests(unittest.TestCase):
"""Set test cases environment up.""" """Set test cases environment up."""
self.initial_data = dict(key='value', self.initial_data = dict(key='value',
category=dict(setting='setting_value')) category=dict(setting='setting_value'))
self.provider = Config(self.initial_data) self.provider = di.Config(self.initial_data)
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(self.provider)) self.assertTrue(di.is_provider(self.provider))
def test_init_without_initial_value(self): def test_init_without_initial_value(self):
"""Test provider's creation with no initial value.""" """Test provider's creation with no initial value."""
self.assertEqual(Config()(), dict()) self.assertEqual(di.Config()(), dict())
def test_call(self): def test_call(self):
"""Test returning of config value.""" """Test returning of config value."""
@ -524,8 +505,8 @@ class ConfigTests(unittest.TestCase):
category = self.provider.category category = self.provider.category
category_setting = self.provider.category.setting category_setting = self.provider.category.setting
self.assertTrue(is_provider(category)) self.assertTrue(di.is_provider(category))
self.assertTrue(is_provider(category_setting)) self.assertTrue(di.is_provider(category_setting))
self.assertEqual(category(), self.initial_data['category']) self.assertEqual(category(), self.initial_data['category'])
self.assertEqual(category_setting(), self.assertEqual(category_setting(),
@ -533,12 +514,12 @@ class ConfigTests(unittest.TestCase):
def test_call_deferred_child_and_update_from(self): def test_call_deferred_child_and_update_from(self):
"""Test returning of deferred child config values.""" """Test returning of deferred child config values."""
self.provider = Config() self.provider = di.Config()
category = self.provider.category category = self.provider.category
category_setting = self.provider.category.setting category_setting = self.provider.category.setting
self.assertTrue(is_provider(category)) self.assertTrue(di.is_provider(category))
self.assertTrue(is_provider(category_setting)) self.assertTrue(di.is_provider(category_setting))
self.provider.update_from(self.initial_data) self.provider.update_from(self.initial_data)
@ -548,6 +529,6 @@ class ConfigTests(unittest.TestCase):
def test_call_deferred_child_with_empty_value(self): def test_call_deferred_child_with_empty_value(self):
"""Test returning of deferred child config values.""" """Test returning of deferred child config values."""
self.provider = Config() self.provider = di.Config()
category_setting = self.provider.category.setting category_setting = self.provider.category.setting
self.assertRaises(Error, category_setting) self.assertRaises(di.Error, category_setting)

View File

@ -1,23 +1,7 @@
"""Dependency injector utils unittests.""" """Dependency injector utils unittests."""
import unittest2 as unittest import unittest2 as unittest
import dependency_injector as di
from dependency_injector.utils import is_provider
from dependency_injector.utils import ensure_is_provider
from dependency_injector.utils import is_injection
from dependency_injector.utils import ensure_is_injection
from dependency_injector.utils import is_kwarg_injection
from dependency_injector.utils import is_attribute_injection
from dependency_injector.utils import is_method_injection
from dependency_injector.providers import Provider
from dependency_injector.injections import Injection
from dependency_injector.injections import KwArg
from dependency_injector.injections import Attribute
from dependency_injector.injections import Method
from dependency_injector.errors import Error
class IsProviderTests(unittest.TestCase): class IsProviderTests(unittest.TestCase):
@ -26,27 +10,27 @@ class IsProviderTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
self.assertTrue(is_provider(Provider())) self.assertTrue(di.is_provider(di.Provider()))
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertFalse(is_provider(Provider)) self.assertFalse(di.is_provider(di.Provider))
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertFalse(is_provider('some_string')) self.assertFalse(di.is_provider('some_string'))
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertFalse(is_provider(object())) self.assertFalse(di.is_provider(object()))
def test_with_subclass_instance(self): def test_with_subclass_instance(self):
"""Test with subclass of provider instance.""" """Test with subclass of provider instance."""
class SomeProvider(Provider): class SomeProvider(di.Provider):
"""Some provider for test.""" """Some provider for test."""
self.assertTrue(is_provider(SomeProvider())) self.assertTrue(di.is_provider(SomeProvider()))
def test_with_class_with_getattr(self): def test_with_class_with_getattr(self):
"""Test with class that has __getattr__() method implementation.""" """Test with class that has __getattr__() method implementation."""
@ -58,7 +42,7 @@ class IsProviderTests(unittest.TestCase):
"""Test implementation that just returns False.""" """Test implementation that just returns False."""
return False return False
self.assertFalse(is_provider(SomeClass())) self.assertFalse(di.is_provider(SomeClass()))
class EnsureIsProviderTests(unittest.TestCase): class EnsureIsProviderTests(unittest.TestCase):
@ -67,20 +51,20 @@ class EnsureIsProviderTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
provider = Provider() provider = di.Provider()
self.assertIs(ensure_is_provider(provider), provider) self.assertIs(di.ensure_is_provider(provider), provider)
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertRaises(Error, ensure_is_provider, Provider) self.assertRaises(di.Error, di.ensure_is_provider, di.Provider)
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertRaises(Error, ensure_is_provider, 'some_string') self.assertRaises(di.Error, di.ensure_is_provider, 'some_string')
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertRaises(Error, ensure_is_provider, object()) self.assertRaises(di.Error, di.ensure_is_provider, object())
class IsInjectionTests(unittest.TestCase): class IsInjectionTests(unittest.TestCase):
@ -89,25 +73,25 @@ class IsInjectionTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
self.assertTrue(is_injection(Injection('name', 'value'))) self.assertTrue(di.is_injection(di.Injection('name', 'value')))
def test_with_subclass_instances(self): def test_with_subclass_instances(self):
"""Test with subclass instances.""" """Test with subclass instances."""
self.assertTrue(is_injection(KwArg('name', 'value'))) self.assertTrue(di.is_injection(di.KwArg('name', 'value')))
self.assertTrue(is_injection(Attribute('name', 'value'))) self.assertTrue(di.is_injection(di.Attribute('name', 'value')))
self.assertTrue(is_injection(Method('name', 'value'))) self.assertTrue(di.is_injection(di.Method('name', 'value')))
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertFalse(is_injection(Injection)) self.assertFalse(di.is_injection(di.Injection))
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertFalse(is_injection('some_string')) self.assertFalse(di.is_injection('some_string'))
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertFalse(is_injection(object())) self.assertFalse(di.is_injection(object()))
class EnsureIsInjectionTests(unittest.TestCase): class EnsureIsInjectionTests(unittest.TestCase):
@ -116,20 +100,20 @@ class EnsureIsInjectionTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
injection = Injection('name', 'value') injection = di.Injection('name', 'value')
self.assertIs(ensure_is_injection(injection), injection) self.assertIs(di.ensure_is_injection(injection), injection)
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertRaises(Error, ensure_is_injection, Injection) self.assertRaises(di.Error, di.ensure_is_injection, di.Injection)
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertRaises(Error, ensure_is_injection, 'some_string') self.assertRaises(di.Error, di.ensure_is_injection, 'some_string')
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertRaises(Error, ensure_is_injection, object()) self.assertRaises(di.Error, di.ensure_is_injection, object())
class IsKwArgInjectionTests(unittest.TestCase): class IsKwArgInjectionTests(unittest.TestCase):
@ -138,23 +122,23 @@ class IsKwArgInjectionTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
self.assertTrue(is_kwarg_injection(KwArg('name', 'value'))) self.assertTrue(di.is_kwarg_injection(di.KwArg('name', 'value')))
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertFalse(is_kwarg_injection(KwArg)) self.assertFalse(di.is_kwarg_injection(di.KwArg))
def test_with_parent_class(self): def test_with_parent_class(self):
"""Test with parent class.""" """Test with parent class."""
self.assertFalse(is_kwarg_injection(Injection)) self.assertFalse(di.is_kwarg_injection(di.Injection))
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertFalse(is_kwarg_injection('some_string')) self.assertFalse(di.is_kwarg_injection('some_string'))
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertFalse(is_kwarg_injection(object())) self.assertFalse(di.is_kwarg_injection(object()))
class IsAttributeInjectionTests(unittest.TestCase): class IsAttributeInjectionTests(unittest.TestCase):
@ -163,23 +147,24 @@ class IsAttributeInjectionTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
self.assertTrue(is_attribute_injection(Attribute('name', 'value'))) self.assertTrue(di.is_attribute_injection(di.Attribute('name',
'value')))
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertFalse(is_attribute_injection(Attribute)) self.assertFalse(di.is_attribute_injection(di.Attribute))
def test_with_parent_class(self): def test_with_parent_class(self):
"""Test with parent class.""" """Test with parent class."""
self.assertFalse(is_attribute_injection(Injection)) self.assertFalse(di.is_attribute_injection(di.Injection))
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertFalse(is_attribute_injection('some_string')) self.assertFalse(di.is_attribute_injection('some_string'))
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertFalse(is_attribute_injection(object())) self.assertFalse(di.is_attribute_injection(object()))
class IsMethodInjectionTests(unittest.TestCase): class IsMethodInjectionTests(unittest.TestCase):
@ -188,20 +173,20 @@ class IsMethodInjectionTests(unittest.TestCase):
def test_with_instance(self): def test_with_instance(self):
"""Test with instance.""" """Test with instance."""
self.assertTrue(is_method_injection(Method('name', 'value'))) self.assertTrue(di.is_method_injection(di.Method('name', 'value')))
def test_with_class(self): def test_with_class(self):
"""Test with class.""" """Test with class."""
self.assertFalse(is_method_injection(Method)) self.assertFalse(di.is_method_injection(di.Method))
def test_with_parent_class(self): def test_with_parent_class(self):
"""Test with parent class.""" """Test with parent class."""
self.assertFalse(is_method_injection(Injection)) self.assertFalse(di.is_method_injection(di.Injection))
def test_with_string(self): def test_with_string(self):
"""Test with string.""" """Test with string."""
self.assertFalse(is_method_injection('some_string')) self.assertFalse(di.is_method_injection('some_string'))
def test_with_object(self): def test_with_object(self):
"""Test with object.""" """Test with object."""
self.assertFalse(is_method_injection(object())) self.assertFalse(di.is_method_injection(object()))