Add unit tests for formularesponse type
This commit is contained in:
@@ -33,10 +33,16 @@ class ResponseTest(unittest.TestCase):
|
||||
xml = self.xml_factory.build_xml(**kwargs)
|
||||
return lcp.LoncapaProblem(xml, '1', system=test_system)
|
||||
|
||||
def assert_grade(self, problem, submission, expected_correctness):
|
||||
def assert_grade(self, problem, submission, expected_correctness, msg=None):
|
||||
input_dict = {'1_2_1': submission}
|
||||
correct_map = problem.grade_answers(input_dict)
|
||||
self.assertEquals(correct_map.get_correctness('1_2_1'), expected_correctness)
|
||||
if msg is None:
|
||||
self.assertEquals(correct_map.get_correctness('1_2_1'), expected_correctness)
|
||||
else:
|
||||
if correct_map.get_correctness('1_2_1')!= expected_correctness:
|
||||
print submission
|
||||
print correct_map
|
||||
self.assertEquals(correct_map.get_correctness('1_2_1'), expected_correctness, msg)
|
||||
|
||||
def assert_answer_format(self, problem):
|
||||
answers = problem.get_question_answers()
|
||||
@@ -358,6 +364,84 @@ class FormulaResponseTest(ResponseTest):
|
||||
self.assert_grade(problem, '3*x', 'incorrect')
|
||||
|
||||
|
||||
def test_parallel_resistors(self):
|
||||
"""Test parallel resistors"""
|
||||
sample_dict = {'R1': (10,10), 'R2': (2,2), 'R3': (5,5), 'R4': (1,1)}
|
||||
|
||||
# Test problem
|
||||
problem = self.build_problem(sample_dict=sample_dict,
|
||||
num_samples=10,
|
||||
tolerance=0.01,
|
||||
answer="R1||R2")
|
||||
# Expect answer to be marked correct
|
||||
input_formula = "R1||R2"
|
||||
self.assert_grade(problem, input_formula, "correct")
|
||||
|
||||
# Expect random number to be marked incorrect
|
||||
input_formula = "13"
|
||||
self.assert_grade(problem, input_formula, "incorrect")
|
||||
|
||||
# Expect incorrect answer marked incorrect
|
||||
input_formula = "R3||R4"
|
||||
self.assert_grade(problem, input_formula, "incorrect")
|
||||
|
||||
def test_default_variables(self):
|
||||
"""Test the default variables provided in common/lib/capa/capa/calc.py"""
|
||||
# which are: j (complex number), e, pi, k, c, T, q
|
||||
|
||||
# Sample x in the range [-10,10]
|
||||
sample_dict = {'x': (-10, 10)}
|
||||
default_variables = [('j', 2, 3), ('e', 2, 3), ('pi', 2, 3), ('c', 2, 3), ('T', 2, 3),
|
||||
('k', 2*10**23, 3*10**23), # note k = scipy.constants.k = 1.3806488e-23
|
||||
('q', 2*10**19, 3*10**19)] # note k = scipy.constants.e = 1.602176565e-19
|
||||
for (var, cscalar, iscalar) in default_variables:
|
||||
# The expected solution is numerically equivalent to cscalar*var
|
||||
correct = '{0}*x*{1}'.format(cscalar, var)
|
||||
incorrect = '{0}*x*{1}'.format(iscalar, var)
|
||||
problem = self.build_problem(sample_dict=sample_dict,
|
||||
num_samples=10,
|
||||
tolerance=0.01,
|
||||
answer=correct)
|
||||
# Expect that the inputs are graded correctly
|
||||
|
||||
self.assert_grade(problem, correct, 'correct',
|
||||
msg="Failed on variable {0}; the given, correct answer was {1} but graded 'incorrect'".format(var, correct))
|
||||
self.assert_grade(problem, incorrect, 'incorrect',
|
||||
msg="Failed on variable {0}; the given, incorrect answer was {1} but graded 'correct'".format(var, incorrect))
|
||||
|
||||
def test_default_functions(self):
|
||||
"""Test the default functions provided in common/lib/capa/capa/calc.py"""
|
||||
# which are: sin, cos, tan, sqrt, log10, log2, ln,
|
||||
# arccos, arcsin, arctan, abs,
|
||||
# fact, factorial
|
||||
|
||||
# Sample x in the range [-10,10]
|
||||
import random
|
||||
w = random.randint(3, 10)
|
||||
sample_dict = {'x': (-10, 10), 'y': (1, 10), 'z':(-1, 1), 'w':(w,w)}
|
||||
default_functions = [('sin', 2, 3, 'x'), ('cos', 2, 3, 'x'), ('tan', 2, 3, 'x'), ('sqrt', 2, 3, 'y'), ('log10', 2, 3, 'y'),
|
||||
('log2', 2, 3, 'y'), ('ln', 2, 3, 'y'), ('arccos', 2, 3, 'z'), ('arcsin', 2, 3, 'z'), ('arctan', 2, 3, 'x'),
|
||||
('abs', 2, 3, 'x'), ('fact', 2, 3, 'w'), ('factorial', 2, 3, 'w'),]
|
||||
for (func, cscalar, iscalar, var) in default_functions:
|
||||
print 'func is: {0}'.format(func)
|
||||
# The expected solution is numerically equivalent to cscalar*func(var)
|
||||
correct = '{0}*{1}({2})'.format(cscalar, func, var)
|
||||
incorrect = '{0}*{1}({2})'.format(iscalar, func, var)
|
||||
problem = self.build_problem(sample_dict=sample_dict,
|
||||
num_samples=10,
|
||||
tolerance=0.01,
|
||||
answer=correct)
|
||||
# Expect that the inputs are graded correctly
|
||||
|
||||
self.assert_grade(problem, correct, 'correct',
|
||||
msg="Failed on function {0}; the given, correct answer was {1} but graded 'incorrect'".format(func, correct))
|
||||
self.assert_grade(problem, incorrect, 'incorrect',
|
||||
msg="Failed on function {0}; the given, incorrect answer was {1} but graded 'correct'".format(func, incorrect))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class StringResponseTest(ResponseTest):
|
||||
from response_xml_factory import StringResponseXMLFactory
|
||||
xml_factory_class = StringResponseXMLFactory
|
||||
|
||||
Reference in New Issue
Block a user