def test_survey_can_be_created_in_a_slightly_less_verbose_manner(self): option_dict_array = [ {"name": "red", "label": "Red"}, {"name": "blue", "label": "Blue"}, ] q = MultipleChoiceQuestion(name="Favorite_Color", choices=option_dict_array) q.type = "select one" s = Survey(name="Roses_are_Red", children=[q]) expected_dict = { "name": "Roses_are_Red", "children": [ { "name": "Favorite_Color", "type": "select one", "children": [ {"label": "Red", "name": "red"}, {"label": "Blue", "name": "blue"}, ], } ], } self.assertEqual(s.to_json_dict(), expected_dict)
def _load_registration_survey_object(): """ Loads a registration survey with all the values necessary to register a surveyor. """ survey = Survey(name="registration", id_string="registration") survey.add_child( create_survey_element_from_dict({ 'type': 'text', 'name': 'name', 'label': 'Name' })) survey.add_child( create_survey_element_from_dict({ 'type': 'start time', 'name': 'start' })) survey.add_child( create_survey_element_from_dict({ 'type': 'end time', 'name': 'end' })) survey.add_child( create_survey_element_from_dict({ 'type': 'imei', 'name': 'device_id' })) return survey
def test_answers_can_be_imported_from_xml(self): surv = Survey(name="data") surv.add_child( create_survey_element_from_dict( {"type": "text", "name": "name", "label": "Name"} ) ) surv.add_child( create_survey_element_from_dict( { "type": "integer", "name": "users_per_month", "label": "Users per month", } ) ) surv.add_child( create_survey_element_from_dict( {"type": "gps", "name": "geopoint", "label": "gps"} ) ) surv.add_child( create_survey_element_from_dict({"type": "imei", "name": "device_id"}) ) instance = surv.instantiate() import_xml = self.config.get( self.cls_name, "test_answers_can_be_imported_from_xml" ) instance.import_from_xml(import_xml)
def __init__(self, descriptor=None, title=None, id_string=None): # Seek to begining of file if it has the seek attribute before loading # the file. if hasattr(descriptor, 'seek'): descriptor.seek(0) try: # descriptor is a file self.descriptor = json.load(descriptor) except AttributeError: try: # descriptor is a JSON string self.descriptor = json.loads(descriptor) except JSONDecodeError: # descriptor is a file path. self.descriptor = json.load( codecs.open(descriptor, encoding='utf-8')) if self.descriptor['profile'] == FLOW_RESULTS_PROFILE: del self.descriptor['profile'] self._package = Package(self.descriptor) self.descriptor = self._package.descriptor self._name = id_string or self._package.descriptor.get('name') assert self._name, "The 'name' property must be defined." title = title or self._package.descriptor.get('title') or self._name survey_dict = { constants.NAME: 'data', constants.ID_STRING: self._name, constants.TITLE: title, constants.TYPE: constants.SURVEY, } self._survey = Survey(**survey_dict) self.build()
def setUp(self): survey_out = Survey(type=u"survey") question = InputQuestion(name=u"age") question.set(InputQuestion.TYPE, u"integer") question.set(InputQuestion.LABEL, u"How old are you?") survey_out.add_child(question) self.survey_out_dict = survey_out.to_dict() print_pyobj_to_json(self.survey_out_dict, "pyxform/tests/how_old_are_you.json")
def setUp(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name=u"age", sms_keyword=u"age", type=u"survey") question = InputQuestion(name=u"age") question.type = u"integer" question.label = u"How old are you?" survey_out.add_child(question) self.survey_out_dict = survey_out.to_json_dict() print_pyobj_to_json(self.survey_out_dict, utils.path_to_text_fixture("how_old_are_you.json"))
def setUp(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name=u"age", type=u"survey") question = InputQuestion(name=u"age") question.type = u"integer" question.label = u"How old are you?" survey_out.add_child(question) self.survey_out_dict = survey_out.to_json_dict() print_pyobj_to_json(self.survey_out_dict, utils.path_to_text_fixture("how_old_are_you.json"))
def setUp(self): survey_out = Survey( type=u"survey" ) question = InputQuestion(name=u"age") question.set(InputQuestion.TYPE, u"integer") question.set(InputQuestion.LABEL, u"How old are you?") survey_out.add_child(question) self.survey_out_dict = survey_out.to_dict() print_pyobj_to_json(self.survey_out_dict, "pyxform/tests/how_old_are_you.json")
def test_simple_survey_instantiation(self): surv = Survey(name="Simple") q = create_survey_element_from_dict( {"type": "text", "name": "survey_question", "label": "Question"} ) surv.add_child(q) i = surv.instantiate() self.assertEquals(i.keys(), ["survey_question"]) self.assertEquals(set(i.xpaths()), {"/Simple", "/Simple/survey_question"})
def test_output_node_for_select_one_question_type(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name="geopgraphy", sms_keyword="geography", type="survey") question = InputQuestion(name="counties") question.type = "select one external" question.label = "county" survey_out.add_child(question) expected = '<input ref="/geopgraphy/counties">' xml = survey_out.to_xml() self.assertEqual(1, xml.count(expected))
def test_simple_survey_instantiation(self): surv = Survey(name="Simple") q = create_survey_element_from_dict({ "type": "text", "name": "survey_question", "label": "Question" }) surv.add_child(q) i = surv.instantiate() self.assertEquals(i.keys(), ["survey_question"]) self.assertEquals(set(i.xpaths()), {"/Simple", "/Simple/survey_question"})
def test_simple_registration_xml(self): reg_xform = Survey(name="Registration") name_question = create_survey_element_from_dict( {"type": "text", "name": "name", "label": "Name"} ) reg_xform.add_child(name_question) reg_instance = reg_xform.instantiate() reg_instance.answer(name="name", value="bob") rx = reg_instance.to_xml() expected_xml = self.config.get( self.cls_name, "test_simple_registration_xml" ).format(reg_xform.id_string) self.assertEqual(rx, expected_xml)
def download_spss_labels(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') try: xlsform_io= xform.to_xlsform() if not xlsform_io: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': form_id_string}) return HttpResponseRedirect("/%s" % username) except: return HttpResponseServerError('Error retrieving XLSForm.') survey= Survey.from_xls(filelike_obj=xlsform_io) zip_filename= '{}_spss_labels.zip'.format(xform.id_string) zip_io= survey_to_spss_label_zip(survey, xform.id_string) response = StreamingHttpResponse(FileWrapper(zip_io), content_type='application/zip; charset=utf-8') response['Content-Disposition'] = 'attachment; filename={}'.format(zip_filename) return response
def test_select_many_q_to_xform(): """ Test select_many floip question to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict( survey, 'ae54d5', { "type": "select_many", "label": "What is your favorite desert?", "type_options": { "choices": ["cake", "fruit", "ice cream"] } }) choices = [ '<item><label>cake</label><value>cake</value></item>', '<item><label>fruit</label><value>fruit</value></item>', '<item><label>ice cream</label><value>ice cream</value>' '</item>' ] expected_data = { 'name': question['name'], 'label': question['label'], 'choices': ''.join(choices) } body_xml = ( u'<select ref="/floip/%(name)s"><label>%(label)s</label>%(choices)s' u'</select>' % expected_data) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="select"/>' % question) assert question.xml_binding().toxml() == bind_xml assert len([child.xml() for child in question.children]) == 3
def test_select_one_q_to_xform(): """ Test select_one floip question to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict( survey, 'ae54d4', { "type": "select_one", "label": "Are you male or female?", "type_options": { "choices": ["male", "female", "not identified"] } }) choices = [ '<item><label>male</label><value>male</value></item>', '<item><label>female</label><value>female</value></item>', '<item><label>not identified</label><value>not identified</value>' '</item>' ] expected_data = { 'name': question['name'], 'label': question['label'], 'choices': ''.join(choices) } body_xml = ( u'<select1 ref="/floip/%(name)s"><label>%(label)s</label>%(choices)s' u'</select1>' % expected_data) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="select1"/>' % question) assert question.xml_binding().toxml() == bind_xml assert len([child.xml() for child in question.children]) == 3
def test_simple_registration_xml(self): reg_xform = Survey(name="Registration") name_question = create_survey_element_from_dict({ "type": "text", "name": "name", "label": "Name" }) reg_xform.add_child(name_question) reg_instance = reg_xform.instantiate() reg_instance.answer(name="name", value="bob") rx = reg_instance.to_xml() expected_xml = self.config.get(self.cls_name, "test_simple_registration_xml").format( reg_xform.id_string) self.assertEqual(rx, expected_xml)
def test_simple_survey_answering(self): surv = Survey(name="Water") q = create_survey_element_from_dict( {"type": "text", "name": "color", "label": "Color"} ) q2 = create_survey_element_from_dict( {"type": "text", "name": "feeling", "label": "Feeling"} ) surv.add_child(q) surv.add_child(q2) i = SurveyInstance(surv) i.answer(name="color", value="blue") self.assertEquals(i.answers()["color"], "blue") i.answer(name="feeling", value="liquidy") self.assertEquals(i.answers()["feeling"], "liquidy")
def test_survey_can_be_created_in_a_verbose_manner(self): s = Survey() s.name = "simple_survey" q = MultipleChoiceQuestion() q.name = "cow_color" q.type = "select one" q.add_choice(label="Green", name="green") s.add_child(q) expected_dict = { "name": "simple_survey", "children": [{ "name": "cow_color", "type": "select one", "children": [{ "label": "Green", "name": "green" }], }], } self.maxDiff = None self.assertEqual(s.to_json_dict(), expected_dict)
def test_survey_can_be_created_in_a_slightly_less_verbose_manner(self): option_dict_array = [ { "name": "red", "label": "Red" }, { "name": "blue", "label": "Blue" }, ] q = MultipleChoiceQuestion(name="Favorite_Color", choices=option_dict_array) q.type = "select one" s = Survey(name="Roses_are_Red", children=[q]) expected_dict = { "name": "Roses_are_Red", "children": [{ "name": "Favorite_Color", "type": "select one", "children": [ { "label": "Red", "name": "red" }, { "label": "Blue", "name": "blue" }, ], }], } self.assertEqual(s.to_json_dict(), expected_dict)
def test_survey_can_be_created_in_a_verbose_manner(self): s = Survey() s.name = "simple_survey" q = MultipleChoiceQuestion() q.name = "cow_color" q.type = "select one" q.add_choice(label="Green", name="green") s.add_child(q) expected_dict = { "name": "simple_survey", "children": [ { "name": "cow_color", "type": "select one", "children": [{"label": "Green", "name": "green"}], } ], } self.maxDiff = None self.assertEqual(s.to_json_dict(), expected_dict)
def test_time_question_to_xform(): """ Test a floip time question to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict(survey, 'ae54d10', { "type": "time", "label": "What is the time?", "type_options": {} }) body_xml = (u'<input ref="/floip/%(name)s">' '<label>%(label)s</label></input>' % question) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="time"/>' % question) assert question.xml_binding().toxml() == bind_xml
def test_video_upload_to_xform(): """ Test a video floip question to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict(survey, 'ae54d9', { "type": "video", "label": "Upload a video recording", "type_options": {} }) body_xml = (u'<upload mediatype="video/*" ref="/floip/%(name)s">' '<label>%(label)s</label></upload>' % question) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="binary"/>' % question) assert question.xml_binding().toxml() == bind_xml
def test_time_question_to_floip(): """ Test XForm time question to FLOIP """ survey = Survey(name='floip') element = xform_from_floip_dict(survey, 'ae54d8', { "type": "time", "label": "What is the time?", "type_options": {} }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "time", "label": "What is the time?", "type_options": {} }
def test_video_upload_to_floip(): """ Test XForm video question to FLOIP. """ survey = Survey(name='floip') element = xform_from_floip_dict(survey, 'ae54d8', { "type": "video", "label": "Upload a video recording", "type_options": {} }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "video", "label": "Upload a video recording", "type_options": {} }
def test_geopoint_question_to_floip(): """ Test geopoint question to FLOIP geo_point dictionary. """ survey = Survey(name='floip') element = xform_from_floip_dict(survey, 'ae54db', { "type": "geo_point", "label": "Where are you?", "type_options": {} }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "geo_point", "label": "Where are you?", "type_options": {} }
def test_text_question_to_xform(): """ Test text floip queston to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict(survey, 'ae54d6', { "type": "text", "label": "What is your name?", "type_options": {} }) body_xml = ( u'<input ref="/floip/%(name)s"><label>%(label)s</label></input>' % question) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="string"/>' % question) assert question.xml_binding().toxml() == bind_xml
def _load_registration_survey_object(): """ Loads a registration survey with all the values necessary to register a surveyor. """ survey = Survey(name=u"registration", id_string=u"registration") survey.add_child(create_survey_element_from_dict({ u'type': u'text', u'name': u'name', u'label': u'Name' })) survey.add_child(create_survey_element_from_dict({ u'type': u'start time', u'name': u'start' })) survey.add_child(create_survey_element_from_dict({ u'type': u'end time', u'name': u'end' })) survey.add_child(create_survey_element_from_dict({ u'type': u'imei', u'name': u'device_id' })) return survey
def test_image_upload_to_floip(): """ Test an XForm image question to FLOIP. """ survey = Survey(name='floip') element = xform_from_floip_dict( survey, 'ae54d8', { "type": "image", "label": "Upload an image of your location", "type_options": {} }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "image", "label": "Upload an image of your location", "type_options": {} }
def test_numeric_question_to_floip(): """ Test numeric floip queston to XForm. """ survey = Survey(name='floip') element = xform_from_floip_dict( survey, 'ae54d8', { "type": "numeric", "label": "How much do you weigh, in lbs?", "type_options": {} }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "numeric", "label": "How much do you weigh, in lbs?", "type_options": {} }
def test_geopoint_question_to_xform(): """ Test geo_point floip queston to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict(survey, 'ae54db', { "type": "geo_point", "label": "Where are you?", "type_options": {} }) body_xml = ( u'<input ref="/floip/%(name)s"><label>%(label)s</label></input>' % question) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="geopoint"/>' % question) assert question.xml_binding().toxml() == bind_xml
def test_numeric_question_to_xform(): """ Test numeric floip queston to XForm. """ survey = Survey(name='floip') question = xform_from_floip_dict( survey, 'ae54d8', { "type": "numeric", "label": "How much do you weigh, in lbs?", "type_options": {} }) body_xml = ( u'<input ref="/floip/%(name)s"><label>%(label)s</label></input>' % question) assert question.xml_control().toxml() == body_xml bind_xml = (u'<bind nodeset="/floip/%(name)s" type="int"/>' % question) assert question.xml_binding().toxml() == bind_xml
def test_dictionary_consolidates_duplicate_entries(self): yes_or_no_dict_array = [ {"label": {"French": "Oui", "English": "Yes"}, "name": "yes"}, {"label": {"French": "Non", "English": "No"}, "name": "no"}, ] first_yesno_question = MultipleChoiceQuestion( name="yn_q1", options=yes_or_no_dict_array, type="select one" ) second_yesno_question = MultipleChoiceQuestion( name="yn_q2", options=yes_or_no_dict_array, type="select one" ) s = Survey(name="yes_or_no_tests") s.add_child(first_yesno_question) s.add_child(second_yesno_question) # begin the processes in survey.to_xml() # 1. validate() s.validate()
def test_select_one_q_to_floip(): """ Test XForm select_one question to FLOIP. """ survey = Survey(name='floip') element = xform_from_floip_dict( survey, 'ae54d8', { "type": "select_one", "label": "Are you male or female?", "type_options": { "choices": ["male", "female", "not identified"] } }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "select_one", "label": "Are you male or female?", "type_options": { "choices": ["male", "female", "not identified"] } }
def test_numeric_range_q_to_floip(): """ Test XForm numeric range queston to FLOIP. """ survey = Survey(name='floip') element = xform_from_floip_dict( survey, 'ae54d8', { "type": "numeric", "label": "How much do you weigh, in lbs?", "type_options": { "range": [1, 250] } }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "numeric", "label": "How much do you weigh, in lbs?", "type_options": { "range": [1, 250] } }
def test_select_many_q_to_floip(): """ Test XForm select_many question to FLOIP. """ survey = Survey(name='floip') element = xform_from_floip_dict( survey, 'ae54d8', { "type": "select_many", "label": "What is your favorite desert?", "type_options": { "choices": ["cake", "fruit", "ice cream"] } }) question = floip_dict_from_xform_dict(element.to_json_dict()) assert question == { "type": "select_many", "label": "What is your favorite desert?", "type_options": { "choices": ["cake", "fruit", "ice cream"] } }
def test_simple_survey_answering(self): surv = Survey(name="Water") q = create_survey_element_from_dict({ "type": "text", "name": "color", "label": "Color" }) q2 = create_survey_element_from_dict({ "type": "text", "name": "feeling", "label": "Feeling" }) surv.add_child(q) surv.add_child(q2) i = SurveyInstance(surv) i.answer(name="color", value="blue") self.assertEquals(i.answers()["color"], "blue") i.answer(name="feeling", value="liquidy") self.assertEquals(i.answers()["feeling"], "liquidy")
def _load_simple_survey_object(): """ Returns a "watersimple" survey object, complete with questions. """ survey = Survey(name="WaterSimple", id_string="WaterSimple") survey.add_child( create_survey_element_from_dict({ 'hint': { 'English': 'What is this point named?' }, 'label': { 'English': 'Water Point Name' }, 'type': 'text', 'name': 'name' })) survey.add_child( create_survey_element_from_dict({ 'hint': { 'English': 'How many people use this every month?' }, 'label': { 'English': 'Monthly Usage' }, 'name': 'users_per_month', 'type': 'integer' })) survey.add_child( create_survey_element_from_dict({ 'type': 'gps', 'name': 'geopoint', 'label': { 'English': 'Location' } })) survey.add_child( create_survey_element_from_dict({ 'type': 'imei', 'name': 'device_id' })) survey.add_child( create_survey_element_from_dict({ 'type': 'start time', 'name': 'start' })) survey.add_child( create_survey_element_from_dict({ 'type': 'end time', 'name': 'end' })) return survey
def _load_simple_survey_object(): """ Returns a "watersimple" survey object, complete with questions. """ survey = Survey(name=u"WaterSimple", id_string=u"WaterSimple") survey.add_child(create_survey_element_from_dict({ u'hint': {u'English': u'What is this point named?'}, u'label': {u'English': u'Water Point Name'}, u'type': u'text', u'name': u'name' })) survey.add_child(create_survey_element_from_dict({ u'hint': {u'English': u'How many people use this every month?'}, u'label': {u'English': u'Monthly Usage'}, u'name': u'users_per_month', u'type': u'integer' })) survey.add_child(create_survey_element_from_dict({ u'type': u'gps', u'name': u'geopoint', u'label': {u'English': u'Location'} })) survey.add_child(create_survey_element_from_dict({ u'type': u'imei', u'name': u'device_id' })) survey.add_child(create_survey_element_from_dict({ u'type': u'start time', u'name': u'start' })) survey.add_child(create_survey_element_from_dict({ u'type': u'end time', u'name': u'end' })) return survey
def setUp(self): self.s = Survey(name="test")
class Json2XformQuestionValidationTests(TestCase): maxDiff = None config = None cls_name = None @classmethod def setUpClass(cls): prep_class_config(cls=cls) def setUp(self): self.s = Survey(name="test") def test_question_type_string(self): simple_string_json = { "label": { "French": "Nom du travailleur agricole:", "English": "Name of Community Agricultural Worker", }, "type": "text", "name": "enumerator_name", } q = create_survey_element_from_dict(simple_string_json) expected_string_control_xml = self.config.get( self.cls_name, "test_question_type_string_control") expected_string_binding_xml = self.config.get( self.cls_name, "test_question_type_string_binding") self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_string_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_string_binding_xml) def test_select_one_question_multilingual(self): """ Test the lowest common denominator of question types. """ simple_select_one_json = { "label": { "f": "ftext", "e": "etext" }, "type": "select one", "name": "qname", "choices": [ { "label": { "f": "fa", "e": "ea" }, "name": "a" }, { "label": { "f": "fb", "e": "eb" }, "name": "b" }, ], } # I copied the response in, since this is not our method of testing # valid return values. expected_select_one_control_xml = self.config.get( self.cls_name, "test_select_one_question_multilingual_control") expected_select_one_binding_xml = self.config.get( self.cls_name, "test_select_one_question_multilingual_binding") q = create_survey_element_from_dict(simple_select_one_json) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_select_one_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_select_one_binding_xml) def test_simple_integer_question_type_multilingual(self): """ not sure how integer questions should show up. """ simple_integer_question = { "label": { "f": "fc", "e": "ec" }, "type": "integer", "name": "integer_q", "attributes": {}, } expected_integer_control_xml = self.config.get( self.cls_name, "test_simple_integer_question_type_multilingual_control") expected_integer_binding_xml = self.config.get( self.cls_name, "test_simple_integer_question_type_multilingual_binding") q = create_survey_element_from_dict(simple_integer_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_integer_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_integer_binding_xml) def test_simple_date_question_type_multilingual(self): """ not sure how date questions should show up. """ simple_date_question = { "label": { "f": "fd", "e": "ed" }, "type": "date", "name": "date_q", "attributes": {}, } expected_date_control_xml = self.config.get( self.cls_name, "test_simple_date_question_type_multilingual_control") expected_date_binding_xml = self.config.get( self.cls_name, "test_simple_date_question_type_multilingual_binding") q = create_survey_element_from_dict(simple_date_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_date_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_date_binding_xml) def test_simple_phone_number_question_type_multilingual(self): """ not sure how phone number questions should show up. """ simple_phone_number_question = { "label": { "f": "fe", "e": "ee" }, "type": "phone number", "name": "phone_number_q", } expected_phone_number_control_xml = self.config.get( self.cls_name, "test_simple_phone_number_question_type_multilingual_control") expected_phone_number_binding_xml = self.config.get( self.cls_name, "test_simple_phone_number_question_type_multilingual_binding") q = create_survey_element_from_dict(simple_phone_number_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_phone_number_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_phone_number_binding_xml) def test_simple_select_all_question_multilingual(self): """ not sure how select all questions should show up... """ simple_select_all_question = { "label": { "f": "f choisit", "e": "e choose" }, "type": "select all that apply", "name": "select_all_q", "choices": [ { "label": { "f": "ff", "e": "ef" }, "name": "f" }, { "label": { "f": "fg", "e": "eg" }, "name": "g" }, { "label": { "f": "fh", "e": "eh" }, "name": "h" }, ], } expected_select_all_control_xml = self.config.get( self.cls_name, "test_simple_select_all_question_multilingual_control") expected_select_all_binding_xml = self.config.get( self.cls_name, "test_simple_select_all_question_multilingual_binding") q = create_survey_element_from_dict(simple_select_all_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_select_all_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_select_all_binding_xml) def test_simple_decimal_question_multilingual(self): """ not sure how decimal should show up. """ simple_decimal_question = { "label": { "f": "f text", "e": "e text" }, "type": "decimal", "name": "decimal_q", "attributes": {}, } expected_decimal_control_xml = self.config.get( self.cls_name, "test_simple_decimal_question_multilingual_control") expected_decimal_binding_xml = self.config.get( self.cls_name, "test_simple_decimal_question_multilingual_binding") q = create_survey_element_from_dict(simple_decimal_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_decimal_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_decimal_binding_xml)
class Json2XformQuestionValidationTests(TestCase): maxDiff = None @classmethod def setUpClass(cls): prep_class_config(cls=cls) def setUp(self): self.s = Survey(name="test") def test_question_type_string(self): simple_string_json = { "label": { "French": "Nom du travailleur agricole:", "English": "Name of Community Agricultural Worker", }, "type": "text", "name": "enumerator_name", } q = create_survey_element_from_dict(simple_string_json) expected_string_control_xml = self.config.get( self.cls_name, "test_question_type_string_control" ) expected_string_binding_xml = self.config.get( self.cls_name, "test_question_type_string_binding" ) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_string_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_string_binding_xml) def test_select_one_question_multilingual(self): """ Test the lowest common denominator of question types. """ simple_select_one_json = { "label": {"f": "ftext", "e": "etext"}, "type": "select one", "name": "qname", "choices": [ {"label": {"f": "fa", "e": "ea"}, "name": "a"}, {"label": {"f": "fb", "e": "eb"}, "name": "b"}, ], } # I copied the response in, since this is not our method of testing # valid return values. expected_select_one_control_xml = self.config.get( self.cls_name, "test_select_one_question_multilingual_control" ) expected_select_one_binding_xml = self.config.get( self.cls_name, "test_select_one_question_multilingual_binding" ) q = create_survey_element_from_dict(simple_select_one_json) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_select_one_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_select_one_binding_xml) def test_simple_integer_question_type_multilingual(self): """ not sure how integer questions should show up. """ simple_integer_question = { "label": {"f": "fc", "e": "ec"}, "type": "integer", "name": "integer_q", "attributes": {}, } expected_integer_control_xml = self.config.get( self.cls_name, "test_simple_integer_question_type_multilingual_control" ) expected_integer_binding_xml = self.config.get( self.cls_name, "test_simple_integer_question_type_multilingual_binding" ) q = create_survey_element_from_dict(simple_integer_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_integer_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_integer_binding_xml) def test_simple_date_question_type_multilingual(self): """ not sure how date questions should show up. """ simple_date_question = { "label": {"f": "fd", "e": "ed"}, "type": "date", "name": "date_q", "attributes": {}, } expected_date_control_xml = self.config.get( self.cls_name, "test_simple_date_question_type_multilingual_control" ) expected_date_binding_xml = self.config.get( self.cls_name, "test_simple_date_question_type_multilingual_binding" ) q = create_survey_element_from_dict(simple_date_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_date_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_date_binding_xml) def test_simple_phone_number_question_type_multilingual(self): """ not sure how phone number questions should show up. """ simple_phone_number_question = { "label": {"f": "fe", "e": "ee"}, "type": "phone number", "name": "phone_number_q", } expected_phone_number_control_xml = self.config.get( self.cls_name, "test_simple_phone_number_question_type_multilingual_control" ) expected_phone_number_binding_xml = self.config.get( self.cls_name, "test_simple_phone_number_question_type_multilingual_binding" ) q = create_survey_element_from_dict(simple_phone_number_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_phone_number_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_phone_number_binding_xml) def test_simple_select_all_question_multilingual(self): """ not sure how select all questions should show up... """ simple_select_all_question = { "label": {"f": "f choisit", "e": "e choose"}, "type": "select all that apply", "name": "select_all_q", "choices": [ {"label": {"f": "ff", "e": "ef"}, "name": "f"}, {"label": {"f": "fg", "e": "eg"}, "name": "g"}, {"label": {"f": "fh", "e": "eh"}, "name": "h"}, ], } expected_select_all_control_xml = self.config.get( self.cls_name, "test_simple_select_all_question_multilingual_control" ) expected_select_all_binding_xml = self.config.get( self.cls_name, "test_simple_select_all_question_multilingual_binding" ) q = create_survey_element_from_dict(simple_select_all_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_select_all_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_select_all_binding_xml) def test_simple_decimal_question_multilingual(self): """ not sure how decimal should show up. """ simple_decimal_question = { "label": {"f": "f text", "e": "e text"}, "type": "decimal", "name": "decimal_q", "attributes": {}, } expected_decimal_control_xml = self.config.get( self.cls_name, "test_simple_decimal_question_multilingual_control" ) expected_decimal_binding_xml = self.config.get( self.cls_name, "test_simple_decimal_question_multilingual_binding" ) q = create_survey_element_from_dict(simple_decimal_question) self.s.add_child(q) self.assertEqual(ctw(q.xml_control()), expected_decimal_control_xml) if TESTING_BINDINGS: self.assertEqual(ctw(q.xml_binding()), expected_decimal_binding_xml)