test_singleton.py 689 B

12345678910111213141516171819202122232425262728293031323334
  1. import unittest
  2. import sys
  3. from six import with_metaclass
  4. from kalliope.core.Models import Singleton
  5. class MyClass(with_metaclass(Singleton, object)):
  6. def __init__(self):
  7. self.value = "test"
  8. class TestSingleton(unittest.TestCase):
  9. def setUp(self):
  10. pass
  11. def test_singleton(self):
  12. obj1 = MyClass()
  13. obj2 = MyClass()
  14. self.assertEqual(id(obj1), id(obj2))
  15. def test_drop_singleton(self):
  16. obj1 = MyClass()
  17. obj2 = MyClass()
  18. # drop the singleton instance
  19. Singleton._instances = {}
  20. obj3 = MyClass()
  21. self.assertEqual(id(obj1), id(obj2))
  22. self.assertNotEqual(id(obj1), id(obj3))