Update project structure

This commit is contained in:
Roman Mogilatov 2016-11-04 15:48:26 +02:00
parent 5695c781c9
commit f5680ff032
15 changed files with 127 additions and 260 deletions

View File

@ -51,6 +51,10 @@ setup(name='dependency-injector',
['src/dependency_injector/providers/injections.c'],
define_macros=defined_macros,
extra_compile_args=['-O2']),
Extension('dependency_injector.providers.utils',
['src/dependency_injector/providers/utils.c'],
define_macros=defined_macros,
extra_compile_args=['-O2']),
],
package_data={
'dependency_injector': ['*.pxd'],

View File

@ -20,6 +20,13 @@ from .creational import (
ThreadLocalSingleton,
DelegatedThreadLocalSingleton,
)
from .utils import (
GLOBAL_LOCK,
is_provider,
ensure_is_provider,
is_delegated,
represent_provider,
)
from .injections import (
Injection,
PositionalInjection,
@ -48,6 +55,12 @@ __all__ = (
'ThreadLocalSingleton',
'DelegatedThreadLocalSingleton',
'GLOBAL_LOCK',
'is_provider',
'ensure_is_provider',
'is_delegated',
'represent_provider',
'Injection',
'PositionalInjection',
'NamedInjection',

View File

@ -3,7 +3,7 @@
import six
from dependency_injector.errors import Error
from dependency_injector.utils import (
from .utils import (
is_provider,
ensure_is_provider,
represent_provider,

View File

@ -7,7 +7,7 @@ from dependency_injector.providers.base import (
_parse_positional_injections,
_parse_keyword_injections,
)
from dependency_injector.utils import represent_provider
from .utils import represent_provider
from dependency_injector.errors import Error

View File

@ -6,7 +6,7 @@ import six
from dependency_injector.providers.callable import Callable
from dependency_injector.providers.base import _parse_keyword_injections
from dependency_injector.utils import GLOBAL_LOCK
from .utils import GLOBAL_LOCK
from dependency_injector.errors import Error

View File

@ -5,7 +5,7 @@ Powered by Cython.
cimport cython
from dependency_injector.utils import (
from .utils cimport (
is_provider,
is_delegated,
)

View File

@ -0,0 +1,9 @@
"""Dependency injector provider utils.
Powered by Cython.
"""
cpdef bint is_provider(object instance)
cpdef object ensure_is_provider(object instance)
cpdef bint is_delegated(object instance)
cpdef str represent_provider(object provider, object provides)

View File

@ -1,10 +1,14 @@
"""Dependency injector utils."""
"""Dependency injector provider utils.
import six
Powered by Cython.
"""
import sys
import types
import threading
from .errors import Error
from dependency_injector.errors import Error
GLOBAL_LOCK = threading.RLock()
"""Global reentrant lock.
@ -12,8 +16,13 @@ GLOBAL_LOCK = threading.RLock()
:type: :py:class:`threading.RLock`
"""
if sys.version_info[0] == 3: # pragma: no cover
_CLASS_TYPES = (type,)
else: # pragma: no cover
_CLASS_TYPES = (type, types.ClassType)
def is_provider(instance):
cpdef bint is_provider(object instance):
"""Check if instance is provider instance.
:param instance: Instance to be checked.
@ -21,11 +30,11 @@ def is_provider(instance):
:rtype: bool
"""
return (not isinstance(instance, six.class_types) and
return (not isinstance(instance, _CLASS_TYPES) and
getattr(instance, '__IS_PROVIDER__', False) is True)
def ensure_is_provider(instance):
cpdef object ensure_is_provider(object instance):
"""Check if instance is provider instance and return it.
:param instance: Instance to be checked.
@ -42,7 +51,7 @@ def ensure_is_provider(instance):
return instance
def is_delegated(instance):
cpdef bint is_delegated(object instance):
"""Check if instance is delegated provider.
:param instance: Instance to be checked.
@ -50,11 +59,11 @@ def is_delegated(instance):
:rtype: bool
"""
return (not isinstance(instance, six.class_types) and
return (not isinstance(instance, _CLASS_TYPES) and
getattr(instance, '__IS_DELEGATED__', False) is True)
def represent_provider(provider, provides):
cpdef str represent_provider(object provider, object provides):
"""Return string representation of provider.
:param provider: Provider object

View File

@ -4,28 +4,22 @@ import unittest2 as unittest
from dependency_injector import (
providers,
utils,
errors,
)
class ProviderTests(unittest.TestCase):
"""Provider test cases."""
def setUp(self):
"""Set test cases environment up."""
self.provider = providers.Provider()
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(self.provider))
self.assertTrue(providers.is_provider(self.provider))
def test_call(self):
"""Test call."""
self.assertRaises(NotImplementedError, self.provider.__call__)
def test_delegate(self):
"""Test creating of provider delegation."""
delegate1 = self.provider.delegate()
self.assertIsInstance(delegate1, providers.Delegate)
@ -39,30 +33,25 @@ class ProviderTests(unittest.TestCase):
self.assertIsNot(delegate1, delegate2)
def test_override(self):
"""Test provider overriding."""
overriding_provider = providers.Provider()
self.provider.override(overriding_provider)
self.assertTrue(self.provider.overridden)
def test_overriding_context(self):
"""Test provider overriding context."""
overriding_provider = providers.Provider()
with self.provider.override(overriding_provider):
self.assertTrue(self.provider.overridden)
self.assertFalse(self.provider.overridden)
def test_override_with_itself(self):
"""Test provider overriding with itself."""
self.assertRaises(errors.Error, self.provider.override, self.provider)
def test_override_with_not_provider(self):
"""Test provider overriding with not provider instance."""
obj = object()
self.provider.override(obj)
self.assertIs(self.provider(), obj)
def test_reset_last_overriding(self):
"""Test reseting of last overriding provider."""
overriding_provider1 = providers.Provider()
overriding_provider2 = providers.Provider()
@ -78,11 +67,9 @@ class ProviderTests(unittest.TestCase):
self.assertFalse(self.provider.overridden)
def test_reset_last_overriding_of_not_overridden_provider(self):
"""Test resetting of last overriding on not overridden provier."""
self.assertRaises(errors.Error, self.provider.reset_last_overriding)
def test_reset_override(self):
"""Test reset of provider's override."""
overriding_provider = providers.Provider()
self.provider.override(overriding_provider)
@ -94,30 +81,24 @@ class ProviderTests(unittest.TestCase):
self.assertEqual(self.provider.overridden, tuple())
def test_repr(self):
"""Test representation of provider."""
self.assertEqual(repr(self.provider),
'<dependency_injector.providers.base.'
'Provider() at {0}>'.format(hex(id(self.provider))))
class DelegateTests(unittest.TestCase):
"""Delegate test cases."""
def setUp(self):
"""Set test cases environment up."""
self.delegated = providers.Provider()
self.delegate = providers.Delegate(delegated=self.delegated)
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(self.delegate))
self.assertTrue(providers.is_provider(self.delegate))
def test_init_with_not_provider(self):
"""Test that delegate accepts only another provider as delegated."""
self.assertRaises(errors.Error, providers.Delegate, delegated=object())
def test_call(self):
"""Test returning of delegated provider."""
delegated1 = self.delegate()
delegated2 = self.delegate()
@ -125,7 +106,6 @@ class DelegateTests(unittest.TestCase):
self.assertIs(delegated2, self.delegated)
def test_repr(self):
"""Test representation of provider."""
self.assertEqual(repr(self.delegate),
'<dependency_injector.providers.base.'
'Delegate({0}) at {1}>'.format(
@ -134,36 +114,28 @@ class DelegateTests(unittest.TestCase):
class ExternalDependencyTests(unittest.TestCase):
"""ExternalDependency test cases."""
def setUp(self):
"""Set test cases environment up."""
self.provider = providers.ExternalDependency(instance_of=list)
def test_init_with_not_class(self):
"""Test creation with not a class."""
self.assertRaises(errors.Error, providers.ExternalDependency, object())
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(self.provider))
self.assertTrue(providers.is_provider(self.provider))
def test_call_overridden(self):
"""Test call of overridden external dependency."""
self.provider.provided_by(providers.Factory(list))
self.assertIsInstance(self.provider(), list)
def test_call_overridden_but_not_instance_of(self):
"""Test call of overridden external dependency, but not instance of."""
self.provider.provided_by(providers.Factory(dict))
self.assertRaises(errors.Error, self.provider)
def test_call_not_overridden(self):
"""Test call of not satisfied external dependency."""
self.assertRaises(errors.Error, self.provider)
def test_repr(self):
"""Test representation of provider."""
self.assertEqual(repr(self.provider),
'<dependency_injector.providers.base.'
'ExternalDependency({0}) at {1}>'.format(

View File

@ -2,82 +2,60 @@
import unittest2 as unittest
from dependency_injector import providers, utils, errors
from dependency_injector import providers, errors
class CallableTests(unittest.TestCase):
"""Callable test cases."""
def example(self, arg1, arg2, arg3, arg4):
"""Example callback."""
return arg1, arg2, arg3, arg4
def test_init_with_callable(self):
"""Test creation of provider with a callable."""
self.assertTrue(providers.Callable(self.example))
def test_init_with_not_callable(self):
"""Test creation of provider with not a callable."""
self.assertRaises(errors.Error, providers.Callable, 123)
def test_call(self):
"""Test call."""
provider = providers.Callable(lambda: True)
self.assertTrue(provider())
def test_call_with_positional_args(self):
"""Test call with positional args.
New simplified syntax.
"""
provider = providers.Callable(self.example,
1, 2, 3, 4)
self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_call_with_keyword_args(self):
"""Test call with keyword args."""
provider = providers.Callable(self.example,
arg1=1, arg2=2, arg3=3, arg4=4)
self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_call_with_positional_and_keyword_args(self):
"""Test call with positional and keyword args."""
provider = providers.Callable(self.example,
1, 2,
arg3=3, arg4=4)
self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_call_with_context_args(self):
"""Test call with context args."""
provider = providers.Callable(self.example, 1, 2)
self.assertTupleEqual(provider(3, 4), (1, 2, 3, 4))
def test_call_with_context_kwargs(self):
"""Test call with context kwargs."""
provider = providers.Callable(self.example, arg1=1)
self.assertTupleEqual(provider(arg2=2, arg3=3, arg4=4), (1, 2, 3, 4))
def test_call_with_context_args_and_kwargs(self):
"""Test call with context args and kwargs."""
provider = providers.Callable(self.example, 1)
self.assertTupleEqual(provider(2, arg3=3, arg4=4), (1, 2, 3, 4))
def test_fluent_interface(self):
"""Test injections definition with fluent interface."""
provider = providers.Singleton(self.example) \
.add_args(1, 2) \
.add_kwargs(arg3=3, arg4=4) \
.add_kwargs(arg3=3, arg4=4)
self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_call_overridden(self):
"""Test creation of new instances on overridden provider."""
provider = providers.Callable(self.example)
provider.override(providers.Object((4, 3, 2, 1)))
@ -86,7 +64,6 @@ class CallableTests(unittest.TestCase):
self.assertTupleEqual(provider(), (1, 2, 3, 4))
def test_repr(self):
"""Test representation of provider."""
provider = providers.Callable(self.example)
self.assertEqual(repr(provider),
@ -97,18 +74,15 @@ class CallableTests(unittest.TestCase):
class DelegatedCallableTests(unittest.TestCase):
"""DelegatedCallable test cases."""
def test_inheritance(self):
"""Test inheritance."""
self.assertIsInstance(providers.DelegatedCallable(len),
providers.Callable)
def test_is_provider(self):
"""Test is_provider."""
self.assertTrue(utils.is_provider(providers.DelegatedCallable(len)))
self.assertTrue(
providers.is_provider(providers.DelegatedCallable(len)))
def test_is_delegated_provider(self):
"""Test is_delegated_provider."""
provider = providers.DelegatedCallable(len)
self.assertIs(provider.provide_injection(), provider)

View File

@ -2,15 +2,13 @@
import unittest2 as unittest
from dependency_injector import providers, utils, errors
from dependency_injector import providers, errors
class Example(object):
"""Example class for Factory provider tests."""
def __init__(self, init_arg1=None, init_arg2=None, init_arg3=None,
init_arg4=None):
"""Initializer."""
self.init_arg1 = init_arg1
self.init_arg2 = init_arg2
self.init_arg3 = init_arg3
@ -21,25 +19,18 @@ class Example(object):
class FactoryTests(unittest.TestCase):
"""Factory test cases."""
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(providers.Factory(Example)))
self.assertTrue(providers.is_provider(providers.Factory(Example)))
def test_init_with_callable(self):
"""Test creation of provider with a callable."""
self.assertTrue(providers.Factory(credits))
def test_init_with_not_callable(self):
"""Test creation of provider with not a callable."""
self.assertRaises(errors.Error, providers.Factory, 123)
def test_init_with_valid_provided_type(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.Factory):
"""Example provider."""
provided_type = Example
example_provider = ExampleProvider(Example, 1, 2)
@ -47,31 +38,24 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(example_provider(), Example)
def test_init_with_valid_provided_subtype(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.Factory):
"""Example provider."""
provided_type = Example
class NewExampe(Example):
"""Example class subclass."""
pass
example_provider = ExampleProvider(NewExampe, 1, 2)
self.assertIsInstance(example_provider(), NewExampe)
def test_init_with_invalid_provided_type(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.Factory):
"""Example provider."""
provided_type = Example
with self.assertRaises(errors.Error):
ExampleProvider(list)
def test_call(self):
"""Test call."""
provider = providers.Factory(Example)
instance1 = provider()
@ -82,7 +66,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_args(self):
"""Test call with init positional args."""
provider = providers.Factory(Example, 'i1', 'i2')
instance1 = provider()
@ -99,7 +82,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_keyword_args(self):
"""Test call with init keyword args."""
provider = providers.Factory(Example, init_arg1='i1', init_arg2='i2')
instance1 = provider()
@ -116,7 +98,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args(self):
"""Test call with init positional and keyword args."""
provider = providers.Factory(Example, 'i1', init_arg2='i2')
instance1 = provider()
@ -133,7 +114,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_attributes(self):
"""Test call with attribute injections."""
provider = providers.Factory(Example)
provider.add_attributes(attribute1='a1', attribute2='a2')
@ -151,7 +131,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self):
"""Test call with context args."""
provider = providers.Factory(Example, 11, 22)
instance = provider(33, 44)
@ -162,7 +141,6 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance.init_arg4, 44)
def test_call_with_context_kwargs(self):
"""Test call with context kwargs."""
provider = providers.Factory(Example, init_arg1=1)
instance1 = provider(init_arg2=22)
@ -174,7 +152,6 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance2.init_arg2, 22)
def test_call_with_context_args_and_kwargs(self):
"""Test call with context args and kwargs."""
provider = providers.Factory(Example, 11)
instance = provider(22, init_arg3=33, init_arg4=44)
@ -185,7 +162,6 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance.init_arg4, 44)
def test_fluent_interface(self):
"""Test injections definition with fluent interface."""
provider = providers.Factory(Example) \
.add_args(1, 2) \
.add_kwargs(init_arg3=3, init_arg4=4) \
@ -201,7 +177,6 @@ class FactoryTests(unittest.TestCase):
self.assertEqual(instance.attribute2, 6)
def test_call_overridden(self):
"""Test call on overridden provider."""
provider = providers.Factory(Example)
overriding_provider1 = providers.Factory(dict)
overriding_provider2 = providers.Factory(list)
@ -217,7 +192,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance2, list)
def test_repr(self):
"""Test representation of provider."""
provider = providers.Factory(Example)
self.assertEqual(repr(provider),
@ -228,43 +202,33 @@ class FactoryTests(unittest.TestCase):
class DelegatedFactoryTests(unittest.TestCase):
"""DelegatedFactory test cases."""
def test_inheritance(self):
"""Test inheritance."""
self.assertIsInstance(providers.DelegatedFactory(object),
providers.Factory)
def test_is_provider(self):
"""Test is_provider."""
self.assertTrue(utils.is_provider(providers.DelegatedFactory(object)))
self.assertTrue(
providers.is_provider(providers.DelegatedFactory(object)))
def test_is_delegated_provider(self):
"""Test is_delegated_provider."""
provider = providers.DelegatedFactory(object)
self.assertIs(provider.provide_injection(), provider)
class SingletonTests(unittest.TestCase):
"""Singleton test cases."""
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(providers.Singleton(Example)))
self.assertTrue(providers.is_provider(providers.Singleton(Example)))
def test_init_with_callable(self):
"""Test creation of provider with a callable."""
self.assertTrue(providers.Singleton(credits))
def test_init_with_not_callable(self):
"""Test creation of provider with not a callable."""
self.assertRaises(errors.Error, providers.Singleton, 123)
def test_init_with_valid_provided_type(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.Singleton):
"""Example provider."""
provided_type = Example
example_provider = ExampleProvider(Example, 1, 2)
@ -272,31 +236,24 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(example_provider(), Example)
def test_init_with_valid_provided_subtype(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.Singleton):
"""Example provider."""
provided_type = Example
class NewExampe(Example):
"""Example class subclass."""
pass
example_provider = ExampleProvider(NewExampe, 1, 2)
self.assertIsInstance(example_provider(), NewExampe)
def test_init_with_invalid_provided_type(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.Singleton):
"""Example provider."""
provided_type = Example
with self.assertRaises(errors.Error):
ExampleProvider(list)
def test_call(self):
"""Test getting of instances."""
provider = providers.Singleton(Example)
instance1 = provider()
@ -307,7 +264,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_args(self):
"""Test getting of instances with init positional args."""
provider = providers.Singleton(Example, 'i1', 'i2')
instance1 = provider()
@ -324,7 +280,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_keyword_args(self):
"""Test getting of instances with init keyword args."""
provider = providers.Singleton(Example, init_arg1='i1', init_arg2='i2')
instance1 = provider()
@ -341,7 +296,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args(self):
"""Test getting of instances with init positional and keyword args."""
provider = providers.Singleton(Example, 'i1', init_arg2='i2')
instance1 = provider()
@ -358,7 +312,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_attributes(self):
"""Test getting of instances with attribute injections."""
provider = providers.Singleton(Example)
provider.add_attributes(attribute1='a1', attribute2='a2')
@ -376,7 +329,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self):
"""Test getting of instances with context args."""
provider = providers.Singleton(Example)
instance = provider(11, 22)
@ -385,7 +337,6 @@ class SingletonTests(unittest.TestCase):
self.assertEqual(instance.init_arg2, 22)
def test_call_with_context_kwargs(self):
"""Test getting of instances with context kwargs."""
provider = providers.Singleton(Example, init_arg1=1)
instance1 = provider(init_arg2=22)
@ -398,7 +349,6 @@ class SingletonTests(unittest.TestCase):
self.assertEqual(instance1.init_arg2, 22)
def test_call_with_context_args_and_kwargs(self):
"""Test getting of instances with context args and kwargs."""
provider = providers.Singleton(Example, 11)
instance = provider(22, init_arg3=33, init_arg4=44)
@ -409,7 +359,6 @@ class SingletonTests(unittest.TestCase):
self.assertEqual(instance.init_arg4, 44)
def test_fluent_interface(self):
"""Test injections definition with fluent interface."""
provider = providers.Singleton(Example) \
.add_args(1, 2) \
.add_kwargs(init_arg3=3, init_arg4=4) \
@ -425,7 +374,6 @@ class SingletonTests(unittest.TestCase):
self.assertEqual(instance.attribute2, 6)
def test_call_overridden(self):
"""Test getting of instances on overridden provider."""
provider = providers.Singleton(Example)
overriding_provider1 = providers.Singleton(dict)
overriding_provider2 = providers.Singleton(object)
@ -441,7 +389,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, object)
def test_reset(self):
"""Test creation and reset of single object."""
provider = providers.Singleton(object)
instance1 = provider()
@ -455,7 +402,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsNot(instance1, instance2)
def test_repr(self):
"""Test representation of provider."""
provider = providers.Singleton(Example)
self.assertEqual(repr(provider),
@ -466,45 +412,34 @@ class SingletonTests(unittest.TestCase):
class DelegatedSingletonTests(unittest.TestCase):
"""DelegatedSingleton test cases."""
def test_inheritance(self):
"""Test inheritance."""
self.assertIsInstance(providers.DelegatedSingleton(object),
providers.Singleton)
def test_is_provider(self):
"""Test is_provider."""
self.assertTrue(utils.is_provider(
providers.DelegatedSingleton(object)))
self.assertTrue(
providers.is_provider(providers.DelegatedSingleton(object)))
def test_is_delegated_provider(self):
"""Test is_delegated_provider."""
provider = providers.DelegatedSingleton(object)
self.assertIs(provider.provide_injection(), provider)
class ThreadLocalSingletonTests(unittest.TestCase):
"""ThreadLocalSingleton test cases."""
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(
utils.is_provider(providers.ThreadLocalSingleton(Example)))
providers.is_provider(providers.ThreadLocalSingleton(Example)))
def test_init_with_callable(self):
"""Test creation of provider with a callable."""
self.assertTrue(providers.ThreadLocalSingleton(credits))
def test_init_with_not_callable(self):
"""Test creation of provider with not a callable."""
self.assertRaises(errors.Error, providers.ThreadLocalSingleton, 123)
def test_init_with_valid_provided_type(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.ThreadLocalSingleton):
"""Example provider."""
provided_type = Example
example_provider = ExampleProvider(Example, 1, 2)
@ -512,31 +447,24 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(example_provider(), Example)
def test_init_with_valid_provided_subtype(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.ThreadLocalSingleton):
"""Example provider."""
provided_type = Example
class NewExampe(Example):
"""Example class subclass."""
pass
example_provider = ExampleProvider(NewExampe, 1, 2)
self.assertIsInstance(example_provider(), NewExampe)
def test_init_with_invalid_provided_type(self):
"""Test creation with not valid provided type."""
class ExampleProvider(providers.ThreadLocalSingleton):
"""Example provider."""
provided_type = Example
with self.assertRaises(errors.Error):
ExampleProvider(list)
def test_call(self):
"""Test getting of instances."""
provider = providers.ThreadLocalSingleton(Example)
instance1 = provider()
@ -547,7 +475,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_args(self):
"""Test getting of instances with init positional args."""
provider = providers.ThreadLocalSingleton(Example, 'i1', 'i2')
instance1 = provider()
@ -564,7 +491,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_keyword_args(self):
"""Test getting of instances with init keyword args."""
provider = providers.ThreadLocalSingleton(Example,
init_arg1='i1',
init_arg2='i2')
@ -583,7 +509,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args(self):
"""Test getting of instances with init positional and keyword args."""
provider = providers.ThreadLocalSingleton(Example,
'i1',
init_arg2='i2')
@ -602,7 +527,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_attributes(self):
"""Test getting of instances with attribute injections."""
provider = providers.ThreadLocalSingleton(Example)
provider.add_attributes(attribute1='a1', attribute2='a2')
@ -620,7 +544,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self):
"""Test getting of instances with context args."""
provider = providers.ThreadLocalSingleton(Example)
instance = provider(11, 22)
@ -629,7 +552,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertEqual(instance.init_arg2, 22)
def test_call_with_context_kwargs(self):
"""Test getting of instances with context kwargs."""
provider = providers.ThreadLocalSingleton(Example, init_arg1=1)
instance1 = provider(init_arg2=22)
@ -642,7 +564,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertEqual(instance1.init_arg2, 22)
def test_call_with_context_args_and_kwargs(self):
"""Test getting of instances with context args and kwargs."""
provider = providers.ThreadLocalSingleton(Example, 11)
instance = provider(22, init_arg3=33, init_arg4=44)
@ -653,7 +574,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertEqual(instance.init_arg4, 44)
def test_fluent_interface(self):
"""Test injections definition with fluent interface."""
provider = providers.ThreadLocalSingleton(Example) \
.add_args(1, 2) \
.add_kwargs(init_arg3=3, init_arg4=4) \
@ -669,7 +589,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertEqual(instance.attribute2, 6)
def test_call_overridden(self):
"""Test getting of instances on overridden provider."""
provider = providers.ThreadLocalSingleton(Example)
overriding_provider1 = providers.ThreadLocalSingleton(dict)
overriding_provider2 = providers.ThreadLocalSingleton(object)
@ -685,7 +604,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsInstance(instance2, object)
def test_reset(self):
"""Test creation and reset of single object."""
provider = providers.ThreadLocalSingleton(object)
instance1 = provider()
@ -699,7 +617,6 @@ class ThreadLocalSingletonTests(unittest.TestCase):
self.assertIsNot(instance1, instance2)
def test_repr(self):
"""Test representation of provider."""
provider = providers.ThreadLocalSingleton(Example)
self.assertEqual(repr(provider),
@ -710,37 +627,32 @@ class ThreadLocalSingletonTests(unittest.TestCase):
class DelegatedThreadLocalSingletonTests(unittest.TestCase):
"""DelegatedThreadLocalSingleton test cases."""
def test_inheritance(self):
"""Test inheritance."""
self.assertIsInstance(providers.DelegatedThreadLocalSingleton(object),
providers.ThreadLocalSingleton)
def test_is_provider(self):
"""Test is_provider."""
self.assertTrue(utils.is_provider(
self.assertTrue(
providers.is_provider(
providers.DelegatedThreadLocalSingleton(object)))
def test_is_delegated_provider(self):
"""Test is_delegated_provider."""
provider = providers.DelegatedThreadLocalSingleton(object)
self.assertIs(provider.provide_injection(), provider)
class FactoryAsDecoratorTests(unittest.TestCase):
"""Factory as decorator tests."""
def test_decoration_and_overriding(self):
"""Test decoration of some class with Factory provider."""
@providers.Factory
class AuthService(object):
"""Auth service."""
pass
@providers.override(AuthService)
@providers.Factory
class ExtAuthService(AuthService.cls):
"""Extended auth service."""
pass
auth_service = AuthService()

View File

@ -2,28 +2,19 @@
import unittest2 as unittest
from dependency_injector import (
providers,
utils,
)
# TODO: move to test_base
from dependency_injector import providers
class ObjectProviderTests(unittest.TestCase):
"""Object provider tests."""
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(providers.Object(object())))
self.assertTrue(providers.is_provider(providers.Object(object())))
def test_call_object_provider(self):
"""Test provider call."""
obj = object()
self.assertIs(providers.Object(obj)(), obj)
def test_call_overridden_object_provider(self):
"""Test overridden provider call."""
obj1 = object()
obj2 = object()
provider = providers.Object(obj1)
@ -31,7 +22,6 @@ class ObjectProviderTests(unittest.TestCase):
self.assertIs(provider(), obj2)
def test_repr(self):
"""Test representation of provider."""
some_object = object()
provider = providers.Object(some_object)
self.assertEqual(repr(provider),

View File

@ -0,0 +1,54 @@
"""Dependency injector provider utils unit tests."""
import unittest2 as unittest
from dependency_injector import providers
from dependency_injector import errors
class IsProviderTests(unittest.TestCase):
def test_with_instance(self):
self.assertTrue(providers.is_provider(providers.Provider()))
def test_with_class(self):
self.assertFalse(providers.is_provider(providers.Provider))
def test_with_string(self):
self.assertFalse(providers.is_provider('some_string'))
def test_with_object(self):
self.assertFalse(providers.is_provider(object()))
def test_with_subclass_instance(self):
class SomeProvider(providers.Provider):
pass
self.assertTrue(providers.is_provider(SomeProvider()))
def test_with_class_with_getattr(self):
class SomeClass(object):
def __getattr__(self, _):
return False
self.assertFalse(providers.is_provider(SomeClass()))
class EnsureIsProviderTests(unittest.TestCase):
def test_with_instance(self):
provider = providers.Provider()
self.assertIs(providers.ensure_is_provider(provider), provider)
def test_with_class(self):
self.assertRaises(errors.Error,
providers.ensure_is_provider,
providers.Provider)
def test_with_string(self):
self.assertRaises(errors.Error,
providers.ensure_is_provider,
'some_string')
def test_with_object(self):
self.assertRaises(errors.Error, providers.ensure_is_provider, object())

View File

@ -1,70 +0,0 @@
"""Dependency injector utils unittests."""
import unittest2 as unittest
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."""
self.assertTrue(utils.is_provider(providers.Provider()))
def test_with_class(self):
"""Test with class."""
self.assertFalse(utils.is_provider(providers.Provider))
def test_with_string(self):
"""Test with string."""
self.assertFalse(utils.is_provider('some_string'))
def test_with_object(self):
"""Test with object."""
self.assertFalse(utils.is_provider(object()))
def test_with_subclass_instance(self):
"""Test with subclass of provider instance."""
class SomeProvider(providers.Provider):
"""Some provider for test."""
self.assertTrue(utils.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(utils.is_provider(SomeClass()))
class EnsureIsProviderTests(unittest.TestCase):
"""`ensure_is_provider` test cases."""
def test_with_instance(self):
"""Test with instance."""
provider = providers.Provider()
self.assertIs(utils.ensure_is_provider(provider), provider)
def test_with_class(self):
"""Test with class."""
self.assertRaises(errors.Error,
utils.ensure_is_provider,
providers.Provider)
def test_with_string(self):
"""Test with string."""
self.assertRaises(errors.Error,
utils.ensure_is_provider,
'some_string')
def test_with_object(self):
"""Test with object."""
self.assertRaises(errors.Error, utils.ensure_is_provider, object())