for the textline
textline_element = response_element.find("div")
assert textline_element.text == "Input Template Render"
# Expect a child
for the solution
# with the rendered template
solution_element = rendered_html.xpath('//div[@class="input-template-render"]')[0]
assert solution_element.text == "Input Template Render"
# Expect that the template renderer was called with the correct
# arguments, once for the textline input and once for
# the solution
expected_textline_context = {
"STATIC_URL": "/dummy-static/",
"status": the_system.STATUS_CLASS("unsubmitted"),
"value": "",
"preprocessor": None,
"msg": "",
"inline": False,
"hidden": False,
"do_math": False,
"id": "1_2_1",
"trailing_text": "",
"size": None,
"response_data": {"label": "Test question", "descriptions": {}},
"describedby_html": HTML('aria-describedby="status_1_2_1"'),
}
expected_solution_context = {"id": "1_solution_1"}
expected_calls = [
mock.call("textline.html", expected_textline_context),
mock.call("solutionspan.html", expected_solution_context),
mock.call("textline.html", expected_textline_context),
mock.call("solutionspan.html", expected_solution_context),
]
assert the_system.render_template.call_args_list == expected_calls
def test_correct_aria_label(self):
"""Check that rendered responses have correct aria-label attributes."""
xml = """
over-suspicious
funny
Urdu
Finnish
"""
problem = new_loncapa_problem(xml)
rendered_html = etree.XML(problem.get_html())
response_elements = rendered_html.findall("div")
assert response_elements[0].attrib["aria-label"] == "Question 1"
assert response_elements[1].attrib["aria-label"] == "Question 2"
def test_render_response_with_overall_msg(self):
"""Verify that CustomResponse overall messages are rendered correctly."""
# CustomResponse script that sets an overall_message
script = textwrap.dedent(
"""
def check_func(*args):
msg = '
Test message 1
Test message 2
'
return {'overall_message': msg,
'input_list': [ {'ok': True, 'msg': '' } ] }
"""
)
# Generate some XML for a CustomResponse
kwargs = {"script": script, "cfn": "check_func"}
xml_str = CustomResponseXMLFactory().build_xml(**kwargs)
# Create the problem and render the html
problem = new_loncapa_problem(xml_str)
# Grade the problem
problem.grade_answers({"1_2_1": "test"})
# Render the html
rendered_html = etree.XML(problem.get_html())
# Expect that there is a
within the response
# with css class response_message
msg_div_element = rendered_html.find(".//div[@class='response_message']")
assert msg_div_element.tag == "div"
assert msg_div_element.get("class") == "response_message"
# Expect that the
contains our message (as part of the XML tree)
msg_p_elements = msg_div_element.findall("p")
assert msg_p_elements[0].tag == "p"
assert msg_p_elements[0].text == "Test message 1"
assert msg_p_elements[1].tag == "p"
assert msg_p_elements[1].text == "Test message 2"
def test_substitute_python_vars(self):
"""Ensure Python variables in XML scripts are substituted correctly in attributes."""
# Generate some XML with Python variables defined in a script
# and used later as attributes
xml_str = textwrap.dedent(
"""
"""
)
# Create the problem and render the HTML
problem = new_loncapa_problem(xml_str)
rendered_html = etree.XML(problem.get_html())
# Expect that the variable $test has been replaced with its value
span_element = rendered_html.find("span")
assert span_element.get("attr") == "TEST"
def test_xml_comments_and_other_odd_things(self):
"""Ensure XML comments and processing instructions are skipped in rendering."""
# Comments and processing instructions should be skipped.
xml_str = textwrap.dedent(
"""\
"""
)
# Create the problem
problem = new_loncapa_problem(xml_str)
# Render the HTML
the_html = problem.get_html()
self.assertRegex(the_html, r"
\s*
")
def _create_test_file(self, path, content_str):
"""Create a temporary test file with the given content and schedule cleanup."""
test_fp = self.capa_system.resources_fs.open(path, "w")
test_fp.write(content_str)
test_fp.close()
self.addCleanup(lambda: os.remove(test_fp.name))