Skip to content

Commit

Permalink
fix: fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan committed Jan 16, 2025
1 parent 988cb0c commit b461d46
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
12 changes: 7 additions & 5 deletions lms/djangoapps/courseware/tests/test_word_cloud.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Word cloud integration tests using mongo modulestore."""

from unittest.mock import patch

import pytest

import json
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
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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)
24 changes: 17 additions & 7 deletions xmodule/tests/test_word_cloud.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -36,13 +38,15 @@ def test_xml_import_export_cycle(self):
runtime.export_fs = MemoryFS()

original_xml = (
'<word_cloud display_name="Favorite Fruits" display_student_percents="false" '
'<word_cloud xblock-family="xblock.v1" display_name="Favorite Fruits" display_student_percents="false" '
'instructions="What are your favorite fruits?" num_inputs="3" num_top_words="100"/>\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'
)
Expand All @@ -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):
"""
Expand Down Expand Up @@ -111,7 +121,7 @@ def test_indexibility(self):

module_system = get_test_system()
block = WordCloudBlock(module_system, DictFieldData(self.raw_field_data), Mock())
assert block.index_dictionary() ==\
assert block.index_dictionary() == \
{'content_type': 'Word Cloud',
'content': {'display_name': 'Word Cloud Block',
'instructions': 'Enter some random words that comes to your mind'}}

0 comments on commit b461d46

Please sign in to comment.