From fa113af97754d3f1eb3fc725b18ae8ba5488e71d Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Sun, 15 Mar 2015 00:50:24 +0200 Subject: [PATCH] Test for providers.Singleton --- objects/providers.py | 6 +++--- tests/test_providers.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/objects/providers.py b/objects/providers.py index bef02325..e8a5c1ee 100644 --- a/objects/providers.py +++ b/objects/providers.py @@ -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.""" diff --git a/tests/test_providers.py b/tests/test_providers.py index bf2af1cf..b5ee24ab 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -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)