From c009d4550ac2e23b986128bafca389dc362fd491 Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Wed, 23 Oct 2019 14:31:33 -0400 Subject: [PATCH] Pass back the answer html as a string instead of bytes. We did this already for the 'get_html' function for a capa problem but there wasn't a test to ensure this works correctly for the get_question_answer function that powers the show answer button. I ran into it while doing a quick smoke test on the python 3 sandbox. --- common/lib/capa/capa/capa_problem.py | 2 +- .../lib/capa/capa/tests/test_capa_problem.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index bc650c426f..ee327dde52 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -485,7 +485,7 @@ class LoncapaProblem(object): # include solutions from ... stanzas for entry in self.tree.xpath("//" + "|//".join(solution_tags)): - answer = etree.tostring(entry) + answer = etree.tostring(entry).decode('utf-8') if answer: answer_map[entry.get('id')] = contextualize_text(answer, self.context) diff --git a/common/lib/capa/capa/tests/test_capa_problem.py b/common/lib/capa/capa/tests/test_capa_problem.py index b5d6e0a107..547f2c7570 100644 --- a/common/lib/capa/capa/tests/test_capa_problem.py +++ b/common/lib/capa/capa/tests/test_capa_problem.py @@ -719,3 +719,24 @@ class CAPAProblemReportHelpersTest(unittest.TestCase): """ ) self.assertEquals(problem.find_answer_text('1_2_1', 'hide'), 'hide') + + def test_get_question_answer(self): + problem = new_loncapa_problem( + """ + + + + + +
+

Explanation

+

Blue is the answer.

+
+
+
+ """ + ) + + # Ensure that the answer is a string so that the dict returned from this + # function can eventualy be serialized to json without issues. + self.assertIsInstance(problem.get_question_answers()['1_solution_1'], six.text_type)