From 42e711e7d2c452513fd63f98158258476847fbc8 Mon Sep 17 00:00:00 2001 From: Diana Huang Date: Wed, 6 Mar 2013 14:53:13 -0500 Subject: [PATCH] Update tests and documentation. --- common/lib/capa/capa/inputtypes.py | 2 +- common/lib/capa/capa/tests/test_inputtypes.py | 30 ++++++++++++++----- .../js/capa/chemical_equation_preview.js | 4 ++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 8f6f0b8c09..70770c63ff 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -756,7 +756,7 @@ class ChemicalEquationInput(InputTypeBase): def handle_ajax(self, dispatch, get): ''' - Since we only have one ajax handler for this input, check to see if it + Since we only have chemcalc preview this input, check to see if it matches the corresponding dispatch and send it through if it does ''' if dispatch == 'preview_chemcalc': diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 4a5ea5c429..54736cfd3c 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -482,27 +482,43 @@ class ChemicalEquationTest(unittest.TestCase): ''' Check that chemical equation inputs work. ''' - - def test_rendering(self): - size = "42" - xml_str = """""".format(size=size) + def setUp(self): + self.size = "42" + xml_str = """""".format(size=self.size) element = etree.fromstring(xml_str) state = {'value': 'H2OYeah', } - the_input = lookup_tag('chemicalequationinput')(test_system, element, state) + self.the_input = lookup_tag('chemicalequationinput')(test_system, element, state) - context = the_input._get_render_context() + + def test_rendering(self): + ''' Verify that the render context matches the expected render context''' + context = self.the_input._get_render_context() expected = {'id': 'prob_1_2', 'value': 'H2OYeah', 'status': 'unanswered', 'msg': '', - 'size': size, + 'size': self.size, 'previewer': '/static/js/capa/chemical_equation_preview.js', } self.assertEqual(context, expected) + + def test_chemcalc_ajax_sucess(self): + ''' Verify that using the correct dispatch and valid data produces a valid response''' + + data = {'formula': "H"} + response = self.the_input.handle_ajax("preview_chemcalc", data) + + self.assertTrue('preview' in response) + self.assertNotEqual(response['preview'], '') + self.assertEqual(response['error'], "") + + + + class DragAndDropTest(unittest.TestCase): ''' diff --git a/common/static/js/capa/chemical_equation_preview.js b/common/static/js/capa/chemical_equation_preview.js index 73c06972b1..10a6b54655 100644 --- a/common/static/js/capa/chemical_equation_preview.js +++ b/common/static/js/capa/chemical_equation_preview.js @@ -13,7 +13,9 @@ prev_id = "#" + this.id + "_preview"; preview_div = $(prev_id); - url = $(this).parents('.problems-wrapper').data('url'); + // find the closest parent problems-wrapper and use that url + url = $(this).closest('.problems-wrapper').data('url'); + // grab the input id from the input input_id = $(this).data('input-id') Problem.inputAjax(url, input_id, 'preview_chemcalc', {"formula" : this.value}, create_handler(preview_div));