def test_hidden(self): x = SurveyReader(utils.path_to_text_fixture("hidden.xls")) x_results = x.to_json_dict() expected_dict = [ { u'type': u'hidden', u'name': u'hidden_test' }, { 'children': [ { 'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, 'name': 'instanceID', 'type': 'calculate' } ], 'control': { 'bodyless': True }, 'name': 'meta', 'type': 'group' } ] self.assertEqual(x_results[u"children"], expected_dict)
def test_gps(self): x = SurveyReader(utils.path_to_text_fixture("gps.xls"), default_name="gps") expected_dict = [ { "type": "gps", "name": "location", "label": "GPS" }, { "children": [{ "bind": { "jr:preload": "uid", "readonly": "true()" }, "name": "instanceID", "type": "calculate", }], "control": { "bodyless": True }, "name": "meta", "type": "group", }, ] self.assertEqual(x.to_json_dict()["children"], expected_dict)
def test_hidden(self): x = SurveyReader(utils.path_to_text_fixture("hidden.xls"), default_name="hidden") x_results = x.to_json_dict() expected_dict = [ { "type": "hidden", "name": "hidden_test" }, { "children": [{ "bind": { "jr:preload": "uid", "readonly": "true()" }, "name": "instanceID", "type": "calculate", }], "control": { "bodyless": True }, "name": "meta", "type": "group", }, ] self.assertEqual(x_results["children"], expected_dict)
def test_specify_other(self): excel_reader = SurveyReader("pyxform/tests/specify_other.xls") d = excel_reader.to_dict() survey = create_survey_element_from_dict(d) expected_dict = { u'name': 'specify_other', u'type': u'survey', u'children': [ { u'name': u'sex', u'label': {u'English': u'What sex are you?'}, u'type': u'select one', u'children': [ { u'name': u'male', u'label': {u'English': u'Male'} }, { u'name': u'female', u'label': {u'English': u'Female'} }, { u'name': u'other', u'label': u'Other' } ] }, { u'name': u'sex_other', u'bind': {u'relevant': u"selected(../sex, 'other')"}, u'label': u'Specify other.', u'type': u'text'} ] } self.assertEqual(survey.to_dict(), expected_dict)
def test_text_and_integer(self): x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls")) expected_dict = [ { "label": {"english": "What is your name?"}, "type": "text", "name": "your_name", }, { "label": {"english": "How many years old are you?"}, "type": "integer", "name": "your_age", }, { "children": [ { "bind": { "calculate": "concat('uuid:', uuid())", "readonly": "true()", }, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ] self.assertEqual(x.to_json_dict()["children"], expected_dict)
def test_include_json(self): excel_reader = SurveyReader("pyxform/tests/include_json.xls") d = excel_reader.to_dict() survey_in = create_survey_element_from_dict(d) for k, v in survey_in.to_dict().items(): if k!="name": self.assertEqual(v, self.survey_out_dict[k])
def test_gps(self): x = SurveyReader(utils.path_to_text_fixture("gps.xls")) expected_dict = [{ u'type': u'gps', u'name': u'location', u'label': u'GPS' }, { 'children': [{ 'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, 'name': 'instanceID', 'type': 'calculate' }], 'control': { 'bodyless': True }, 'name': 'meta', 'type': 'group' }] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def test_table(self): x = SurveyReader(utils.path_to_text_fixture("simple_loop.xls")) expected_dict = { u"type": u"survey", u"name": u"simple_loop", u"id_string": u"simple_loop", u"default_language": u"default", u"title": u"simple_loop", u"children": [ { u"children": [ { u"type": u"integer", u"name": u"count", u"label": {u"English": u"How many are there in this group?"}, } ], u"type": u"loop", u"name": u"my_table", u"columns": [ {u"name": u"col1", u"label": {u"English": u"Column 1"}}, {u"name": u"col2", u"label": {u"English": u"Column 2"}}, ], u"label": {u"English": u"My Table"}, } ], } self.maxDiff = None self.assertEqual(x.to_json_dict(), expected_dict)
def test_text_and_integer(self): x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls")) expected_dict = [{ u'label': { u'english': u'What is your name?' }, u'type': u'text', u'name': u'your_name' }, { u'label': { u'english': u'How many years old are you?' }, u'type': u'integer', u'name': u'your_age' }, { u'children': [{ u'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, u'name': 'instanceID', u'type': 'calculate' }], u'control': { 'bodyless': True }, u'name': 'meta', u'type': u'group' }] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def test_hidden(self): x = SurveyReader(utils.path_to_text_fixture("hidden.xls")) x_results = x.to_json_dict() expected_dict = [{ u'type': u'hidden', u'name': u'hidden_test' }, { 'children': [{ 'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, 'name': 'instanceID', 'type': 'calculate' }], 'control': { 'bodyless': True }, 'name': 'meta', 'type': 'group' }] self.assertEqual(x_results[u"children"], expected_dict)
def test_xl_date_ambigous(self): """Test non standard sheet with exception is processed successfully.""" filename = "xl_date_ambiguous.xlsx" path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename) xls_reader = SurveyReader(path_to_excel_file) survey_dict = xls_reader.to_json_dict() self.assertTrue(len(survey_dict) > 0)
def create_survey_from_xls(path_or_file): excel_reader = SurveyReader(path_or_file) d = excel_reader.to_json_dict() survey = create_survey_element_from_dict(d) if not survey.id_string: survey.id_string = excel_reader._name return survey
def test_simple_yes_or_no_question(self): x = SurveyReader(utils.path_to_text_fixture("yes_or_no_question.xls")) x_results = x.to_json_dict() expected_dict = [{ u'label': { u'english': u'have you had a good day today?' }, u'type': u'select one', u'name': u'good_day', u'choices': [{ u'label': { u'english': u'yes' }, u'name': u'yes' }, { u'label': { u'english': u'no' }, u'name': u'no' }] }] self.assertEqual(x_results[u"children"], expected_dict)
def test_include_json(self): excel_reader = SurveyReader("pyxform/tests/include_json.xls") d = excel_reader.to_dict() survey_in = create_survey_element_from_dict(d) for k, v in survey_in.to_dict().items(): if k != "name": self.assertEqual(v, self.survey_out_dict[k])
def test_table(self): x = SurveyReader(absolute_path(__file__, "simple_loop.xls")) expected_dict = { u'type': u'survey', u'name': 'simple_loop', u'children': [ { u'children': [ { u'type': u'integer', u'name': u'count', u'label': {u'English': u'How many are there in this group?'} } ], u'type': u'loop', u'name': u'my_table', u'columns': [ { u'name': u'col1', u'label': {u'English': u'Column 1'} }, { u'name': u'col2', u'label': {u'English': u'Column 2'} } ], u'label': {u'English': u'My Table'} }]} self.assertEqual(x.to_dict(), expected_dict)
def test_gps(self): x = SurveyReader(utils.path_to_text_fixture("gps.xls")) expected_dict = [ { u'type': u'gps', u'name': u'location', u'label': u'GPS'}, { 'children': [ { 'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, 'name': 'instanceID', 'type': 'calculate' } ], 'control': { 'bodyless': True }, 'name': 'meta', 'type': 'group' }] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def test_table(self): x = SurveyReader(utils.path_to_text_fixture("simple_loop.xls")) expected_dict = { u'type': u'survey', u'name': u'simple_loop', u'id_string': u'simple_loop', u'default_language': u'default', u'title': u'simple_loop', u'children': [ { u'children': [ { u'type': u'integer', u'name': u'count', u'label': {u'English': u'How many are there in this group?'} } ], u'type': u'loop', u'name': u'my_table', u'columns': [ { u'name': u'col1', u'label': {u'English': u'Column 1'} }, { u'name': u'col2', u'label': {u'English': u'Column 2'} } ], u'label': {u'English': u'My Table'} }]} self.maxDiff = None self.assertEqual(x.to_json_dict(), expected_dict)
def test_blank_second_row(self): filename = "blank_second_row.xls" path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename) xls_reader = SurveyReader(path_to_excel_file, default_name="blank_second_row") survey_dict = xls_reader.to_json_dict() self.assertTrue(len(survey_dict) > 0)
def test_choice_name_as_type(self): filename = "choice_name_as_type.xls" path_to_excel_file = os.path.join(DIR, "example_xls", filename) xls_reader = SurveyReader(path_to_excel_file, default_name="choice_name_as_type") survey_dict = xls_reader.to_json_dict() self.assertTrue(has_external_choices(survey_dict))
def test_survey_reader(self): survey_reader = SurveyReader(self.path) expected_dict = { u'id_string': u'new_id', u'default_language': u'default', u'name': u'settings', u'title': u'My Survey', u'type': u'survey', u'children': [{ u'name': u'your_name', u'text': { u'english': u'What is your name?' }, u'type': u'text' }, { u'name': u'your_age', u'text': { u'english': u'How many years old are you?' }, u'type': u'integer' }], } self.assertEqual(survey_reader.to_json_dict(), expected_dict)
def test_text_and_integer(self): x = SurveyReader( utils.path_to_text_fixture("text_and_integer.xls"), default_name="text_and_integer", ) expected_dict = [ { "label": {"english": "What is your name?"}, "type": "text", "name": "your_name", }, { "label": {"english": "How many years old are you?"}, "type": "integer", "name": "your_age", }, { "children": [ { "bind": {"jr:preload": "uid", "readonly": "true()"}, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ] self.assertEqual(x.to_json_dict()["children"], expected_dict)
def test_json(self): x = SurveyReader("pyxform/tests/group.xls") x_results = x.to_dict() expected_dict = { u'name': 'group', u'type': u'survey', u'children': [ { u'name': u'family_name', u'type': u'text', u'label': {u'English': u"What's your family name?"} }, { u'name': u'father', u'type': u'group', u'label': {u'English': u'Father'}, u'children': [ { u'name': u'phone_number', u'type': u'phone number', u'label': {u'English': u"What's your father's phone number?"} }, { u'name': u'age', u'type': u'integer', u'label': {u'English': u'How old is your father?'} } ], } ], } self.assertEqual(x_results, expected_dict)
def test_include(self): excel_reader = SurveyReader("pyxform/tests/include.xls") d = excel_reader.to_dict() survey = create_survey_element_from_dict(d) expected_dict = { u'name': 'include', u'type': u'survey', u'children': [ { u'name': u'name', u'label': {u'English': u"What's your name?"}, u'type': u'text' }, { u'name': u'good_day', u'label': {u'english': u'have you had a good day today?'}, u'type': u'select one', u'children': [ { u'name': u'yes', u'label': {u'english': u'yes'} }, { u'name': u'no', u'label': {u'english': u'no'} } ]}]} self.assertEqual(survey.to_dict(), expected_dict)
def test_text_and_integer(self): x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls")) expected_dict = [ {u"text": {u"english": u"What is your name?"}, u"type": u"text", u"name": u"your_name"}, {u"text": {u"english": u"How many years old are you?"}, u"type": u"integer", u"name": u"your_age"}, ] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def test_json(self): x = SurveyReader(utils.path_to_text_fixture("group.xls")) x_results = x.to_json_dict() expected_dict = { u'name': u'group', u'title': u'group', u'id_string': u'group', u'sms_keyword': u'group', u'default_language': u'default', u'type': u'survey', u'children': [ { u'name': u'family_name', u'type': u'text', u'label': {u'English': u"What's your family name?"} }, { u'name': u'father', u'type': u'group', u'label': {u'English': u'Father'}, u'children': [ { u'name': u'phone_number', u'type': u'phone number', u'label': { u'English': u"What's your father's phone number?"} }, { u'name': u'age', u'type': u'integer', u'label': {u'English': u'How old is your father?'} } ], }, { u'children': [ { u'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, u'name': 'instanceID', u'type': 'calculate' } ], u'control': { 'bodyless': True }, u'name': 'meta', u'type': u'group' } ], } self.maxDiff = None self.assertEqual(x_results, expected_dict)
def test_gps(self): x = SurveyReader(utils.path_to_text_fixture("gps.xls")) expected_dict = [{ u'type': u'gps', u'name': u'location', u'label': u'GPS' }] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def test_hidden(self): x = SurveyReader(utils.path_to_text_fixture("hidden.xls")) x_results = x.to_json_dict() expected_dict = [ { u'type': u'hidden', u'name': u'hidden_test', } ] self.assertEqual(x_results[u"children"], expected_dict)
def test_survey_reader(self): survey_reader = SurveyReader(self.path) expected_dict = { u"id_string": u"new_id", u"sms_keyword": u"new_id", u"default_language": u"default", u"name": u"settings", u"title": u"My Survey", u"type": u"survey", u"attribute": { u"my_number": u"1234567890", u"my_string": u"lor\xe9m ipsum", }, u"children": [ { u"name": u"your_name", u"label": { u"english": u"What is your name?" }, u"type": u"text", }, { u"name": u"your_age", u"label": { u"english": u"How many years old are you?" }, u"type": u"integer", }, { "children": [{ "bind": { "calculate": "concat('uuid:', uuid())", "readonly": "true()", }, "name": "instanceID", "type": "calculate", }], "control": { "bodyless": True }, "name": "meta", "type": "group", }, ], } self.assertEqual(survey_reader.to_json_dict(), expected_dict)
def test_json(self): x = SurveyReader(utils.path_to_text_fixture("group.xls")) x_results = x.to_json_dict() expected_dict = { u'name': u'group', u'title': u'group', u'id_string': u'group', u'sms_keyword': u'group', u'default_language': u'default', u'type': u'survey', u'children': [ { u'name': u'family_name', u'type': u'text', u'label': {u'English': u"What's your family name?"} }, { u'name': u'father', u'type': u'group', u'label': {u'English': u'Father'}, u'children': [ { u'name': u'phone_number', u'type': u'phone number', u'label': {u'English': u"What's your father's phone number?"} }, { u'name': u'age', u'type': u'integer', u'label': {u'English': u'How old is your father?'} } ], }, { u'children': [ { u'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, u'name': 'instanceID', u'type': 'calculate' } ], u'control': { 'bodyless': True }, u'name': 'meta', u'type': u'group' } ], } self.maxDiff = None self.assertEqual(x_results, expected_dict)
def process_xls_io_to_section_json(file_io): # I agree that this function is not pretty, but I don't think we # should move this into the model because I prefer to think of the # model as file-format independent. path = save_in_temp_dir(file_io) m = re.search(r'([^/]+).xls$', path) slug = m.group(1) xlr = SurveyReader(path) xls_vals = xlr.to_dict() qjson = json.dumps(xls_vals) os.remove(path) return (slug, qjson)
def test_json(self): x = SurveyReader(utils.path_to_text_fixture("group.xls")) x_results = x.to_json_dict() expected_dict = { "name": "group", "title": "group", "id_string": "group", "sms_keyword": "group", "default_language": "default", "type": "survey", "children": [ { "name": "family_name", "type": "text", "label": {"English": "What's your family name?"}, }, { "name": "father", "type": "group", "label": {"English": "Father"}, "children": [ { "name": "phone_number", "type": "phone number", "label": {"English": "What's your father's phone number?"}, }, { "name": "age", "type": "integer", "label": {"English": "How old is your father?"}, }, ], }, { "children": [ { "bind": { "calculate": "concat('uuid:', uuid())", "readonly": "true()", }, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ], } self.maxDiff = None self.assertEqual(x_results, expected_dict)
def test_equality_of_to_dict(self): x = SurveyReader(utils.path_to_text_fixture("group.xls")) x_results = x.to_json_dict() survey = create_survey_element_from_dict(x_results) survey_dict = survey.to_json_dict() # using the builder sets the title attribute to equal name # this won't happen through reading the excel file as done by # SurveyReader. # Now it happens. #del survey_dict[u'title'] self.maxDiff = None self.assertEqual(x_results, survey_dict)
def test_equality_of_to_dict(self): x = SurveyReader(utils.path_to_text_fixture("group.xls")) x_results = x.to_json_dict() survey = create_survey_element_from_dict(x_results) survey_dict = survey.to_json_dict() # using the builder sets the title attribute to equal name # this won't happen through reading the excel file as done by # SurveyReader. # Now it happens. # del survey_dict[u'title'] self.maxDiff = None self.assertEqual(x_results, survey_dict)
def load_file_to_dict(path): """ Takes a file path and loads it into a nested json dict following the format in json_form_schema.json The file may be a xls file or json file. If it is xls it is converted using xls2json. """ if path.endswith(".json"): name = _section_name(path) return name, utils.get_pyobj_from_json(path) else: name = _section_name(path) excel_reader = SurveyReader(path) return name, excel_reader.to_json_dict()
def test_values_with_spaces_are_cleaned(self): """ Test that values with leading and trailing whitespaces are processed. This test checks that the submission_url provided is cleaned of leading and trailing whitespaces. """ filename = "spaces_in_choices_header.xls" path_to_excel_file = os.path.join(DIR, "bug_example_xls", filename) survey_reader = SurveyReader(path_to_excel_file) result = survey_reader.to_json_dict() self.assertEqual(result["submission_url"], "https://odk.ona.io/random_person/submission")
def test_json(self): x = SurveyReader(utils.path_to_text_fixture("group.xls")) x_results = x.to_json_dict() expected_dict = { "name": "group", "title": "group", "id_string": "group", "sms_keyword": "group", "default_language": "default", "type": "survey", "children": [ { "name": "family_name", "type": "text", "label": {"English": "What's your family name?"}, }, { "name": "father", "type": "group", "label": {"English": "Father"}, "children": [ { "name": "phone_number", "type": "phone number", "label": {"English": "What's your father's phone number?"}, }, { "name": "age", "type": "integer", "label": {"English": "How old is your father?"}, }, ], }, { "children": [ { "bind": {"jr:preload": "uid", "readonly": "true()"}, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ], } self.maxDiff = None self.assertEqual(x_results, expected_dict)
def test_simple_yes_or_no_question(self): filename = "yes_or_no_question.xls" path_to_excel_file = os.path.join(DIR, "example_xls", filename) #Get the xform output path: root_filename, ext = os.path.splitext(filename) output_path = os.path.join(DIR, "test_output", root_filename + ".json") expected_output_path = os.path.join(DIR, "test_expected_output", root_filename + ".json") x = SurveyReader(path_to_excel_file) x_results = x.to_json_dict() with codecs.open(output_path, mode="w", encoding="utf-8") as fp: json.dump(x_results, fp=fp, ensure_ascii=False, indent=4) #Compare with the expected output: with codecs.open(expected_output_path, 'rb', encoding="utf-8") as expected_file: with codecs.open(output_path, 'rb', encoding="utf-8") as actual_file: self.assertMultiLineEqual(expected_file.read(), actual_file.read())
def test_simple_yes_or_no_question(self): x = SurveyReader(utils.path_to_text_fixture("yes_or_no_question.xls")) x_results = x.to_json_dict() expected_dict = [ { u"label": {u"english": u"have you had a good day today?"}, u"type": u"select one", u"name": u"good_day", u"choices": [ {u"label": {u"english": u"yes"}, u"name": u"yes"}, {u"label": {u"english": u"no"}, u"name": u"no"}, ], } ] self.assertEqual(x_results[u"children"], expected_dict)
def test_survey_reader(self): survey_reader = SurveyReader(self.path) expected_dict = { u'id_string': u'new_id', u'sms_keyword': u'new_id', u'default_language': u'default', u'name': u'settings', u'title': u'My Survey', u'type': u'survey', u'children': [{ u'name': u'your_name', u'label': { u'english': u'What is your name?' }, u'type': u'text' }, { u'name': u'your_age', u'label': { u'english': u'How many years old are you?' }, u'type': u'integer' }, { 'children': [{ 'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, 'name': 'instanceID', 'type': 'calculate' }], 'control': { 'bodyless': True }, 'name': 'meta', 'type': 'group' }], } self.assertEqual(survey_reader.to_json_dict(), expected_dict)
def save(self, *args, **kwargs): if self.xls: # check if version is set excel_reader = SurveyReader(self.xls) survey_dict = excel_reader.to_json_dict() if has_external_choices(survey_dict): self.survey_dict = survey_dict self.has_external_choices = True survey = create_survey_element_from_dict(survey_dict) survey = self._check_version_set(survey) self.json = survey.to_json() self.xml = survey.to_xml() self.version = survey.get('version') self._mark_start_time_boolean() set_uuid(self) self._set_uuid_in_xml() super(DataDictionary, self).save(*args, **kwargs)
def test_text_and_integer(self): x = SurveyReader(utils.path_to_text_fixture("text_and_integer.xls")) expected_dict = [{ u'text': { u'english': u'What is your name?' }, u'type': u'text', u'name': u'your_name' }, { u'text': { u'english': u'How many years old are you?' }, u'type': u'integer', u'name': u'your_age' }] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def test_survey_reader(self): survey_reader = SurveyReader(self.path) expected_dict = { u'id_string': u'new_id', u'sms_keyword': u'new_id', u'default_language': u'default', u'name': u'settings', u'title': u'My Survey', u'type': u'survey', u'attribute': { u'my_number': u'1234567890', u'my_string': u'lor\xe9m ipsum' }, u'children': [ { u'name': u'your_name', u'label': {u'english': u'What is your name?'}, u'type': u'text' }, { u'name': u'your_age', u'label': {u'english': u'How many years old are you?'}, u'type': u'integer' }, { 'children': [ { 'bind': { 'calculate': "concat('uuid:', uuid())", 'readonly': 'true()' }, 'name': 'instanceID', 'type': 'calculate' } ], 'control': { 'bodyless': True }, 'name': 'meta', 'type': 'group' } ], } self.assertEqual(survey_reader.to_json_dict(), expected_dict)
def test_table(self): x = SurveyReader(utils.path_to_text_fixture("simple_loop.xls")) expected_dict = { u'type': u'survey', u'name': u'simple_loop', u'id_string': u'simple_loop', u'default_language': u'default', u'title': u'simple_loop', u'children': [{ u'children': [{ u'type': u'integer', u'name': u'count', u'label': { u'English': u'How many are there in this group?' } }], u'type': u'loop', u'name': u'my_table', u'columns': [{ u'name': u'col1', u'label': { u'English': u'Column 1' } }, { u'name': u'col2', u'label': { u'English': u'Column 2' } }], u'label': { u'English': u'My Table' } }] } self.maxDiff = None self.assertEqual(x.to_json_dict(), expected_dict)
def test_table(self): filename = "simple_loop.xls" path_to_excel_file = os.path.join(DIR, "example_xls", filename) # Get the xform output path: root_filename, ext = os.path.splitext(filename) output_path = os.path.join(DIR, "test_output", root_filename + ".json") expected_output_path = os.path.join( DIR, "test_expected_output", root_filename + ".json" ) x = SurveyReader(path_to_excel_file) x_results = x.to_json_dict() with codecs.open(output_path, mode="w", encoding="utf-8") as fp: json.dump(x_results, fp=fp, ensure_ascii=False, indent=4) # Compare with the expected output: with codecs.open(expected_output_path, "rb", encoding="utf-8") as expected_file: with codecs.open(output_path, "rb", encoding="utf-8") as actual_file: expected_json = json.load(expected_file) actual_json = json.load(actual_file) self.assertEqual(expected_json, actual_json)
def test_table(self): filename = "simple_loop.xls" path_to_excel_file = os.path.join(DIR, "example_xls", filename) # Get the xform output path: root_filename, ext = os.path.splitext(filename) output_path = os.path.join(DIR, "test_output", root_filename + ".json") expected_output_path = os.path.join( DIR, "test_expected_output", root_filename + ".json" ) x = SurveyReader(path_to_excel_file, default_name="simple_loop") x_results = x.to_json_dict() with codecs.open(output_path, mode="w", encoding="utf-8") as fp: json.dump(x_results, fp=fp, ensure_ascii=False, indent=4) # Compare with the expected output: with codecs.open(expected_output_path, "rb", encoding="utf-8") as expected_file: with codecs.open(output_path, "rb", encoding="utf-8") as actual_file: expected_json = json.load(expected_file) actual_json = json.load(actual_file) self.assertEqual(expected_json, actual_json)
def test_survey_reader(self): survey_reader = SurveyReader(self.path) expected_dict = { u"id_string": u"new_id", u"sms_keyword": u"new_id", u"default_language": u"default", u"name": u"settings", u"title": u"My Survey", u"type": u"survey", u"attribute": { u"my_number": u"1234567890", u"my_string": u"lor\xe9m ipsum", }, u"children": [ { u"name": u"your_name", u"label": {u"english": u"What is your name?"}, u"type": u"text", }, { u"name": u"your_age", u"label": {u"english": u"How many years old are you?"}, u"type": u"integer", }, { "children": [ { "bind": { "calculate": "concat('uuid:', uuid())", "readonly": "true()", }, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ], } self.assertEqual(survey_reader.to_json_dict(), expected_dict)
def test_gps(self): x = SurveyReader(utils.path_to_text_fixture("gps.xls")) expected_dict = [ {u"type": u"gps", u"name": u"location", u"label": u"GPS"}, { "children": [ { "bind": {"calculate": "concat('uuid:', uuid())", "readonly": "true()"}, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ] self.assertEqual(x.to_json_dict()[u"children"], expected_dict)
def clean_xform_file( self ): ''' Clean & checks the validity of the uploaded xform file. ''' data = self.cleaned_data[ 'xform_file' ] if data is None: return None name, file_ext = os.path.splitext( data.name ) # Parse file depending on file type if file_ext == '.xls': survey = SurveyReader( data ) elif file_ext == '.xml': survey = XFormReader( data ) else: raise forms.ValidationError( 'Unable to read XForm' ) return survey.to_json_dict()
def test_hidden(self): x = SurveyReader(utils.path_to_text_fixture("hidden.xls")) x_results = x.to_json_dict() expected_dict = [ {u"type": u"hidden", u"name": u"hidden_test"}, { "children": [ { "bind": {"calculate": "concat('uuid:', uuid())", "readonly": "true()"}, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ] self.assertEqual(x_results[u"children"], expected_dict)
def setUp(self): self.excel_files = [ "gps.xls", "include.xls", "specify_other.xls", "group.xls", "loop.xls", "text_and_integer.xls", # todo: this file uses json that was written in # builder_tests.py this needs to be fixed. # "include_json.xls", "simple_loop.xls", "yes_or_no_question.xls", ] self.surveys = {} for filename in self.excel_files: path = "pyxform/tests/%s" % filename excel_reader = SurveyReader(path) d = excel_reader.to_dict() self.surveys[filename] = create_survey_element_from_dict(d)
def test_specify_other(self): excel_reader = SurveyReader("pyxform/tests/specify_other.xls") d = excel_reader.to_dict() survey = create_survey_element_from_dict(d) expected_dict = { u'name': 'specify_other', u'type': u'survey', u'children': [{ u'name': u'sex', u'label': { u'English': u'What sex are you?' }, u'type': u'select one', u'children': [{ u'name': u'male', u'label': { u'English': u'Male' } }, { u'name': u'female', u'label': { u'English': u'Female' } }, { u'name': u'other', u'label': u'Other' }] }, { u'name': u'sex_other', u'bind': { u'relevant': u"selected(../sex, 'other')" }, u'label': u'Specify other.', u'type': u'text' }] } self.assertEqual(survey.to_dict(), expected_dict)
def clean_xform_file(self): """ Clean & checks the validity of the uploaded xform file. """ data = self.cleaned_data['xform_file'] if data is None: return None name, file_ext = os.path.splitext(data.name) # Parse file depending on file type if file_ext == '.xls': survey = SurveyReader(data) elif file_ext == '.xml': survey = XFormReader(data) else: raise forms.ValidationError('Unable to read XForm') return survey.to_json_dict()
def test_simple_yes_or_no_question(self): x = SurveyReader(utils.path_to_text_fixture("yes_or_no_question.xls")) x_results = x.to_json_dict() expected_dict = [ { u'label': {u'english': u'have you had a good day today?'}, u'type': u'select one', u'name': u'good_day', u'choices': [ { u'label': {u'english': u'yes'}, u'name': u'yes' }, { u'label': {u'english': u'no'}, u'name': u'no' } ] } ] self.assertEqual(x_results[u"children"], expected_dict)