2015-03-12 13:46:14 +03:00
|
|
|
"""Objects providers unittests."""
|
2015-03-12 13:45:15 +03:00
|
|
|
|
2015-03-12 13:46:14 +03:00
|
|
|
import unittest2 as unittest
|
2015-03-14 01:40:35 +03:00
|
|
|
from collections import namedtuple
|
2015-03-12 13:46:14 +03:00
|
|
|
|
|
|
|
from objects.providers import Provider
|
2015-03-14 01:02:01 +03:00
|
|
|
from objects.providers import Delegate
|
2015-03-12 13:46:14 +03:00
|
|
|
from objects.providers import NewInstance
|
|
|
|
from objects.providers import Singleton
|
|
|
|
from objects.providers import Scoped
|
|
|
|
from objects.providers import ExternalDependency
|
|
|
|
from objects.providers import Class
|
|
|
|
from objects.providers import Object
|
|
|
|
from objects.providers import Function
|
|
|
|
from objects.providers import Value
|
|
|
|
from objects.providers import Callable
|
|
|
|
from objects.providers import Config
|
|
|
|
|
2015-03-14 01:40:35 +03:00
|
|
|
from objects.injections import InitArg
|
|
|
|
from objects.injections import Attribute
|
|
|
|
from objects.injections import Method
|
|
|
|
|
2015-03-12 13:46:14 +03:00
|
|
|
from objects.utils import is_provider
|
|
|
|
|
2015-03-13 18:31:07 +03:00
|
|
|
from objects.errors import Error
|
|
|
|
|
2015-03-12 13:46:14 +03:00
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class ProviderTests(unittest.TestCase):
|
2015-03-12 13:46:14 +03:00
|
|
|
|
|
|
|
"""Provider test cases."""
|
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
def setUp(self):
|
|
|
|
"""Set test cases environment up."""
|
|
|
|
self.provider = Provider()
|
2015-03-12 13:46:14 +03:00
|
|
|
|
|
|
|
def test_is_provider(self):
|
|
|
|
"""Test `is_provider` check."""
|
2015-03-13 18:46:03 +03:00
|
|
|
self.assertTrue(is_provider(self.provider))
|
2015-03-12 13:46:14 +03:00
|
|
|
|
|
|
|
def test_call(self):
|
|
|
|
"""Test call."""
|
2015-03-13 18:46:03 +03:00
|
|
|
self.assertRaises(NotImplementedError, self.provider.__call__)
|
2015-03-12 13:46:14 +03:00
|
|
|
|
|
|
|
def test_delegate(self):
|
|
|
|
"""Test creating of provider delegation."""
|
2015-03-13 18:46:03 +03:00
|
|
|
delegate1 = self.provider.delegate()
|
2015-03-12 13:46:14 +03:00
|
|
|
|
2015-03-14 01:02:01 +03:00
|
|
|
self.assertIsInstance(delegate1, Delegate)
|
2015-03-13 18:46:03 +03:00
|
|
|
self.assertIs(delegate1.delegated, self.provider)
|
|
|
|
|
|
|
|
delegate2 = self.provider.delegate()
|
|
|
|
|
2015-03-14 01:02:01 +03:00
|
|
|
self.assertIsInstance(delegate2, Delegate)
|
2015-03-13 18:46:03 +03:00
|
|
|
self.assertIs(delegate2.delegated, self.provider)
|
|
|
|
|
|
|
|
self.assertIsNot(delegate1, delegate2)
|
2015-03-12 20:24:29 +03:00
|
|
|
|
|
|
|
def test_override(self):
|
|
|
|
"""Test provider overriding."""
|
|
|
|
overriding_provider = Provider()
|
2015-03-13 18:46:03 +03:00
|
|
|
self.provider.override(overriding_provider)
|
|
|
|
self.assertTrue(self.provider.overridden)
|
2015-03-12 20:24:29 +03:00
|
|
|
|
|
|
|
def test_override_with_not_provider(self):
|
|
|
|
"""Test provider overriding with not provider instance."""
|
2015-03-13 18:46:03 +03:00
|
|
|
self.assertRaises(Error, self.provider.override, object())
|
2015-03-12 20:24:29 +03:00
|
|
|
|
|
|
|
def test_last_overriding(self):
|
|
|
|
"""Test getting last overriding provider."""
|
|
|
|
overriding_provider1 = Provider()
|
|
|
|
overriding_provider2 = Provider()
|
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
self.provider.override(overriding_provider1)
|
|
|
|
self.assertIs(self.provider.last_overriding, overriding_provider1)
|
2015-03-12 20:24:29 +03:00
|
|
|
|
2015-03-13 18:46:03 +03:00
|
|
|
self.provider.override(overriding_provider2)
|
|
|
|
self.assertIs(self.provider.last_overriding, overriding_provider2)
|
2015-03-12 20:24:29 +03:00
|
|
|
|
|
|
|
def test_last_overriding_of_not_overridden_provider(self):
|
|
|
|
"""Test getting last overriding from not overridden provider."""
|
|
|
|
try:
|
2015-03-13 18:46:03 +03:00
|
|
|
self.provider.last_overriding
|
2015-03-13 18:31:07 +03:00
|
|
|
except Error:
|
2015-03-12 20:24:29 +03:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail('Got en error in {}'.format(
|
|
|
|
str(self.test_last_overriding_of_not_overridden_provider)))
|
2015-03-13 18:46:03 +03:00
|
|
|
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class DelegateTests(unittest.TestCase):
|
2015-03-13 18:46:03 +03:00
|
|
|
|
2015-03-14 01:40:35 +03:00
|
|
|
"""Delegate test cases."""
|
2015-03-13 18:46:03 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set test cases environment up."""
|
|
|
|
self.delegated = Provider()
|
2015-03-14 01:02:01 +03:00
|
|
|
self.delegate = Delegate(delegated=self.delegated)
|
2015-03-13 18:46:03 +03:00
|
|
|
|
|
|
|
def test_is_provider(self):
|
|
|
|
"""Test `is_provider` check."""
|
|
|
|
self.assertTrue(is_provider(self.delegate))
|
|
|
|
|
|
|
|
def test_init_with_not_provider(self):
|
|
|
|
"""Test that delegate accepts only another provider as delegated."""
|
2015-03-14 01:02:01 +03:00
|
|
|
self.assertRaises(Error, Delegate, delegated=object())
|
2015-03-13 18:46:03 +03:00
|
|
|
|
|
|
|
def test_call(self):
|
|
|
|
""" Test returning of delegated provider."""
|
|
|
|
delegated1 = self.delegate()
|
|
|
|
delegated2 = self.delegate()
|
|
|
|
|
|
|
|
self.assertIs(delegated1, self.delegated)
|
|
|
|
self.assertIs(delegated2, self.delegated)
|
2015-03-14 01:02:01 +03:00
|
|
|
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class NewInstanceTests(unittest.TestCase):
|
2015-03-14 01:02:01 +03:00
|
|
|
|
|
|
|
"""NewInstance test cases."""
|
2015-03-14 01:40:35 +03:00
|
|
|
|
2015-03-14 16:57:30 +03:00
|
|
|
class Example(object):
|
|
|
|
|
|
|
|
"""Example class for NewInstance provider tests."""
|
|
|
|
|
|
|
|
def __init__(self, init_arg1=None, init_arg2=None):
|
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:param init_arg1:
|
|
|
|
:param init_arg2:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.init_arg1 = init_arg1
|
|
|
|
self.init_arg2 = init_arg2
|
|
|
|
|
|
|
|
self.attribute1 = None
|
|
|
|
self.attribute2 = None
|
|
|
|
|
|
|
|
self.method1_value = None
|
|
|
|
self.method2_value = None
|
|
|
|
|
|
|
|
def method1(self, value):
|
|
|
|
"""Setter method 1."""
|
|
|
|
self.method1_value = value
|
|
|
|
|
|
|
|
def method2(self, value):
|
|
|
|
"""Setter method 2."""
|
|
|
|
self.method2_value = value
|
|
|
|
|
2015-03-14 01:40:35 +03:00
|
|
|
def test_is_provider(self):
|
|
|
|
"""Test `is_provider` check."""
|
2015-03-14 16:57:30 +03:00
|
|
|
self.assertTrue(is_provider(NewInstance(self.Example)))
|
|
|
|
|
|
|
|
def test_init_with_not_class(self):
|
|
|
|
"""Test creation of provider with not a class."""
|
|
|
|
self.assertRaises(Error, NewInstance, 123)
|
2015-03-14 01:40:35 +03:00
|
|
|
|
|
|
|
def test_call(self):
|
|
|
|
"""Test creation of new instances."""
|
2015-03-14 16:57:30 +03:00
|
|
|
provider = NewInstance(self.Example)
|
|
|
|
instance1 = provider()
|
|
|
|
instance2 = provider()
|
|
|
|
|
|
|
|
self.assertIsNot(instance1, instance2)
|
|
|
|
self.assertIsInstance(instance1, self.Example)
|
|
|
|
self.assertIsInstance(instance2, self.Example)
|
|
|
|
|
|
|
|
def test_call_with_init_args(self):
|
|
|
|
"""Test creation of new instances with init args injections."""
|
|
|
|
provider = NewInstance(self.Example,
|
|
|
|
InitArg('init_arg1', 'i1'),
|
|
|
|
InitArg('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.assertIsNot(instance1, instance2)
|
|
|
|
self.assertIsInstance(instance1, self.Example)
|
|
|
|
self.assertIsInstance(instance2, self.Example)
|
|
|
|
|
|
|
|
def test_call_with_attributes(self):
|
|
|
|
"""Test creation of new instances with attribute injections."""
|
|
|
|
provider = NewInstance(self.Example,
|
|
|
|
Attribute('attribute1', 'a1'),
|
|
|
|
Attribute('attribute2', 'a2'))
|
|
|
|
|
|
|
|
instance1 = provider()
|
2015-03-14 01:40:35 +03:00
|
|
|
instance2 = provider()
|
|
|
|
|
2015-03-14 16:57:30 +03:00
|
|
|
self.assertEqual(instance1.attribute1, 'a1')
|
|
|
|
self.assertEqual(instance1.attribute2, 'a2')
|
|
|
|
|
|
|
|
self.assertEqual(instance2.attribute1, 'a1')
|
|
|
|
self.assertEqual(instance2.attribute2, 'a2')
|
|
|
|
|
|
|
|
self.assertIsNot(instance1, instance2)
|
|
|
|
self.assertIsInstance(instance1, self.Example)
|
|
|
|
self.assertIsInstance(instance2, self.Example)
|
|
|
|
|
|
|
|
def test_call_with_methods(self):
|
|
|
|
"""Test creation of new instances with method injections."""
|
|
|
|
provider = NewInstance(self.Example,
|
|
|
|
Method('method1', 'm1'),
|
|
|
|
Method('method2', 'm2'))
|
|
|
|
|
|
|
|
instance1 = provider()
|
|
|
|
instance2 = provider()
|
|
|
|
|
|
|
|
self.assertEqual(instance1.method1_value, 'm1')
|
|
|
|
self.assertEqual(instance1.method2_value, 'm2')
|
|
|
|
|
|
|
|
self.assertEqual(instance2.method1_value, 'm1')
|
|
|
|
self.assertEqual(instance2.method2_value, 'm2')
|
|
|
|
|
2015-03-14 01:40:35 +03:00
|
|
|
self.assertIsNot(instance1, instance2)
|
2015-03-14 16:57:30 +03:00
|
|
|
self.assertIsInstance(instance1, self.Example)
|
|
|
|
self.assertIsInstance(instance2, self.Example)
|
2015-03-14 01:40:35 +03:00
|
|
|
|
2015-03-15 01:41:19 +03:00
|
|
|
def test_call_with_context_args(self):
|
|
|
|
"""Test creation of new instances with context args."""
|
|
|
|
provider = NewInstance(self.Example)
|
|
|
|
instance = provider(11, 22)
|
|
|
|
|
|
|
|
self.assertEqual(instance.init_arg1, 11)
|
|
|
|
self.assertEqual(instance.init_arg2, 22)
|
|
|
|
|
|
|
|
def test_call_with_context_kwargs(self):
|
|
|
|
"""Test creation of new instances with context kwargs."""
|
|
|
|
provider = NewInstance(self.Example,
|
|
|
|
InitArg('init_arg1', 1))
|
|
|
|
|
|
|
|
instance1 = provider(init_arg2=22)
|
|
|
|
self.assertEqual(instance1.init_arg1, 1)
|
|
|
|
self.assertEqual(instance1.init_arg2, 22)
|
|
|
|
|
|
|
|
instance1 = provider(init_arg1=11, init_arg2=22)
|
|
|
|
self.assertEqual(instance1.init_arg1, 11)
|
|
|
|
self.assertEqual(instance1.init_arg2, 22)
|
|
|
|
|
2015-03-14 01:40:35 +03:00
|
|
|
def test_call_overridden(self):
|
|
|
|
"""Test creation of new instances on overridden provider."""
|
2015-03-14 16:57:30 +03:00
|
|
|
provider = NewInstance(self.Example)
|
2015-03-14 01:40:35 +03:00
|
|
|
overriding_provider1 = NewInstance(dict)
|
|
|
|
overriding_provider2 = NewInstance(list)
|
|
|
|
|
|
|
|
provider.override(overriding_provider1)
|
|
|
|
provider.override(overriding_provider2)
|
|
|
|
|
|
|
|
instance1 = provider()
|
|
|
|
instance2 = provider()
|
|
|
|
|
|
|
|
self.assertIsNot(instance1, instance2)
|
|
|
|
self.assertIsInstance(instance1, list)
|
|
|
|
self.assertIsInstance(instance2, list)
|
2015-03-15 01:50:24 +03:00
|
|
|
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
class SingletonTests(unittest.TestCase):
|
2015-03-15 01:50:24 +03:00
|
|
|
|
|
|
|
"""Singleton test cases."""
|
|
|
|
|
|
|
|
def test_call(self):
|
|
|
|
"""Test creation and returning of single object."""
|
|
|
|
provider = Singleton(object)
|
|
|
|
|
|
|
|
instance1 = provider()
|
|
|
|
instance2 = provider()
|
|
|
|
|
|
|
|
self.assertIsInstance(instance1, object)
|
|
|
|
self.assertIsInstance(instance2, object)
|
|
|
|
self.assertIs(instance1, instance2)
|
|
|
|
|
|
|
|
def test_reset(self):
|
|
|
|
"""Test creation and reset of single object."""
|
|
|
|
provider = Singleton(object)
|
|
|
|
|
|
|
|
instance1 = provider()
|
|
|
|
self.assertIsInstance(instance1, object)
|
|
|
|
|
|
|
|
provider.reset()
|
|
|
|
|
|
|
|
instance2 = provider()
|
|
|
|
self.assertIsInstance(instance1, object)
|
|
|
|
|
|
|
|
self.assertIsNot(instance1, instance2)
|
2015-03-15 16:31:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ScopedTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Scoped test cases."""
|
|
|
|
|
|
|
|
APPLICATION_SCOPE = 'application'
|
|
|
|
REQUEST_SCOPE = 'request'
|
|
|
|
|
2015-03-15 16:35:33 +03:00
|
|
|
def setUp(self):
|
|
|
|
"""Set test cases environment up."""
|
|
|
|
self.provider = Scoped(object)
|
|
|
|
|
2015-03-15 16:31:05 +03:00
|
|
|
def test_call(self):
|
|
|
|
"""Test creation and returning of scope single object."""
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.APPLICATION_SCOPE)
|
2015-03-15 16:31:05 +03:00
|
|
|
|
2015-03-15 16:35:33 +03:00
|
|
|
instance1 = self.provider()
|
|
|
|
instance2 = self.provider()
|
2015-03-15 16:31:05 +03:00
|
|
|
|
|
|
|
self.assertIsInstance(instance1, object)
|
|
|
|
self.assertIsInstance(instance2, object)
|
|
|
|
self.assertIs(instance1, instance2)
|
|
|
|
|
|
|
|
def test_call_several_scopes(self):
|
|
|
|
"""Test creation of several scopes instances."""
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.APPLICATION_SCOPE)
|
|
|
|
app_instance1 = self.provider()
|
|
|
|
app_instance2 = self.provider()
|
2015-03-15 16:31:05 +03:00
|
|
|
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.REQUEST_SCOPE)
|
|
|
|
request_instance1 = self.provider()
|
|
|
|
request_instance2 = self.provider()
|
2015-03-15 16:31:05 +03:00
|
|
|
|
|
|
|
self.assertIsInstance(app_instance1, object)
|
|
|
|
self.assertIsInstance(app_instance2, object)
|
|
|
|
self.assertIs(app_instance1, app_instance2)
|
|
|
|
|
|
|
|
self.assertIsInstance(request_instance1, object)
|
|
|
|
self.assertIsInstance(request_instance2, object)
|
|
|
|
self.assertIs(request_instance1, request_instance2)
|
|
|
|
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.APPLICATION_SCOPE)
|
|
|
|
app_instance3 = self.provider()
|
2015-03-15 16:31:05 +03:00
|
|
|
self.assertIsInstance(app_instance3, object)
|
|
|
|
self.assertIs(app_instance3, app_instance1)
|
|
|
|
self.assertIs(app_instance3, app_instance2)
|
|
|
|
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.REQUEST_SCOPE)
|
|
|
|
request_instance3 = self.provider()
|
2015-03-15 16:31:05 +03:00
|
|
|
self.assertIsInstance(request_instance3, object)
|
|
|
|
self.assertIs(request_instance3, request_instance1)
|
|
|
|
self.assertIs(request_instance3, request_instance2)
|
|
|
|
|
|
|
|
def test_call_not_in_scope(self):
|
|
|
|
"""Test creation of instance with no active scope."""
|
2015-03-15 16:35:33 +03:00
|
|
|
self.assertRaises(Error, self.provider)
|
2015-03-15 16:31:05 +03:00
|
|
|
|
|
|
|
def test_call_in_out_scope(self):
|
|
|
|
"""Test creation of instances within in and out of scope."""
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.REQUEST_SCOPE)
|
|
|
|
instance1 = self.provider()
|
|
|
|
instance2 = self.provider()
|
|
|
|
self.provider.out_of_scope(self.REQUEST_SCOPE)
|
2015-03-15 16:31:05 +03:00
|
|
|
|
2015-03-15 16:35:33 +03:00
|
|
|
self.provider.in_scope(self.REQUEST_SCOPE)
|
|
|
|
instance3 = self.provider()
|
|
|
|
instance4 = self.provider()
|
|
|
|
self.provider.out_of_scope(self.REQUEST_SCOPE)
|
2015-03-15 16:31:05 +03:00
|
|
|
|
|
|
|
self.assertIs(instance1, instance2)
|
|
|
|
self.assertIs(instance3, instance4)
|
|
|
|
|
|
|
|
self.assertIsNot(instance1, instance3)
|
|
|
|
self.assertIsNot(instance2, instance3)
|
|
|
|
|
|
|
|
self.assertIsNot(instance1, instance4)
|
|
|
|
self.assertIsNot(instance2, instance4)
|
|
|
|
|
|
|
|
def test_out_of_scope(self):
|
|
|
|
"""Test call `out_of_scope()` on provider that has no such scope."""
|
2015-03-15 16:35:33 +03:00
|
|
|
self.assertRaises(Error,
|
|
|
|
self.provider.out_of_scope,
|
|
|
|
self.REQUEST_SCOPE)
|
2015-03-16 01:52:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ExternalDependencyTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""ExternalDependency test cases."""
|
|
|
|
|
2015-03-16 02:10:43 +03:00
|
|
|
def test_call_satisfied(self):
|
|
|
|
"""Test call of satisfied external dependency."""
|
|
|
|
provider = ExternalDependency(instance_of=object)
|
|
|
|
provider.satisfy(NewInstance(object))
|
|
|
|
self.assertIsInstance(provider(), object)
|
|
|
|
|
|
|
|
def test_call_satisfied_but_not_instance_of(self):
|
|
|
|
"""Test call of satisfied external dependency, but not instance of."""
|
|
|
|
provider = ExternalDependency(instance_of=list)
|
|
|
|
provider.satisfy(NewInstance(dict))
|
|
|
|
self.assertRaises(Error, provider)
|
|
|
|
|
|
|
|
def test_call_not_satisfied(self):
|
|
|
|
"""Test call of not satisfied external dependency."""
|
|
|
|
provider = ExternalDependency(instance_of=object)
|
|
|
|
self.assertRaises(Error, provider)
|
|
|
|
|
2015-03-16 01:52:29 +03:00
|
|
|
|
|
|
|
class StaticProvidersTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Static providers test cases."""
|
|
|
|
|
|
|
|
def test_call_class_provider(self):
|
|
|
|
"""Test Class provider call."""
|
|
|
|
self.assertIs(Class(dict)(), dict)
|
|
|
|
|
|
|
|
def test_call_object_provider(self):
|
|
|
|
"""Test Object provider call."""
|
|
|
|
obj = object()
|
|
|
|
self.assertIs(Object(obj)(), obj)
|
|
|
|
|
|
|
|
def test_call_function_provider(self):
|
|
|
|
"""Test Function provider call."""
|
|
|
|
self.assertIs(Function(map)(), map)
|
|
|
|
|
|
|
|
def test_call_value_provider(self):
|
|
|
|
"""Test Value provider call."""
|
|
|
|
self.assertEqual(Value(123)(), 123)
|
|
|
|
|
|
|
|
def test_call_overridden_class_provider(self):
|
|
|
|
"""Test overridden Class provider call."""
|
|
|
|
cls_provider = Class(dict)
|
|
|
|
cls_provider.override(Object(list))
|
|
|
|
self.assertIs(cls_provider(), list)
|
|
|
|
|
|
|
|
def test_call_overridden_object_provider(self):
|
|
|
|
"""Test overridden Object provider call."""
|
|
|
|
obj1 = object()
|
|
|
|
obj2 = object()
|
|
|
|
obj_provider = Object(obj1)
|
|
|
|
obj_provider.override(Object(obj2))
|
|
|
|
self.assertIs(obj_provider(), obj2)
|
|
|
|
|
|
|
|
def test_call_overridden_function_provider(self):
|
|
|
|
"""Test overridden Function provider call."""
|
|
|
|
function_provider = Function(map)
|
|
|
|
function_provider.override(Function(reduce))
|
|
|
|
self.assertIs(function_provider(), reduce)
|
|
|
|
|
|
|
|
def test_call_overridden_value_provider(self):
|
|
|
|
"""Test overridden Value provider call."""
|
|
|
|
value_provider = Value(123)
|
|
|
|
value_provider.override(Value(321))
|
|
|
|
self.assertEqual(value_provider(), 321)
|