Test for providers.Singleton

This commit is contained in:
Roman Mogilatov 2015-03-15 00:50:24 +02:00
parent 62e0a55b1b
commit fa113af977
2 changed files with 33 additions and 3 deletions

View File

@ -129,7 +129,7 @@ class Singleton(NewInstance):
self.instance = super(Singleton, self).__call__(*args, **kwargs)
return self.instance
def _reset_instance(self):
def reset(self):
"""Reset instance."""
self.instance = None
@ -152,12 +152,12 @@ class Scoped(Singleton):
def in_scope(self):
"""Set provider in "in scope" state."""
self.is_in_scope = True
self._reset_instance()
self.reset()
def out_of_scope(self):
"""Set provider in "out of scope" state."""
self.is_in_scope = False
self._reset_instance()
self.reset()
def __call__(self, *args, **kwargs):
"""Return provided instance."""

View File

@ -256,3 +256,33 @@ class NewInstanceTest(unittest.TestCase):
self.assertIsNot(instance1, instance2)
self.assertIsInstance(instance1, list)
self.assertIsInstance(instance2, list)
class SingletonTest(unittest.TestCase):
"""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)