Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / singleton / tests.py @ 19092a69

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.common.singleton
41

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

45
"""
46

    
47
import unittest
48

    
49
from synnefo.lib.singleton import ArgBasedSingleton
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
    def ret5(self):
106
        return 5
107

    
108

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

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

    
115

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

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

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

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

    
136

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

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

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

    
158

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

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

    
175

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

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

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

    
189

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

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

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

    
203

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

    
209
        self.assertTrue(o1 is o2)
210

    
211

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