def test_xls_to_csv(self): specify_other_xls = utils.path_to_text_fixture("specify_other.xls") converted_xls = convert_file_to_csv_string(specify_other_xls) specify_other_csv = utils.path_to_text_fixture("specify_other.csv") converted_csv = convert_file_to_csv_string(specify_other_csv) # print "csv:" # print converted_csv # print "xls:" # print converted_xls self.assertEqual(converted_csv, converted_xls)
def test_equivalency(self): equivalent_fixtures = ['group', 'loop', #'gps', 'specify_other', 'include', 'text_and_integer', \ 'include_json', 'yes_or_no_question'] for fixture in equivalent_fixtures: xls_path = utils.path_to_text_fixture("%s.xls" % fixture) csv_path = utils.path_to_text_fixture("%s.csv" % fixture) xls_inp = xls_to_dict(xls_path) csv_inp = csv_to_dict(csv_path) self.assertEqual(csv_inp, xls_inp)
def test_equivalency(self): equivalent_fixtures = ['group', 'loop', #'gps', 'specify_other', 'include', 'text_and_integer', \ 'include_json', 'yes_or_no_question'] for fixture in equivalent_fixtures: xls_path = utils.path_to_text_fixture("%s.xls" % fixture) csv_path = utils.path_to_text_fixture("%s.csv" % fixture) xls_inp = xls_to_dict(xls_path) csv_inp = csv_to_dict(csv_path) self.maxDiff = None self.assertEqual(csv_inp, xls_inp)
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_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_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 setUp(self): self.excel_files = [ "gps.xls", #"include.xls", "specify_other.xls", "loop.xls", "text_and_integer.xls", # todo: this is looking for json that is created (and # deleted) by another test, is should just add that json # to the directory. # "include_json.xls", "simple_loop.xls", "yes_or_no_question.xls", "xlsform_spec_test.xlsx", "group.xls", ] self.surveys = {} self.this_directory = os.path.dirname(__file__) for filename in self.excel_files: path = utils.path_to_text_fixture(filename) try: self.surveys[filename] = create_survey_from_path(path) except Exception as e: print("Error on : " + filename) raise e
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_json(self): x = SurveyReader(utils.path_to_text_fixture("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_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 runTest(self): files_to_test = ["instance_xmlns_test.xls"] for file_to_test in files_to_test: path_to_excel_file = utils.path_to_text_fixture(file_to_test) #Get the xform output path: directory, filename = os.path.split(path_to_excel_file) root_filename, ext = os.path.splitext(filename) path_to_output_xform = os.path.join(directory, root_filename + "_output.xml") path_to_expected_xform = os.path.join(directory, root_filename + ".xml") #Do the conversion: json_survey = xls2json.parse_file_to_json(path_to_excel_file) survey = pyxform.create_survey_element_from_dict(json_survey) survey.print_xform_to_file(path_to_output_xform) #Compare with the expected output: with codecs.open(path_to_expected_xform, 'rb', encoding="utf-8") as expected_file: expected = etree.fromstring(expected_file.read()) result = etree.fromstring(survey.to_xml()) reporter = lambda x: sys.stdout.write(x + "\n") self.assertTrue( xml_compare(expected, result, reporter=reporter)) os.remove(path_to_output_xform)
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_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_new_widgets(self): survey = utils.build_survey('widgets.xls') path = utils.path_to_text_fixture('widgets.xml') survey.to_xml with open(path) as f: expected = etree.fromstring(survey.to_xml()) result = etree.fromstring(f.read()) self.assertTrue(xml_compare(expected, result))
def test_create_from_path(self): path = utils.path_to_text_fixture("area.xlsx") survey = create_survey_from_path(path) path = os.path.join( os.path.dirname(__file__), 'test_expected_output', 'area.xml') with codecs.open(path, encoding='utf-8') as f: self.assertMultiLineEqual(survey.to_xml(), f.read())
def runTest(self): path_to_excel_file = utils.path_to_text_fixture("warnings.xls") warnings = [] xls2json.parse_file_to_json(path_to_excel_file, warnings=warnings) #print '\n'.join(warnings) self.assertEquals(len(warnings), 17, "Found " + str(len(warnings)) + " warnings")
def test_equivalency(self): equivalent_fixtures = [ "group", "loop", #'gps', "specify_other", "include", "text_and_integer", "include_json", "yes_or_no_question", ] for fixture in equivalent_fixtures: xls_path = utils.path_to_text_fixture("%s.xls" % fixture) csv_path = utils.path_to_text_fixture("%s.csv" % fixture) xls_inp = xls_to_dict(xls_path) csv_inp = csv_to_dict(csv_path) self.maxDiff = None self.assertEqual(csv_inp, xls_inp)
def test_a_unicode_csv_works(self): """ Simply tests that xls2json_backends.csv_to_dict does not have a problem with a csv with unicode characters """ utf_csv_path = utils.path_to_text_fixture("utf_csv.csv") dict_value = csv_to_dict(utf_csv_path) self.assertTrue("\ud83c" in json.dumps(dict_value))
def test_for_loop(self): path = utils.path_to_text_fixture('for_loop.xls') survey = create_survey_from_xls(path) self.maxDiff = None try: actual_xml = survey.to_xml() except ValidationError: self.fail("survey.to_xml() raised ValidationError.")
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 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 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 runTest(self): path_to_excel_file = utils.path_to_text_fixture("xlsform_spec_test.xls") #Get the xform output path: directory, filename = os.path.split(path_to_excel_file) root_filename, ext = os.path.splitext(filename) path_to_xform = os.path.join(directory, root_filename + ".xml") #Do the conversion: json_survey = xls2json.parse_file_to_json(path_to_excel_file) survey = pyxform.create_survey_element_from_dict(json_survey) survey.print_xform_to_file(path_to_xform) #Compare with the expected output: expected_path = utils.path_to_text_fixture("spec_test_expected_output.xml") with codecs.open(expected_path, 'rb', encoding="utf-8") as expected_file: expected = etree.fromstring(expected_file.read()) result = etree.fromstring(survey.to_xml()) self.assertTrue(xml_compare(expected, result))
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_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_create_from_path(self): path = utils.path_to_text_fixture("osm.xlsx") survey = create_survey_from_path(path) path = os.path.join(os.path.dirname(__file__), 'test_expected_output', 'osm.xml') with codecs.open(path, encoding='utf-8') as f: expected_xml = f.read() self.assertXFormEqual(survey.to_xml(), expected_xml) survey = create_survey_element_from_json(survey.to_json()) self.assertXFormEqual(survey.to_xml(), expected_xml)
def test_create_from_path(self): path = utils.path_to_text_fixture("file_type.xlsx") survey = create_survey_from_path(path) path = os.path.join( os.path.dirname(__file__), 'test_expected_output', 'file_type.xml') with codecs.open(path, encoding='utf-8') as f: expected_xml = f.read() self.assertXFormEqual(survey.to_xml(), expected_xml) survey = create_survey_element_from_json(survey.to_json()) self.assertXFormEqual(survey.to_xml(), expected_xml)
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_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_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_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_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")) 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_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_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_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 runTest(self): files_to_test = ["instance_xmlns_test.xls"] for file_to_test in files_to_test: path_to_excel_file = utils.path_to_text_fixture(file_to_test) #Get the xform output path: directory, filename = os.path.split(path_to_excel_file) root_filename, ext = os.path.splitext(filename) path_to_output_xform = os.path.join(directory, root_filename + "_output.xml") path_to_expected_xform = os.path.join(directory, root_filename + ".xml") #Do the conversion: json_survey = xls2json.parse_file_to_json(path_to_excel_file) survey = pyxform.create_survey_element_from_dict(json_survey) survey.print_xform_to_file(path_to_output_xform) #Compare with the expected output: with codecs.open(path_to_expected_xform, 'rb', encoding="utf-8") as expected_file: expected = etree.fromstring(expected_file.read()) result = etree.fromstring(survey.to_xml()) reporter = lambda x: sys.stdout.write(x + "\n") self.assertTrue(xml_compare(expected, result, reporter=reporter)) os.remove(path_to_output_xform)
def test_create_from_file_object(self): path = utils.path_to_text_fixture('yes_or_no_question.xls') with open(path) as f: s = create_survey_from_xls(f)
def setUp(self): self.survey = create_survey_from_xls( utils.path_to_text_fixture("yes_or_no_question.xls"))
def test_loop(self): path = utils.path_to_text_fixture('another_loop.xls') survey = create_survey_from_xls(path) self.maxDiff = None expected_dict = { u'name': u'another_loop', u'id_string': u'another_loop', u'sms_keyword': u'another_loop', u'default_language': u'default', u'title': u'another_loop', u'type': u'survey', u'children': [{ u'name': u'loop_vehicle_types', u'type': u'group', u'children': [{ u'label': { u'English': u'Car', u'French': u'Voiture' }, u'name': u'car', u'type': u'group', u'children': [{ u'label': { u'English': u'How many do you have?', u'French': u'Combien avoir?' }, u'name': u'total', u'type': u'integer' }, { u'bind': { u'constraint': u'. <= ../total' }, u'label': { u'English': u'How many are working?', u'French': u'Combien marcher?' }, u'name': u'working', u'type': u'integer' }], }, { u'label': { u'English': u'Motorcycle', u'French': u'Moto' }, u'name': u'motor_cycle', u'type': u'group', u'children': [{ u'label': { u'English': u'How many do you have?', u'French': u'Combien avoir?' }, u'name': u'total', u'type': u'integer' }, { u'bind': { u'constraint': u'. <= ../total' }, u'label': { u'English': u'How many are working?', u'French': u'Combien marcher?' }, u'name': u'working', u'type': u'integer' }], }] }, { 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.assertEquals(survey.to_json_dict(), expected_dict)
def test_loop(self): path = utils.path_to_text_fixture('another_loop.xls') survey = create_survey_from_xls(path) self.maxDiff = None expected_dict = { u'name': u'another_loop', u'id_string': u'another_loop', u'default_language': u'default', u'title': u'another_loop', u'type': u'survey', u'children': [{ u'label': { u'English': u'Car', u'French': u'Voiture' }, u'name': u'car', u'type': u'group', u'children': [{ u'label': { u'English': u'How many do you have?', u'French': u'Combien avoir?' }, u'name': u'total', u'type': u'integer' }, { u'bind': { u'constraint': u'. <= ../total' }, u'label': { u'English': u'How many are working?', u'French': u'Combien marcher?' }, u'name': u'working', u'type': u'integer' }], }, { u'label': { u'English': u'Motorcycle', u'French': u'Moto' }, u'name': u'motor_cycle', u'type': u'group', u'children': [{ u'label': { u'English': u'How many do you have?', u'French': u'Combien avoir?' }, u'name': u'total', u'type': u'integer' }, { u'bind': { u'constraint': u'. <= ../total' }, u'label': { u'English': u'How many are working?', u'French': u'Combien marcher?' }, u'name': u'working', u'type': u'integer' }], }], } self.assertEquals(survey.to_json_dict(), expected_dict)
def tearDown(self): import os os.remove(utils.path_to_text_fixture("how_old_are_you.json"))
def test_create_from_file_object(self): path = utils.path_to_text_fixture('yes_or_no_question.xls') with open(path) as f: create_survey_from_xls(f)
def load_xlsx_file(): x = SurveyReader( utils.path_to_text_fixture("text_and_integer_xlsx.xlsx"))
def setUp(self): self.path = utils.path_to_text_fixture("settings.xls")