From e2b60e1c97d290c6a5179ace5a3a517fe3bc96c6 Mon Sep 17 00:00:00 2001 From: Awais Jibran Date: Mon, 20 Jul 2020 14:37:57 +0500 Subject: [PATCH] bug: fix an edge case in capa problem tolerance PROD-1586 --- common/lib/capa/capa/responsetypes.py | 4 ++-- common/lib/capa/capa/tests/test_responsetypes.py | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index e6bf7a9763..7ca8e837d0 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -1551,7 +1551,7 @@ class NumericalResponse(LoncapaResponse): id=xml.get('id') ) if tolerance_xml: # If it isn't an empty list... - self.tolerance = contextualize_text(tolerance_xml[0], context) + self.tolerance = contextualize_text(tolerance_xml[0].strip(), context) def get_staff_ans(self, answer): """ @@ -3057,7 +3057,7 @@ class FormulaResponse(LoncapaResponse): id=xml.get('id') ) if tolerance_xml: # If it isn't an empty list... - self.tolerance = contextualize_text(tolerance_xml[0], context) + self.tolerance = contextualize_text(tolerance_xml[0].strip(), context) types = xml.get('type') if types is None: diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 50370a9586..1549dd277c 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -1515,6 +1515,15 @@ class NumericalResponseTest(ResponseTest): # pylint: disable=missing-class-docs incorrect_responses = ["", "3.01", "-1.01"] self.assert_multiple_grade(problem, correct_responses, incorrect_responses) + def test_grade_percent_tolerance_with_spaces(self): + """ + Tests that system does not throw an error when tolerance data contains spaces before or after. + """ + problem = self.build_problem(answer=4, tolerance="10% ") + correct_responses = ["4.0", "4.00", "4.39", "3.61"] + incorrect_responses = ["", "4.41", "3.59", "0"] + self.assert_multiple_grade(problem, correct_responses, incorrect_responses) + def test_floats(self): """ Default tolerance for all responsetypes is 1e-3%.