From 8b5473b6f62587bcf51838c7eb15449abd2551c4 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Mon, 25 Feb 2013 15:30:55 -0500 Subject: [PATCH] Wrote unit tests for NumericalResponse capa response type --- common/lib/capa/capa/responsetypes.py | 29 ++++---- .../lib/capa/capa/tests/test_responsetypes.py | 67 +++++++++++++++++++ 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index a1a4e6b65e..8811e7d863 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -1048,19 +1048,24 @@ def sympy_check2(): correct = ['correct'] * len(idset) if ret['ok'] else ['incorrect'] * len(idset) msg = ret['msg'] - if 1: - # try to clean up message html - msg = '' + msg + '' - msg = msg.replace('<', '<') - #msg = msg.replace('<','<') - msg = etree.tostring(fromstring_bs(msg, convertEntities=None), - pretty_print=True) - #msg = etree.tostring(fromstring_bs(msg),pretty_print=True) - msg = msg.replace(' ', '') - #msg = re.sub('(.*)','\\1',msg,flags=re.M|re.DOTALL) # python 2.7 - msg = re.sub('(?ms)(.*)', '\\1', msg) + def _cleanup_msg_html(msg_html): + cleaned = msg_html - messages[0] = msg + # try to clean up message html + cleaned = '' + cleaned + '' + cleaned = cleaned.replace('<', '<') + cleaned = etree.tostring(fromstring_bs(cleaned, convertEntities=None), + pretty_print=True) + cleaned = cleaned.replace(' ', '') + cleaned = re.sub('(?ms)(.*)', '\\1', cleaned) + + return cleaned + + if type(msg) == str: + messages[0] = _cleanup_msg_html(msg) + elif type(msg) == list: + for i in range(0, len(msg)): + messages[i] = _cleanup_msg_html(msg[i]) else: correct = ['correct'] * len(idset) if ret else ['incorrect'] * len(idset) diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 18da338b91..97d9ce33da 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -426,3 +426,70 @@ class JavascriptResponseTest(unittest.TestCase): self.assertEquals(test_lcp.grade_answers(incorrect_answers).get_correctness('1_2_1'), 'incorrect') self.assertEquals(test_lcp.grade_answers(correct_answers).get_correctness('1_2_1'), 'correct') + +from response_xml_factory import NumericalResponseXMLFactory +class NumericalResponseTest(unittest.TestCase): + + def setUp(self): + self.xml_factory = NumericalResponseXMLFactory() + + def test_grade_exact(self): + xml = self.xml_factory.build_xml(question_text="What is 2 + 2?", + explanation="The answer is 4", + answer=4) + correct_responses = ["4", "4.0", "4.00"] + incorrect_responses = ["", "3.9", "4.1", "0"] + self._test_grading(xml, correct_responses, incorrect_responses) + + + def test_grade_decimal_tolerance(self): + xml = self.xml_factory.build_xml(question_text="What is 2 + 2 approximately?", + explanation="The answer is 4", + answer=4, + tolerance=0.1) + correct_responses = ["4.0", "4.00", "4.09", "3.91"] + incorrect_responses = ["", "4.11", "3.89", "0"] + self._test_grading(xml, correct_responses, incorrect_responses) + + def test_grade_percent_tolerance(self): + xml = self.xml_factory.build_xml(question_text="What is 2 + 2 approximately?", + explanation="The answer is 4", + answer=4, + tolerance="10%") + correct_responses = ["4.0", "4.3", "3.7", "4.30", "3.70"] + incorrect_responses = ["", "4.5", "3.5", "0"] + self._test_grading(xml, correct_responses, incorrect_responses) + + def test_grade_with_script(self): + script_text = "computed_response = math.sqrt(4)" + xml = self.xml_factory.build_xml(question_text="What is sqrt(4)?", + explanation="The answer is 2", + answer="$computed_response", + script=script_text) + correct_responses = ["2", "2.0"] + incorrect_responses = ["", "2.01", "1.99", "0"] + self._test_grading(xml, correct_responses, incorrect_responses) + + def test_grade_with_script_and_tolerance(self): + script_text = "computed_response = math.sqrt(4)" + xml = self.xml_factory.build_xml(question_text="What is sqrt(4)?", + explanation="The answer is 2", + answer="$computed_response", + tolerance="0.1", + script=script_text) + correct_responses = ["2", "2.0", "2.05", "1.95"] + incorrect_responses = ["", "2.11", "1.89", "0"] + self._test_grading(xml, correct_responses, incorrect_responses) + + + def _test_grading(self, xml, correct_answers, incorrect_answers): + + problem = lcp.LoncapaProblem(xml, '1', system=test_system) + + for input_str in correct_answers: + result = problem.grade_answers({'1_2_1': input_str}).get_correctness('1_2_1') + self.assertEqual(result, 'correct') + + for input_str in incorrect_answers: + result = problem.grade_answers({'1_2_1': input_str}).get_correctness('1_2_1') + self.assertEqual(result, 'incorrect')