From 6584510d390a37153c20974da6704a907058fea0 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 19 Oct 2009 04:38:32 +0000 Subject: Merge gitorious@git.sugarlabs.org:tutorius/michaeljm-dev into merge_michaeljm-dev --- (limited to 'tests/propertiestests.py') diff --git a/tests/propertiestests.py b/tests/propertiestests.py index 46346c4..0b8251a 100644 --- a/tests/propertiestests.py +++ b/tests/propertiestests.py @@ -15,6 +15,9 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import unittest +import uuid +import os +import copy from sugar.tutorius.constraints import * from sugar.tutorius.properties import * @@ -81,7 +84,128 @@ class BasePropertyTest(unittest.TestCase): obj.prop = 2 assert obj.prop == 2, "Unable to set a value on base class" + + def test_eq_(self): + class klass(TPropContainer): + prop = TutoriusProperty() + obj = klass() + + obj2 = klass() + + assert obj == obj2, "Base property containers should be identical" +class AdvancedPropertyTest(unittest.TestCase): + def test_properties_groups(self): + """ + Tests complex properties containers for identity. + """ + + class klass1(TPropContainer): + message = TutoriusProperty() + property = TutoriusProperty() + data = TutoriusProperty() + + class klass3(TPropContainer): + property = TutoriusProperty() + message = TutoriusProperty() + data = TutoriusProperty() + extra_prop = TutoriusProperty() + + class klass4(TPropContainer): + property = TutoriusProperty() + message = TutoriusProperty() + data = TFloatProperty(13.0) + + obj1 = klass1() + obj1.property = 12 + obj1.message = "Initial message" + obj1.data = [132, 208, 193, 142] + + obj2 = klass1() + obj2.property = 12 + obj2.message = "Initial message" + obj2.data = [132, 208, 193, 142] + + obj3 = klass3() + obj3.property = 12 + obj3.message = "Initial message" + obj3.data = [132, 208, 193, 142] + obj3.extra_prop = "Suprprise!" + + obj4 = klass4() + obj4.property = 12 + obj4.message = "Initial message" + obj4.data = 13.4 + + # Ensure that both obj1 and obj2 are identical (they have the same list of + # properties and they have the same values + assert obj1 == obj1, "Identical objects were considered as different" + + # Ensure that obj1 is different from obj3, since obj3 has an extra property + assert not (obj1 == obj3), "Objects should not be identical since obj3 has more props" + assert not (obj3 == obj1), "Objects should not be identical since obj3 has more properties" + + # Ensure that properties of different type are considered as different + assert not (obj1 == obj4), "Properties of different type should not be equal" + + def test_addon_properties(self): + """Test an addon property. + + This tests creates a class with a single addon property (klass1) and + assigns a new addon to it (inner1).""" + class klass1(TPropContainer): + addon = TAddonProperty() + + class inner1(TPropContainer): + internal = TutoriusProperty() + def __init__(self, value): + TPropContainer.__init__(self) + self.internal = value + + obj1 = klass1() + obj1.addon = inner1("Hi!") + + obj2 = klass1() + obj2.addon = inner1("Hi!") + + assert obj1 == obj2, "Identical objects with addon properties were treated as different" + + obj3 = klass1() + obj3.addon = inner1("Hello!") + + assert not (obj1 == obj3), "Objects with addon property having a different value should be considered different" + + def test_addonlist_properties(self): + class klass1(TPropContainer): + addon_list = TAddonListProperty() + + class inner1(TPropContainer): + message = TutoriusProperty() + data = TutoriusProperty() + def __init__(self, message, data): + TPropContainer.__init__(self) + self.message = message + self.data = data + + class inner2(TPropContainer): + message = TutoriusProperty() + other_data = TutoriusProperty() + def __init__(self, message, data): + TPropContainer.__init__(self) + self.message = message + self.other_data = data + + obj1 = klass1() + obj1.addon_list = [inner1('Hi!', 12), inner1('Hello.', [1,2])] + obj2 = klass1() + obj2.addon_list = [inner1('Hi!', 12), inner1('Hello.', [1,2])] + + assert obj1 == obj2, "Addon lists with the same containers were considered different" + + obj3 = klass1() + obj3.addon_list = [inner1('Hi!', 12), inner2('Hello.', [1,2])] + assert not (obj1 == obj3), "Differently named properties should be considered different in the addon list tests" + class TIntPropertyTest(unittest.TestCase): def test_int_property(self): class klass(TPropContainer): @@ -377,19 +501,38 @@ class TEnumPropertyTest(unittest.TestCase): try_wrong_values(self.obj) class TFilePropertyTest(unittest.TestCase): + root_folder = "/tmp/tutorius" + def setUp(self): + try: + os.mkdir(self.root_folder) + except: + pass + # Create some sample, unique files for the tests + self.temp_filename1 = os.path.join(self.root_folder, "sample_file1_" + str(uuid.uuid1()) + ".txt") + self.temp_file1 = file(self.temp_filename1, "w") + self.temp_file1.close() + self.temp_filename2 = os.path.join(self.root_folder, "sample_file2_" + str(uuid.uuid1()) + ".txt") + self.temp_file2 = file(self.temp_filename2, "w") + self.temp_file2.close() + class klass(TPropContainer): - prop = TFileProperty("propertiestests.py") + prop = TFileProperty(self.temp_filename1) self.obj = klass() + + def tearDown(self): + # Unlink the files from the disk when tests are over + os.unlink(self.temp_filename1) + os.unlink(self.temp_filename2) def test_basic_file(self): - assert self.obj.prop == "propertiestests.py", "Could not set initial value" + assert self.obj.prop == self.temp_filename1, "Could not set initial value" assert type(self.obj).prop.type == "file", "Wrong type for TFileProperty : %s"%type(self.obj).prop.type - self.obj.prop = "run-tests.py" + self.obj.prop = self.temp_filename2 - assert self.obj.prop == "run-tests.py", "Could not change value" + assert self.obj.prop == self.temp_filename2, "Could not change value" try: self.obj.prop = "unknown/file/on/disk.gif" @@ -397,6 +540,45 @@ class TFilePropertyTest(unittest.TestCase): except FileConstraintError: pass +class TAddonPropertyTest(unittest.TestCase): + def test_wrong_value(self): + class klass1(TPropContainer): + addon = TAddonProperty() + + class wrongAddon(object): + pass + + obj1 = klass1() + obj1.addon = klass1() + + try: + obj1.addon = wrongAddon() + assert False, "Addon Property should not accept non-TPropContainer values" + except ValueError: + pass + +class TAddonPropertyList(unittest.TestCase): + def test_wrong_value(self): + class klass1(TPropContainer): + addonlist = TAddonListProperty() + + class wrongAddon(object): + pass + + obj1 = klass1() + + obj1.addonlist = [klass1(), klass1()] + + try: + obj1.addonlist = klass1() + assert False, "TAddonPropeprty shouldn't accept anything else than a list" + except ValueError: + pass + + try: + obj1.addonlist = [klass1(), klass1(), wrongAddon(), klass1()] + except ValueError: + pass if __name__ == "__main__": unittest.main() -- cgit v0.9.1