Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / singleton / tests.py @ 749b8b8e

History | View | Annotate | Download (6 kB)

1
#!/usr/bin/env python
2
#
3
# -*- coding: utf-8 -*-
4
#
5
# Copyright 2011 GRNET S.A. All rights reserved.
6
#
7
# Redistribution and use in source and binary forms, with or
8
# without modification, are permitted provided that the following
9
# conditions are met:
10
#
11
#   1. Redistributions of source code must retain the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer.
14
#
15
#   2. Redistributions in binary form must reproduce the above
16
#      copyright notice, this list of conditions and the following
17
#      disclaimer in the documentation and/or other materials
18
#      provided with the distribution.
19
#
20
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
# POSSIBILITY OF SUCH DAMAGE.
32
#
33
# The views and conclusions contained in the software and
34
# documentation are those of the authors and should not be
35
# interpreted as representing official policies, either expressed
36
# or implied, of GRNET S.A.
37
#
38
#
39

    
40
"""Unit Tests for the Singleton classes in synnefo.lib.singleton
41

42
Provides unit tests for the code implementing Singleton
43
classes in the synnefo.lib.singleton module.
44

45
"""
46

    
47
import unittest
48

    
49
from synnefo.lib.singleton import ArgBasedSingleton, ArgBasedSingletonMeta
50

    
51

    
52
class SubClassOne(ArgBasedSingleton):
53
    name = None
54

    
55
    def __init__(self, name):
56
        self.name = name
57

    
58

    
59
class SubClassTwo(ArgBasedSingleton):
60
    name = None
61

    
62
    def __init__(self, name):
63
        self.name = name
64

    
65

    
66
class SubClassThree(SubClassTwo):
67
    name2 = None
68

    
69
    def __init__(self, name):
70
        self.name2 = name
71

    
72

    
73
class SubClassKwArgs(ArgBasedSingleton):
74
    name = None
75

    
76
    def __init__(self, onearg, **kwargs):
77
        self.name = onearg
78
        for x in kwargs:
79
            setattr(self, x, kwargs[x])
80

    
81

    
82
class SubClassNoReinit(ArgBasedSingleton):
83
    initialized = None
84

    
85
    def __init__(self, *args, **kwargs):
86
        if self.initialized:
87
            raise Exception("__init__ called twice!")
88
        self.initialized = True
89

    
90

    
91
class ArgBasedSingletonTestCase(unittest.TestCase):
92
    def test_same_object(self):
93
        o1 = ArgBasedSingleton()
94
        o2 = ArgBasedSingleton()
95
        self.assertTrue(o1 is o2)
96

    
97

    
98
class MyMeta(ArgBasedSingletonMeta):
99
    def __call__(cls, *args, **kw):
100
        return super(MyMeta, cls).__call__(*args, **kw)
101

    
102

    
103
class BaseClass(object):
104
    __metaclass__ = MyMeta
105

    
106
    def ret5(self):
107
        return 5
108

    
109

    
110
class SubClassMultiple(BaseClass, ArgBasedSingleton):
111
    name = None
112

    
113
    def __init__(self, name):
114
        name = name
115

    
116

    
117
class SubClassTestCase(unittest.TestCase):
118
    def test_same_object(self):
119
        o1 = SubClassOne('one')
120
        o2 = SubClassOne('two')
121
        o1a = SubClassOne('one')
122

    
123
        self.assertEqual(o1.name, 'one')
124
        self.assertEqual(o2.name, 'two')
125
        self.assertEqual(o1a.name, 'one')
126
        self.assertFalse(o1 is o2)
127
        self.assertTrue(o1 is o1a)
128

    
129
    def test_different_classes(self):
130
        o1 = SubClassOne('one')
131
        o2 = SubClassTwo('one')
132

    
133
        self.assertEqual(o1.name, 'one')
134
        self.assertEqual(o2.name, 'one')
135
        self.assertFalse(o1 is o2)
136

    
137

    
138
class SubClassKwArgsTestCase(unittest.TestCase):
139
    def test_init_signature(self):
140
        self.assertRaises(TypeError, SubClassKwArgs, 'one', 'two')
141

    
142
    def test_distinct_kwargs(self):
143
        o1 = SubClassKwArgs('one', a=1)
144
        o2 = SubClassKwArgs('two')
145
        o1a = SubClassKwArgs('one', a=2)
146
        o1b = SubClassKwArgs('one', a=1)
147
        o1c = SubClassKwArgs('one', a=1, b=2)
148
        o1d = SubClassKwArgs('one', b=2, a=1)
149

    
150
        self.assertEqual(o1.a, 1)
151
        self.assertEqual(o1a.a, 2)
152
        self.assertEqual(o1b.a, 1)
153
        self.assertRaises(AttributeError, getattr, o2, 'a')
154
        self.assertFalse(o1 is o2)
155
        self.assertFalse(o1 is o1a)
156
        self.assertTrue(o1 is o1b)
157
        self.assertTrue(o1c is o1d)
158

    
159

    
160
class SubClassDistinctDicts(unittest.TestCase):
161
    def test_distinct_storage_per_subclass(self):
162
        o1 = SubClassOne('one')
163
        o2 = SubClassTwo('one')
164
        o1a = SubClassOne('two')
165
        o2a = SubClassTwo('two')
166

    
167
        self.assertEqual(o1.name, 'one')
168
        self.assertEqual(o2.name, 'one')
169
        self.assertEqual(o1a.name, 'two')
170
        self.assertEqual(o2a.name, 'two')
171
        self.assertTrue(o1._singles is o1a._singles)
172
        self.assertTrue(o2._singles is o2a._singles)
173
        self.assertFalse(o1._singles is o2._singles)
174
        self.assertFalse(o1a._singles is o2a._singles)
175

    
176

    
177
class SubClassThreeTestCase(unittest.TestCase):
178
    def test_singleton_inheritance(self):
179
        o1 = SubClassThree('one')
180
        o2 = SubClassThree('two')
181
        o1a = SubClassThree('one')
182

    
183
        self.assertEquals(o1.name2, 'one')
184
        self.assertEquals(o2.name2, 'two')
185
        self.assertEquals(o1a.name2, 'one')
186

    
187
        self.assertTrue(o1 is o1a)
188
        self.assertFalse(o1 is o2)
189

    
190

    
191
class SubClassMultipleTestCase(unittest.TestCase):
192
    def test_multiple_inheritance(self):
193
        o1 = SubClassMultiple('one')
194
        o2 = SubClassMultiple('two')
195
        o1a = SubClassMultiple('one')
196

    
197
        self.assertEquals(o1.ret5(), 5)
198
        self.assertEquals(o2.ret5(), 5)
199
        self.assertEquals(o1a.ret5(), 5)
200

    
201
        self.assertTrue(o1 is o1a)
202
        self.assertFalse(o1 is o2)
203

    
204

    
205
class SubClassNoReinitTestCase(unittest.TestCase):
206
    def test_no_reinit(self):
207
        o1 = SubClassNoReinit('one')
208
        o2 = SubClassNoReinit('one')
209

    
210
        self.assertTrue(o1 is o2)
211

    
212

    
213
if __name__ == '__main__':
214
    unittest.main()