From 33013a0b150f5c2f4c0b7e716ca2b53dd53a9c15 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sat, 11 Feb 2012 15:07:15 -0500 Subject: [PATCH] Test cases, basic framework for moving id tags out of content_parser --HG-- branch : pmitros-mod-template --- courseware/content_parser.py | 9 ++++++++- courseware/modules/__init__.py | 8 ++++++++ courseware/modules/video_module.py | 2 +- courseware/modules/x_module.py | 2 +- courseware/tests.py | 24 ++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/courseware/content_parser.py b/courseware/content_parser.py index a3143724f9..e49c7b1b9f 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -8,6 +8,8 @@ from lxml import etree from mako.template import Template from mako.lookup import TemplateLookup +#import courseware.modules + try: # This lets us do __name__ == ='__main__' from django.conf import settings from student.models import UserProfile @@ -94,7 +96,12 @@ def id_tag(course): 'tab':'id', 'schematic':'id', 'book' : 'id'} - +# TODO: +# alt_ids = courseware.modules.get_default_ids() + +# print default_ids, alt_ids +# print default_ids == alt_ids + # Tag elements with unique IDs elements = course.xpath("|".join(['//'+c for c in default_ids])) for elem in elements: diff --git a/courseware/modules/__init__.py b/courseware/modules/__init__.py index db8b1abf2d..ed80aeaa86 100644 --- a/courseware/modules/__init__.py +++ b/courseware/modules/__init__.py @@ -11,6 +11,8 @@ import template_module import vertical_module import video_module +from courseware import content_parser + # Import all files in modules directory, excluding backups (# and . in name) # and __init__ # @@ -47,3 +49,9 @@ def get_module_id(tag): def get_valid_tags(): return modx_modules.keys() + +def get_default_ids(): + tags = get_valid_tags() + ids = map(get_module_id, tags) + return dict(zip(tags, ids)) + diff --git a/courseware/modules/video_module.py b/courseware/modules/video_module.py index 518413946c..2063c18953 100644 --- a/courseware/modules/video_module.py +++ b/courseware/modules/video_module.py @@ -12,7 +12,7 @@ from x_module import XModule log = logging.getLogger("mitx.courseware.modules") class Module(XModule): - #id_attribute = 'youtube' + id_attribute = 'youtube' video_time = 0 def handle_ajax(self, dispatch, get): diff --git a/courseware/modules/x_module.py b/courseware/modules/x_module.py index 1a2154256c..4a379253c4 100644 --- a/courseware/modules/x_module.py +++ b/courseware/modules/x_module.py @@ -8,7 +8,7 @@ class XModule(object): Initialized on access with __init__, first time with state=None, and then with state ''' - id_attribute='name' # An attribute guaranteed to be unique + id_attribute='id' # An attribute guaranteed to be unique @classmethod def get_xml_tags(c): diff --git a/courseware/tests.py b/courseware/tests.py index 03bdbb7ecf..66a7ef181e 100644 --- a/courseware/tests.py +++ b/courseware/tests.py @@ -1,5 +1,9 @@ import unittest + +import numpy + import courseware.modules +import courseware.capa.calc as calc class ModelsTest(unittest.TestCase): def setUp(self): @@ -9,4 +13,24 @@ class ModelsTest(unittest.TestCase): vc = courseware.modules.get_module_class('video') vc_str = "" self.assertEqual(str(vc), vc_str) + video_id = courseware.modules.get_default_ids()['video'] + self.assertEqual(video_id, 'youtube') + def test_calc(self): + variables={'R1':2.0, 'R3':4.0} + functions={'sin':numpy.sin, 'cos':numpy.cos} + + self.assertEqual(calc.evaluator(variables, functions, "10000||sin(7+5)-6k"), 4000.0) + self.assertEqual(calc.evaluator({'R1': 2.0, 'R3':4.0}, {}, "13"), 13) + self.assertEqual(calc.evaluator(variables, functions, "13"), 13) + self.assertEqual(calc.evaluator({'a': 2.2997471478310274, 'k': 9, 'm': 8, 'x': 0.66009498411213041}, {}, "5"), 5) + self.assertEqual(calc.evaluator({},{}, "-1"), -1) + self.assertEqual(calc.evaluator({},{}, "-0.33"), -.33) + self.assertEqual(calc.evaluator({},{}, "-.33"), -.33) + exception_happened = False + try: + evaluator({},{}, "5+7 QWSEKO") + except: + exception_happened = True + self.assertTrue(exception_happened) +