Commit 33013a0b by Piotr Mitros

Test cases, basic framework for moving id tags out of content_parser

--HG--
branch : pmitros-mod-template
parent b274c2e2
...@@ -8,6 +8,8 @@ from lxml import etree ...@@ -8,6 +8,8 @@ from lxml import etree
from mako.template import Template from mako.template import Template
from mako.lookup import TemplateLookup from mako.lookup import TemplateLookup
#import courseware.modules
try: # This lets us do __name__ == ='__main__' try: # This lets us do __name__ == ='__main__'
from django.conf import settings from django.conf import settings
from student.models import UserProfile from student.models import UserProfile
...@@ -94,7 +96,12 @@ def id_tag(course): ...@@ -94,7 +96,12 @@ def id_tag(course):
'tab':'id', 'tab':'id',
'schematic':'id', 'schematic':'id',
'book' : '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 # Tag elements with unique IDs
elements = course.xpath("|".join(['//'+c for c in default_ids])) elements = course.xpath("|".join(['//'+c for c in default_ids]))
for elem in elements: for elem in elements:
......
...@@ -11,6 +11,8 @@ import template_module ...@@ -11,6 +11,8 @@ import template_module
import vertical_module import vertical_module
import video_module import video_module
from courseware import content_parser
# Import all files in modules directory, excluding backups (# and . in name) # Import all files in modules directory, excluding backups (# and . in name)
# and __init__ # and __init__
# #
...@@ -47,3 +49,9 @@ def get_module_id(tag): ...@@ -47,3 +49,9 @@ def get_module_id(tag):
def get_valid_tags(): def get_valid_tags():
return modx_modules.keys() return modx_modules.keys()
def get_default_ids():
tags = get_valid_tags()
ids = map(get_module_id, tags)
return dict(zip(tags, ids))
...@@ -12,7 +12,7 @@ from x_module import XModule ...@@ -12,7 +12,7 @@ from x_module import XModule
log = logging.getLogger("mitx.courseware.modules") log = logging.getLogger("mitx.courseware.modules")
class Module(XModule): class Module(XModule):
#id_attribute = 'youtube' id_attribute = 'youtube'
video_time = 0 video_time = 0
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
......
...@@ -8,7 +8,7 @@ class XModule(object): ...@@ -8,7 +8,7 @@ class XModule(object):
Initialized on access with __init__, first time with state=None, and Initialized on access with __init__, first time with state=None, and
then with state then with state
''' '''
id_attribute='name' # An attribute guaranteed to be unique id_attribute='id' # An attribute guaranteed to be unique
@classmethod @classmethod
def get_xml_tags(c): def get_xml_tags(c):
......
import unittest import unittest
import numpy
import courseware.modules import courseware.modules
import courseware.capa.calc as calc
class ModelsTest(unittest.TestCase): class ModelsTest(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -9,4 +13,24 @@ class ModelsTest(unittest.TestCase): ...@@ -9,4 +13,24 @@ class ModelsTest(unittest.TestCase):
vc = courseware.modules.get_module_class('video') vc = courseware.modules.get_module_class('video')
vc_str = "<class 'courseware.modules.video_module.Module'>" vc_str = "<class 'courseware.modules.video_module.Module'>"
self.assertEqual(str(vc), 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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment