python-dependency-injector/tests/unit/providers/test_singletons_py2_py3.py

543 lines
18 KiB
Python
Raw Normal View History

2016-11-04 21:45:26 +03:00
"""Dependency injector singleton providers unit tests."""
2015-03-12 13:45:15 +03:00
import sys
2015-03-12 13:46:14 +03:00
import unittest2 as unittest
2015-11-23 22:45:58 +03:00
2016-11-04 17:13:21 +03:00
from dependency_injector import (
providers,
errors,
)
2015-03-13 18:31:07 +03:00
2015-03-12 13:46:14 +03:00
class Example(object):
def __init__(self, init_arg1=None, init_arg2=None, init_arg3=None,
init_arg4=None):
self.init_arg1 = init_arg1
self.init_arg2 = init_arg2
self.init_arg3 = init_arg3
self.init_arg4 = init_arg4
self.attribute1 = None
self.attribute2 = None
2016-11-10 22:19:17 +03:00
class _BaseSingletonTestCase(object):
singleton_cls = None
2015-03-15 01:50:24 +03:00
def test_is_provider(self):
2016-11-10 22:19:17 +03:00
self.assertTrue(providers.is_provider(self.singleton_cls(Example)))
def test_init_with_callable(self):
2016-11-10 22:19:17 +03:00
self.assertTrue(self.singleton_cls(credits))
def test_init_with_not_callable(self):
2016-11-10 22:19:17 +03:00
self.assertRaises(errors.Error, self.singleton_cls, 123)
2015-12-11 23:46:49 +03:00
def test_init_with_valid_provided_type(self):
2016-11-10 22:19:17 +03:00
class ExampleProvider(self.singleton_cls):
2015-12-11 23:46:49 +03:00
provided_type = Example
2016-05-29 16:39:39 +03:00
example_provider = ExampleProvider(Example, 1, 2)
2015-12-11 23:46:49 +03:00
self.assertIsInstance(example_provider(), Example)
def test_init_with_valid_provided_subtype(self):
2016-11-10 22:19:17 +03:00
class ExampleProvider(self.singleton_cls):
2015-12-11 23:46:49 +03:00
provided_type = Example
class NewExampe(Example):
2016-11-04 16:48:26 +03:00
pass
2015-12-11 23:46:49 +03:00
2016-05-29 16:39:39 +03:00
example_provider = ExampleProvider(NewExampe, 1, 2)
2015-12-11 23:46:49 +03:00
self.assertIsInstance(example_provider(), NewExampe)
def test_init_with_invalid_provided_type(self):
2016-11-10 22:19:17 +03:00
class ExampleProvider(self.singleton_cls):
2015-12-11 23:46:49 +03:00
provided_type = Example
with self.assertRaises(errors.Error):
ExampleProvider(list)
def test_provided_instance_provider(self):
provider = providers.Singleton(Example)
self.assertIsInstance(provider.provided, providers.ProvidedInstance)
2015-03-15 01:50:24 +03:00
def test_call(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example)
instance1 = provider()
instance2 = provider()
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_args(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example, 'i1', 'i2')
2015-03-15 01:50:24 +03:00
instance1 = provider()
instance2 = provider()
self.assertEqual(instance1.init_arg1, 'i1')
self.assertEqual(instance1.init_arg2, 'i2')
self.assertEqual(instance2.init_arg1, 'i1')
self.assertEqual(instance2.init_arg2, 'i2')
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_init_keyword_args(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example, init_arg1='i1', init_arg2='i2')
instance1 = provider()
instance2 = provider()
self.assertEqual(instance1.init_arg1, 'i1')
self.assertEqual(instance1.init_arg2, 'i2')
self.assertEqual(instance2.init_arg1, 'i1')
self.assertEqual(instance2.init_arg2, 'i2')
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_init_positional_and_keyword_args(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example, 'i1', init_arg2='i2')
instance1 = provider()
instance2 = provider()
self.assertEqual(instance1.init_arg1, 'i1')
self.assertEqual(instance1.init_arg2, 'i2')
self.assertEqual(instance2.init_arg1, 'i1')
self.assertEqual(instance2.init_arg2, 'i2')
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_attributes(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example)
2016-05-29 16:39:39 +03:00
provider.add_attributes(attribute1='a1', attribute2='a2')
instance1 = provider()
instance2 = provider()
self.assertEqual(instance1.attribute1, 'a1')
self.assertEqual(instance1.attribute2, 'a2')
self.assertEqual(instance2.attribute1, 'a1')
self.assertEqual(instance2.attribute2, 'a2')
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example)
instance = provider(11, 22)
self.assertEqual(instance.init_arg1, 11)
self.assertEqual(instance.init_arg2, 22)
def test_call_with_context_kwargs(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example, init_arg1=1)
instance1 = provider(init_arg2=22)
self.assertEqual(instance1.init_arg1, 1)
self.assertEqual(instance1.init_arg2, 22)
# Instance is created earlier
instance1 = provider(init_arg1=11, init_arg2=22)
self.assertEqual(instance1.init_arg1, 1)
self.assertEqual(instance1.init_arg2, 22)
def test_call_with_context_args_and_kwargs(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example, 11)
2016-05-29 16:39:39 +03:00
instance = provider(22, init_arg3=33, init_arg4=44)
self.assertEqual(instance.init_arg1, 11)
self.assertEqual(instance.init_arg2, 22)
self.assertEqual(instance.init_arg3, 33)
self.assertEqual(instance.init_arg4, 44)
2016-05-29 16:39:39 +03:00
def test_fluent_interface(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example) \
2016-05-29 16:39:39 +03:00
.add_args(1, 2) \
.add_kwargs(init_arg3=3, init_arg4=4) \
.add_attributes(attribute1=5, attribute2=6)
instance = provider()
self.assertEqual(instance.init_arg1, 1)
self.assertEqual(instance.init_arg2, 2)
self.assertEqual(instance.init_arg3, 3)
self.assertEqual(instance.init_arg4, 4)
self.assertEqual(instance.attribute1, 5)
self.assertEqual(instance.attribute2, 6)
2016-11-10 22:19:17 +03:00
def test_set_args(self):
provider = self.singleton_cls(Example) \
.add_args(1, 2) \
.set_args(3, 4)
self.assertEqual(provider.args, tuple([3, 4]))
2016-11-10 22:19:17 +03:00
def test_set_kwargs(self):
provider = self.singleton_cls(Example) \
.add_kwargs(init_arg3=3, init_arg4=4) \
.set_kwargs(init_arg3=4, init_arg4=5)
self.assertEqual(provider.kwargs, dict(init_arg3=4, init_arg4=5))
2016-11-10 22:19:17 +03:00
def test_set_attributes(self):
provider = self.singleton_cls(Example) \
.add_attributes(attribute1=5, attribute2=6) \
.set_attributes(attribute1=6, attribute2=7)
self.assertEqual(provider.attributes, dict(attribute1=6, attribute2=7))
2016-11-10 22:19:17 +03:00
def test_clear_args(self):
provider = self.singleton_cls(Example) \
.add_args(1, 2) \
.clear_args()
self.assertEqual(provider.args, tuple())
2016-11-10 22:19:17 +03:00
def test_clear_kwargs(self):
provider = self.singleton_cls(Example) \
.add_kwargs(init_arg3=3, init_arg4=4) \
.clear_kwargs()
self.assertEqual(provider.kwargs, dict())
2015-03-15 01:50:24 +03:00
2016-11-10 22:19:17 +03:00
def test_clear_attributes(self):
provider = self.singleton_cls(Example) \
.add_attributes(attribute1=5, attribute2=6) \
.clear_attributes()
self.assertEqual(provider.attributes, dict())
2015-03-15 01:50:24 +03:00
2016-11-10 22:19:17 +03:00
def test_call_overridden(self):
provider = self.singleton_cls(Example)
overriding_provider1 = self.singleton_cls(dict)
overriding_provider2 = self.singleton_cls(list)
provider.override(overriding_provider1)
provider.override(overriding_provider2)
2015-03-15 01:50:24 +03:00
2016-11-10 22:19:17 +03:00
instance1 = provider()
2015-03-15 01:50:24 +03:00
instance2 = provider()
2016-11-10 22:19:17 +03:00
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, list)
self.assertIsInstance(instance2, list)
2016-11-10 22:19:17 +03:00
def test_deepcopy(self):
provider = self.singleton_cls(Example)
2016-05-18 00:05:10 +03:00
2016-11-10 22:19:17 +03:00
provider_copy = providers.deepcopy(provider)
2016-11-10 22:19:17 +03:00
self.assertIsNot(provider, provider_copy)
self.assertIs(provider.cls, provider_copy.cls)
self.assertIsInstance(provider, self.singleton_cls)
2016-11-10 22:19:17 +03:00
def test_deepcopy_from_memo(self):
provider = self.singleton_cls(Example)
provider_copy_memo = self.singleton_cls(Example)
2016-11-10 22:19:17 +03:00
provider_copy = providers.deepcopy(
provider, memo={id(provider): provider_copy_memo})
2016-11-10 22:19:17 +03:00
self.assertIs(provider_copy, provider_copy_memo)
2016-11-10 22:19:17 +03:00
def test_deepcopy_args(self):
provider = self.singleton_cls(Example)
dependent_provider1 = self.singleton_cls(list)
dependent_provider2 = self.singleton_cls(dict)
2015-12-11 12:18:09 +03:00
2016-11-10 22:19:17 +03:00
provider.add_args(dependent_provider1, dependent_provider2)
2015-12-11 12:18:09 +03:00
2016-11-10 22:19:17 +03:00
provider_copy = providers.deepcopy(provider)
dependent_provider_copy1 = provider_copy.args[0]
dependent_provider_copy2 = provider_copy.args[1]
2016-11-10 22:19:17 +03:00
self.assertNotEqual(provider.args, provider_copy.args)
2016-11-10 22:19:17 +03:00
self.assertIs(dependent_provider1.cls, dependent_provider_copy1.cls)
self.assertIsNot(dependent_provider1, dependent_provider_copy1)
2016-11-10 22:19:17 +03:00
self.assertIs(dependent_provider2.cls, dependent_provider_copy2.cls)
self.assertIsNot(dependent_provider2, dependent_provider_copy2)
2016-11-10 22:19:17 +03:00
def test_deepcopy_kwargs(self):
provider = self.singleton_cls(Example)
dependent_provider1 = self.singleton_cls(list)
dependent_provider2 = self.singleton_cls(dict)
2016-11-10 22:19:17 +03:00
provider.add_kwargs(a1=dependent_provider1, a2=dependent_provider2)
2016-11-10 22:19:17 +03:00
provider_copy = providers.deepcopy(provider)
dependent_provider_copy1 = provider_copy.kwargs['a1']
dependent_provider_copy2 = provider_copy.kwargs['a2']
2016-11-10 22:19:17 +03:00
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
2016-11-10 22:19:17 +03:00
self.assertIs(dependent_provider1.cls, dependent_provider_copy1.cls)
self.assertIsNot(dependent_provider1, dependent_provider_copy1)
2016-11-10 22:19:17 +03:00
self.assertIs(dependent_provider2.cls, dependent_provider_copy2.cls)
self.assertIsNot(dependent_provider2, dependent_provider_copy2)
2016-11-10 22:19:17 +03:00
def test_deepcopy_attributes(self):
provider = self.singleton_cls(Example)
dependent_provider1 = self.singleton_cls(list)
dependent_provider2 = self.singleton_cls(dict)
2016-11-10 22:19:17 +03:00
provider.add_attributes(a1=dependent_provider1, a2=dependent_provider2)
2016-11-10 22:19:17 +03:00
provider_copy = providers.deepcopy(provider)
dependent_provider_copy1 = provider_copy.attributes['a1']
dependent_provider_copy2 = provider_copy.attributes['a2']
2016-11-10 22:19:17 +03:00
self.assertNotEqual(provider.attributes, provider_copy.attributes)
2016-11-10 22:19:17 +03:00
self.assertIs(dependent_provider1.cls, dependent_provider_copy1.cls)
self.assertIsNot(dependent_provider1, dependent_provider_copy1)
2016-11-10 22:19:17 +03:00
self.assertIs(dependent_provider2.cls, dependent_provider_copy2.cls)
self.assertIsNot(dependent_provider2, dependent_provider_copy2)
2016-11-10 22:19:17 +03:00
def test_deepcopy_overridden(self):
provider = self.singleton_cls(Example)
object_provider = providers.Object(object())
2016-11-10 22:19:17 +03:00
provider.override(object_provider)
2016-11-10 22:19:17 +03:00
provider_copy = providers.deepcopy(provider)
object_provider_copy = provider_copy.overridden[0]
2016-11-10 22:19:17 +03:00
self.assertIsNot(provider, provider_copy)
self.assertIs(provider.cls, provider_copy.cls)
self.assertIsInstance(provider, self.singleton_cls)
2016-11-10 22:19:17 +03:00
self.assertIsNot(object_provider, object_provider_copy)
self.assertIsInstance(object_provider_copy, providers.Object)
def test_deepcopy_with_sys_streams(self):
provider = providers.Singleton(Example)
provider.add_args(sys.stdin)
provider.add_kwargs(a2=sys.stdout)
provider.add_attributes(a3=sys.stderr)
provider_copy = providers.deepcopy(provider)
self.assertIsNot(provider, provider_copy)
self.assertIsInstance(provider_copy, providers.Singleton)
self.assertIs(provider.args[0], sys.stdin)
self.assertIs(provider.kwargs['a2'], sys.stdout)
self.assertIs(provider.attributes['a3'], sys.stderr)
2016-11-10 22:19:17 +03:00
def test_reset(self):
provider = self.singleton_cls(object)
instance1 = provider()
2016-11-10 22:19:17 +03:00
self.assertIsInstance(instance1, object)
2016-11-10 22:19:17 +03:00
provider.reset()
instance2 = provider()
self.assertIsInstance(instance2, object)
2016-11-10 22:19:17 +03:00
self.assertIsNot(instance1, instance2)
2016-11-10 22:19:17 +03:00
class SingletonTests(_BaseSingletonTestCase, unittest.TestCase):
2016-11-10 22:19:17 +03:00
singleton_cls = providers.Singleton
2016-11-10 22:19:17 +03:00
def test_repr(self):
provider = self.singleton_cls(Example)
2016-11-10 22:19:17 +03:00
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
2016-11-10 22:19:17 +03:00
'Singleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
2016-11-10 22:19:17 +03:00
class DelegatedSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
2016-11-10 22:19:17 +03:00
singleton_cls = providers.DelegatedSingleton
2016-11-10 22:19:17 +03:00
def test_is_delegated_provider(self):
provider = self.singleton_cls(object)
self.assertTrue(providers.is_delegated(provider))
2016-11-10 22:19:17 +03:00
def test_repr(self):
provider = self.singleton_cls(Example)
2016-11-10 22:19:17 +03:00
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
2016-11-10 22:19:17 +03:00
'DelegatedSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
2016-11-10 22:19:17 +03:00
class ThreadLocalSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
2016-11-10 22:19:17 +03:00
singleton_cls = providers.ThreadLocalSingleton
2016-11-10 22:19:17 +03:00
def test_repr(self):
provider = providers.ThreadLocalSingleton(Example)
2016-11-10 22:19:17 +03:00
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
2016-11-10 22:19:17 +03:00
'ThreadLocalSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
def test_reset(self):
provider = providers.ThreadLocalSingleton(Example)
instance1 = provider()
self.assertIsInstance(instance1, Example)
provider.reset()
instance2 = provider()
self.assertIsInstance(instance2, Example)
self.assertIsNot(instance1, instance2)
2016-11-10 22:19:17 +03:00
class DelegatedThreadLocalSingletonTests(_BaseSingletonTestCase,
unittest.TestCase):
2016-11-10 22:19:17 +03:00
singleton_cls = providers.DelegatedThreadLocalSingleton
2016-11-10 22:19:17 +03:00
def test_is_delegated_provider(self):
provider = self.singleton_cls(object)
self.assertTrue(providers.is_delegated(provider))
2016-11-10 22:19:17 +03:00
def test_repr(self):
provider = self.singleton_cls(Example)
2016-11-10 22:19:17 +03:00
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
2016-11-10 22:19:17 +03:00
'DelegatedThreadLocalSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
2016-11-10 22:19:17 +03:00
class ThreadSafeSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
2016-11-10 22:19:17 +03:00
singleton_cls = providers.ThreadSafeSingleton
def test_repr(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(Example)
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
2016-11-10 22:19:17 +03:00
'ThreadSafeSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
2016-11-10 22:19:17 +03:00
class DelegatedThreadSafeSingletonTests(_BaseSingletonTestCase,
unittest.TestCase):
2016-11-10 22:19:17 +03:00
singleton_cls = providers.DelegatedThreadSafeSingleton
def test_is_delegated_provider(self):
2016-11-10 22:19:17 +03:00
provider = self.singleton_cls(object)
self.assertTrue(providers.is_delegated(provider))
2016-11-10 22:19:17 +03:00
def test_repr(self):
provider = self.singleton_cls(Example)
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
2016-11-10 22:19:17 +03:00
'DelegatedThreadSafeSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
2017-04-06 18:17:06 +03:00
class AbstractSingletonTests(unittest.TestCase):
def test_inheritance(self):
self.assertIsInstance(providers.AbstractSingleton(Example),
providers.BaseSingleton)
def test_call_overridden_by_singleton(self):
provider = providers.AbstractSingleton(object)
provider.override(providers.Singleton(Example))
self.assertIsInstance(provider(), Example)
def test_call_overridden_by_delegated_singleton(self):
provider = providers.AbstractSingleton(object)
provider.override(providers.DelegatedSingleton(Example))
self.assertIsInstance(provider(), Example)
def test_call_not_overridden(self):
provider = providers.AbstractSingleton(object)
with self.assertRaises(errors.Error):
provider()
def test_reset_overridden(self):
provider = providers.AbstractSingleton(object)
provider.override(providers.Singleton(Example))
instance1 = provider()
provider.reset()
instance2 = provider()
self.assertIsNot(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_reset_not_overridden(self):
provider = providers.AbstractSingleton(object)
with self.assertRaises(errors.Error):
provider.reset()
def test_override_by_not_singleton(self):
provider = providers.AbstractSingleton(object)
with self.assertRaises(errors.Error):
provider.override(providers.Factory(object))
def test_repr(self):
provider = providers.AbstractSingleton(Example)
self.assertEqual(repr(provider),
'<dependency_injector.providers.'
'AbstractSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
2017-07-09 23:34:14 +03:00
class SingletonDelegateTests(unittest.TestCase):
def setUp(self):
self.delegated = providers.Singleton(Example)
self.delegate = providers.SingletonDelegate(self.delegated)
def test_is_delegate(self):
self.assertIsInstance(self.delegate, providers.Delegate)
def test_init_with_not_singleton(self):
self.assertRaises(errors.Error,
providers.SingletonDelegate,
providers.Object(object()))