# 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 def try_good_values(constraint, type): if type == "int": constraint.validate(1) if type == "float": constraint.validate(1.2) if type == "str": constraint.validate("This is a string") if type == "tuple": constraint.validate((1,2.0,'3',[4], (5,))) if type == "list": constraint.validate([1,2.0,'3',[4],(5,)]) if type == "bool": constraint.validate(True) constraint.validate(False) def try_wrong_values(testcase, constraint, type): if type != "int": testcase.assertRaises(TypeConstraintError, constraint.validate, 1) if type != "float": testcase.assertRaises(TypeConstraintError, constraint.validate, 1.2) if type != "str": testcase.assertRaises(TypeConstraintError, constraint.validate, "1") if type != "tuple": testcase.assertRaises(TypeConstraintError, constraint.validate, (1,)) if type != "list": testcase.assertRaises(TypeConstraintError, constraint.validate, [1,]) if type != "bool": testcase.assertRaises(TypeConstraintError, constraint.validate, True) class TypeConstraintsTest(unittest.TestCase): """ Tests all the type constraints at the same time to ensure that they only allow a single type of item. """ def test_int_type(self): constraint = IntTypeConstraint() try_good_values(constraint, 'int') try_wrong_values(self, constraint, 'int') def test_float_type(self): constraint = FloatTypeConstraint() try_good_values(constraint, 'float') try_wrong_values(self, constraint, 'float') def test_string_type(self): constraint = StringTypeConstraint() try_good_values(constraint, 'str') try_wrong_values(self, constraint, 'str') def test_tuple_type(self): constraint = TupleTypeConstraint() try_good_values(constraint, 'tuple') try_wrong_values(self, constraint, 'tuple') def test_list_type(self): constraint = ListTypeConstraint() try_good_values(constraint, 'list') try_wrong_values(self, constraint, 'list') def test_bool_type(self): constraint = BoolTypeConstraint() try_good_values(constraint, 'bool') try_wrong_values(self, constraint, 'bool') class MultiTypeConstraintsTest(unittest.TestCase): """ Tests all the type constraints at the same time to ensure that they only allow the specified types of item. """ def _try_good_values(self, constraint, types_list): if "int" in types_list: constraint.validate(1) if "float" in types_list: constraint.validate(1.0) if "str" in types_list: constraint.validate("This is a string.") if "tuple" in types_list: constraint.validate((1, 2.0, '3', [4], (5,))) if "list" in types_list: constraint.validate([1, 2.0, '3', [4], (5,)]) if "bool" in types_list: constraint.validate(True) def _try_wrong_values(self, constraint, types_list): if "int" not in types_list: self.assertRaises(TypeConstraintError, constraint.validate, 1) if "float" not in types_list: self.assertRaises(TypeConstraintError, constraint.validate, 1.0) if "str" not in types_list: self.assertRaises(TypeConstraintError, constraint.validate, "1") if "tuple" not in types_list: self.assertRaises(TypeConstraintError, constraint.validate, (1,)) if "list" not in types_list: self.assertRaises(TypeConstraintError, constraint.validate, [1,]) if "bool" not in types_list: self.assertRaises(TypeConstraintError, constraint.validate, True) def test_array_type(self): constraint = ArrayTypeConstraint() self._try_good_values(constraint, ['tuple', 'list']) self._try_wrong_values(constraint, ['tuple', 'list']) def test_sequence_type(self): constraint = SequenceTypeConstraint() self._try_good_values(constraint, ['list', 'tuple', 'str']) self._try_wrong_values(constraint, ['list', 'tuple', 'str']) 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()