Commit 3ced4bee by Vasyl Nakvasiuk

add `ConditionalDescriptor.definition_to_xml`, test_ajax_request, import_test, export_test

parent 4b612bd5
...@@ -10,9 +10,10 @@ from pkg_resources import resource_string ...@@ -10,9 +10,10 @@ from pkg_resources import resource_string
from xmodule.x_module import XModule from xmodule.x_module import XModule
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.seq_module import SequenceDescriptor from xmodule.seq_module import SequenceDescriptor
from xblock.core import String, Scope from xblock.core import String, Scope, List
from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.exceptions import ItemNotFoundError
log = logging.getLogger('mitx.' + __name__) log = logging.getLogger('mitx.' + __name__)
...@@ -109,6 +110,8 @@ class ConditionalModule(XModule): ...@@ -109,6 +110,8 @@ class ConditionalModule(XModule):
class ConditionalDescriptor(SequenceDescriptor): class ConditionalDescriptor(SequenceDescriptor):
"""Descriptor for conditional xmodule.""" """Descriptor for conditional xmodule."""
_tag_name = 'conditional'
module_class = ConditionalModule module_class = ConditionalModule
filename_extension = "xml" filename_extension = "xml"
...@@ -116,6 +119,8 @@ class ConditionalDescriptor(SequenceDescriptor): ...@@ -116,6 +119,8 @@ class ConditionalDescriptor(SequenceDescriptor):
stores_state = True stores_state = True
has_score = False has_score = False
show_tag_list = List(help="Poll answers", scope=Scope.content)
def get_required_module_descriptors(self): def get_required_module_descriptors(self):
"""TODO: Returns a list of XModuleDescritpor instances upon which this module depends, but are """TODO: Returns a list of XModuleDescritpor instances upon which this module depends, but are
not children of this module""" not children of this module"""
...@@ -152,9 +157,12 @@ class ConditionalDescriptor(SequenceDescriptor): ...@@ -152,9 +157,12 @@ class ConditionalDescriptor(SequenceDescriptor):
return urls return urls
children = [] children = []
show_tag_list = []
for child in xml_object: for child in xml_object:
if child.tag == 'show': if child.tag == 'show':
children.extend(parse_show_tag(child)) location = parse_show_tag(child)
children.extend(location)
show_tag_list.extend(location)
else: else:
try: try:
descriptor = system.process_xml(etree.tostring(child)) descriptor = system.process_xml(etree.tostring(child))
...@@ -162,11 +170,17 @@ class ConditionalDescriptor(SequenceDescriptor): ...@@ -162,11 +170,17 @@ class ConditionalDescriptor(SequenceDescriptor):
children.append(module_url) children.append(module_url)
except: except:
log.exception("Unable to load child when parsing Conditional.") log.exception("Unable to load child when parsing Conditional.")
return {}, children return {'show_tag_list': show_tag_list}, children
def definition_to_xml(self, resource_fs): def definition_to_xml(self, resource_fs):
xml_object = etree.Element('sequential') xml_object = etree.Element(self._tag_name)
for child in self.get_children(): for child in self.get_children():
xml_object.append( location = str(child.location)
etree.fromstring(child.export_to_xml(resource_fs))) if location in self.show_tag_list:
show_str = '<{tag_name} sources="{sources}" />'.format(
tag_name='show', sources=location)
xml_object.append(etree.fromstring(show_str))
else:
xml_object.append(
etree.fromstring(child.export_to_xml(resource_fs)))
return xml_object return xml_object
...@@ -75,7 +75,7 @@ class PollModule(XModule): ...@@ -75,7 +75,7 @@ class PollModule(XModule):
'poll_answers': self.poll_answers, 'poll_answers': self.poll_answers,
'total': sum(self.poll_answers.values()) 'total': sum(self.poll_answers.values())
}) })
else: # return error message else: # return error message
return json.dumps({'error': 'Unknown Command!'}) return json.dumps({'error': 'Unknown Command!'})
def get_html(self): def get_html(self):
......
...@@ -98,8 +98,8 @@ class RoundTripTestCase(unittest.TestCase): ...@@ -98,8 +98,8 @@ class RoundTripTestCase(unittest.TestCase):
def test_full_roundtrip(self): def test_full_roundtrip(self):
self.check_export_roundtrip(DATA_DIR, "full") self.check_export_roundtrip(DATA_DIR, "full")
def test_poll_roundtrip(self): def test_conditional_and_poll_roundtrip(self):
self.check_export_roundtrip(DATA_DIR, "poll") self.check_export_roundtrip(DATA_DIR, "conditional_and_poll")
def test_selfassessment_roundtrip(self): def test_selfassessment_roundtrip(self):
#Test selfassessment xmodule to see if it exports correctly #Test selfassessment xmodule to see if it exports correctly
......
...@@ -319,7 +319,7 @@ class ImportTestCase(unittest.TestCase): ...@@ -319,7 +319,7 @@ class ImportTestCase(unittest.TestCase):
self.assertEqual(len(video.url_name), len('video_') + 12) self.assertEqual(len(video.url_name), len('video_') + 12)
def test_poll_xmodule(self): def test_poll_xmodule(self):
modulestore = XMLModuleStore(DATA_DIR, course_dirs=['poll']) modulestore = XMLModuleStore(DATA_DIR, course_dirs=['conditional_and_poll'])
course = modulestore.get_courses()[0] course = modulestore.get_courses()[0]
chapters = course.get_children() chapters = course.get_children()
...@@ -332,7 +332,7 @@ class ImportTestCase(unittest.TestCase): ...@@ -332,7 +332,7 @@ class ImportTestCase(unittest.TestCase):
location = Location(location.tag, location.org, location.course, location = Location(location.tag, location.org, location.course,
'sequential', 'Problem_Demos') 'sequential', 'Problem_Demos')
module = modulestore.get_instance(course.id, location) module = modulestore.get_instance(course.id, location)
self.assertEqual(len(module.children), 1) self.assertEqual(len(module.children), 2)
def test_error_on_import(self): def test_error_on_import(self):
'''Check that when load_error_module is false, an exception is raised, rather than returning an ErrorModule''' '''Check that when load_error_module is false, an exception is raised, rather than returning an ErrorModule'''
......
...@@ -4,6 +4,7 @@ import json ...@@ -4,6 +4,7 @@ import json
import unittest import unittest
from xmodule.poll_module import PollModule from xmodule.poll_module import PollModule
from xmodule.conditional_module import ConditionalModule
class LogicTest(unittest.TestCase): class LogicTest(unittest.TestCase):
...@@ -46,3 +47,19 @@ class PollModuleTest(LogicTest): ...@@ -46,3 +47,19 @@ class PollModuleTest(LogicTest):
self.assertEqual(total, 2) self.assertEqual(total, 2)
self.assertDictEqual(callback, {'objectName': 'Conditional'}) self.assertDictEqual(callback, {'objectName': 'Conditional'})
self.assertEqual(self.xmodule.poll_answer, 'No') self.assertEqual(self.xmodule.poll_answer, 'No')
class ConditionalModuleTest(LogicTest):
xmodule_class = ConditionalModule
raw_model_data = {
'contents': 'Some content'
}
def test_ajax_request(self):
# Mock is_condition_satisfied
self.xmodule.is_condition_satisfied = lambda: True
response = self.ajax_request('No', {})
html = response['html']
self.assertEqual(html, 'Some content')
roots/2013_Spring.xml
\ No newline at end of file
...@@ -9,5 +9,23 @@ ...@@ -9,5 +9,23 @@
<answer id="No">No</answer> <answer id="No">No</answer>
<answer id="Dont_know">Don't know</answer> <answer id="Dont_know">Don't know</answer>
</poll_question> </poll_question>
<poll_question id="second_poll" display_name="second poll">
<h3>What's the Right Thing to Do?</h3>
<p>Suppose four shipwrecked sailors are stranded at sea in a lifeboat, without
food or water. Would it be wrong for three of them to kill and eat the cabin
boy, in order to save their own lives?</p>
<answer id="Yes">Yes</answer>
<answer id="No">No</answer>
<answer id="Dont_know">Don't know</answer>
</poll_question>
</vertical> </vertical>
<wrapper>
<!-- Test many show tags -->
<html>Condition: first_poll - Yes</html>
<conditional sources="i4x://HarvardX/ER22x/poll_question/first_poll" poll_answer="Yes">
<html>In first condition.</html>
<show sources="i4x://HarvardX/ER22x/poll_question/second_poll"/>
</conditional>
</wrapper>
</sequential> </sequential>
roots/2013_Spring.xml
\ No newline at end of file
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