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

448 lines
14 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
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
class SingletonTests(unittest.TestCase):
2015-03-15 01:50:24 +03:00
def test_is_provider(self):
2016-11-04 16:48:26 +03:00
self.assertTrue(providers.is_provider(providers.Singleton(Example)))
def test_init_with_callable(self):
2015-11-23 22:45:58 +03:00
self.assertTrue(providers.Singleton(credits))
def test_init_with_not_callable(self):
2015-11-23 22:45:58 +03:00
self.assertRaises(errors.Error, providers.Singleton, 123)
2015-12-11 23:46:49 +03:00
def test_init_with_valid_provided_type(self):
class ExampleProvider(providers.Singleton):
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):
class ExampleProvider(providers.Singleton):
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):
class ExampleProvider(providers.Singleton):
provided_type = Example
with self.assertRaises(errors.Error):
ExampleProvider(list)
2015-03-15 01:50:24 +03:00
def test_call(self):
2015-11-23 22:45:58 +03:00
provider = providers.Singleton(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-05-29 16:39:39 +03:00
provider = providers.Singleton(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-05-29 16:39:39 +03:00
provider = providers.Singleton(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-05-29 16:39:39 +03:00
provider = providers.Singleton(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-05-29 16:39:39 +03:00
provider = providers.Singleton(Example)
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):
2015-11-23 22:45:58 +03:00
provider = providers.Singleton(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-05-29 16:39:39 +03:00
provider = providers.Singleton(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-05-29 16:39:39 +03:00
provider = providers.Singleton(Example, 11)
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):
provider = providers.Singleton(Example) \
.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)
def test_call_overridden(self):
2015-11-23 22:45:58 +03:00
provider = providers.Singleton(Example)
overriding_provider1 = providers.Singleton(dict)
overriding_provider2 = providers.Singleton(object)
provider.override(overriding_provider1)
provider.override(overriding_provider2)
instance1 = provider()
instance2 = provider()
self.assertIs(instance1, instance2)
2015-03-15 01:50:24 +03:00
self.assertIsInstance(instance1, object)
self.assertIsInstance(instance2, object)
2015-03-15 01:50:24 +03:00
def test_reset(self):
2015-11-23 22:45:58 +03:00
provider = providers.Singleton(object)
2015-03-15 01:50:24 +03:00
instance1 = provider()
self.assertIsInstance(instance1, object)
provider.reset()
instance2 = provider()
self.assertIsInstance(instance1, object)
self.assertIsNot(instance1, instance2)
2015-12-11 12:18:09 +03:00
def test_repr(self):
2016-05-29 16:39:39 +03:00
provider = providers.Singleton(Example)
2016-05-18 00:05:10 +03:00
2015-12-11 12:18:09 +03:00
self.assertEqual(repr(provider),
'<dependency_injector.providers.creational.'
2015-12-11 12:18:09 +03:00
'Singleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
class DelegatedSingletonTests(unittest.TestCase):
def test_inheritance(self):
self.assertIsInstance(providers.DelegatedSingleton(object),
providers.Singleton)
def test_is_provider(self):
2016-11-04 16:48:26 +03:00
self.assertTrue(
providers.is_provider(providers.DelegatedSingleton(object)))
def test_is_delegated_provider(self):
2016-05-29 16:39:39 +03:00
provider = providers.DelegatedSingleton(object)
self.assertIs(provider.provide_injection(), provider)
2015-12-11 12:18:09 +03:00
class ThreadLocalSingletonTests(unittest.TestCase):
def test_is_provider(self):
self.assertTrue(
2016-11-04 16:48:26 +03:00
providers.is_provider(providers.ThreadLocalSingleton(Example)))
def test_init_with_callable(self):
self.assertTrue(providers.ThreadLocalSingleton(credits))
def test_init_with_not_callable(self):
self.assertRaises(errors.Error, providers.ThreadLocalSingleton, 123)
def test_init_with_valid_provided_type(self):
class ExampleProvider(providers.ThreadLocalSingleton):
provided_type = Example
example_provider = ExampleProvider(Example, 1, 2)
self.assertIsInstance(example_provider(), Example)
def test_init_with_valid_provided_subtype(self):
class ExampleProvider(providers.ThreadLocalSingleton):
provided_type = Example
class NewExampe(Example):
2016-11-04 16:48:26 +03:00
pass
example_provider = ExampleProvider(NewExampe, 1, 2)
self.assertIsInstance(example_provider(), NewExampe)
def test_init_with_invalid_provided_type(self):
class ExampleProvider(providers.ThreadLocalSingleton):
provided_type = Example
with self.assertRaises(errors.Error):
ExampleProvider(list)
def test_call(self):
provider = providers.ThreadLocalSingleton(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):
provider = providers.ThreadLocalSingleton(Example, 'i1', '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_keyword_args(self):
provider = providers.ThreadLocalSingleton(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):
provider = providers.ThreadLocalSingleton(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):
provider = providers.ThreadLocalSingleton(Example)
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):
provider = providers.ThreadLocalSingleton(Example)
instance = provider(11, 22)
self.assertEqual(instance.init_arg1, 11)
self.assertEqual(instance.init_arg2, 22)
def test_call_with_context_kwargs(self):
provider = providers.ThreadLocalSingleton(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):
provider = providers.ThreadLocalSingleton(Example, 11)
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)
def test_fluent_interface(self):
provider = providers.ThreadLocalSingleton(Example) \
.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)
def test_call_overridden(self):
provider = providers.ThreadLocalSingleton(Example)
overriding_provider1 = providers.ThreadLocalSingleton(dict)
overriding_provider2 = providers.ThreadLocalSingleton(object)
provider.override(overriding_provider1)
provider.override(overriding_provider2)
instance1 = provider()
instance2 = provider()
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, object)
self.assertIsInstance(instance2, object)
def test_reset(self):
provider = providers.ThreadLocalSingleton(object)
instance1 = provider()
self.assertIsInstance(instance1, object)
provider.reset()
instance2 = provider()
self.assertIsInstance(instance1, object)
self.assertIsNot(instance1, instance2)
def test_repr(self):
provider = providers.ThreadLocalSingleton(Example)
self.assertEqual(repr(provider),
'<dependency_injector.providers.creational.'
'ThreadLocalSingleton({0}) at {1}>'.format(
repr(Example),
hex(id(provider))))
class DelegatedThreadLocalSingletonTests(unittest.TestCase):
def test_inheritance(self):
self.assertIsInstance(providers.DelegatedThreadLocalSingleton(object),
providers.ThreadLocalSingleton)
def test_is_provider(self):
2016-11-04 16:48:26 +03:00
self.assertTrue(
providers.is_provider(
providers.DelegatedThreadLocalSingleton(object)))
def test_is_delegated_provider(self):
provider = providers.DelegatedThreadLocalSingleton(object)
self.assertIs(provider.provide_injection(), provider)