From 8162d5473f464c40b0d6717ba2aefe68fe70583e Mon Sep 17 00:00:00 2001 From: Arthur Barrett Date: Tue, 26 Feb 2013 17:08:01 -0500 Subject: [PATCH] added more unit tests for module --- .../lib/xmodule/xmodule/annotatable_module.py | 10 +-- .../xmodule/tests/test_annotatable_module.py | 70 ++++++++++++++++++- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/common/lib/xmodule/xmodule/annotatable_module.py b/common/lib/xmodule/xmodule/annotatable_module.py index 9e492f755a..665be210e4 100644 --- a/common/lib/xmodule/xmodule/annotatable_module.py +++ b/common/lib/xmodule/xmodule/annotatable_module.py @@ -29,14 +29,13 @@ class AnnotatableModule(XModule): attr = {} cls = ['annotatable-span', 'highlight'] - valid_colors = ['yellow', 'orange', 'purple', 'blue', 'green'] highlight_key = 'highlight' - color = el.get(highlight_key) - if color is not None and color in valid_colors: - cls.append('highlight-'+color) - attr['_delete'] = highlight_key + if color is not None: + if color in self.highlight_colors: + cls.append('highlight-'+color) + attr['_delete'] = highlight_key attr['value'] = ' '.join(cls) return { 'class' : attr } @@ -120,6 +119,7 @@ class AnnotatableModule(XModule): self.instructions = self._extract_instructions(xmltree) self.content = etree.tostring(xmltree, encoding='unicode') self.element_id = self.location.html_id() + self.highlight_colors = ['yellow', 'orange', 'purple', 'blue', 'green'] class AnnotatableDescriptor(RawDescriptor): module_class = AnnotatableModule diff --git a/common/lib/xmodule/xmodule/tests/test_annotatable_module.py b/common/lib/xmodule/xmodule/tests/test_annotatable_module.py index 7a87dcc16d..3a470879e8 100644 --- a/common/lib/xmodule/xmodule/tests/test_annotatable_module.py +++ b/common/lib/xmodule/xmodule/tests/test_annotatable_module.py @@ -45,6 +45,70 @@ class AnnotatableModuleTestCase(unittest.TestCase): 'data-problem-id': {'value': '0', '_delete': 'problem'} } - data_attr = self.annotatable._get_annotation_data_attr(0, el) - self.assertTrue(type(data_attr) is dict) - self.assertDictEqual(expected_attr, data_attr) \ No newline at end of file + actual_attr = self.annotatable._get_annotation_data_attr(0, el) + + self.assertTrue(type(actual_attr) is dict) + self.assertDictEqual(expected_attr, actual_attr) + + def test_annotation_class_attr_default(self): + xml = 'test' + el = etree.fromstring(xml) + + expected_attr = { 'class': { 'value': 'annotatable-span highlight' } } + actual_attr = self.annotatable._get_annotation_class_attr(0, el) + + self.assertTrue(type(actual_attr) is dict) + self.assertDictEqual(expected_attr, actual_attr) + + def test_annotation_class_attr_with_valid_highlight(self): + xml = 'test' + + for color in self.annotatable.highlight_colors: + el = etree.fromstring(xml.format(highlight=color)) + value = 'annotatable-span highlight highlight-{highlight}'.format(highlight=color) + + expected_attr = { 'class': { + 'value': value, + '_delete': 'highlight' } + } + actual_attr = self.annotatable._get_annotation_class_attr(0, el) + + self.assertTrue(type(actual_attr) is dict) + self.assertDictEqual(expected_attr, actual_attr) + + def test_annotation_class_attr_with_invalid_highlight(self): + xml = 'test' + + for invalid_color in ['rainbow', 'blink', 'invisible', '', None]: + el = etree.fromstring(xml.format(highlight=invalid_color)) + expected_attr = { 'class': { + 'value': 'annotatable-span highlight', + '_delete': 'highlight' } + } + actual_attr = self.annotatable._get_annotation_class_attr(0, el) + + self.assertTrue(type(actual_attr) is dict) + self.assertDictEqual(expected_attr, actual_attr) + + def test_render_annotation(self): + expected_html = 'z' + expected_el = etree.fromstring(expected_html) + + actual_el = etree.fromstring('z') + self.annotatable._render_annotation(0, actual_el) + + self.assertEqual(expected_el.tag, actual_el.tag) + self.assertEqual(expected_el.text, actual_el.text) + self.assertDictEqual(dict(expected_el.attrib), dict(actual_el.attrib)) + + def test_extract_instructions(self): + xmltree = etree.fromstring(self.sample_text) + + expected_xml = u"
Read the text.
" + actual_xml = self.annotatable._extract_instructions(xmltree) + self.assertIsNotNone(actual_xml) + self.assertEqual(expected_xml.strip(), actual_xml.strip()) + + xmltree = etree.fromstring('foo') + actual = self.annotatable._extract_instructions(xmltree) + self.assertIsNone(actual) \ No newline at end of file