Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/constraintstests.py
blob: a5ccf262fd462c30504017f3feb54447ee04a348 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright (C) 2009, Tutorius.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import unittest

import uuid
import os

from sugar.tutorius.constraints import *

class ConstraintTest(unittest.TestCase):
    def test_base_class(self):
        cons = Constraint()
        try:
            cons.validate(1)
            assert False, "Base class should throw an assertion"
        except NotImplementedError:
            pass

class ValueConstraintTest(unittest.TestCase):
    def test_limit_set(self):
        cons = ValueConstraint(12)
        
        assert cons.limit == 12
        
class UpperLimitConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = UpperLimitConstraint(None)
        try:
            cons.validate(20)
        except UpperLimitConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = UpperLimitConstraint(10)
        
        try:
            cons.validate(20)
            assert False, "Validation of UpperLimit(10) on 20 should raise an exception"
        except UpperLimitConstraintError:
            pass
        
        try:
            cons.validate(5)
        except UpperLimitConstraintError:
            assert True, "Validation of UpperLimit(10) on 5 should not raise an exception"
    
class LowerLimitConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = LowerLimitConstraint(None)
        try:
            cons.validate(20)
        except LowerLimitConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = LowerLimitConstraint(10)
        
        try:
            cons.validate(5)
            assert False, "Validation of LowerLimit(10) on 5 should raise an exception"
        except LowerLimitConstraintError:
            pass
        
        try:
            cons.validate(20)
        except LowerLimitConstraintError:
            assert True, "Validation of LowerLimit(10) on 20 should not raise an exception"

class MaxSizeConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = MaxSizeConstraint(None)
        try:
            cons.validate(20)
        except MaxSizeConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = MaxSizeConstraint(10)
        
        try:
            cons.validate(range(0, 20))
            assert False, "Validation of MaxSizeConstraint(10) on list of length 20 should raise an exception"
        except MaxSizeConstraintError:
            pass
        
        try:
            cons.validate(range(0,5))
        except MaxSizeConstraintError:
            assert True, "Validation of MaxSizeConstraint(10) on list of length 5 should not raise an exception"
    
class MinSizeConstraintTest(unittest.TestCase):
    def test_empty_constraint(self):
        cons = MinSizeConstraint(None)
        try:
            cons.validate(20)
        except MinSizeConstraintError:
            assert False, "Empty contraint should not raise an exception"
    
    def test_validate(self):
        cons = MinSizeConstraint(10)
        
        try:
            cons.validate(range(0, 5))
            assert False, "Validation of MinSizeConstraint(10) on list of length 20 should raise an exception"
        except MinSizeConstraintError:
            pass
        
        try:
            cons.validate(range(0,20))
        except MinSizeConstraintError:
            assert True, "Validation of MinSizeConstraint(10) on list of length 5 should not raise an exception"

class ColorConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = ColorConstraint()
        
        try:
            cons.validate([0, 0])
            assert False, "ColorConstraint on list of length 2 should raise an exception"
        except ColorArraySizeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([0, 0, "str"])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorTypeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([0, "str", 0])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorTypeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate(["str", 0, 0])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorTypeError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([1, 2, 300])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorValueError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
            
        try:
            cons.validate([1, -100, 30])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorValueError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([999999, 2, 300])
            assert False, "ColorConstraint on with non-integers values should raise an exception"
        except ColorValueError:
            pass
        except ColorConstraintError:
            assert False, "ColorConstraint threw the wrong type of error"
        
        try:
            cons.validate([12, 23, 34])
        except LowerLimitConstraintError:
            assert True, "ColorConstraint on expected input should not raise an exception"

class BooleanConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = BooleanConstraint()
        
        cons.validate(True)
        cons.validate(False)
        
        try:
            cons.validate(18)
            assert False, "Setting integer on constraint should raise an error"
        except BooleanConstraintError:
            pass
        except:
            assert False, "Wrong exception type raised when setting wrong type"

class EnumConstraintTest(unittest.TestCase):
    def test_validate(self):
        cons = EnumConstraint([1,2,3,7,8,9, "ex"])
        
        cons.validate(8)
        
        cons.validate("ex")
        
        try:
            cons.validate(4)
            assert False, "There should be an exception on setting a value out of the enum"
        except EnumConstraintError:
            pass
        except:
            assert False, "Wrong exception type thrown"
            
class FileConstraintTest(unittest.TestCase):
    def setUp(self):
        self.temp_filename = "sample_file_" + str(uuid.uuid1()) + ".txt"
        self.file1 = file(self.temp_filename, "w")
        self.file1.close()

    def tearDown(self):
        os.unlink(self.temp_filename)

    def test_validate(self):
        cons = FileConstraint()
        
        cons.validate(self.temp_filename)
        
        try:
            cons.validate("unknown/file.py")
            assert False, "Non-existing file check should throw an exception"
        except FileConstraintError:
            pass

class ResourceConstraintTest(unittest.TestCase):
    def test_valid_names(self):
        name1 = "file_" + unicode(uuid.uuid1()) + ".png"
        name2 = unicode(uuid.uuid1()) + "_" + unicode(uuid.uuid1()) + ".extension"
        name3 = "/home/user/.sugar/_random/new_image1231_" + unicode(uuid.uuid1()).upper() + ".mp3"
        name4 = "a_" + unicode(uuid.uuid1())
        name5 = ""
    
        cons = ResourceConstraint()
        
        # All of those names should pass without exceptions
        cons.validate(name1)
        cons.validate(name2)
        cons.validate(name3)
        cons.validate(name4)
        cons.validate(name5)

    def test_invalid_names(self):
        bad_name1 = ".jpg"
        bad_name2 = "_.jpg"
        bad_name3 = "_" + unicode(uuid.uuid1())

        cons = ResourceConstraint()
        
        try:
            cons.validate(bad_name1)
            assert False, "%s should not be a valid resource name" % bad_name1
        except ResourceConstraintError:
            pass

        try:
            cons.validate(bad_name2)
            assert False, "%s should not be a valid resource name" % bad_name2
        except ResourceConstraintError:
            pass

        try:
            cons.validate(bad_name3)
            assert False, "%s should not be a valid resource name" % bad_name3
        except ResourceConstraintError:
            pass

if __name__ == "__main__":
    unittest.main()