Made tests of the crowdsource hinter module more standardized and easier to read. Fixed database non-initialization bug in crowdsource hinter module.

This commit is contained in:
Felix Sun
2013-06-21 12:15:03 -04:00
parent b64fe5c537
commit 6ccfa2e38d
2 changed files with 44 additions and 80 deletions

View File

@@ -42,9 +42,9 @@ class CrowdsourceHinterFields(object):
user_voted = Boolean(help='Specifies if the user has voted on this problem or not.',
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.
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')
mod_queue = Dict(help='''Contains hints that have not been approved by the staff yet. Structured
@@ -73,6 +73,10 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
'''
# Reset the user vote, for debugging only! Remove for prod.
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():
out = child.get_html()
# The event listener uses the ajax url to find the child.
@@ -123,11 +127,7 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
print self.hints
answer = self.ans_to_text(get)
# 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):
>>>>>>> Began work on instructor view to hinting system.
# No hints to give. Return.
self.previous_answers += [(answer, (None, None, None))]
return json.dumps({'contents': ' '})
@@ -138,14 +138,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
if len(self.hints[answer]) == 1:
rand_hint_1 = ''
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]]]
elif n_hints == 2:
best_hint = self.hints[answer].values()[0][0]
@@ -154,7 +146,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
hint_index_1 = self.hints[answer].keys()[1]
rand_hint_2 = ''
self.previous_answers += [[answer, [best_hint_index, hint_index_1, None]]]
>>>>>>> Began work on instructor view to hinting system.
else:
hint_index_1, hint_index_2 = random.sample(xrange(len(self.hints[answer])), 2)
rand_hint_1 = self.hints[answer][hint_index_1][0]
@@ -188,10 +179,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
# Add each hint to the html string, with a vote button.
for hint_id in hints_offered:
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)
try:
out += '<br /><input class="vote" data-answer="'+str(i)+'" data-hintno="'+hint_id+\
@@ -199,7 +186,6 @@ class CrowdsourceHinterModule(CrowdsourceHinterFields, XModule):
except KeyError:
# Sometimes, the hint that a user saw will have been deleted by the instructor.
continue
>>>>>>> Began work on instructor view to hinting system.
# Or, let the student create his own hint
@@ -260,12 +246,16 @@ What would you say to help someone who got this wrong answer?
answer = self.previous_answers[int(get['answer'])][0]
# Add the new hint to self.hints. (Awkward because a direct write
# is necessary.)
<<<<<<< HEAD
<<<<<<< HEAD
temp_dict = self.hints
temp_dict[answer].append([hint, 1]) # With one vote (the user himself).
self.hints = temp_dict
=======
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
else:
temp_dict = self.hints
@@ -274,7 +264,7 @@ What would you say to help someone who got this wrong answer?
else:
temp_dict[answer] = {self.hint_pk: [hint, 1]}
self.hint_pk += 1
if self.moderate:
if self.moderate == 'True':
self.mod_queue = temp_dict
else:
self.hints = temp_dict

View File

@@ -1,5 +1,6 @@
from mock import Mock, patch
import unittest
import copy
import xmodule
from xmodule.crowdsource_hinter import CrowdsourceHinterModule
@@ -168,19 +169,15 @@ class CrowdsourceHinterTest(unittest.TestCase):
a voting dialog, with the correct choices, plus a hint submission
dialog.
'''
m = CHModuleFactory.create(hints={
'24.0': {'0': ['a hint', 42],
'1': ['another hint', 35],
'2': ['irrelevent hint', 25.0]}
},
m = CHModuleFactory.create(
previous_answers=[
['24.0', [0, 1, None]]],
['24.0', [0, 3, None]]],
)
json_in = {'problem_name': '42.5'}
json_out = json.loads(m.get_feedback(json_in))['contents']
self.assertTrue('a hint' in json_out)
self.assertTrue('another hint' in json_out)
self.assertTrue('irrelevent hint' not in json_out)
self.assertTrue('Best hint' in json_out)
self.assertTrue('Another hint' in json_out)
self.assertTrue('third hint' not in json_out)
self.assertTrue('textarea' in json_out)
@@ -189,51 +186,33 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user tries to vote for a hint, but he has already voted!
Should not change any vote tallies.
'''
m = CHModuleFactory.create(hints={
'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
)
m = CHModuleFactory.create(user_voted=True)
json_in = {'answer': 0, 'hint': 1}
old_hints = copy.deepcopy(m.hints)
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']['1'][1] == 35)
self.assertTrue(m.hints['24.0']['2'][1] == 25.0)
self.assertTrue(m.hints == old_hints)
def test_vote_withpermission(self):
'''
A user votes for a hint.
'''
m = CHModuleFactory.create(hints={
'24.0': {'0': ['a hint', 42],
'1': ['another hint', 35],
'2': ['irrelevent hint', 25.0]}
},
previous_answers=[
['24.0', [0, 1, None]]],
)
json_in = {'answer': 0, 'hint': 1}
m = CHModuleFactory.create()
json_in = {'answer': 0, 'hint': 3}
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']['1'][1] == 36)
self.assertTrue(m.hints['24.0']['2'][1] == 25.0)
self.assertTrue(m.hints['24.0']['0'][1] == 40)
self.assertTrue(m.hints['24.0']['3'][1] == 31)
self.assertTrue(m.hints['24.0']['4'][1] == 20)
def test_submithint_nopermission(self):
'''
A user tries to submit a hint, but he has already voted.
'''
m = CHModuleFactory.create(previous_answers=[
['24.0', [None, None, None]]],
user_voted=True)
json_in = {'answer': 0, 'hint': 'This is a new hint.'}
m = CHModuleFactory.create(user_voted=True)
json_in = {'answer': 1, 'hint': 'This is a new hint.'}
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):
@@ -241,13 +220,11 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user submits a hint to an answer for which no hints
exist yet.
'''
m = CHModuleFactory.create(previous_answers=[
['24.0', [None, None, None]]],
)
json_in = {'answer': 0, 'hint': 'This is a new hint.'}
m = CHModuleFactory.create()
json_in = {'answer': 1, 'hint': 'This is a new hint.'}
m.submit_hint(json_in)
# 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']
self.assertTrue('This is a new hint.' in json_out)
@@ -257,30 +234,27 @@ class CrowdsourceHinterTest(unittest.TestCase):
A user submits a hint to an answer that has other hints
already.
'''
m = CHModuleFactory.create(previous_answers=[
['24.0', [0, None, None]]],
hints={'24.0': {'0': ['Existing hint.', 1]}}
)
m = CHModuleFactory.create(previous_answers = [['25.0', [1, None, None]]])
json_in = {'answer': 0, 'hint': 'This is a new hint.'}
m.submit_hint(json_in)
# 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']
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={
'24.0': {'0': ['Deleted hint', 5],
'1': ['Safe hint', 4]}
})
m.delete_hint('24.0', '0')
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)
m = CHModuleFactory.create(moderate='True')
json_in = {'answer': 1, 'hint': 'This is a new hint.'}
m.submit_hint(json_in)
self.assertTrue('29.0' not in m.hints)
self.assertTrue('29.0' in m.mod_queue)