mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 01:26:51 +03:00
Test for providers.Singleton
This commit is contained in:
parent
62e0a55b1b
commit
fa113af977
|
@ -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."""
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user