From 1060ce495d75d2c041f63c5d7b704f1bb3a4b0d5 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Tue, 26 Feb 2013 16:14:59 -0500 Subject: [PATCH] Modified OptionResponseTest to use XML factory --- .../capa/capa/tests/response_xml_factory.py | 36 +++++++++++++++++-- .../lib/capa/capa/tests/test_responsetypes.py | 29 ++++++++------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/common/lib/capa/capa/tests/response_xml_factory.py b/common/lib/capa/capa/tests/response_xml_factory.py index b6aa1bbf3b..4f4870594d 100644 --- a/common/lib/capa/capa/tests/response_xml_factory.py +++ b/common/lib/capa/capa/tests/response_xml_factory.py @@ -335,11 +335,43 @@ class TrueFalseResponseXMLFactory(ResponseXMLFactory): return ResponseXMLFactory.choicegroup_input_xml(**kwargs) class OptionResponseXMLFactory(ResponseXMLFactory): + """ Factory for producing XML""" + def create_response_element(self, **kwargs): - raise NotImplemented + """ Create the element""" + return etree.Element("optionresponse") def create_input_element(self, **kwargs): - raise NotImplemented + """ Create the element. + + Uses **kwargs: + + *options*: a list of possible options the user can choose from [REQUIRED] + You must specify at least 2 options. + *correct_option*: the correct choice from the list of options [REQUIRED] + """ + + options_list = kwargs.get('options', None) + correct_option = kwargs.get('correct_option', None) + + assert(options_list and correct_option) + assert(len(options_list) > 1) + assert(correct_option in options_list) + + # Create the element + optioninput_element = etree.Element("optioninput") + + # Set the "options" attribute + # Format: "('first', 'second', 'third')" + options_attr_string = ",".join(["'%s'" % str(o) for o in options_list]) + options_attr_string = "(%s)" % options_attr_string + optioninput_element.set('options', options_attr_string) + + # Set the "correct" attribute + optioninput_element.set('correct', str(correct_option)) + + return optioninput_element + class StringResponseXMLFactory(ResponseXMLFactory): def create_response_element(self, **kwargs): diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index f6ed0161cb..f1817aef52 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -250,22 +250,21 @@ class SymbolicResponseTest(unittest.TestCase): self.assertEquals(test_lcp.grade_answers(wrong_answers).get_correctness('1_2_1'), 'incorrect') -class OptionResponseTest(unittest.TestCase): - ''' - Run this with +class OptionResponseTest(ResponseTest): + from response_xml_factory import OptionResponseXMLFactory + xml_factory_class = OptionResponseXMLFactory - python manage.py test courseware.OptionResponseTest - ''' - def test_or_grade(self): - optionresponse_file = os.path.dirname(__file__) + "/test_files/optionresponse.xml" - test_lcp = lcp.LoncapaProblem(open(optionresponse_file).read(), '1', system=test_system) - correct_answers = {'1_2_1': 'True', - '1_2_2': 'False'} - test_answers = {'1_2_1': 'True', - '1_2_2': 'True', - } - self.assertEquals(test_lcp.grade_answers(test_answers).get_correctness('1_2_1'), 'correct') - self.assertEquals(test_lcp.grade_answers(test_answers).get_correctness('1_2_2'), 'incorrect') + def test_grade(self): + problem = self.build_problem(options=["first", "second", "third"], + correct_option="second") + + # Assert that we get the expected grades + self.assert_grade(problem, "first", "incorrect") + self.assert_grade(problem, "second", "correct") + self.assert_grade(problem, "third", "incorrect") + + # Options not in the list should be marked incorrect + self.assert_grade(problem, "invalid_option", "incorrect") class FormulaResponseWithHintTest(unittest.TestCase):