Commit 6ccfa2e3 by Felix Sun

Made tests of the crowdsource hinter module more standardized and easier to…

Made tests of the crowdsource hinter module more standardized and easier to read.  Fixed database non-initialization bug in crowdsource hinter module.
parent b64fe5c5
...@@ -42,9 +42,9 @@ class CrowdsourceHinterFields(object): ...@@ -42,9 +42,9 @@ class CrowdsourceHinterFields(object):
user_voted = Boolean(help='Specifies if the user has voted on this problem or not.', user_voted = Boolean(help='Specifies if the user has voted on this problem or not.',
scope=Scope.user_state, default=False) scope=Scope.user_state, default=False)
moderate = String(help='''If 'True', then all hints must be approved by staff before moderate = String(help='''If True, then all hints must be approved by staff before
becoming visible. becoming visible.
This field is automatically populated from the xml metadata.''', scope=Scope.settings, This field is automatically populated from the xml metadata.''', scope=Scope.content,
default='False') default='False')
mod_queue = Dict(help='''Contains hints that have not been approved by the staff yet. Structured mod_queue = Dict(help='''Contains hints that have not been approved by the staff yet. Structured
...@@ -73,6 +73,10 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -73,6 +73,10 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
''' '''
# Reset the user vote, for debugging only! Remove for prod. # Reset the user vote, for debugging only! Remove for prod.
self.user_voted = False self.user_voted = False
# You are invited to guess what the lines below do :)
if self.hints == {}:
self.hints = {}
for child in self.get_display_items(): for child in self.get_display_items():
out = child.get_html() out = child.get_html()
# The event listener uses the ajax url to find the child. # The event listener uses the ajax url to find the child.
...@@ -123,11 +127,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -123,11 +127,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
print self.hints print self.hints
answer = self.ans_to_text(get) answer = self.ans_to_text(get)
# Look for a hint to give. # Look for a hint to give.
<<<<<<< HEAD
if answer not in self.hints:
=======
if (answer not in self.hints) or (len(self.hints[answer]) == 0): if (answer not in self.hints) or (len(self.hints[answer]) == 0):
>>>>>>> Began work on instructor view to hinting system.
# No hints to give. Return. # No hints to give. Return.
self.previous_answers += [(answer, (None, None, None))] self.previous_answers += [(answer, (None, None, None))]
return json.dumps({'contents': ' '}) return json.dumps({'contents': ' '})
...@@ -138,14 +138,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -138,14 +138,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
if len(self.hints[answer]) == 1: if len(self.hints[answer]) == 1:
rand_hint_1 = '' rand_hint_1 = ''
rand_hint_2 = '' rand_hint_2 = ''
<<<<<<< HEAD
self.previous_answers += [(answer, (0, None, None))]
elif len(self.hints[answer]) == 2:
best_hint = self.hints[answer][0][0]
rand_hint_1 = self.hints[answer][1][0]
rand_hint_2 = ''
self.previous_answers += [(answer, (0, 1, None))]
=======
self.previous_answers += [[answer, [best_hint_index, None, None]]] self.previous_answers += [[answer, [best_hint_index, None, None]]]
elif n_hints == 2: elif n_hints == 2:
best_hint = self.hints[answer].values()[0][0] best_hint = self.hints[answer].values()[0][0]
...@@ -154,7 +146,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -154,7 +146,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
hint_index_1 = self.hints[answer].keys()[1] hint_index_1 = self.hints[answer].keys()[1]
rand_hint_2 = '' rand_hint_2 = ''
self.previous_answers += [[answer, [best_hint_index, hint_index_1, None]]] self.previous_answers += [[answer, [best_hint_index, hint_index_1, None]]]
>>>>>>> Began work on instructor view to hinting system.
else: else:
hint_index_1, hint_index_2 = random.sample(xrange(len(self.hints[answer])), 2) hint_index_1, hint_index_2 = random.sample(xrange(len(self.hints[answer])), 2)
rand_hint_1 = self.hints[answer][hint_index_1][0] rand_hint_1 = self.hints[answer][hint_index_1][0]
...@@ -188,10 +179,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -188,10 +179,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
# Add each hint to the html string, with a vote button. # Add each hint to the html string, with a vote button.
for hint_id in hints_offered: for hint_id in hints_offered:
if hint_id != None: if hint_id != None:
<<<<<<< HEAD
out += '<br /><input class="vote" data-answer="'+str(i)+'" data-hintno="'+str(j)+\
'" type="button" value="Vote"> ' + self.hints[answer][hint_id][0]
=======
hint_id = str(hint_id) hint_id = str(hint_id)
try: try:
out += '<br /><input class="vote" data-answer="'+str(i)+'" data-hintno="'+hint_id+\ out += '<br /><input class="vote" data-answer="'+str(i)+'" data-hintno="'+hint_id+\
...@@ -199,7 +186,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule): ...@@ -199,7 +186,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
except KeyError: except KeyError:
# Sometimes, the hint that a user saw will have been deleted by the instructor. # Sometimes, the hint that a user saw will have been deleted by the instructor.
continue continue
>>>>>>> Began work on instructor view to hinting system.
# Or, let the student create his own hint # Or, let the student create his own hint
...@@ -261,11 +247,15 @@ What would you say to help someone who got this wrong answer? ...@@ -261,11 +247,15 @@ What would you say to help someone who got this wrong answer?
# Add the new hint to self.hints. (Awkward because a direct write # Add the new hint to self.hints. (Awkward because a direct write
# is necessary.) # is necessary.)
<<<<<<< HEAD <<<<<<< HEAD
<<<<<<< HEAD
temp_dict = self.hints temp_dict = self.hints
temp_dict[answer].append([hint, 1]) # With one vote (the user himself). temp_dict[answer].append([hint, 1]) # With one vote (the user himself).
self.hints = temp_dict self.hints = temp_dict
======= =======
if self.moderate: if self.moderate:
=======
if self.moderate == 'True':
>>>>>>> Made tests of the crowdsource hinter module more standardized and easier to read. Fixed database non-initialization bug in crowdsource hinter module.
temp_dict = self.mod_queue temp_dict = self.mod_queue
else: else:
temp_dict = self.hints temp_dict = self.hints
...@@ -274,7 +264,7 @@ What would you say to help someone who got this wrong answer? ...@@ -274,7 +264,7 @@ What would you say to help someone who got this wrong answer?
else: else:
temp_dict[answer] = {self.hint_pk: [hint, 1]} temp_dict[answer] = {self.hint_pk: [hint, 1]}
self.hint_pk += 1 self.hint_pk += 1
if self.moderate: if self.moderate == 'True':
self.mod_queue = temp_dict self.mod_queue = temp_dict
else: else:
self.hints = temp_dict self.hints = temp_dict
......
from mock import Mock, patch from mock import Mock, patch
import unittest import unittest
import copy
import xmodule import xmodule
from xmodule.crowdsource_hinter import CrowdsourceHinterModule from xmodule.crowdsource_hinter import CrowdsourceHinterModule
...@@ -168,19 +169,15 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -168,19 +169,15 @@ class CrowdsourceHinterTest(unittest.TestCase):
a voting dialog, with the correct choices, plus a hint submission a voting dialog, with the correct choices, plus a hint submission
dialog. dialog.
''' '''
m = CHModuleFactory.create(hints={ m = CHModuleFactory.create(
'24.0': {'0': ['a hint', 42],
'1': ['another hint', 35],
'2': ['irrelevent hint', 25.0]}
},
previous_answers=[ previous_answers=[
['24.0', [0, 1, None]]], ['24.0', [0, 3, None]]],
) )
json_in = {'problem_name': '42.5'} json_in = {'problem_name': '42.5'}
json_out = json.loads(m.get_feedback(json_in))['contents'] json_out = json.loads(m.get_feedback(json_in))['contents']
self.assertTrue('a hint' in json_out) self.assertTrue('Best hint' in json_out)
self.assertTrue('another hint' in json_out) self.assertTrue('Another hint' in json_out)
self.assertTrue('irrelevent hint' not in json_out) self.assertTrue('third hint' not in json_out)
self.assertTrue('textarea' in json_out) self.assertTrue('textarea' in json_out)
...@@ -189,51 +186,33 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -189,51 +186,33 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user tries to vote for a hint, but he has already voted! A user tries to vote for a hint, but he has already voted!
Should not change any vote tallies. Should not change any vote tallies.
''' '''
m = CHModuleFactory.create(hints={ m = CHModuleFactory.create(user_voted=True)
'24.0': {'0': ['a hint', 42],
'1': ['another hint', 35],
'2': ['irrelevent hint', 25.0]}
},
previous_answers=[
['24.0', [0, 1, None]]],
user_voted=True
)
json_in = {'answer': 0, 'hint': 1} json_in = {'answer': 0, 'hint': 1}
old_hints = copy.deepcopy(m.hints)
json_out = json.loads(m.tally_vote(json_in))['contents'] json_out = json.loads(m.tally_vote(json_in))['contents']
self.assertTrue(m.hints['24.0']['0'][1] == 42) self.assertTrue(m.hints == old_hints)
self.assertTrue(m.hints['24.0']['1'][1] == 35)
self.assertTrue(m.hints['24.0']['2'][1] == 25.0)
def test_vote_withpermission(self): def test_vote_withpermission(self):
''' '''
A user votes for a hint. A user votes for a hint.
''' '''
m = CHModuleFactory.create(hints={ m = CHModuleFactory.create()
'24.0': {'0': ['a hint', 42], json_in = {'answer': 0, 'hint': 3}
'1': ['another hint', 35],
'2': ['irrelevent hint', 25.0]}
},
previous_answers=[
['24.0', [0, 1, None]]],
)
json_in = {'answer': 0, 'hint': 1}
json_out = json.loads(m.tally_vote(json_in))['contents'] json_out = json.loads(m.tally_vote(json_in))['contents']
self.assertTrue(m.hints['24.0']['0'][1] == 42) self.assertTrue(m.hints['24.0']['0'][1] == 40)
self.assertTrue(m.hints['24.0']['1'][1] == 36) self.assertTrue(m.hints['24.0']['3'][1] == 31)
self.assertTrue(m.hints['24.0']['2'][1] == 25.0) self.assertTrue(m.hints['24.0']['4'][1] == 20)
def test_submithint_nopermission(self): def test_submithint_nopermission(self):
''' '''
A user tries to submit a hint, but he has already voted. A user tries to submit a hint, but he has already voted.
''' '''
m = CHModuleFactory.create(previous_answers=[ m = CHModuleFactory.create(user_voted=True)
['24.0', [None, None, None]]], json_in = {'answer': 1, 'hint': 'This is a new hint.'}
user_voted=True)
json_in = {'answer': 0, 'hint': 'This is a new hint.'}
m.submit_hint(json_in) m.submit_hint(json_in)
self.assertTrue('24.0' not in m.hints) self.assertTrue('29.0' not in m.hints)
def test_submithint_withpermission_new(self): def test_submithint_withpermission_new(self):
...@@ -241,13 +220,11 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -241,13 +220,11 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user submits a hint to an answer for which no hints A user submits a hint to an answer for which no hints
exist yet. exist yet.
''' '''
m = CHModuleFactory.create(previous_answers=[ m = CHModuleFactory.create()
['24.0', [None, None, None]]], json_in = {'answer': 1, 'hint': 'This is a new hint.'}
)
json_in = {'answer': 0, 'hint': 'This is a new hint.'}
m.submit_hint(json_in) m.submit_hint(json_in)
# Make a hint request. # Make a hint request.
json_in = {'problem name': '24.0'} json_in = {'problem name': '29.0'}
json_out = json.loads(m.get_hint(json_in))['contents'] json_out = json.loads(m.get_hint(json_in))['contents']
self.assertTrue('This is a new hint.' in json_out) self.assertTrue('This is a new hint.' in json_out)
...@@ -257,30 +234,27 @@ class CrowdsourceHinterTest(unittest.TestCase): ...@@ -257,30 +234,27 @@ 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.
''' '''
m = CHModuleFactory.create(previous_answers=[ m = CHModuleFactory.create(previous_answers = [['25.0', [1, None, None]]])
['24.0', [0, None, None]]],
hints={'24.0': {'0': ['Existing hint.', 1]}}
)
json_in = {'answer': 0, 'hint': 'This is a new hint.'} json_in = {'answer': 0, 'hint': 'This is a new hint.'}
m.submit_hint(json_in) m.submit_hint(json_in)
# Make a hint request. # Make a hint request.
json_in = {'problem name': '24.0'} json_in = {'problem name': '25.0'}
json_out = json.loads(m.get_hint(json_in))['contents'] json_out = json.loads(m.get_hint(json_in))['contents']
self.assertTrue('This is a new hint.' in json_out) self.assertTrue('This is a new hint.' in json_out)
def test_deletehint(self):
def test_submithint_moderate(self):
''' '''
An admin / instructor deletes a hint. A user submits a hint, but moderation is on. The hint should
show up in the mod_queue, not the public-facing hints
dict.
''' '''
m = CHModuleFactory.create(hints={ m = CHModuleFactory.create(moderate='True')
'24.0': {'0': ['Deleted hint', 5], json_in = {'answer': 1, 'hint': 'This is a new hint.'}
'1': ['Safe hint', 4]} m.submit_hint(json_in)
}) self.assertTrue('29.0' not in m.hints)
m.delete_hint('24.0', '0') self.assertTrue('29.0' in m.mod_queue)
json_in = {'problem name': '24.0'}
json_out = json.loads(m.get_hint(json_in))['contents']
self.assertTrue('Deleted hint' not in json_out)
self.assertTrue('Safe hint' in json_out)
......
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