2016-11-04 21:45:26 +03:00
|
|
|
"""Dependency injector singleton providers unit tests."""
|
2015-03-12 13:45:15 +03:00
|
|
|
|
2021-03-06 04:17:28 +03:00
|
|
|
import 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
|
|
|
|
2021-04-19 04:37:55 +03:00
|
|
|
from .singleton_common import Example, _BaseSingletonTestCase
|
2021-03-03 16:28:10 +03:00
|
|
|
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
class SingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
singleton_cls = providers.Singleton
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
def test_repr(self):
|
|
|
|
provider = self.singleton_cls(Example)
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
self.assertEqual(repr(provider),
|
2017-03-25 23:38:48 +03:00
|
|
|
'<dependency_injector.providers.'
|
2016-11-10 22:19:17 +03:00
|
|
|
'Singleton({0}) at {1}>'.format(
|
|
|
|
repr(Example),
|
|
|
|
hex(id(provider))))
|
2016-08-18 23:28:39 +03:00
|
|
|
|
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
class DelegatedSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
singleton_cls = providers.DelegatedSingleton
|
2016-08-18 23:28:39 +03:00
|
|
|
|
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-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
def test_repr(self):
|
|
|
|
provider = self.singleton_cls(Example)
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
self.assertEqual(repr(provider),
|
2017-03-25 23:38:48 +03:00
|
|
|
'<dependency_injector.providers.'
|
2016-11-10 22:19:17 +03:00
|
|
|
'DelegatedSingleton({0}) at {1}>'.format(
|
|
|
|
repr(Example),
|
|
|
|
hex(id(provider))))
|
2016-08-18 23:28:39 +03:00
|
|
|
|
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
class ThreadLocalSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
singleton_cls = providers.ThreadLocalSingleton
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
def test_repr(self):
|
|
|
|
provider = providers.ThreadLocalSingleton(Example)
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
self.assertEqual(repr(provider),
|
2017-03-25 23:38:48 +03:00
|
|
|
'<dependency_injector.providers.'
|
2016-11-10 22:19:17 +03:00
|
|
|
'ThreadLocalSingleton({0}) at {1}>'.format(
|
|
|
|
repr(Example),
|
|
|
|
hex(id(provider))))
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2019-03-22 05:04:20 +03:00
|
|
|
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)
|
|
|
|
|
2021-03-03 16:28:10 +03:00
|
|
|
def test_reset_clean(self):
|
|
|
|
provider = providers.ThreadLocalSingleton(Example)
|
|
|
|
instance1 = provider()
|
|
|
|
|
|
|
|
provider.reset()
|
|
|
|
provider.reset()
|
|
|
|
|
|
|
|
instance2 = provider()
|
|
|
|
self.assertIsNot(instance1, instance2)
|
|
|
|
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
class DelegatedThreadLocalSingletonTests(_BaseSingletonTestCase,
|
|
|
|
unittest.TestCase):
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
singleton_cls = providers.DelegatedThreadLocalSingleton
|
2016-08-18 23:28:39 +03:00
|
|
|
|
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-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
def test_repr(self):
|
|
|
|
provider = self.singleton_cls(Example)
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
self.assertEqual(repr(provider),
|
2017-03-25 23:38:48 +03:00
|
|
|
'<dependency_injector.providers.'
|
2016-11-10 22:19:17 +03:00
|
|
|
'DelegatedThreadLocalSingleton({0}) at {1}>'.format(
|
|
|
|
repr(Example),
|
|
|
|
hex(id(provider))))
|
2016-08-18 23:28:39 +03:00
|
|
|
|
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
class ThreadSafeSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
singleton_cls = providers.ThreadSafeSingleton
|
2016-08-18 23:28:39 +03:00
|
|
|
|
|
|
|
def test_repr(self):
|
2016-11-10 22:19:17 +03:00
|
|
|
provider = self.singleton_cls(Example)
|
2016-08-18 23:28:39 +03:00
|
|
|
|
|
|
|
self.assertEqual(repr(provider),
|
2017-03-25 23:38:48 +03:00
|
|
|
'<dependency_injector.providers.'
|
2016-11-10 22:19:17 +03:00
|
|
|
'ThreadSafeSingleton({0}) at {1}>'.format(
|
2016-08-18 23:28:39 +03:00
|
|
|
repr(Example),
|
|
|
|
hex(id(provider))))
|
|
|
|
|
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
class DelegatedThreadSafeSingletonTests(_BaseSingletonTestCase,
|
|
|
|
unittest.TestCase):
|
2016-08-18 23:28:39 +03:00
|
|
|
|
2016-11-10 22:19:17 +03:00
|
|
|
singleton_cls = providers.DelegatedThreadSafeSingleton
|
2016-08-18 23:28:39 +03:00
|
|
|
|
|
|
|
def test_is_delegated_provider(self):
|
2016-11-10 22:19:17 +03:00
|
|
|
provider = self.singleton_cls(object)
|
2016-11-06 18:14:12 +03:00
|
|
|
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),
|
2017-03-25 23:38:48 +03:00
|
|
|
'<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()))
|