From 80c7d20f83d15890a31b5e0a584dfcb24fb3f2a6 Mon Sep 17 00:00:00 2001 From: JCTutorius Date: Thu, 05 Nov 2009 18:42:53 +0000 Subject: Merge branch 'master' of gitorious@git.sugarlabs.org:tutorius/mainline Conflicts: tests/vaulttests.py tutorius/vault.py --- (limited to 'tests') diff --git a/tests/vaulttests.py b/tests/vaulttests.py index d6787ef..d085faf 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -29,9 +29,10 @@ import unittest import os import shutil import zipfile +import cStringIO from sugar.tutorius import addon -from sugar.tutorius.core import State, FiniteStateMachine, Tutorial +from sugar.tutorius.tutorial import Tutorial from sugar.tutorius.actions import * from sugar.tutorius.filters import * from sugar.tutorius.vault import Vault, XMLSerializer, Serializer, TutorialBundler @@ -100,18 +101,14 @@ class VaultInterfaceTest(unittest.TestCase): ini_file2.close() # Create a dummy fsm - self.fsm = FiniteStateMachine("testingMachine") + self.fsm = Tutorial("TestTutorial1") # Add a few states act1 = addon.create('BubbleMessage', message="Hi", position=[300, 450]) ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked") act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2]) - st1 = State("INIT") - st1.add_action(act1) - st1.add_event_filter(ev1, 'Second') - st2 = State("Second") - st2.add_action(act2) - self.fsm.add_state(st1) - self.fsm.add_state(st2) + self.fsm.add_action("INIT", act1) + st2 = self.fsm.add_state((act2,)) + self.fsm.add_transition("INIT",(ev1, st2)) self.tuto_guid = uuid1() # Create a dummy metadata dictionnary @@ -146,7 +143,8 @@ class VaultInterfaceTest(unittest.TestCase): # Creat a dummy tutorial .xml file serializer = XMLSerializer() - serializer.save_fsm(self.fsm, 'tutorial.xml', test_path) + with file(os.path.join(test_path, 'tutorial.xml'), 'w') as fsmfile: + serializer.save_tutorial(self.fsm, fsmfile) # Create a dummy tutorial metadata file self.create_test_metadata_file(os.path.join(test_path, 'meta.ini'), self.tuto_guid) @@ -238,16 +236,8 @@ class VaultInterfaceTest(unittest.TestCase): reloaded_tuto = vault.loadTutorial(self.tuto_guid) # Compare the two FSMs - reloaded_fsm = reloaded_tuto.state_machine - - assert reloaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \ - 'FSM underlying dictionary differ from original to reformed one' - assert reloaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \ + assert reloaded_tuto.get_state_dict().keys() == self.fsm.get_state_dict().keys(), \ 'FSM underlying dictionary differ from original to reformed one' - assert reloaded_fsm._states.get("INIT").get_action_list()[0].message == \ - self.fsm._states.get("INIT").get_action_list()[0].message, \ - 'FSM underlying State underlying Action differ from original to reformed one' - assert len(reloaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself" def test_saveTutorial(self): """ @@ -256,35 +246,27 @@ class VaultInterfaceTest(unittest.TestCase): # Save the tutorial in the vault vault = Vault() - tutorial = Tutorial('test', self.fsm) + tutorial = self.fsm vault.saveTutorial(tutorial, self.test_metadata_dict) # Get the tutorial back reloaded_tuto = vault.loadTutorial(self.save_test_guid) # Compare the two FSMs - reloaded_fsm = reloaded_tuto.state_machine - - assert reloaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \ + assert reloaded_tuto.get_state_dict().keys() == self.fsm.get_state_dict().keys(), \ 'FSM underlying dictionary differ from original to reformed one' - assert reloaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \ - 'FSM underlying dictionary differ from original to reformed one' - assert reloaded_fsm._states.get("INIT").get_action_list()[0].message == \ - self.fsm._states.get("INIT").get_action_list()[0].message, \ - 'FSM underlying State underlying Action differ from original to reformed one' - assert len(reloaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself" # TODO : Compare the initial and reloaded metadata when vault.Query() will accept specifc argument # (so we can specifiy that we want only the metadata for this particular tutorial - def test_add_delete_get_path_ressource(self): + def test_add_delete_get_path_resource(self): """ - This test verify that the vault interface function add_ressource succesfully add ressource in the vault - and return the new ressource id. It also test the deletion of the ressource. + This test verify that the vault interface function add_resource succesfully add resource in the vault + and return the new resource id. It also test the deletion of the resource. """ # Path of an image file in the test folder - image_path = os.path.join(os.getcwd(), 'tests', 'ressources', 'icon.svg') + image_path = os.path.join(os.getcwd(), 'tests', 'resources', 'icon.svg') assert os.path.isfile(image_path), 'cannot find the test image file' # Create and save a tutorial @@ -294,30 +276,28 @@ class VaultInterfaceTest(unittest.TestCase): bundler = TutorialBundler(self.save_test_guid) tuto_path = bundler.get_tutorial_path(self.save_test_guid) - # add the ressource to the tutorial - ressource_id = Vault.add_ressource(self.save_test_guid, image_path) + # add the resource to the tutorial + resource_id = Vault.add_resource(self.save_test_guid, image_path) # Check that the image file is now in the vault - assert os.path.isfile(os.path.join(tuto_path, 'ressources', ressource_id)), 'image file not found in vault' + assert os.path.isfile(os.path.join(tuto_path, 'resources', resource_id)), 'image file not found in vault' - # Check if get_ressource_path Vault interface function is working - vault_path = Vault.get_ressource_path(self.save_test_guid, ressource_id) + # Check if get_resource_path Vault interface function is working + vault_path = Vault.get_resource_path(self.save_test_guid, resource_id) assert os.path.isfile(vault_path), 'path returned is not a file' basename, extension = os.path.splitext(vault_path) assert extension == '.svg', 'wrong file path have been returned' - # Delete the ressource - Vault.delete_ressource(self.save_test_guid, ressource_id) + # Delete the resource + Vault.delete_resource(self.save_test_guid, resource_id) - # Check that the ressource is not in the vault anymore - assert os.path.isfile(os.path.join(tuto_path, 'ressources', ressource_id)) == False, 'image file found in vault when it should have been deleted.' + # Check that the resource is not in the vault anymore + assert os.path.isfile(os.path.join(tuto_path, 'resources', resource_id)) == False, 'image file found in vault when it should have been deleted.' - - def tearDown(self): folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); for file in os.listdir(folder): @@ -337,8 +317,8 @@ class SerializerInterfaceTest(unittest.TestCase): ser = Serializer() try: - ser.save_fsm(None) - assert False, "save_fsm() should throw an unimplemented error" + ser.save_tutorial(None) + assert False, "save_tutorial() should throw an unimplemented error" except: pass @@ -346,8 +326,8 @@ class SerializerInterfaceTest(unittest.TestCase): ser = Serializer() try: - ser.load_fsm(str(uuid.uuid1())) - assert False, "load_fsm() should throw an unimplemented error" + ser.load_tutorial(str(uuid.uuid1())) + assert False, "load_tutorial() should throw an unimplemented error" except: pass @@ -357,103 +337,61 @@ class XMLSerializerTest(unittest.TestCase): """ def setUp(self): - - os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path') - path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path') - if os.path.isdir(path) != True: - os.makedirs(path) - # Create the sample FSM - self.fsm = FiniteStateMachine("testingMachine") + self.fsm = Tutorial("TestTutorial1") # Add a few states act1 = addon.create('BubbleMessage', message="Hi", position=[300, 450]) ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked") act2 = addon.create('BubbleMessage', message="Second message", position=[250, 150], tail_pos=[1,2]) - st1 = State("INIT") - st1.add_action(act1) - st1.add_event_filter(ev1, 'Second') - - st2 = State("Second") - - st2.add_action(act2) - - self.fsm.add_state(st1) - self.fsm.add_state(st2) + self.fsm.add_action("INIT",act1) + st2 = self.fsm.add_state((act2,)) + self.fsm.add_transition("INIT",(ev1, st2)) self.uuid = uuid1() - # Flag to set to True if the output can be deleted after execution of - # the test - self.remove = True - def tearDown(self): """ - Removes the created files, if need be. + Nothing to do anymore. """ - if self.remove == True: - shutil.rmtree(os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')) - - folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); - for file in os.listdir(folder): - file_path = os.path.join(folder, file) - shutil.rmtree(file_path) + pass - def create_test_metadata(self, ini_file_path, guid): - """ - Create a basinc .ini file for testing purpose. - """ - ini_file = open(ini_file_path, 'wt') - ini_file.write("[GENERAL_METADATA]\n") - ini_file.write('guid=' + str(guid) + '\n') - ini_file.write('name=TestTutorial1\n') - ini_file.write('version=1\n') - ini_file.write('description=This is a test tutorial 1\n') - ini_file.write('rating=3.5\n') - ini_file.write('category=Test\n') - ini_file.write('publish_state=false\n') - ini_file.write('[RELATED_ACTIVITIES]\n') - ini_file.write('org.laptop.TutoriusActivity = 1\n') - ini_file.write('org.laptop.Writus = 1\n') - ini_file.close() - - def test_save(self): - """ - Writes an FSM to disk, then compares the file to the expected results. - "Remove" boolean argument specify if the test data must be removed or not - """ - xml_ser = XMLSerializer() - os.makedirs(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid))) - xml_ser.save_fsm(self.fsm, sugar.tutorius.vault.TUTORIAL_FILENAME, os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid))) - self.create_test_metadata(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid), 'meta.ini'), self.uuid) - + def create_test_metadata(self, file_obj, guid): + file_obj.write("[GENERAL_METADATA]\n") + file_obj.write('guid=' + str(guid) + '\n') + file_obj.write('name=TestTutorial1\n') + file_obj.write('version=1\n') + file_obj.write('description=This is a test tutorial 1\n') + file_obj.write('rating=3.5\n') + file_obj.write('category=Test\n') + file_obj.write('publish_state=false\n') + file_obj.write('[RELATED_ACTIVITIES]\n') + file_obj.write('org.laptop.TutoriusActivity = 1\n') + file_obj.write('org.laptop.Writus = 1\n') def test_save_and_load(self): """ + Writes an FSM to disk, then compares the file to the expected results. Load up the written FSM and compare it with the object representation. """ - self.test_save() xml_ser = XMLSerializer() - - loaded_fsm = xml_ser.load_fsm(str(self.uuid)) + tuto_file = cStringIO.StringIO() + xml_ser.save_tutorial(self.fsm, tuto_file) + + xml_ser = XMLSerializer() + load_tuto_file = cStringIO.StringIO(tuto_file.getvalue()) + loaded_fsm = xml_ser.load_tutorial(load_tuto_file) # Compare the two FSMs - assert loaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \ - 'FSM underlying dictionary differ from original to reformed one' - assert loaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \ - 'FSM underlying dictionary differ from original to reformed one' - assert loaded_fsm._states.get("INIT").get_action_list()[0].message == \ - self.fsm._states.get("INIT").get_action_list()[0].message, \ - 'FSM underlying State underlying Action differ from original to reformed one' - assert len(loaded_fsm.get_action_list()) == 0, "FSM should not have any actions on itself" + assert loaded_fsm == self.fsm, "Loaded FSM differs from original one" def test_all_actions(self): """ Inserts all the known action types in a FSM, then attempt to load it. """ - st = State("INIT") - + fsm = Tutorial("TestActions") + tuto_file = cStringIO.StringIO() act1 = addon.create('BubbleMessage', "Hi!", position=[10,120], tail_pos=[-12,30]) act2 = addon.create('DialogMessage', "Hello again.", position=[120,10]) act3 = addon.create('WidgetIdentifyAction') @@ -465,26 +403,24 @@ class XMLSerializerTest(unittest.TestCase): actions = [act1, act2, act3, act4, act5, act6, act7, act8] for action in actions: - st.add_action(action) + fsm.add_action("INIT", action) - self.fsm.remove_state("Second") - self.fsm.remove_state("INIT") - self.fsm.add_state(st) - xml_ser = XMLSerializer() + xml_ser.save_tutorial(fsm, tuto_file) + load_tuto_file = cStringIO.StringIO(tuto_file.getvalue()) - self.test_save() - - reloaded_fsm = xml_ser.load_fsm(str(self.uuid)) - - # TODO : Cannot do object equivalence, must check equality of all underlying object - # assert self.fsm == reloaded_fsm, "Expected equivalence before saving vs after loading." + reloaded_fsm = xml_ser.load_tutorial(load_tuto_file) + # Compare the two FSMs + assert reloaded_fsm == fsm, "Loaded FSM differs from original one" + assert fsm.get_action_dict() == reloaded_fsm.get_action_dict(), \ + "Actions should be the same" def test_all_filters(self): """ Inserts all the known action filters in a FSM, then attempt to load it. """ - st = State("INIT") + fsm = Tutorial("TestFilters") + tuto_file = cStringIO.StringIO() ev1 = addon.create('TimerEvent', 1000) ev2 = addon.create('GtkWidgetEventFilter', object_id="0.0.1.1.0.0.1", event_name="clicked") @@ -492,20 +428,18 @@ class XMLSerializerTest(unittest.TestCase): ev4 = addon.create('GtkWidgetTypeFilter', "0.0.1.1.1.2.3", strokes="acbd") filters = [ev1, ev2, ev3, ev4] - for filter in filters: - st.add_event_filter(filter, 'Second') + for efilter in filters: + fsm.add_transition("INIT", (efilter, 'END')) - self.fsm.remove_state("INIT") - self.fsm.add_state(st) - xml_ser = XMLSerializer() - - self.test_save() - - reloaded_fsm = xml_ser.load_fsm(str(self.uuid)) - - # TODO : Cannot do object equivalence, must check equality of all underlying object - # assert self.fsm == reloaded_fsm, "Expected equivalence before saving vs after loading." + xml_ser.save_tutorial(fsm, tuto_file) + load_tuto_file = cStringIO.StringIO(tuto_file.getvalue()) + + reloaded_fsm = xml_ser.load_tutorial(load_tuto_file) + # Compare the two FSMs + assert reloaded_fsm == fsm, "Loaded FSM differs from original one" + assert fsm.get_transition_dict() == reloaded_fsm.get_transition_dict(), \ + "Transitions should be the same" class TutorialBundlerTests(unittest.TestCase): @@ -514,7 +448,7 @@ class TutorialBundlerTests(unittest.TestCase): This module contains all the tests for the storage mecanisms for tutorials This mean testing saving and loading tutorial, .ini file management and - adding ressources to tutorial + adding resources to tutorial """ def setUp(self): @@ -555,4 +489,4 @@ class TutorialBundlerTests(unittest.TestCase): if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() -- cgit v0.9.1