diff --git a/common/lib/capa/capa/verifiers/draganddrop.py b/common/lib/capa/capa/verifiers/draganddrop.py index 12695703d6..239ff2b9a4 100644 --- a/common/lib/capa/capa/verifiers/draganddrop.py +++ b/common/lib/capa/capa/verifiers/draganddrop.py @@ -111,7 +111,7 @@ class DragAndDrop(object): Returns: bool. ''' for draggable in self.excess_draggables: - if not self.excess_draggables[draggable]: + if self.excess_draggables[draggable]: return False # user answer has more draggables than correct answer # Number of draggables in user_groups may be differ that in @@ -304,8 +304,13 @@ class DragAndDrop(object): user_answer = json.loads(user_answer) - # check if we have draggables that are not in correct answer: - self.excess_draggables = {} + # This dictionary will hold a key for each draggable the user placed on + # the image. The value is True if that draggable is not mentioned in any + # correct_answer entries. If the draggable is mentioned in at least one + # correct_answer entry, the value is False. + # default to consider every user answer excess until proven otherwise. + self.excess_draggables = dict((users_draggable.keys()[0],True) + for users_draggable in user_answer['draggables']) # create identical data structures from user answer and correct answer for i in xrange(0, len(correct_answer)): @@ -322,15 +327,8 @@ class DragAndDrop(object): self.user_groups[groupname].append(draggable_name) self.user_positions[groupname]['user'].append( draggable_dict[draggable_name]) - self.excess_draggables[draggable_name] = True - else: - self.excess_draggables[draggable_name] = \ - self.excess_draggables.get(draggable_name, False) - if len(correct_answer)==0: - for draggable_dict in user_answer['draggables']: - # draggable_dict is 1-to-1 {draggable_name: position} - draggable_name = draggable_dict.keys()[0] - self.excess_draggables[draggable_name] = False + # proved that this is not excess + self.excess_draggables[draggable_name] = False def grade(user_input, correct_answer): """ Creates DragAndDrop instance from user_input and correct_answer and diff --git a/common/lib/capa/capa/verifiers/tests_draganddrop.py b/common/lib/capa/capa/verifiers/tests_draganddrop.py index 9b1b15ce0c..bcd024fa89 100644 --- a/common/lib/capa/capa/verifiers/tests_draganddrop.py +++ b/common/lib/capa/capa/verifiers/tests_draganddrop.py @@ -46,6 +46,18 @@ class Test_DragAndDrop_Grade(unittest.TestCase): correct_answer = {'1': 't1', 'name_with_icon': 't2'} self.assertTrue(draganddrop.grade(user_input, correct_answer)) + def test_expect_no_actions_wrong(self): + user_input = '{"draggables": [{"1": "t1"}, \ + {"name_with_icon": "t2"}]}' + correct_answer = [] + self.assertFalse(draganddrop.grade(user_input, correct_answer)) + + def test_expect_no_actions_right(self): + user_input = '{"draggables": []}' + correct_answer = [] + self.assertTrue(draganddrop.grade(user_input, correct_answer)) + + def test_targets_false(self): user_input = '{"draggables": [{"1": "t1"}, \ {"name_with_icon": "t2"}]}'