From 80d0952d161a6ef4e795336bd8109e155807ed46 Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Fri, 26 Oct 2012 18:29:46 -0400 Subject: [PATCH] Refactor schematic input --- common/lib/capa/capa/inputtypes.py | 52 ++++++++++-------- common/lib/capa/capa/tests/test_inputtypes.py | 55 ++++++++++++++++++- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index bbbddc0fb6..cbd5d1d02e 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -546,29 +546,37 @@ register_input_class(CodeInput) #----------------------------------------------------------------------------- -def schematic(element, value, status, render_template, msg=''): - eid = element.get('id') - height = element.get('height') - width = element.get('width') - parts = element.get('parts') - analyses = element.get('analyses') - initial_value = element.get('initial_value') - submit_analyses = element.get('submit_analyses') - context = { - 'id': eid, - 'value': value, - 'initial_value': initial_value, - 'state': status, - 'width': width, - 'height': height, - 'parts': parts, - 'analyses': analyses, - 'submit_analyses': submit_analyses, - } - html = render_template("schematicinput.html", context) - return etree.XML(html) +class Schematic(InputTypeBase): + """ + """ -_reg(schematic) + template = "schematicinput.html" + tags = ['schematic'] + + def __init__(self, system, xml, state): + super(Schematic, self).__init__(system, xml, state) + self.height = xml.get('height') + self.width = xml.get('width') + self.parts = xml.get('parts') + self.analyses = xml.get('analyses') + self.initial_value = xml.get('initial_value') + self.submit_analyses = xml.get('submit_analyses') + + + def _get_render_context(self): + + context = {'id': self.id, + 'value': self.value, + 'initial_value': self.initial_value, + 'state': self.status, + 'width': self.width, + 'height': self.height, + 'parts': self.parts, + 'analyses': self.analyses, + 'submit_analyses': self.submit_analyses, } + return context + +register_input_class(Schematic) #----------------------------------------------------------------------------- ### TODO: Move out of inputtypes diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 8cfbbc54ef..573f52a01a 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -1,5 +1,9 @@ """ -Tests of input types (and actually responsetypes too) +Tests of input types (and actually responsetypes too). + +TODO: +- test unicode in values, parameters, etc. +- test various html escapes """ from datetime import datetime @@ -318,3 +322,52 @@ class CodeInputTest(unittest.TestCase): self.assertEqual(context, expected) + +class SchematicTest(unittest.TestCase): + ''' + Check that schematic inputs work + ''' + + def test_rendering(self): + height = '12' + width = '33' + parts = 'resistors, capacitors, and flowers' + analyses = 'fast, slow, and pink' + initial_value = 'two large batteries' + submit_analyses = 'maybe' + + + xml_str = """""".format(h=height, w=width, p=parts, a=analyses, + iv=initial_value, sa=submit_analyses) + + element = etree.fromstring(xml_str) + + value = 'three resistors and an oscilating pendulum' + state = {'value': value, + 'status': 'unsubmitted', + 'feedback' : {'message': '3'}, } + + the_input = inputtypes.get_class_for_tag('schematic')(system, element, state) + + context = the_input._get_render_context() + + expected = {'id': 'prob_1_2', + 'value': value, + 'initial_value': initial_value, + 'state': 'unsubmitted', + 'width': width, + 'height': height, + 'parts': parts, + 'analyses': analyses, + 'submit_analyses': submit_analyses, + } + + self.assertEqual(context, expected) +