diff options
author | JCTutorius <charlie@tutorius-dev.(none)> | 2009-10-18 20:20:57 (GMT) |
---|---|---|
committer | JCTutorius <charlie@tutorius-dev.(none)> | 2009-10-18 20:20:57 (GMT) |
commit | 0e68858aeff8c858e606f1acff1b39d4d15cbda4 (patch) | |
tree | 923bf138135bf64aaf506df92e3d690f520cc9f4 | |
parent | 765818bbd69ebd9b42d5fdc46e22a3741ce98348 (diff) |
Modifications after Mike architecture code review
-rw-r--r-- | tests/serializertests.py | 197 | ||||
-rw-r--r-- | tests/vaulttests.py | 45 | ||||
-rw-r--r-- | tutorius/vault.py | 9 |
3 files changed, 25 insertions, 226 deletions
diff --git a/tests/serializertests.py b/tests/serializertests.py deleted file mode 100644 index 6c25bae..0000000 --- a/tests/serializertests.py +++ /dev/null @@ -1,197 +0,0 @@ -# Copyright (C) 2009, Tutorius.org -# Copyright (C) 2009, Jean-Christophe Savard <savard.jean.christophe@gmail.com> -# -# 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 -""" -Serialization Tests - -This module contains all the tests that pertain to the usage of the Tutorius -Serializer object. This means testing saving a tutorial dictionary to a .tml -file, loading the list of tutorials for this activity and building chosen -tutorial. -""" - -import unittest - -import os -import shutil - -from sugar.tutorius import bundler, addon -from sugar.tutorius.core import State, FiniteStateMachine -from sugar.tutorius.actions import * -from sugar.tutorius.filters import * -from sugar.tutorius.bundler import XMLSerializer, Serializer -import sugar -from uuid import uuid1 - -class SerializerInterfaceTest(unittest.TestCase): - """ - For completeness' sake. - """ - def test_save(self): - ser = Serializer() - - try: - ser.save_fsm(None) - assert False, "save_fsm() should throw an unimplemented error" - except: - pass - - def test_load(self): - ser = Serializer() - - try: - ser.load_fsm(str(uuid.uuid1())) - assert False, "load_fsm() should throw an unimplemented error" - except: - pass - -class XMLSerializerTest(unittest.TestCase): - """ - Tests the transformation of XML to FSM, then back. - """ - def setUp(self): - # Make the serializer believe the test is in a activity path - self.testpath = "/tmp/testdata/" - os.environ["SUGAR_BUNDLE_PATH"] = self.testpath - os.environ["SUGAR_PREFIX"] = self.testpath - os.environ["SUGAR_PROFILE"] = 'test' -## os.mkdir(sugar.tutorius.bundler._get_store_root()) - - # Create the sample FSM - self.fsm = FiniteStateMachine("testingMachine") - - # Add a few states - act1 = addon.create('BubbleMessage', message="Hi", pos=[300, 450]) - ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked", "Second") - act2 = addon.create('BubbleMessage', message="Second message", pos=[250, 150], tailpos=[1,2]) - - st1 = State("INIT") - st1.add_action(act1) - st1.add_event_filter(ev1) - - st2 = State("Second") - - st2.add_action(act2) - - self.fsm.add_state(st1) - self.fsm.add_state(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. - """ - if self.remove == True: - shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar",os.getenv("SUGAR_PROFILE"))) - if os.path.isdir(self.testpath): - shutil.rmtree(self.testpath) - - 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.bundler._get_store_root(), str(self.uuid))) - #rpdb2.start_embedded_debugger('flakyPass') - xml_ser.save_fsm(self.fsm, bundler.TUTORIAL_FILENAME, os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid))) - - def test_save_and_load(self): - """ - Load up the written FSM and compare it with the object representation. - """ - self.test_save() - testpath = "/tmp/testdata/" - #rpdb2.start_embedded_debugger('flakyPass') - xml_ser = XMLSerializer() - - # This interface needs to be redone... It's not clean because there is - # a responsibility mixup between the XML reader and the bundler. - loaded_fsm = xml_ser.load_fsm(str(self.uuid)) - - # Compare the two FSMs - assert loaded_fsm._states.get("INIT").name == self.fsm._states.get("INIT").name, \ - 'FSM underlying dictionary differ from original to pickled/reformed one' - assert loaded_fsm._states.get("Second").name == self.fsm._states.get("Second").name, \ - 'FSM underlying dictionary differ from original to pickled/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" - - def test_all_actions(self): - """ - Inserts all the known action types in a FSM, then attempt to load it. - """ - st = State("INIT") - - act1 = addon.create('BubbleMessage', "Hi!", pos=[10,120], tailpos=[-12,30]) - act2 = addon.create('DialogMessage', "Hello again.", pos=[120,10]) - act3 = WidgetIdentifyAction() - act4 = DisableWidgetAction("0.0.0.1.0.0.0") - act5 = TypeTextAction("0.0.0.1.1.1.0.0", "New text") - act6 = ClickAction("0.0.1.0.1.1") - act7 = OnceWrapper(act1) - act8 = ChainAction([act1, act2, act3, act4]) - actions = [act1, act2, act3, act4, act5, act6, act7, act8] - - for action in actions: - st.add_action(action) - - self.fsm.remove_state("Second") - 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)) - assert self.fsm == reloaded_fsm, "Expected equivalence before saving vs after loading." - - def test_all_filters(self): - """ - Inserts all the known action types in a FSM, then attempt to load it. - """ - st = State("INIT") - - ev1 = TimerEvent("Second", 1000) - ev2 = addon.create('GtkWidgetEventFilter', "Second", "0.0.1.1.0.0.1", "clicked") - ev3 = GtkWidgetTypeFilter("Second", "0.0.1.1.1.2.3", text="Typed stuff") - ev4 = GtkWidgetTypeFilter("Second", "0.0.1.1.1.2.3", strokes="acbd") - filters = [ev1, ev2, ev3, ev4] - - for filter in filters: - st.add_event_filter(filter) - - 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)) - - assert self.fsm == reloaded_fsm, "Expected equivalence before saving vs after loading." - -if __name__ == "__main__": - unittest.main() diff --git a/tests/vaulttests.py b/tests/vaulttests.py index d210af7..a9a53ab 100644 --- a/tests/vaulttests.py +++ b/tests/vaulttests.py @@ -30,13 +30,12 @@ import os import shutil import zipfile -# from mock import Mock from sugar.tutorius import addon from sugar.tutorius.core import State, FiniteStateMachine, Tutorial from sugar.tutorius.actions import * from sugar.tutorius.filters import * from sugar.tutorius.vault import Vault, XMLSerializer, Serializer, TutorialBundler -# from sugar.tutorius.bundler import XMLSerializer, Serializer + import sugar from uuid import uuid1 @@ -131,12 +130,15 @@ class VaultInterfaceTest(unittest.TestCase): self.test_metadata_dict['activities'] = activities_dict - def test_installTutorials(self): - # create dummy tutorial + def test_installTutorials(self): + + # TODO : Test for erronous file too (not .xml, not .ini, not .zip, etc.) + + # create dummy tutorial # create a test folder in the file # system outside the Vault - test_path = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp', 'testdata') + test_path = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp') if os.path.isdir(test_path) == True: shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp')) os.makedirs(test_path) @@ -218,7 +220,7 @@ class VaultInterfaceTest(unittest.TestCase): assert False, 'list is empty or name property is wrong' - def test_openTutorial(self): + def test_loadTutorial(self): """ Test the opening of a tutorial from the vault by passing it is guid and returning the Tutorial object representation. This test verify that the @@ -231,7 +233,7 @@ class VaultInterfaceTest(unittest.TestCase): test = bundler.get_tutorial_path(self.tuto_guid) # load tutorial created in the test_installTutorial function vault = Vault() - reloaded_tuto = vault.openTutorial(self.tuto_guid) + reloaded_tuto = vault.loadTutorial(self.tuto_guid) # Compare the two FSMs reloaded_fsm = reloaded_tuto.state_machine @@ -256,7 +258,7 @@ class VaultInterfaceTest(unittest.TestCase): vault.saveTutorial(tutorial, self.test_metadata_dict, None) # Get the tutorial back - reloaded_tuto = vault.openTutorial(self.save_test_guid) + reloaded_tuto = vault.loadTutorial(self.save_test_guid) # Compare the two FSMs reloaded_fsm = reloaded_tuto.state_machine @@ -276,7 +278,6 @@ class VaultInterfaceTest(unittest.TestCase): def tearDown(self): - pass folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data'); for file in os.listdir(folder): file_path = os.path.join(folder, file) @@ -501,25 +502,13 @@ class TutorialBundlerTests(unittest.TestCase): ini_file.close() def tearDown(self): - pass -## os.remove(self.ini_file) -## os.rmdir(self.guid_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) - -## def test_add_ressource(self): -## bund = bundler.TutorialBundler(str(self.test_guid)) -## -## temp_file = open("test.txt",'w') -## temp_file.write('test') -## temp_file.close() -## -## bund.add_resource("test.txt") -## -## assert os.path.exists(os.path.join(self.guid_path,"test.txt")), "add_ressource did not create the file" + os.remove(self.ini_file) + os.rmdir(self.guid_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) if __name__ == "__main__": diff --git a/tutorius/vault.py b/tutorius/vault.py index 9f36647..6c40f26 100644 --- a/tutorius/vault.py +++ b/tutorius/vault.py @@ -187,6 +187,12 @@ class Vault(object): @returns a list of Tutorial meta-data (TutorialID, Description, Rating, Category, PublishState, etc...) + TODO : Search for tuto caracterised by the entry : OR between [], and between each + + The returned dictionnary is of this format : key = property name, value = property value + The dictionnary also contain one dictionnary element whose key is the string 'activities' + and whose value is another dictionnary of this form : key = related activity name, + value = related activity version number """ # Temp solution for returning all tutorials metadata @@ -225,6 +231,7 @@ class Vault(object): # Get Related Activities data from.ini files related_act_list = ini_file.options(INI_ACTIVITY_SECTION) for related_act in related_act_list: + # For related activites, the format is : key = activity name, value = activity version related_act_dictionnary[related_act] = ini_file.get(INI_ACTIVITY_SECTION, related_act) # Add Related Activities dictionnary to metadata dictionnary @@ -237,7 +244,7 @@ class Vault(object): return tutorial_list - def openTutorial(self, Guid): + def loadTutorial(self, Guid): """ Creates an executable version of a tutorial from its saved representation. @returns an executable representation of a tutorial |