Commit db17bf4c by Calen Pennington

Add tests of XML exporting to test_xblock_wrappers

parent 85c31dd2
......@@ -48,6 +48,10 @@ from xmodule.vertical_module import VerticalDescriptor
from xmodule.wrapper_module import WrapperDescriptor
from xmodule.tests import get_test_descriptor_system, get_test_system
# A dictionary that maps specific XModuleDescriptor classes without children
# to a list of sample field values to test with.
# TODO: Add more types of sample data
LEAF_XMODULES = {
AnnotatableDescriptor: [{}],
CapaDescriptor: [{}],
......@@ -63,6 +67,9 @@ LEAF_XMODULES = {
}
# A dictionary that maps specific XModuleDescriptor classes with children
# to a list of sample field values to test with.
# TODO: Add more types of sample data
CONTAINER_XMODULES = {
ConditionalDescriptor: [{}],
CourseDescriptor: [{}],
......@@ -83,7 +90,8 @@ NOT_STUDIO_EDITABLE = (
def flatten(class_dict):
"""
Flatten a dict from cls -> [fields, ...] to a list of the form [(cls, fields), ...]
Flatten a dict from cls -> [fields, ...] and yields values of the form (cls, fields)
for each entry in the dictionary value.
"""
for cls, fields_list in class_dict.items():
for fields in fields_list:
......@@ -200,9 +208,14 @@ class ContainerModuleFactory(LeafModuleFactory):
@ddt.ddt
class TestXBlockWrapper(object):
class XBlockWrapperTestMixin(object):
"""
This is a mixin for building tests of the implementation of the XBlock
api by wrapping XModule native functions.
__test__ = False
You can creat an actual test case by inheriting from this class and UnitTest,
and implement skip_if_invalid and check_property.
"""
def skip_if_invalid(self, descriptor_cls):
"""
......@@ -244,9 +257,10 @@ class TestXBlockWrapper(object):
raise SkipTest("XBlock support in XModules not yet fully implemented")
class TestStudentView(TestXBlockWrapper, TestCase):
__test__ = True
class TestStudentView(XBlockWrapperTestMixin, TestCase):
"""
This tests that student_view and XModule.get_html produce the same results.
"""
def skip_if_invalid(self, descriptor_cls):
if descriptor_cls.module_class.student_view != XModule.student_view:
raise SkipTest(descriptor_cls.__name__ + " implements student_view")
......@@ -261,9 +275,10 @@ class TestStudentView(TestXBlockWrapper, TestCase):
)
class TestStudioView(TestXBlockWrapper, TestCase):
__test__ = True
class TestStudioView(XBlockWrapperTestMixin, TestCase):
"""
This tests that studio_view and XModuleDescriptor.get_html produce the same results
"""
def skip_if_invalid(self, descriptor_cls):
if descriptor_cls in NOT_STUDIO_EDITABLE:
raise SkipTest(descriptor_cls.__name__ + " is not editable in studio")
......@@ -300,3 +315,25 @@ class TestXModuleHandler(TestCase):
response = self.module.xmodule_handler(self.request)
self.assertIsInstance(response, webob.Response)
self.assertEqual(response.body, '{}')
class TestXmlExport(XBlockWrapperTestMixin, TestCase):
"""
This tests that XModuleDescriptor.export_to_xml and add_xml_to_node produce the same results.
"""
def skip_if_invalid(self, descriptor_cls):
if descriptor_cls.add_xml_to_node != XModuleDescriptor.add_xml_to_node:
raise SkipTest(descriptor_cls.__name__ + " implements add_xml_to_node")
def check_property(self, descriptor):
xmodule_api_fs = MemoryFS()
xblock_api_fs = MemoryFS()
descriptor.runtime.export_fs = xblock_api_fs
xblock_node = etree.Element('unknown')
descriptor.add_xml_to_node(xblock_node)
xmodule_node = etree.fromstring(descriptor.export_to_xml(xmodule_api_fs))
self.assertEquals(list(xmodule_api_fs.walk()), list(xblock_api_fs.walk()))
self.assertEquals(etree.tostring(xmodule_node), etree.tostring(xblock_node))
......@@ -363,6 +363,10 @@ class XmlDescriptor(XModuleDescriptor):
resource_fs is a pyfilesystem object (from the fs package)
"""
# Set up runtime.export_fs so that it's available through future
# uses of the pure xblock add_xml_to_node api
self.runtime.export_fs = resource_fs
# Get the definition
xml_object = self.definition_to_xml(resource_fs)
self.clean_metadata_from_xml(xml_object)
......@@ -377,12 +381,11 @@ class XmlDescriptor(XModuleDescriptor):
val = serialize_field(self._field_data.get(self, attr))
try:
xml_object.set(attr, val)
except Exception, e:
except Exception:
logging.exception(
u'Failed to serialize metadata attribute %s with value %s in module %s. This could mean data loss!!! Exception: %s',
attr, val, self.url_name, e
u'Failed to serialize metadata attribute %s with value %s in module %s. This could mean data loss!!!',
attr, val, self.url_name
)
pass
for key, value in self.xml_attributes.items():
if key not in self.metadata_to_strip:
......
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