adding tests for ProviderDelegate, making of some refactoring

This commit is contained in:
Roman Mogilatov 2015-03-13 17:46:03 +02:00
parent 9d72eaf675
commit abdb1eeac0
3 changed files with 62 additions and 25 deletions

View File

@ -2,7 +2,7 @@
from collections import Iterable from collections import Iterable
from .utils import is_provider from .utils import ensure_is_provider
from .utils import is_injection from .utils import is_injection
from .utils import is_init_arg_injection from .utils import is_init_arg_injection
from .utils import is_attribute_injection from .utils import is_attribute_injection
@ -32,10 +32,7 @@ class Provider(object):
def override(self, provider): def override(self, provider):
"""Override provider with another provider.""" """Override provider with another provider."""
if not is_provider(provider): self.overridden.append(ensure_is_provider(provider))
raise Error('Expected provider as an overriding instance, '
'got {}'.format(str(provider)))
self.overridden.append(provider)
@property @property
def last_overriding(self): def last_overriding(self):
@ -58,7 +55,7 @@ class ProviderDelegate(Provider):
:type delegated: Provider :type delegated: Provider
""" """
self.delegated = delegated self.delegated = ensure_is_provider(delegated)
super(ProviderDelegate, self).__init__() super(ProviderDelegate, self).__init__()
def __call__(self): def __call__(self):

View File

@ -2,6 +2,8 @@
from inspect import isclass from inspect import isclass
from .errors import Error
def is_provider(instance): def is_provider(instance):
"""Check if instance is provider instance.""" """Check if instance is provider instance."""
@ -9,6 +11,14 @@ def is_provider(instance):
hasattr(instance, '__IS_OBJECTS_PROVIDER__')) hasattr(instance, '__IS_OBJECTS_PROVIDER__'))
def ensure_is_provider(instance):
"""Check if instance is provider instance, otherwise raise and error."""
if not is_provider(instance):
raise Error('Expected provider instance, '
'got {}'.format(str(instance)))
return instance
def is_injection(instance): def is_injection(instance):
"""Check if instance is injection instance.""" """Check if instance is injection instance."""
return (not isclass(instance) and return (not isclass(instance) and

View File

@ -24,55 +24,85 @@ class ProviderTest(unittest.TestCase):
"""Provider test cases.""" """Provider test cases."""
def test_init(self): def setUp(self):
"""Test creating and initialization.""" """Set test cases environment up."""
self.assertIsInstance(Provider(), Provider) self.provider = Provider()
def test_is_provider(self): def test_is_provider(self):
"""Test `is_provider` check.""" """Test `is_provider` check."""
self.assertTrue(is_provider(Provider())) self.assertTrue(is_provider(self.provider))
def test_call(self): def test_call(self):
"""Test call.""" """Test call."""
self.assertRaises(NotImplementedError, Provider().__call__) self.assertRaises(NotImplementedError, self.provider.__call__)
def test_delegate(self): def test_delegate(self):
"""Test creating of provider delegation.""" """Test creating of provider delegation."""
provider = Provider() delegate1 = self.provider.delegate()
delegate = provider.delegate()
self.assertIsInstance(delegate, ProviderDelegate) self.assertIsInstance(delegate1, ProviderDelegate)
self.assertIs(delegate.delegated, provider) self.assertIs(delegate1.delegated, self.provider)
delegate2 = self.provider.delegate()
self.assertIsInstance(delegate2, ProviderDelegate)
self.assertIs(delegate2.delegated, self.provider)
self.assertIsNot(delegate1, delegate2)
def test_override(self): def test_override(self):
"""Test provider overriding.""" """Test provider overriding."""
provider = Provider()
overriding_provider = Provider() overriding_provider = Provider()
provider.override(overriding_provider) self.provider.override(overriding_provider)
self.assertTrue(provider.overridden) self.assertTrue(self.provider.overridden)
def test_override_with_not_provider(self): def test_override_with_not_provider(self):
"""Test provider overriding with not provider instance.""" """Test provider overriding with not provider instance."""
self.assertRaises(Error, Provider().override, object()) self.assertRaises(Error, self.provider.override, object())
def test_last_overriding(self): def test_last_overriding(self):
"""Test getting last overriding provider.""" """Test getting last overriding provider."""
provider = Provider()
overriding_provider1 = Provider() overriding_provider1 = Provider()
overriding_provider2 = Provider() overriding_provider2 = Provider()
provider.override(overriding_provider1) self.provider.override(overriding_provider1)
self.assertIs(provider.last_overriding, overriding_provider1) self.assertIs(self.provider.last_overriding, overriding_provider1)
provider.override(overriding_provider2) self.provider.override(overriding_provider2)
self.assertIs(provider.last_overriding, overriding_provider2) self.assertIs(self.provider.last_overriding, overriding_provider2)
def test_last_overriding_of_not_overridden_provider(self): def test_last_overriding_of_not_overridden_provider(self):
"""Test getting last overriding from not overridden provider.""" """Test getting last overriding from not overridden provider."""
try: try:
Provider().last_overriding self.provider.last_overriding
except Error: except Error:
pass pass
else: else:
self.fail('Got en error in {}'.format( self.fail('Got en error in {}'.format(
str(self.test_last_overriding_of_not_overridden_provider))) str(self.test_last_overriding_of_not_overridden_provider)))
class ProviderDelegateTest(unittest.TestCase):
"""ProviderDelegate test cases."""
def setUp(self):
"""Set test cases environment up."""
self.delegated = Provider()
self.delegate = ProviderDelegate(delegated=self.delegated)
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."""
self.assertRaises(Error, ProviderDelegate, delegated=object())
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)