From 2f03b97636cc50ee7f4d7a4aae9edd2670552cd5 Mon Sep 17 00:00:00 2001 From: "E. Kolpakov" Date: Tue, 17 Mar 2015 15:00:59 +0300 Subject: [PATCH] Index dictionary for CapaDescriptor + extending tests to cover it --- common/lib/xmodule/xmodule/capa_module.py | 16 ++++++++++ .../xmodule/xmodule/tests/test_capa_module.py | 30 ++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index 7364730815..d647aa8c0d 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -113,6 +113,7 @@ class CapaDescriptor(CapaFields, RawDescriptor): Module implementing problems in the LON-CAPA format, as implemented by capa.capa_problem """ + INDEX_CONTENT_TYPE = 'CAPA' module_class = CapaModule @@ -186,6 +187,21 @@ class CapaDescriptor(CapaFields, RawDescriptor): registered_tags = responsetypes.registry.registered_tags() return set([node.tag for node in tree.iter() if node.tag in registered_tags]) + def index_dictionary(self): + """ + Return dictionary prepared with module content and type for indexing. + """ + result = super(CapaDescriptor, self).index_dictionary() + if not result: + result = {} + index = { + 'content_type': self.INDEX_CONTENT_TYPE, + 'problem_types': list(self.problem_types), + "display_name": self.display_name + } + result.update(index) + return result + # Proxy to CapaModule for access to any of its attributes answer_available = module_attr('answer_available') check_button_name = module_attr('check_button_name') diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index 2c666743ae..e88e59782d 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -1659,18 +1659,26 @@ class CapaModuleTest(unittest.TestCase): @ddt.ddt class CapaDescriptorTest(unittest.TestCase): - def _create_descriptor(self, xml): + def _create_descriptor(self, xml, name=None): """ Creates a CapaDescriptor to run test against """ descriptor = CapaDescriptor(get_test_system(), scope_ids=1) descriptor.data = xml + if name: + descriptor.display_name = name return descriptor @ddt.data(*responsetypes.registry.registered_tags()) def test_all_response_types(self, response_tag): """ Tests that every registered response tag is correctly returned """ xml = "<{response_tag}>".format(response_tag=response_tag) - descriptor = self._create_descriptor(xml) + name = "Some Capa Problem" + descriptor = self._create_descriptor(xml, name=name) self.assertEquals(descriptor.problem_types, {response_tag}) + self.assertEquals(descriptor.index_dictionary(), { + 'content_type': CapaDescriptor.INDEX_CONTENT_TYPE, + 'display_name': name, + 'problem_types': [response_tag] + }) def test_response_types_ignores_non_response_tags(self): xml = textwrap.dedent(""" @@ -1687,8 +1695,14 @@ class CapaDescriptorTest(unittest.TestCase): """) - descriptor = self._create_descriptor(xml) + name = "Test Capa Problem" + descriptor = self._create_descriptor(xml, name=name) self.assertEquals(descriptor.problem_types, {"multiplechoiceresponse"}) + self.assertEquals(descriptor.index_dictionary(), { + 'content_type': CapaDescriptor.INDEX_CONTENT_TYPE, + 'display_name': name, + 'problem_types': ["multiplechoiceresponse"] + }) def test_response_types_multiple_tags(self): xml = textwrap.dedent(""" @@ -1710,8 +1724,16 @@ class CapaDescriptorTest(unittest.TestCase): """) - descriptor = self._create_descriptor(xml) + name = "Other Test Capa Problem" + descriptor = self._create_descriptor(xml, name=name) self.assertEquals(descriptor.problem_types, {"multiplechoiceresponse", "optionresponse"}) + self.assertEquals( + descriptor.index_dictionary(), { + 'content_type': CapaDescriptor.INDEX_CONTENT_TYPE, + 'display_name': name, + 'problem_types': ["optionresponse", "multiplechoiceresponse"] + } + ) class ComplexEncoderTest(unittest.TestCase):