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

207 lines
6.4 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 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
from .singleton_common import Example, _BaseSingletonTestCase
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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"Singleton({0}) at {1}>".format(
2016-11-10 22:19:17 +03:00
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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"DelegatedSingleton({0}) at {1}>".format(
2016-11-10 22:19:17 +03:00
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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"ThreadLocalSingleton({0}) at {1}>".format(
2016-11-10 22:19:17 +03:00
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)
def test_reset_clean(self):
provider = providers.ThreadLocalSingleton(Example)
instance1 = provider()
provider.reset()
provider.reset()
instance2 = provider()
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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"DelegatedThreadLocalSingleton({0}) at {1}>".format(
2016-11-10 22:19:17 +03:00
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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"DelegatedThreadSafeSingleton({0}) at {1}>".format(
2016-11-10 22:19:17 +03:00
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),
2021-10-01 03:09:42 +03:00
"<dependency_injector.providers."
"AbstractSingleton({0}) at {1}>".format(
2017-04-06 18:17:06 +03:00
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()))