Add functionality for resetting of last overriding provider

This commit is contained in:
Roman Mogilatov 2015-07-21 09:29:50 +03:00
parent 0efebb337f
commit 7d3460deba
2 changed files with 50 additions and 25 deletions

View File

@ -47,18 +47,23 @@ class Provider(object):
else: else:
self.overridden = self.overridden + (ensure_is_provider(provider),) self.overridden = self.overridden + (ensure_is_provider(provider),)
def reset_override(self):
"""Reset all overriding providers."""
self.overridden = None
@property @property
def last_overriding(self): def last_overriding(self):
"""Return last overriding provider.""" """Return last overriding provider."""
try: try:
return self.overridden[-1] return self.overridden[-1]
except (TypeError, IndexError): except (TypeError, IndexError):
raise Error('Provider {0} '.format(str(self)) + raise Error('Provider {0} is not overridden'.format(str(self)))
'is not overridden')
def reset_last_overriding(self):
"""Reset last overriding provider."""
if not self.overridden:
raise Error('Provider {0} is not overridden'.format(str(self)))
self.overridden = self.overridden[:-1]
def reset_override(self):
"""Reset all overriding providers."""
self.overridden = None
class Delegate(Provider): class Delegate(Provider):

View File

@ -63,25 +63,6 @@ class ProviderTests(unittest.TestCase):
"""Test provider overriding with not provider instance.""" """Test provider overriding with not provider instance."""
self.assertRaises(Error, self.provider.override, object()) self.assertRaises(Error, self.provider.override, object())
def test_reset_override(self):
"""Test reset of provider's override."""
overriding_provider = Provider()
self.provider.override(overriding_provider)
self.assertTrue(self.provider.overridden)
self.assertIs(self.provider.last_overriding, overriding_provider)
self.provider.reset_override()
self.assertFalse(self.provider.overridden)
try:
self.provider.last_overriding
except Error:
pass
else:
self.fail('Got en error in {}'.format(
str(self.test_last_overriding_of_not_overridden_provider)))
def test_last_overriding(self): def test_last_overriding(self):
"""Test getting last overriding provider.""" """Test getting last overriding provider."""
overriding_provider1 = Provider() overriding_provider1 = Provider()
@ -103,6 +84,45 @@ class ProviderTests(unittest.TestCase):
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)))
def test_reset_last_overriding(self):
"""Test reseting of last overriding provider."""
overriding_provider1 = Provider()
overriding_provider2 = Provider()
self.provider.override(overriding_provider1)
self.provider.override(overriding_provider2)
self.assertIs(self.provider.last_overriding, overriding_provider2)
self.provider.reset_last_overriding()
self.assertIs(self.provider.last_overriding, overriding_provider1)
self.provider.reset_last_overriding()
self.assertFalse(self.provider.overridden)
def test_reset_last_overriding_of_not_overridden_provider(self):
"""Test resetting of last overriding on not overridden provier."""
self.assertRaises(Error, self.provider.reset_last_overriding)
def test_reset_override(self):
"""Test reset of provider's override."""
overriding_provider = Provider()
self.provider.override(overriding_provider)
self.assertTrue(self.provider.overridden)
self.assertIs(self.provider.last_overriding, overriding_provider)
self.provider.reset_override()
self.assertFalse(self.provider.overridden)
try:
self.provider.last_overriding
except Error:
pass
else:
self.fail('Got en error in {}'.format(
str(self.test_last_overriding_of_not_overridden_provider)))
class DelegateTests(unittest.TestCase): class DelegateTests(unittest.TestCase):