Commit 52c2f3ae by Will Daly

Tested additional values of rerandomize (true/false/per_student)

Pylint fixes
parent 754d1eb7
"""Tests for the logic in input type mako templates.""" """Tests for the logic in input type mako templates."""
#pylint: disable=R0904
import unittest import unittest
import capa import capa
...@@ -11,26 +10,22 @@ from mako.template import Template as MakoTemplate ...@@ -11,26 +10,22 @@ from mako.template import Template as MakoTemplate
class TemplateTestCase(unittest.TestCase): class TemplateTestCase(unittest.TestCase):
"""Utilitites for testing templates""" """Utilitites for testing templates"""
# Allow us to pass an extra arg to setUp to configure # Subclasses override this to specify the file name of the template
# the test case. Also allow setUp as a valid method name. # to be loaded from capa/templates.
#pylint: disable=W0221 # The template name should include the .html extension:
#pylint: disable=C0103 # for example: choicegroup.html
def setUp(self, template_name): TEMPLATE_NAME = None
"""Load the template
def setUp(self):
`template_name` is the file name of the template """Load the template"""
to be loaded from capa/templates.
The template name should include the .html extension:
for example: choicegroup.html
"""
capa_path = capa.__path__[0] capa_path = capa.__path__[0]
self.template_path = os.path.join(capa_path, 'templates', template_name) self.template_path = os.path.join(capa_path,
'templates',
self.TEMPLATE_NAME)
template_file = open(self.template_path) template_file = open(self.template_path)
self.template = MakoTemplate(template_file.read()) self.template = MakoTemplate(template_file.read())
template_file.close() template_file.close()
# Allow us to pass **context_dict to render_unicode()
#pylint: disable=W0142
def render_to_xml(self, context_dict): def render_to_xml(self, context_dict):
"""Render the template using the `context_dict` dict. """Render the template using the `context_dict` dict.
...@@ -65,10 +60,8 @@ class TemplateTestCase(unittest.TestCase): ...@@ -65,10 +60,8 @@ class TemplateTestCase(unittest.TestCase):
class TestChoiceGroupTemplate(TemplateTestCase): class TestChoiceGroupTemplate(TemplateTestCase):
"""Test mako template for `<choicegroup>` input""" """Test mako template for `<choicegroup>` input"""
# Allow us to pass an extra arg to setUp to configure TEMPLATE_NAME = 'choicegroup.html'
# the test case. Also allow setUp as a valid method name.
#pylint: disable=W0221
#pylint: disable=C0103
def setUp(self): def setUp(self):
choices = [('1', 'choice 1'), ('2', 'choice 2'), ('3', 'choice 3')] choices = [('1', 'choice 1'), ('2', 'choice 2'), ('3', 'choice 3')]
self.context = {'id': '1', self.context = {'id': '1',
...@@ -77,7 +70,7 @@ class TestChoiceGroupTemplate(TemplateTestCase): ...@@ -77,7 +70,7 @@ class TestChoiceGroupTemplate(TemplateTestCase):
'input_type': 'checkbox', 'input_type': 'checkbox',
'name_array_suffix': '1', 'name_array_suffix': '1',
'value': '3'} 'value': '3'}
super(TestChoiceGroupTemplate, self).setUp('choicegroup.html') super(TestChoiceGroupTemplate, self).setUp()
def test_problem_marked_correct(self): def test_problem_marked_correct(self):
"""Test conditions under which the entire problem """Test conditions under which the entire problem
......
"""Tests of the Capa XModule"""
#pylint: disable=C0111
#pylint: disable=R0904
#pylint: disable=C0103
#pylint: disable=C0302
import datetime import datetime
import json from mock import Mock, patch
from mock import Mock, MagicMock, patch
from pprint import pprint
import unittest import unittest
import random import random
import xmodule import xmodule
import capa
from capa.responsetypes import StudentInputError, \ from capa.responsetypes import StudentInputError, \
LoncapaProblemError, ResponseError LoncapaProblemError, ResponseError
from xmodule.capa_module import CapaModule from xmodule.capa_module import CapaModule
from xmodule.modulestore import Location from xmodule.modulestore import Location
from lxml import etree
from django.http import QueryDict from django.http import QueryDict
...@@ -384,7 +386,7 @@ class CapaModuleTest(unittest.TestCase): ...@@ -384,7 +386,7 @@ class CapaModuleTest(unittest.TestCase):
# what the input is, by patching CorrectMap.is_correct() # what the input is, by patching CorrectMap.is_correct()
# Also simulate rendering the HTML # Also simulate rendering the HTML
# TODO: pep8 thinks the following line has invalid syntax # TODO: pep8 thinks the following line has invalid syntax
with patch('capa.correctmap.CorrectMap.is_correct') as mock_is_correct,\ with patch('capa.correctmap.CorrectMap.is_correct') as mock_is_correct, \
patch('xmodule.capa_module.CapaModule.get_problem_html') as mock_html: patch('xmodule.capa_module.CapaModule.get_problem_html') as mock_html:
mock_is_correct.return_value = True mock_is_correct.return_value = True
mock_html.return_value = "Test HTML" mock_html.return_value = "Test HTML"
...@@ -435,32 +437,38 @@ class CapaModuleTest(unittest.TestCase): ...@@ -435,32 +437,38 @@ class CapaModuleTest(unittest.TestCase):
self.assertEqual(module.attempts, 3) self.assertEqual(module.attempts, 3)
def test_check_problem_resubmitted_with_randomize(self): def test_check_problem_resubmitted_with_randomize(self):
# Randomize turned on rerandomize_values = ['always', 'true']
module = CapaFactory.create(rerandomize='always', attempts=0)
# Simulate that the problem is completed for rerandomize in rerandomize_values:
module.done = True # Randomize turned on
module = CapaFactory.create(rerandomize=rerandomize, attempts=0)
# Expect that we cannot submit # Simulate that the problem is completed
with self.assertRaises(xmodule.exceptions.NotFoundError): module.done = True
get_request_dict = {CapaFactory.input_key(): '3.14'}
module.check_problem(get_request_dict)
# Expect that number of attempts NOT incremented # Expect that we cannot submit
self.assertEqual(module.attempts, 0) with self.assertRaises(xmodule.exceptions.NotFoundError):
get_request_dict = {CapaFactory.input_key(): '3.14'}
module.check_problem(get_request_dict)
# Expect that number of attempts NOT incremented
self.assertEqual(module.attempts, 0)
def test_check_problem_resubmitted_no_randomize(self): def test_check_problem_resubmitted_no_randomize(self):
# Randomize turned off rerandomize_values = ['never', 'false', 'per_student']
module = CapaFactory.create(rerandomize='never', attempts=0, done=True)
# Expect that we can submit successfully for rerandomize in rerandomize_values:
get_request_dict = {CapaFactory.input_key(): '3.14'} # Randomize turned off
result = module.check_problem(get_request_dict) module = CapaFactory.create(rerandomize=rerandomize, attempts=0, done=True)
self.assertEqual(result['success'], 'correct') # Expect that we can submit successfully
get_request_dict = {CapaFactory.input_key(): '3.14'}
result = module.check_problem(get_request_dict)
# Expect that number of attempts IS incremented self.assertEqual(result['success'], 'correct')
self.assertEqual(module.attempts, 1)
# Expect that number of attempts IS incremented
self.assertEqual(module.attempts, 1)
def test_check_problem_queued(self): def test_check_problem_queued(self):
module = CapaFactory.create(attempts=1) module = CapaFactory.create(attempts=1)
...@@ -615,24 +623,34 @@ class CapaModuleTest(unittest.TestCase): ...@@ -615,24 +623,34 @@ class CapaModuleTest(unittest.TestCase):
self.assertTrue('success' in result and not result['success']) self.assertTrue('success' in result and not result['success'])
def test_save_problem_submitted_with_randomize(self): def test_save_problem_submitted_with_randomize(self):
module = CapaFactory.create(rerandomize='always', done=True)
# Try to save # Capa XModule treats 'always' and 'true' equivalently
get_request_dict = {CapaFactory.input_key(): '3.14'} rerandomize_values = ['always', 'true']
result = module.save_problem(get_request_dict)
# Expect that we cannot save for rerandomize in rerandomize_values:
self.assertTrue('success' in result and not result['success']) module = CapaFactory.create(rerandomize=rerandomize, done=True)
# Try to save
get_request_dict = {CapaFactory.input_key(): '3.14'}
result = module.save_problem(get_request_dict)
# Expect that we cannot save
self.assertTrue('success' in result and not result['success'])
def test_save_problem_submitted_no_randomize(self): def test_save_problem_submitted_no_randomize(self):
module = CapaFactory.create(rerandomize='never', done=True)
# Try to save # Capa XModule treats 'false' and 'per_student' equivalently
get_request_dict = {CapaFactory.input_key(): '3.14'} rerandomize_values = ['never', 'false', 'per_student']
result = module.save_problem(get_request_dict)
# Expect that we succeed for rerandomize in rerandomize_values:
self.assertTrue('success' in result and result['success']) module = CapaFactory.create(rerandomize=rerandomize, done=True)
# Try to save
get_request_dict = {CapaFactory.input_key(): '3.14'}
result = module.save_problem(get_request_dict)
# Expect that we succeed
self.assertTrue('success' in result and result['success'])
def test_check_button_name(self): def test_check_button_name(self):
...@@ -681,21 +699,30 @@ class CapaModuleTest(unittest.TestCase): ...@@ -681,21 +699,30 @@ class CapaModuleTest(unittest.TestCase):
# If user submitted a problem but hasn't reset, # If user submitted a problem but hasn't reset,
# do NOT show the check button # do NOT show the check button
# Note: we can only reset when rerandomize="always" # Note: we can only reset when rerandomize="always" or "true"
module = CapaFactory.create(rerandomize="always", done=True) module = CapaFactory.create(rerandomize="always", done=True)
self.assertFalse(module.should_show_check_button()) self.assertFalse(module.should_show_check_button())
module = CapaFactory.create(rerandomize="true", done=True)
self.assertFalse(module.should_show_check_button())
# Otherwise, DO show the check button # Otherwise, DO show the check button
module = CapaFactory.create() module = CapaFactory.create()
self.assertTrue(module.should_show_check_button()) self.assertTrue(module.should_show_check_button())
# If the user has submitted the problem # If the user has submitted the problem
# and we do NOT have a reset button, then we can show the check button # and we do NOT have a reset button, then we can show the check button
# Setting rerandomize to "never" ensures that the reset button # Setting rerandomize to "never" or "false" ensures that the reset button
# is not shown # is not shown
module = CapaFactory.create(rerandomize="never", done=True) module = CapaFactory.create(rerandomize="never", done=True)
self.assertTrue(module.should_show_check_button()) self.assertTrue(module.should_show_check_button())
module = CapaFactory.create(rerandomize="false", done=True)
self.assertTrue(module.should_show_check_button())
module = CapaFactory.create(rerandomize="per_student", done=True)
self.assertTrue(module.should_show_check_button())
def test_should_show_reset_button(self): def test_should_show_reset_button(self):
attempts = random.randint(1, 10) attempts = random.randint(1, 10)
...@@ -712,6 +739,14 @@ class CapaModuleTest(unittest.TestCase): ...@@ -712,6 +739,14 @@ class CapaModuleTest(unittest.TestCase):
module = CapaFactory.create(rerandomize="never", done=True) module = CapaFactory.create(rerandomize="never", done=True)
self.assertFalse(module.should_show_reset_button()) self.assertFalse(module.should_show_reset_button())
# If we're NOT randomizing, then do NOT show the reset button
module = CapaFactory.create(rerandomize="per_student", done=True)
self.assertFalse(module.should_show_reset_button())
# If we're NOT randomizing, then do NOT show the reset button
module = CapaFactory.create(rerandomize="false", done=True)
self.assertFalse(module.should_show_reset_button())
# If the user hasn't submitted an answer yet, # If the user hasn't submitted an answer yet,
# then do NOT show the reset button # then do NOT show the reset button
module = CapaFactory.create(done=False) module = CapaFactory.create(done=False)
...@@ -742,13 +777,19 @@ class CapaModuleTest(unittest.TestCase): ...@@ -742,13 +777,19 @@ class CapaModuleTest(unittest.TestCase):
module = CapaFactory.create(rerandomize="always", done=True) module = CapaFactory.create(rerandomize="always", done=True)
self.assertFalse(module.should_show_save_button()) self.assertFalse(module.should_show_save_button())
module = CapaFactory.create(rerandomize="true", done=True)
self.assertFalse(module.should_show_save_button())
# If the user has unlimited attempts and we are not randomizing, # If the user has unlimited attempts and we are not randomizing,
# then do NOT show a save button # then do NOT show a save button
# because they can keep using "Check" # because they can keep using "Check"
module = CapaFactory.create(max_attempts=None, rerandomize="never", done=False) module = CapaFactory.create(max_attempts=None, rerandomize="never", done=False)
self.assertFalse(module.should_show_save_button()) self.assertFalse(module.should_show_save_button())
module = CapaFactory.create(max_attempts=None, rerandomize="never", done=True) module = CapaFactory.create(max_attempts=None, rerandomize="false", done=True)
self.assertFalse(module.should_show_save_button())
module = CapaFactory.create(max_attempts=None, rerandomize="per_student", done=True)
self.assertFalse(module.should_show_save_button()) self.assertFalse(module.should_show_save_button())
# Otherwise, DO show the save button # Otherwise, DO show the save button
...@@ -759,6 +800,12 @@ class CapaModuleTest(unittest.TestCase): ...@@ -759,6 +800,12 @@ class CapaModuleTest(unittest.TestCase):
module = CapaFactory.create(rerandomize="never", max_attempts=2, done=True) module = CapaFactory.create(rerandomize="never", max_attempts=2, done=True)
self.assertTrue(module.should_show_save_button()) self.assertTrue(module.should_show_save_button())
module = CapaFactory.create(rerandomize="false", max_attempts=2, done=True)
self.assertTrue(module.should_show_save_button())
module = CapaFactory.create(rerandomize="per_student", max_attempts=2, done=True)
self.assertTrue(module.should_show_save_button())
# If survey question for capa (max_attempts = 0), # If survey question for capa (max_attempts = 0),
# DO show the save button # DO show the save button
module = CapaFactory.create(max_attempts=0, done=False) module = CapaFactory.create(max_attempts=0, done=False)
...@@ -788,9 +835,15 @@ class CapaModuleTest(unittest.TestCase): ...@@ -788,9 +835,15 @@ class CapaModuleTest(unittest.TestCase):
done=True) done=True)
self.assertTrue(module.should_show_save_button()) self.assertTrue(module.should_show_save_button())
module = CapaFactory.create(force_save_button="true",
rerandomize="true",
done=True)
self.assertTrue(module.should_show_save_button())
def test_no_max_attempts(self): def test_no_max_attempts(self):
module = CapaFactory.create(max_attempts='') module = CapaFactory.create(max_attempts='')
html = module.get_problem_html() html = module.get_problem_html()
self.assertTrue(html is not None)
# assert that we got here without exploding # assert that we got here without exploding
def test_get_problem_html(self): def test_get_problem_html(self):
...@@ -875,6 +928,8 @@ class CapaModuleTest(unittest.TestCase): ...@@ -875,6 +928,8 @@ class CapaModuleTest(unittest.TestCase):
# Try to render the module with DEBUG turned off # Try to render the module with DEBUG turned off
html = module.get_problem_html() html = module.get_problem_html()
self.assertTrue(html is not None)
# Check the rendering context # Check the rendering context
render_args, _ = module.system.render_template.call_args render_args, _ = module.system.render_template.call_args
context = render_args[1] context = render_args[1]
...@@ -886,7 +941,9 @@ class CapaModuleTest(unittest.TestCase): ...@@ -886,7 +941,9 @@ class CapaModuleTest(unittest.TestCase):
def test_random_seed_no_change(self): def test_random_seed_no_change(self):
# Run the test for each possible rerandomize value # Run the test for each possible rerandomize value
for rerandomize in ['never', 'per_student', 'always', 'onreset']: for rerandomize in ['false', 'never',
'per_student', 'always',
'true', 'onreset']:
module = CapaFactory.create(rerandomize=rerandomize) module = CapaFactory.create(rerandomize=rerandomize)
# Get the seed # Get the seed
...@@ -896,8 +953,9 @@ class CapaModuleTest(unittest.TestCase): ...@@ -896,8 +953,9 @@ class CapaModuleTest(unittest.TestCase):
# If we're not rerandomizing, the seed is always set # If we're not rerandomizing, the seed is always set
# to the same value (1) # to the same value (1)
if rerandomize == 'never': if rerandomize in ['never']:
self.assertEqual(seed, 1) self.assertEqual(seed, 1,
msg="Seed should always be 1 when rerandomize='%s'" % rerandomize)
# Check the problem # Check the problem
get_request_dict = {CapaFactory.input_key(): '3.14'} get_request_dict = {CapaFactory.input_key(): '3.14'}
...@@ -947,7 +1005,8 @@ class CapaModuleTest(unittest.TestCase): ...@@ -947,7 +1005,8 @@ class CapaModuleTest(unittest.TestCase):
return success return success
# Run the test for each possible rerandomize value # Run the test for each possible rerandomize value
for rerandomize in ['never', 'per_student', 'always', 'onreset']: for rerandomize in ['never', 'false', 'per_student',
'always', 'true', 'onreset']:
module = CapaFactory.create(rerandomize=rerandomize) module = CapaFactory.create(rerandomize=rerandomize)
# Get the seed # Get the seed
...@@ -959,7 +1018,7 @@ class CapaModuleTest(unittest.TestCase): ...@@ -959,7 +1018,7 @@ class CapaModuleTest(unittest.TestCase):
# is set to 'never' -- it should still be 1 # is set to 'never' -- it should still be 1
# The seed also stays the same if we're randomizing # The seed also stays the same if we're randomizing
# 'per_student': the same student should see the same problem # 'per_student': the same student should see the same problem
if rerandomize in ['never', 'per_student']: if rerandomize in ['never', 'false', 'per_student']:
self.assertEqual(seed, _reset_and_get_seed(module)) self.assertEqual(seed, _reset_and_get_seed(module))
# Otherwise, we expect the seed to change # Otherwise, we expect the seed to change
...@@ -969,10 +1028,8 @@ class CapaModuleTest(unittest.TestCase): ...@@ -969,10 +1028,8 @@ class CapaModuleTest(unittest.TestCase):
# Since there's a small chance we might get the # Since there's a small chance we might get the
# same seed again, give it 5 chances # same seed again, give it 5 chances
# to generate a different seed # to generate a different seed
success = _retry_and_check(5, success = _retry_and_check(5, lambda: _reset_and_get_seed(module) != seed)
lambda: _reset_and_get_seed(module) != seed)
# TODO: change this comparison to module.seed is not None? self.assertTrue(module.seed is not None)
self.assertTrue(module.seed != None)
msg = 'Could not get a new seed from reset after 5 tries' msg = 'Could not get a new seed from reset after 5 tries'
self.assertTrue(success, msg) self.assertTrue(success, msg)
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