diff --git a/lms/djangoapps/courseware/tests/test_word_cloud.py b/lms/djangoapps/courseware/tests/test_word_cloud.py index 06217628cbca..f36b847847a1 100644 --- a/lms/djangoapps/courseware/tests/test_word_cloud.py +++ b/lms/djangoapps/courseware/tests/test_word_cloud.py @@ -1,5 +1,5 @@ """Word cloud integration tests using mongo modulestore.""" - +from unittest.mock import patch import pytest @@ -7,7 +7,7 @@ from operator import itemgetter # noinspection PyUnresolvedReferences -from xmodule.tests.helpers import override_descriptor_system # pylint: disable=unused-import +from xmodule.tests.helpers import override_descriptor_system, mock_render_template # pylint: disable=unused-import from xmodule.x_module import STUDENT_VIEW from .helpers import BaseTestXmodule @@ -214,7 +214,8 @@ def test_handle_ajax_incorrect_dispatch(self): } ) - def test_word_cloud_constructor(self): + @patch('xblock.utils.resources.ResourceLoader.render_django_template', side_effect=mock_render_template) + def test_word_cloud_constructor(self, mock_render_django_template): """ Make sure that all parameters extracted correctly from xml. """ @@ -223,10 +224,11 @@ def test_word_cloud_constructor(self): 'ajax_url': self.block.ajax_url, 'display_name': self.block.display_name, 'instructions': self.block.instructions, - 'element_class': self.block.location.block_type, 'element_id': self.block.location.html_id(), 'num_inputs': 5, # default value + 'range_num_inputs': range(5), 'submitted': False, # default value, } - assert fragment.content == self.runtime.render_template('word_cloud.html', expected_context) + mock_render_django_template.assert_called_once() + assert fragment.content == self.runtime.render_template('templates/word_cloud.html', expected_context) diff --git a/xmodule/tests/test_word_cloud.py b/xmodule/tests/test_word_cloud.py index 6c928465262b..29b20995ea3f 100644 --- a/xmodule/tests/test_word_cloud.py +++ b/xmodule/tests/test_word_cloud.py @@ -1,6 +1,7 @@ """Test for Word Cloud Block functional logic.""" import json +import os from unittest.mock import Mock from django.test import TestCase @@ -9,6 +10,7 @@ from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator from webob.multidict import MultiDict from xblock.field_data import DictFieldData +from xblock.fields import ScopeIds from xmodule.word_cloud_block import WordCloudBlock from . import get_test_descriptor_system, get_test_system @@ -36,13 +38,15 @@ def test_xml_import_export_cycle(self): runtime.export_fs = MemoryFS() original_xml = ( - '\n' ) olx_element = etree.fromstring(original_xml) runtime.id_generator = Mock() - block = WordCloudBlock.parse_xml(olx_element, runtime, None) + def_id = runtime.id_generator.create_definition(olx_element.tag, olx_element.get('url_name')) + keys = ScopeIds(None, olx_element.tag, def_id, runtime.id_generator.create_usage(def_id)) + block = WordCloudBlock.parse_xml(olx_element, runtime, keys) block.location = BlockUsageLocator( CourseLocator('org', 'course', 'run', branch='revision'), 'word_cloud', 'block_id' ) @@ -53,13 +57,19 @@ def test_xml_import_export_cycle(self): assert block.num_inputs == 3 assert block.num_top_words == 100 - node = etree.Element("unknown_root") - # This will export the olx to a separate file. - block.add_xml_to_node(node) + filepath = 'word_cloud/block_id.xml' + runtime.export_fs.makedirs(os.path.dirname(filepath), recreate=True) + with runtime.export_fs.open(filepath, 'wb') as fileobj: + runtime.export_to_xml(block, fileobj) + with runtime.export_fs.open('word_cloud/block_id.xml') as f: exported_xml = f.read() - assert exported_xml == original_xml + exported_xml_tree = etree.fromstring(exported_xml.encode('utf-8')) + etree.cleanup_namespaces(exported_xml_tree) + exported_xml = etree.tostring(exported_xml_tree, encoding='unicode', pretty_print=True) + + assert original_xml == exported_xml def test_bad_ajax_request(self): """