diff --git a/common/lib/xmodule/tests/__init__.py b/common/lib/xmodule/tests/__init__.py index 5483015a82..2f12c96a7a 100644 --- a/common/lib/xmodule/tests/__init__.py +++ b/common/lib/xmodule/tests/__init__.py @@ -13,6 +13,7 @@ import numpy import xmodule import capa.calc as calc import capa.capa_problem as lcp +from capa.correctmap import CorrectMap from xmodule import graders, x_module from xmodule.graders import Score, aggregate_scores from xmodule.progress import Progress @@ -271,6 +272,50 @@ class StringResponseWithHintTest(unittest.TestCase): self.assertEquals(cmap.get_correctness('1_2_1'), 'incorrect') self.assertTrue('St. Paul' in cmap.get_hint('1_2_1')) +class CodeResponseTest(unittest.TestCase): + ''' + Test CodeResponse + + ''' + def test_update_score(self): + problem_file = os.path.dirname(__file__)+"/test_files/coderesponse.xml" + test_lcp = lcp.LoncapaProblem(open(problem_file).read(), '1', system=i4xs) + + # CodeResponse requires internal CorrectMap state. Build it now + old_cmap = CorrectMap() + answer_ids = sorted(test_lcp.get_question_answers().keys()) + numAnswers = len(answer_ids) + for i in range(numAnswers): + old_cmap.update(CorrectMap(answer_id=answer_ids[i], queuekey=1000+i)) + + # Message format inherited from ExternalResponse + correct_score_msg = "EXACT_ANSMESSAGE" + incorrect_score_msg = "WRONG_FORMATMESSAGE" + xserver_msgs = {'correct': correct_score_msg, + 'incorrect': incorrect_score_msg, + } + + # Incorrect queuekey, state should not be updated + for correctness in ['correct', 'incorrect']: + test_lcp.correct_map = CorrectMap() + test_lcp.correct_map.update(old_cmap) # Deep copy + + test_lcp.update_score(xserver_msgs[correctness], queuekey=0) + self.assertEquals(test_lcp.correct_map.get_dict(), old_cmap.get_dict()) # Deep comparison + + # Correct queuekey, state should be updated + for correctness in ['correct', 'incorrect']: + for i in range(numAnswers): # Target specific answer_id's + test_lcp.correct_map = CorrectMap() + test_lcp.correct_map.update(old_cmap) + + new_cmap = CorrectMap() + new_cmap.update(old_cmap) + new_cmap.set(answer_id=answer_ids[i], correctness=correctness, msg='MESSAGE', queuekey=None) + + test_lcp.update_score(xserver_msgs[correctness], queuekey=1000+i) + self.assertEquals(test_lcp.correct_map.get_dict(), new_cmap.get_dict()) + #----------------------------------------------------------------------------- # Grading tests diff --git a/common/lib/xmodule/tests/test_files/coderesponse.xml b/common/lib/xmodule/tests/test_files/coderesponse.xml new file mode 100644 index 0000000000..42b6e0a54a --- /dev/null +++ b/common/lib/xmodule/tests/test_files/coderesponse.xml @@ -0,0 +1,101 @@ + + +

Code response

+ +

+

+ + +Write a program to compute the square of a number + + + + + + + + +Write a program to compute the cube of a number + + + + + + + +
+