forked from fedbiomed/fedbiomed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_singleton.py
102 lines (74 loc) · 2.2 KB
/
test_singleton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import unittest
import random
from fedbiomed.common.singleton import SingletonMeta
class DummySingleton(metaclass=SingletonMeta):
"""
provide a singleton for the test
"""
def __init__(self):
self._id = random.random()
def id(self):
return self._id
def setId(self, id):
self._id = id
class AnotherSingleton(metaclass=SingletonMeta):
"""
another one bites the dust
"""
def __init__(self, id = None):
if id is None:
self._id = random.random()
else:
self._id = id
def identity(self):
return self._id
def setIdentity(self, id):
self._id = id
class TestSingleton(unittest.TestCase):
'''
Test the SingletonMeta mechanism
'''
# before all tests
def setUp(self):
self.d1 = DummySingleton()
pass
# after all tests
def tearDown(self):
pass
def test_01_dummysingleton(self):
'''
test singleton mechanism for DummySingleton
'''
d2 = DummySingleton()
d3 = DummySingleton()
self.assertEqual( self.d1, d2)
self.assertEqual( self.d1, d3)
self.assertEqual( self.d1, DummySingleton())
old = self.d1.id()
new = 3.14
self.d1.setId( new )
self.assertEqual( self.d1.id() , new)
self.assertEqual( d2.id() , new)
self.assertEqual( d3.id() , new)
self.assertEqual( DummySingleton().id() , new)
self.assertTrue( self.d1 is d3)
self.assertTrue( self.d1 is d3)
self.assertTrue( d2 is d3)
self.assertTrue( DummySingleton() is d3)
pass
def test_02_another_singleton(self):
'''
test singleton mechanism for 2 singletons
'''
s1 = AnotherSingleton()
s2 = AnotherSingleton( id = 3.14 )
self.assertTrue( s1 is s2 )
def test_03_both_singleton(self):
'''
test singleton mechanism for 2 singletons
'''
s1 = DummySingleton()
s2 = AnotherSingleton()
self.assertFalse( s1 is s2 )
if __name__ == '__main__': # pragma: no cover
unittest.main()