Commit b4b8f6bc by kimth

Use with open(file) structure

parent 2919389d
...@@ -323,55 +323,56 @@ class CodeResponseTest(unittest.TestCase): ...@@ -323,55 +323,56 @@ class CodeResponseTest(unittest.TestCase):
Test whether LoncapaProblem.update_score can deliver queued result to the right subproblem Test whether LoncapaProblem.update_score can deliver queued result to the right subproblem
''' '''
problem_file = os.path.join(os.path.dirname(__file__), "test_files/coderesponse.xml") problem_file = os.path.join(os.path.dirname(__file__), "test_files/coderesponse.xml")
test_lcp = lcp.LoncapaProblem(open(problem_file).read(), '1', system=i4xs) with open(problem_file) as input_file:
test_lcp = lcp.LoncapaProblem(input_file.read(), '1', system=i4xs)
answer_ids = sorted(test_lcp.get_question_answers().keys()) answer_ids = sorted(test_lcp.get_question_answers().keys())
num_answers = len(answer_ids) num_answers = len(answer_ids)
# CodeResponse requires internal CorrectMap state. Build it now in the queued state # CodeResponse requires internal CorrectMap state. Build it now in the queued state
old_cmap = CorrectMap() old_cmap = CorrectMap()
for i in range(num_answers): for i in range(num_answers):
queuekey = 1000 + i queuekey = 1000 + i
queuestate = CodeResponseTest.make_queuestate(1000+i, datetime.now()) queuestate = CodeResponseTest.make_queuestate(1000+i, datetime.now())
old_cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=queuestate)) old_cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=queuestate))
# Message format common to external graders # Message format common to external graders
correct_score_msg = json.dumps({'correct':True, 'score':1, 'msg':'MESSAGE'}) correct_score_msg = json.dumps({'correct':True, 'score':1, 'msg':'MESSAGE'})
incorrect_score_msg = json.dumps({'correct':False, 'score':0, 'msg':'MESSAGE'}) incorrect_score_msg = json.dumps({'correct':False, 'score':0, 'msg':'MESSAGE'})
xserver_msgs = {'correct': correct_score_msg, xserver_msgs = {'correct': correct_score_msg,
'incorrect': incorrect_score_msg,} 'incorrect': incorrect_score_msg,}
# Incorrect queuekey, state should not be updated # Incorrect queuekey, state should not be updated
for correctness in ['correct', 'incorrect']: for correctness in ['correct', 'incorrect']:
test_lcp.correct_map = CorrectMap() test_lcp.correct_map = CorrectMap()
test_lcp.correct_map.update(old_cmap) # Deep copy test_lcp.correct_map.update(old_cmap) # Deep copy
test_lcp.update_score(xserver_msgs[correctness], queuekey=0) test_lcp.update_score(xserver_msgs[correctness], queuekey=0)
self.assertEquals(test_lcp.correct_map.get_dict(), old_cmap.get_dict()) # Deep comparison self.assertEquals(test_lcp.correct_map.get_dict(), old_cmap.get_dict()) # Deep comparison
for i in range(num_answers): for i in range(num_answers):
self.assertTrue(test_lcp.correct_map.is_queued(answer_ids[i])) # Should be still queued, since message undelivered self.assertTrue(test_lcp.correct_map.is_queued(answer_ids[i])) # Should be still queued, since message undelivered
# Correct queuekey, state should be updated # Correct queuekey, state should be updated
for correctness in ['correct', 'incorrect']: for correctness in ['correct', 'incorrect']:
for i in range(num_answers): # Target specific answer_id's for i in range(num_answers): # Target specific answer_id's
test_lcp.correct_map = CorrectMap() test_lcp.correct_map = CorrectMap()
test_lcp.correct_map.update(old_cmap) test_lcp.correct_map.update(old_cmap)
new_cmap = CorrectMap() new_cmap = CorrectMap()
new_cmap.update(old_cmap) new_cmap.update(old_cmap)
npoints = 1 if correctness=='correct' else 0 npoints = 1 if correctness=='correct' else 0
new_cmap.set(answer_id=answer_ids[i], npoints=npoints, correctness=correctness, msg='MESSAGE', queuestate=None) new_cmap.set(answer_id=answer_ids[i], npoints=npoints, correctness=correctness, msg='MESSAGE', queuestate=None)
test_lcp.update_score(xserver_msgs[correctness], queuekey=1000 + i) test_lcp.update_score(xserver_msgs[correctness], queuekey=1000 + i)
self.assertEquals(test_lcp.correct_map.get_dict(), new_cmap.get_dict()) self.assertEquals(test_lcp.correct_map.get_dict(), new_cmap.get_dict())
for j in range(num_answers): for j in range(num_answers):
if j == i: if j == i:
self.assertFalse(test_lcp.correct_map.is_queued(answer_ids[j])) # Should be dequeued, message delivered self.assertFalse(test_lcp.correct_map.is_queued(answer_ids[j])) # Should be dequeued, message delivered
else: else:
self.assertTrue(test_lcp.correct_map.is_queued(answer_ids[j])) # Should be queued, message undelivered self.assertTrue(test_lcp.correct_map.is_queued(answer_ids[j])) # Should be queued, message undelivered
def test_recentmost_queuetime(self): def test_recentmost_queuetime(self):
...@@ -379,48 +380,49 @@ class CodeResponseTest(unittest.TestCase): ...@@ -379,48 +380,49 @@ class CodeResponseTest(unittest.TestCase):
Test whether the LoncapaProblem knows about the time of queue requests Test whether the LoncapaProblem knows about the time of queue requests
''' '''
problem_file = os.path.join(os.path.dirname(__file__), "test_files/coderesponse.xml") problem_file = os.path.join(os.path.dirname(__file__), "test_files/coderesponse.xml")
test_lcp = lcp.LoncapaProblem(open(problem_file).read(), '1', system=i4xs) with open(problem_file) as input_file:
test_lcp = lcp.LoncapaProblem(input_file.read(), '1', system=i4xs)
answer_ids = sorted(test_lcp.get_question_answers().keys()) answer_ids = sorted(test_lcp.get_question_answers().keys())
num_answers = len(answer_ids) num_answers = len(answer_ids)
# CodeResponse requires internal CorrectMap state. Build it now in the unqueued state # CodeResponse requires internal CorrectMap state. Build it now in the unqueued state
cmap = CorrectMap() cmap = CorrectMap()
for i in range(num_answers): for i in range(num_answers):
cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=None)) cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=None))
test_lcp.correct_map.update(cmap) test_lcp.correct_map.update(cmap)
self.assertEquals(test_lcp.get_recentmost_queuetime(), None) self.assertEquals(test_lcp.get_recentmost_queuetime(), None)
# CodeResponse requires internal CorrectMap state. Build it now in the queued state # CodeResponse requires internal CorrectMap state. Build it now in the queued state
cmap = CorrectMap() cmap = CorrectMap()
answer_ids = sorted(test_lcp.get_question_answers().keys()) answer_ids = sorted(test_lcp.get_question_answers().keys())
num_answers = len(answer_ids) num_answers = len(answer_ids)
for i in range(num_answers): for i in range(num_answers):
queuekey = 1000 + i queuekey = 1000 + i
latest_timestamp = datetime.now() latest_timestamp = datetime.now()
queuestate = CodeResponseTest.make_queuestate(1000+i, latest_timestamp) queuestate = CodeResponseTest.make_queuestate(1000+i, latest_timestamp)
cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=queuestate)) cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=queuestate))
test_lcp.correct_map.update(cmap) test_lcp.correct_map.update(cmap)
# Queue state only tracks up to second # Queue state only tracks up to second
latest_timestamp = datetime.strptime(datetime.strftime(latest_timestamp,'%Y%m%d%H%M%S'),'%Y%m%d%H%M%S') latest_timestamp = datetime.strptime(datetime.strftime(latest_timestamp,'%Y%m%d%H%M%S'),'%Y%m%d%H%M%S')
self.assertEquals(test_lcp.get_recentmost_queuetime(), latest_timestamp) self.assertEquals(test_lcp.get_recentmost_queuetime(), latest_timestamp)
def test_convert_files_to_filenames(self): def test_convert_files_to_filenames(self):
''' '''
Test whether file objects are converted to filenames without altering other structures Test whether file objects are converted to filenames without altering other structures
''' '''
problem_file = os.path.join(os.path.dirname(__file__), "test_files/coderesponse.xml") problem_file = os.path.join(os.path.dirname(__file__), "test_files/coderesponse.xml")
fp = open(problem_file) with open(problem_file) as fp:
answers_with_file = {'1_2_1': 'String-based answer', answers_with_file = {'1_2_1': 'String-based answer',
'1_3_1': ['answer1', 'answer2', 'answer3'], '1_3_1': ['answer1', 'answer2', 'answer3'],
'1_4_1': [fp, fp]} '1_4_1': [fp, fp]}
answers_converted = convert_files_to_filenames(answers_with_file) answers_converted = convert_files_to_filenames(answers_with_file)
self.assertEquals(answers_converted['1_2_1'], 'String-based answer') self.assertEquals(answers_converted['1_2_1'], 'String-based answer')
self.assertEquals(answers_converted['1_3_1'], ['answer1', 'answer2', 'answer3']) self.assertEquals(answers_converted['1_3_1'], ['answer1', 'answer2', 'answer3'])
self.assertEquals(answers_converted['1_4_1'], [fp.name, fp.name]) self.assertEquals(answers_converted['1_4_1'], [fp.name, fp.name])
class ChoiceResponseTest(unittest.TestCase): class ChoiceResponseTest(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment