Commit 69380756 by Felix Sun

Fixed tests to work with new vote-tabbing method. Fixed some string-int incompatibility errors.

Conflicts:
	common/lib/xmodule/xmodule/crowdsource_hinter.py
	common/lib/xmodule/xmodule/tests/test_crowdsource_hinter.py
parent fc8acb61
...@@ -199,7 +199,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -199,7 +199,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
# Make a hint-voting interface for each wrong answer. The student will only # Make a hint-voting interface for each wrong answer. The student will only
# be allowed to make one vote / submission, but he can choose which wrong answer # be allowed to make one vote / submission, but he can choose which wrong answer
# he wants to look at. # he wants to look at.
answer_to_hints = {} #answer_to_hints[answer text][hint pk] -> hint text answer_to_hints = {} # answer_to_hints[answer text][hint pk] -> hint text
# Go through each previous answer, and populate index_to_hints and index_to_answer. # Go through each previous answer, and populate index_to_hints and index_to_answer.
for i in xrange(len(self.previous_answers)): for i in xrange(len(self.previous_answers)):
...@@ -224,7 +224,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -224,7 +224,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
Args: Args:
`data` -- expected to have the following keys: `data` -- expected to have the following keys:
'answer': ans_no (index in previous_answers) 'answer': text of answer we're voting on
'hint': hint_pk 'hint': hint_pk
'pk_list': We will return a list of how many votes each hint has so far. 'pk_list': We will return a list of how many votes each hint has so far.
It's up to the browser to specify which hints to return vote counts for. It's up to the browser to specify which hints to return vote counts for.
...@@ -246,7 +246,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -246,7 +246,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
# Return a list of how many votes each hint got. # Return a list of how many votes each hint got.
hint_and_votes = [] hint_and_votes = []
for vote_pk in pk_list: for vote_pk in pk_list:
hint_and_votes.append(temp_dict[ans][vote_pk]) hint_and_votes.append(temp_dict[ans][str(vote_pk)])
hint_and_votes.sort(key=lambda pair: pair[1], reverse=True) hint_and_votes.sort(key=lambda pair: pair[1], reverse=True)
# Reset self.previous_answers. # Reset self.previous_answers.
...@@ -259,7 +259,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -259,7 +259,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
Args: Args:
`data` -- expected to have the following keys: `data` -- expected to have the following keys:
'answer': answer index in previous_answers 'answer': text of answer
'hint': text of the new hint that the user is adding 'hint': text of the new hint that the user is adding
Returns a thank-you message. Returns a thank-you message.
""" """
...@@ -276,9 +276,9 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -276,9 +276,9 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
else: else:
temp_dict = self.hints temp_dict = self.hints
if answer in temp_dict: if answer in temp_dict:
temp_dict[answer][self.hint_pk] = [hint, 1] # With one vote (the user himself). temp_dict[answer][str(self.hint_pk)] = [hint, 1] # With one vote (the user himself).
else: else:
temp_dict[answer] = {self.hint_pk: [hint, 1]} temp_dict[answer] = {str(self.hint_pk): [hint, 1]}
self.hint_pk += 1 self.hint_pk += 1
if self.moderate == 'True': if self.moderate == 'True':
self.mod_queue = temp_dict self.mod_queue = temp_dict
......
...@@ -294,9 +294,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -294,9 +294,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
mock_module = CHModuleFactory.create(previous_answers=[['26.0', [None, None, None]]]) mock_module = CHModuleFactory.create(previous_answers=[['26.0', [None, None, None]]])
json_in = {'problem_name': '42.5'} json_in = {'problem_name': '42.5'}
out = mock_module.get_feedback(json_in) out = mock_module.get_feedback(json_in)
print out['index_to_answer'] self.assertTrue(out['answer_to_hints'] == {'26.0': {}})
self.assertTrue(out['index_to_hints'][0] == [])
self.assertTrue(out['index_to_answer'][0] == '26.0')
def test_getfeedback_1wronganswer_withhints(self): def test_getfeedback_1wronganswer_withhints(self):
""" """
...@@ -307,8 +305,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -307,8 +305,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
mock_module = CHModuleFactory.create(previous_answers=[['24.0', [0, 3, None]]]) mock_module = CHModuleFactory.create(previous_answers=[['24.0', [0, 3, None]]])
json_in = {'problem_name': '42.5'} json_in = {'problem_name': '42.5'}
out = mock_module.get_feedback(json_in) out = mock_module.get_feedback(json_in)
print out['index_to_hints'] self.assertTrue(len(out['answer_to_hints']['24.0']) == 2)
self.assertTrue(len(out['index_to_hints'][0]) == 2)
def test_getfeedback_missingkey(self): def test_getfeedback_missingkey(self):
""" """
...@@ -319,7 +316,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -319,7 +316,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
previous_answers=[['24.0', [0, 100, None]]]) previous_answers=[['24.0', [0, 100, None]]])
json_in = {'problem_name': '42.5'} json_in = {'problem_name': '42.5'}
out = mock_module.get_feedback(json_in) out = mock_module.get_feedback(json_in)
self.assertTrue(len(out['index_to_hints'][0]) == 1) self.assertTrue(len(out['answer_to_hints']['24.0']) == 1)
def test_vote_nopermission(self): def test_vote_nopermission(self):
""" """
...@@ -327,7 +324,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -327,7 +324,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
Should not change any vote tallies. Should not change any vote tallies.
""" """
mock_module = CHModuleFactory.create(user_voted=True) mock_module = CHModuleFactory.create(user_voted=True)
json_in = {'answer': 0, 'hint': 1} json_in = {'answer': '24.0', 'hint': 1, 'pk_list': json.dumps([1, 3])}
old_hints = copy.deepcopy(mock_module.hints) old_hints = copy.deepcopy(mock_module.hints)
mock_module.tally_vote(json_in) mock_module.tally_vote(json_in)
self.assertTrue(mock_module.hints == old_hints) self.assertTrue(mock_module.hints == old_hints)
...@@ -339,7 +336,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -339,7 +336,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
""" """
mock_module = CHModuleFactory.create( mock_module = CHModuleFactory.create(
previous_answers=[['24.0', [0, 3, None]]]) previous_answers=[['24.0', [0, 3, None]]])
json_in = {'answer': 0, 'hint': 3} json_in = {'answer': '24.0', 'hint': 3, 'pk_list': json.dumps([0, 3])}
dict_out = mock_module.tally_vote(json_in) dict_out = mock_module.tally_vote(json_in)
self.assertTrue(mock_module.hints['24.0']['0'][1] == 40) self.assertTrue(mock_module.hints['24.0']['0'][1] == 40)
self.assertTrue(mock_module.hints['24.0']['3'][1] == 31) self.assertTrue(mock_module.hints['24.0']['3'][1] == 31)
...@@ -351,7 +348,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -351,7 +348,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user tries to submit a hint, but he has already voted. A user tries to submit a hint, but he has already voted.
""" """
mock_module = CHModuleFactory.create(user_voted=True) mock_module = CHModuleFactory.create(user_voted=True)
json_in = {'answer': 1, 'hint': 'This is a new hint.'} json_in = {'answer': '29.0', 'hint': 'This is a new hint.'}
print mock_module.user_voted print mock_module.user_voted
mock_module.submit_hint(json_in) mock_module.submit_hint(json_in)
print mock_module.hints print mock_module.hints
...@@ -363,7 +360,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -363,7 +360,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
exist yet. exist yet.
""" """
mock_module = CHModuleFactory.create() mock_module = CHModuleFactory.create()
json_in = {'answer': 1, 'hint': 'This is a new hint.'} json_in = {'answer': '29.0', 'hint': 'This is a new hint.'}
mock_module.submit_hint(json_in) mock_module.submit_hint(json_in)
self.assertTrue('29.0' in mock_module.hints) self.assertTrue('29.0' in mock_module.hints)
...@@ -372,8 +369,8 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -372,8 +369,8 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user submits a hint to an answer that has other hints A user submits a hint to an answer that has other hints
already. already.
""" """
mock_module = CHModuleFactory.create(previous_answers=[['25.0', [1, None, None]]]) mock_module = CHModuleFactory.create(previous_answers = [['25.0', [1, None, None]]])
json_in = {'answer': 0, 'hint': 'This is a new hint.'} json_in = {'answer': '25.0', 'hint': 'This is a new hint.'}
mock_module.submit_hint(json_in) mock_module.submit_hint(json_in)
# Make a hint request. # Make a hint request.
json_in = {'problem name': '25.0'} json_in = {'problem name': '25.0'}
...@@ -388,7 +385,7 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -388,7 +385,7 @@ class CrowdsourceHinterTest(unittest.TestCase):
dict. dict.
""" """
mock_module = CHModuleFactory.create(moderate='True') mock_module = CHModuleFactory.create(moderate='True')
json_in = {'answer': 1, 'hint': 'This is a new hint.'} json_in = {'answer': '29.0', 'hint': 'This is a new hint.'}
mock_module.submit_hint(json_in) mock_module.submit_hint(json_in)
self.assertTrue('29.0' not in mock_module.hints) self.assertTrue('29.0' not in mock_module.hints)
self.assertTrue('29.0' in mock_module.mod_queue) self.assertTrue('29.0' in mock_module.mod_queue)
...@@ -398,10 +395,9 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -398,10 +395,9 @@ class CrowdsourceHinterTest(unittest.TestCase):
Make sure that hints are being html-escaped. Make sure that hints are being html-escaped.
""" """
mock_module = CHModuleFactory.create() mock_module = CHModuleFactory.create()
json_in = {'answer': 1, 'hint': '<script> alert("Trololo"); </script>'} json_in = {'answer': '29.0', 'hint': '<script> alert("Trololo"); </script>'}
mock_module.submit_hint(json_in) mock_module.submit_hint(json_in)
print mock_module.hints self.assertTrue(mock_module.hints['29.0']['0'][0] == u'&lt;script&gt; alert(&quot;Trololo&quot;); &lt;/script&gt;')
self.assertTrue(mock_module.hints['29.0'][0][0] == u'&lt;script&gt; alert(&quot;Trololo&quot;); &lt;/script&gt;')
def test_template_gethint(self): def test_template_gethint(self):
""" """
......
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