From ceeea66417832a21d1a7fa17e9e1ed8d4ee87d89 Mon Sep 17 00:00:00 2001 From: kimth Date: Tue, 7 Aug 2012 09:24:17 -0400 Subject: [PATCH 1/2] Fix overrzealous file_to_filename conversion and fix CapaProblem unit tests --- common/lib/capa/capa/util.py | 18 +++++++++++++----- common/lib/xmodule/xmodule/tests/__init__.py | 2 +- common/lib/xmodule/xmodule/x_module.py | 7 +++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index 211ada4d62..98c250297d 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -39,10 +39,18 @@ def convert_files_to_filenames(answers): ''' new_answers = dict() for answer_id in answers.keys(): - # TODO This should be done more cleanly; however, this fixes bugs - # that were introduced with this function. - if isinstance(answers[answer_id], list): - new_answers[answer_id] = answers[answer_id] + if is_uploaded_file(answers[answer_id]): + new_answers[answer_id] = answers[answer_id].name else: - new_answers[answer_id] = unicode(answers[answer_id]) + new_answers[answer_id] = answers[answer_id] return new_answers + +def is_uploaded_file(file_to_test): + ''' + Duck typing to check if 'file_to_test' is a File object + ''' + is_file = True + for method in ['read', 'name']: + if not hasattr(file_to_test, method): + is_file = False + return is_file diff --git a/common/lib/xmodule/xmodule/tests/__init__.py b/common/lib/xmodule/xmodule/tests/__init__.py index 7b1641956c..072abb4908 100644 --- a/common/lib/xmodule/xmodule/tests/__init__.py +++ b/common/lib/xmodule/xmodule/tests/__init__.py @@ -31,7 +31,7 @@ i4xs = ModuleSystem( user=Mock(), filestore=fs.osfs.OSFS(os.path.dirname(os.path.realpath(__file__))), debug=True, - xqueue=None, # TODO FIXME + xqueue=None, is_staff=False ) diff --git a/common/lib/xmodule/xmodule/x_module.py b/common/lib/xmodule/xmodule/x_module.py index f013aac785..9b005d1dae 100644 --- a/common/lib/xmodule/xmodule/x_module.py +++ b/common/lib/xmodule/xmodule/x_module.py @@ -643,7 +643,7 @@ class ModuleSystem(object): user=None, filestore=None, debug=False, - xqueue = None, + xqueue=None, is_staff=False): ''' Create a closure around the system environment. @@ -678,7 +678,10 @@ class ModuleSystem(object): TODO (vshnayder): this will need to change once we have real user roles. ''' self.ajax_url = ajax_url - self.xqueue = xqueue + if xqueue is None: + self.xqueue = {'interface':None, 'callback_url':'/', 'default_queuename':'null'} + else: + self.xqueue = xqueue self.track_function = track_function self.filestore = filestore self.get_module = get_module From cb2fbfa54c5b16696859dd84d33226deb9887f9c Mon Sep 17 00:00:00 2001 From: kimth Date: Tue, 7 Aug 2012 09:38:36 -0400 Subject: [PATCH 2/2] Added test for answers file-to-filename conversion --- common/lib/xmodule/xmodule/tests/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/__init__.py b/common/lib/xmodule/xmodule/tests/__init__.py index 072abb4908..4c0b6fc9b3 100644 --- a/common/lib/xmodule/xmodule/tests/__init__.py +++ b/common/lib/xmodule/xmodule/tests/__init__.py @@ -15,6 +15,7 @@ import xmodule import capa.calc as calc import capa.capa_problem as lcp from capa.correctmap import CorrectMap +from capa.util import convert_files_to_filenames from xmodule import graders, x_module from xmodule.x_module import ModuleSystem from xmodule.graders import Score, aggregate_scores @@ -278,7 +279,6 @@ class StringResponseWithHintTest(unittest.TestCase): class CodeResponseTest(unittest.TestCase): ''' Test CodeResponse - ''' def test_update_score(self): problem_file = os.path.dirname(__file__) + "/test_files/coderesponse.xml" @@ -327,7 +327,18 @@ class CodeResponseTest(unittest.TestCase): self.assertFalse(test_lcp.correct_map.is_queued(answer_ids[j])) # Should be dequeued, message delivered else: self.assertTrue(test_lcp.correct_map.is_queued(answer_ids[j])) # Should be queued, message undelivered - + + def test_convert_files_to_filenames(self): + problem_file = os.path.dirname(__file__) + "/test_files/coderesponse.xml" + fp = open(problem_file) + answers_with_file = {'1_2_1': 'String-based answer', + '1_3_1': ['answer1', 'answer2', 'answer3'], + '1_4_1': fp} + answers_converted = convert_files_to_filenames(answers_with_file) + 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_4_1'], fp.name) + class ChoiceResponseTest(unittest.TestCase):