diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index 84af4db4b6..ee2c9967c6 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -669,11 +669,17 @@ class LoncapaProblem(object): answer_id=answer_id, choice_number=current_answer )) - assert len(elems) == 1 - choicegroup = elems[0].getparent() - input_cls = inputtypes.registry.get_class_for_tag(choicegroup.tag) - choices_map = dict(input_cls.extract_choices(choicegroup, self.capa_system.i18n, text_only=True)) - answer_text = choices_map[current_answer] + if len(elems) == 0: + log.warning("Answer Text Missing for answer id: %s and choice number: %s", answer_id, current_answer) + answer_text = "Answer Text Missing" + elif len(elems) == 1: + choicegroup = elems[0].getparent() + input_cls = inputtypes.registry.get_class_for_tag(choicegroup.tag) + choices_map = dict(input_cls.extract_choices(choicegroup, self.capa_system.i18n, text_only=True)) + answer_text = choices_map.get(current_answer, "Answer Text Missing") + else: + log.warning("Multiple answers found for answer id: %s and choice number: %s", answer_id, current_answer) + answer_text = "Multiple answers found" elif isinstance(current_answer, six.string_types): # Already a string with the answer @@ -682,7 +688,7 @@ class LoncapaProblem(object): else: raise NotImplementedError() - return answer_text + return answer_text or "Answer Text Missing" def do_targeted_feedback(self, tree): """ diff --git a/common/lib/capa/capa/tests/test_capa_problem.py b/common/lib/capa/capa/tests/test_capa_problem.py index f4cbf8eb2a..9ff748618d 100644 --- a/common/lib/capa/capa/tests/test_capa_problem.py +++ b/common/lib/capa/capa/tests/test_capa_problem.py @@ -627,6 +627,43 @@ class CAPAProblemReportHelpersTest(unittest.TestCase): ) assert problem.find_answer_text(answer_id, choice_id) == answer_text + @ddt.data( + # Test for ChoiceResponse + ('1_2_1', 'choice_0', 'Answer Text Missing'), + ('1_2_1', 'choice_1', 'funny'), + # Test for MultipleChoiceResponse + ('1_3_1', 'choice_0', 'The iPad'), + ('1_3_1', 'choice_2', 'Answer Text Missing'), + ('1_3_1', ['choice_0', 'choice_1'], 'The iPad, Answer Text Missing'), + # Test for OptionResponse + ('1_4_1', '', 'Answer Text Missing'), + ) + @ddt.unpack + def test_find_answer_text_choices_with_missing_text(self, answer_id, choice_id, answer_text): + problem = new_loncapa_problem( + """ + + + + + funny + + + + + The iPad + + + + + + + + + """ + ) + assert problem.find_answer_text(answer_id, choice_id) == answer_text + @ddt.data( # Test for ChoiceResponse ('1_2_1', 'over-suspicious'),