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.maxDiff = None self.assertEqual(csv_inp, xls_inp)
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_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 allow_surveys_with_comment_rows(self): """assume that a survey with rows that don't have name, type, or label headings raise warning only""" path = utils.path_to_text_fixture("allow_comment_rows_test.xls") survey = create_survey_from_xls(path) expected_dict = { "default_language": "default", "id_string": "allow_comment_rows_test", "children": [ { "name": "farmer_name", "label": {"English": "First and last name of farmer"}, "type": "text", } ], "name": "allow_comment_rows_test", "_translations": { "English": { "/allow_comment_rows_test/farmer_name:label": { "long": "First and last name of farmer" } } }, "title": "allow_comment_rows_test", "_xpath": { "allow_comment_rows_test": "/allow_comment_rows_test", "farmer_name": "/allow_comment_rows_test/farmer_name", }, "type": "survey", } self.assertEquals(survey.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()) def write_line(x): sys.stdout.write(x + "\n") reporter = write_line self.assertTrue( xml_compare(expected, result, reporter=reporter)) os.remove(path_to_output_xform)
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 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()) def write_line(x): sys.stdout.write(x + "\n") reporter = write_line self.assertTrue(xml_compare( expected, result, reporter=reporter)) os.remove(path_to_output_xform)
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_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 allow_surveys_with_comment_rows(self): """assume that a survey with rows that don't have name, type, or label headings raise warning only""" path = utils.path_to_text_fixture('allow_comment_rows_test.xls') survey = create_survey_from_xls(path) expected_dict = { "default_language": "default", "id_string": "allow_comment_rows_test", "children": [ { "name": "farmer_name", "label": { "English": "First and last name of farmer" }, "type": "text" } ], "name": "allow_comment_rows_test", "_translations": { "English": { "/allow_comment_rows_test/farmer_name:label": { "long": "First and last name of farmer" } } }, "title": "allow_comment_rows_test", "_xpath": { "allow_comment_rows_test": "/allow_comment_rows_test", "farmer_name": "/allow_comment_rows_test/farmer_name" }, "type": "survey" } self.assertEquals(survey.to_json_dict(), 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_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_default_sheet_name_to_survey(self): xls_path = utils.path_to_text_fixture("survey_no_name.xlsx") dict_value = xls_to_dict(xls_path) print(json.dumps(dict_value)) self.assertTrue("survey" in json.dumps(dict_value)) self.assertTrue("state" in json.dumps(dict_value)) self.assertTrue("The State" in json.dumps(dict_value))
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_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_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_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_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_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_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 setUp(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name="age", sms_keyword="age", type="survey") question = InputQuestion(name="age") question.type = "integer" question.label = "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_underscore_warnings(self): """Raise warnings incase there are underscores in column names""" warnings = [] parse_file_to_json(utils.path_to_text_fixture("hidden.xls"), warnings=warnings) self.assertGreater(len(warnings), 0) warning = ("Google Sheets submissions don't allow underscores in the " "column name. If you intend to use Google Sheets " "submissions, replace underscores with hyphens in the " "following names: hidden_test") self.assertIn(warning, warnings)
def setUp(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name="age", sms_keyword="age", type="survey") question = InputQuestion(name="age") question.type = "integer" question.label = "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_underscore_warnings(self): """Raise warnings incase there are underscores in column names""" warnings = [] parse_file_to_json(utils.path_to_text_fixture("hidden.xls"), warnings=warnings) self.assertGreater(len(warnings), 0) warning = ( "Google Sheets submissions don't allow underscores in the " "column name. If you intend to use Google Sheets " "submissions, replace underscores with hyphens in the " "following names: hidden_test" ) self.assertIn(warning, warnings)
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_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 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_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 = [ {"type": "hidden", "name": "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["children"], expected_dict)
def test_gps(self): x = SurveyReader(utils.path_to_text_fixture("gps.xls")) expected_dict = [ {"type": "gps", "name": "location", "label": "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()["children"], expected_dict)
def test_choice_filter_choice_fields(self): """ Test that the choice filter fields appear on children field of json """ choice_filter_survey = SurveyReader( utils.path_to_text_fixture("choice_filter_test.xlsx")) expected_dict = [{ u'choices': [{ u'name': u'texas', u'label': u'Texas' }, { u'name': u'washington', u'label': u'Washington' }], u'type': u'select one', u'name': u'state', u'parameters': {}, u'label': u'state' }, { u'name': u'county', u'parameters': {}, u'choice_filter': u'${state}=cf', u'label': u'county', u'itemset': u'counties', u'choices': [{ u'label': u'King', u'cf': u'washington', u'name': u'king' }, { u'label': u'Pierce', u'cf': u'washington', u'name': u'pierce' }, { u'label': u'King', u'cf': u'texas', u'name': u'king' }, { u'label': u'Cameron', u'cf': u'texas', u'name': u'cameron' }], u'type': u'select one' }, { u'name': u'city', u'parameters': {}, u'choice_filter': u'${county}=cf', u'label': u'city', u'itemset': u'cities', u'choices': [{ u'label': u'Dumont', u'cf': u'king', u'name': u'dumont' }, { u'label': u'Finney', u'cf': u'king', u'name': u'finney' }, { u'label': u'brownsville', u'cf': u'cameron', u'name': u'brownsville' }, { u'label': u'harlingen', u'cf': u'cameron', u'name': u'harlingen' }, { u'label': u'Seattle', u'cf': u'king', u'name': u'seattle' }, { u'label': u'Redmond', u'cf': u'king', u'name': u'redmond' }, { u'label': u'Tacoma', u'cf': u'pierce', u'name': u'tacoma' }, { u'label': u'Puyallup', u'cf': u'pierce', u'name': u'puyallup' }], u'type': u'select one' }, { u'control': { u'bodyless': True }, u'type': u'group', u'name': u'meta', u'children': [{ u'bind': { u'readonly': u'true()', u'calculate': u"concat('uuid:', uuid())" }, u'type': u'calculate', u'name': u'instanceID' }] }] self.assertEqual(choice_filter_survey.to_json_dict()[u"children"], expected_dict)
def test_choice_filter_choice_fields(self): """ Test that the choice filter fields appear on children field of json """ choice_filter_survey = SurveyReader( utils.path_to_text_fixture("choice_filter_test.xlsx") ) expected_dict = [ { "choices": [ {"name": "texas", "label": "Texas"}, {"name": "washington", "label": "Washington"}, ], "type": "select one", "name": "state", "parameters": {}, "label": "state", }, { "name": "county", "parameters": {}, "choice_filter": "${state}=cf", "label": "county", "itemset": "counties", "choices": [ {"label": "King", "cf": "washington", "name": "king"}, {"label": "Pierce", "cf": "washington", "name": "pierce"}, {"label": "King", "cf": "texas", "name": "king"}, {"label": "Cameron", "cf": "texas", "name": "cameron"}, ], "type": "select one", }, { "name": "city", "parameters": {}, "choice_filter": "${county}=cf", "label": "city", "itemset": "cities", "choices": [ {"label": "Dumont", "cf": "king", "name": "dumont"}, {"label": "Finney", "cf": "king", "name": "finney"}, {"label": "brownsville", "cf": "cameron", "name": "brownsville"}, {"label": "harlingen", "cf": "cameron", "name": "harlingen"}, {"label": "Seattle", "cf": "king", "name": "seattle"}, {"label": "Redmond", "cf": "king", "name": "redmond"}, {"label": "Tacoma", "cf": "pierce", "name": "tacoma"}, {"label": "Puyallup", "cf": "pierce", "name": "puyallup"}, ], "type": "select one", }, { "control": {"bodyless": True}, "type": "group", "name": "meta", "children": [ { "bind": { "readonly": "true()", "calculate": "concat('uuid:', uuid())", }, "type": "calculate", "name": "instanceID", } ], }, ] self.assertEqual(choice_filter_survey.to_json_dict()["children"], expected_dict)
def test_choice_filter_choice_fields(self): """ Test that the choice filter fields appear on children field of json """ choice_filter_survey = SurveyReader( utils.path_to_text_fixture("choice_filter_test.xlsx"), default_name="choice_filter_test", ) expected_dict = [ { "choices": [ { "name": "texas", "label": "Texas" }, { "name": "washington", "label": "Washington" }, ], "type": "select one", "name": "state", "list_name": "states", "parameters": {}, "label": "state", }, { "name": "county", "parameters": {}, "choice_filter": "${state}=cf", "label": "county", "itemset": "counties", "list_name": "counties", "choices": [ { "label": "King", "cf": "washington", "name": "king" }, { "label": "Pierce", "cf": "washington", "name": "pierce" }, { "label": "King", "cf": "texas", "name": "king" }, { "label": "Cameron", "cf": "texas", "name": "cameron" }, ], "type": "select one", }, { "name": "city", "parameters": {}, "choice_filter": "${county}=cf", "label": "city", "itemset": "cities", "list_name": "cities", "choices": [ { "label": "Dumont", "cf": "king", "name": "dumont" }, { "label": "Finney", "cf": "king", "name": "finney" }, { "label": "brownsville", "cf": "cameron", "name": "brownsville" }, { "label": "harlingen", "cf": "cameron", "name": "harlingen" }, { "label": "Seattle", "cf": "king", "name": "seattle" }, { "label": "Redmond", "cf": "king", "name": "redmond" }, { "label": "Tacoma", "cf": "pierce", "name": "tacoma" }, { "label": "Puyallup", "cf": "pierce", "name": "puyallup" }, ], "type": "select one", }, { "control": { "bodyless": True }, "type": "group", "name": "meta", "children": [{ "bind": { "readonly": "true()", "jr:preload": "uid" }, "type": "calculate", "name": "instanceID", }], }, ] self.assertEqual(choice_filter_survey.to_json_dict()["children"], expected_dict)
class XLS2XFormTests(TestCase): survey_package = { "id_string": "test_2011_08_29b", "name_of_main_section": "gps", "sections": { "gps": { "children": [{ "name": "location", "type": "gps" }], "name": "gps", "type": "survey", } }, "title": "test", } survey = pyxform.create_survey(**survey_package) def test_create_parser_without_args(self): """Should exit when no args provided.""" with self.assertRaises(SystemExit): _create_parser().parse_args([]) def test_create_parser_optional_output_path(self): """ Should run fine for a single argument i.e. that is the path to the xlsx file path, while the output path is left out """ try: _create_parser().parse_args(["/some/path/tofile.xlsx"]) except SystemExit: self.fail() def test_create_parser_with_args(self): """Should parse the provided arguments.""" arg_xlsform = "xlsform.xlsx" arg_output = "." arg_list = [ "--json", "--skip_validate", "--pretty_print", arg_xlsform, arg_output, ] args = _create_parser().parse_args(arg_list) self.assertEqual(arg_xlsform, args.path_to_XLSForm) self.assertEqual(arg_output, args.output_path) self.assertEqual(True, args.json) self.assertEqual(False, args.skip_validate) self.assertEqual(True, args.pretty_print) def test_create_parser_file_name_with_space(self): """Should interpret the path correctly.""" arg_xlsform = "some/path/my xlsform.xlsx" arg_output = "." arg_list = [arg_xlsform, arg_output] args = _create_parser().parse_args(arg_list) self.assertEqual(arg_xlsform, args.path_to_XLSForm) def test_create_parser_json_default_false(self): """Should have json=False if not specified.""" arg_xlsform = "xlsform.xlsx" arg_output = "." arg_list = [arg_xlsform, arg_output] args = _create_parser().parse_args(arg_list) self.assertEqual(False, args.json) def test_create_parser_skip_validate_default_true(self): """Should have skip_validate=True if not specified.""" arg_xlsform = "xlsform.xlsx" arg_output = "." arg_list = [arg_xlsform, arg_output] args = _create_parser().parse_args(arg_list) self.assertEqual(True, args.skip_validate) def test_create_parser_no_enketo_default_false(self): """Should have enketo_validate=False if not specified.""" arg_xlsform = "xlsform.xlsx" arg_output = "." arg_list = [arg_xlsform, arg_output] args = _create_parser().parse_args(arg_list) self.assertEqual(False, args.enketo_validate) def test_create_parser_pretty_print_default_False(self): """Should have pretty_print=False if not specified.""" args = _create_parser().parse_args(["xlsform.xlsx", "."]) self.assertFalse(args.pretty_print) def test_validator_args_logic_skip_validate_alone(self): """Should deactivate both validators.""" raw_args = _create_parser().parse_args( ["xlsform.xlsx", ".", "--skip_validate"]) args = _validator_args_logic(args=raw_args) self.assertEqual(False, args.odk_validate) self.assertEqual(False, args.enketo_validate) def test_validator_args_logic_odk_default(self): """Should activate ODK only.""" raw_args = _create_parser().parse_args(["xlsform.xlsx", "."]) args = _validator_args_logic(args=raw_args) self.assertEqual(True, args.odk_validate) self.assertEqual(False, args.enketo_validate) def test_validator_args_logic_enketo_only(self): """Should activate Enketo only.""" raw_args = _create_parser().parse_args( ["xlsform.xlsx", ".", "--enketo_validate"]) args = _validator_args_logic(args=raw_args) self.assertEqual(False, args.odk_validate) self.assertEqual(True, args.enketo_validate) def test_validator_args_logic_odk_only(self): """Should activate ODK only.""" raw_args = _create_parser().parse_args( ["xlsform.xlsx", ".", "--odk_validate"]) args = _validator_args_logic(args=raw_args) self.assertEqual(True, args.odk_validate) self.assertEqual(False, args.enketo_validate) def test_validator_args_logic_odk_and_enketo(self): """Should activate ODK and Enketo.""" raw_args = _create_parser().parse_args( ["xlsform.xlsx", ".", "--odk_validate", "--enketo_validate"]) args = _validator_args_logic(args=raw_args) self.assertEqual(True, args.odk_validate) self.assertEqual(True, args.enketo_validate) def test_validator_args_logic_skip_validate_override(self): """Should deactivate both validators""" raw_args = _create_parser().parse_args([ "xlsform.xlsx", ".", "--skip_validate", "--odk_validate", "--enketo_validate", ]) args = _validator_args_logic(args=raw_args) self.assertEqual(False, args.odk_validate) self.assertEqual(False, args.enketo_validate) @mock.patch( "argparse.ArgumentParser.parse_args", return_value=argparse.Namespace( path_to_XLSForm="xlsform.xlsx", output_path=None, json=False, skip_validate=False, odk_validate=False, enketo_validate=False, pretty_print=False, ), ) @mock.patch("pyxform.xls2xform.xls2xform_convert") def test_xls2form_convert_parameters(self, converter_mock, parser_mock_args): """ Checks that xls2xform_convert is given the right arguments, when the output-path is not given """ converter_mock.return_value = "{}" main_cli() converter_mock.assert_called_once_with( xlsform_path="xlsform.xlsx", xform_path="xlsform.xml", validate=False, pretty_print=False, enketo=False, ) @mock.patch( "argparse.ArgumentParser.parse_args", return_value=argparse.Namespace( path_to_XLSForm="xlsform.xlsx", output_path=None, json=True, skip_validate=False, odk_validate=False, enketo_validate=False, pretty_print=False, ), ) @mock.patch("pyxform.xls2xform.xls2xform_convert") def test_xls2xform_convert_params_with_flags(self, converter_mock, parser_mock_args): """ Should call xlsform_convert with the correct input for output path where only the xlsform input path and json flag were provided, since the xlsform-convert can be called if json flag was set or when not """ converter_mock.return_value = "{}" main_cli() converter_mock.assert_called_once_with( xlsform_path="xlsform.xlsx", xform_path="xlsform.xml", validate=False, pretty_print=False, enketo=False, ) @mock.patch( "argparse.ArgumentParser.parse_args", return_value=argparse.Namespace( path_to_XLSForm=path_to_text_fixture("bad_calc.xlsx"), output_path=None, json=False, skip_validate=True, odk_validate=True, enketo_validate=True, pretty_print=True, ), ) def test_xls2xform_convert_throwing_odk_error(self, parser_mock_args): """ Parse and validate bad_calc.xlsx """ logger = logging.getLogger("pyxform.xls2xform") with mock.patch.object(logger, "error") as mock_debug: main_cli() self.assertEqual(mock_debug.call_count, 1) def test_get_xml_path_function(self): """Should return an xml path in the same directory as the xlsx file""" xlsx_path = "/home/user/Desktop/xlsform.xlsx" expected = "/home/user/Desktop/xlsform.xml" assert expected == get_xml_path(xlsx_path) # check that it also handles spaced routes xlsx_path = "/home/user/Desktop/my xlsform.xlsx" expected = "/home/user/Desktop/my xlsform.xml" assert expected == get_xml_path(xlsx_path)
def test_loop(self): path = utils.path_to_text_fixture("another_loop.xls") survey = create_survey_from_xls(path) self.maxDiff = None expected_dict = { "name": "another_loop", "id_string": "another_loop", "sms_keyword": "another_loop", "default_language": "default", "title": "another_loop", "type": "survey", "children": [ { "name": "loop_vehicle_types", "type": "group", "children": [ { "label": {"English": "Car", "French": "Voiture"}, "name": "car", "type": "group", "children": [ { "label": { "English": "How many do you have?", "French": "Combien avoir?", }, "name": "total", "type": "integer", }, { "bind": {"constraint": ". <= ../total"}, "label": { "English": "How many are working?", "French": "Combien marcher?", }, "name": "working", "type": "integer", }, ], }, { "label": {"English": "Motorcycle", "French": "Moto"}, "name": "motor_cycle", "type": "group", "children": [ { "label": { "English": "How many do you have?", "French": "Combien avoir?", }, "name": "total", "type": "integer", }, { "bind": {"constraint": ". <= ../total"}, "label": { "English": "How many are working?", "French": "Combien marcher?", }, "name": "working", "type": "integer", }, ], }, ], }, { "children": [ { "bind": {"jr:preload": "uid", "readonly": "true()"}, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ], } self.assertEquals(survey.to_json_dict(), expected_dict)
def setUp(self): self.survey = create_survey_from_xls( utils.path_to_text_fixture("yes_or_no_question.xls"))
def setUp(self): self.path = utils.path_to_text_fixture("settings.xls")
def setUp(self): self.survey = create_survey_from_xls( utils.path_to_text_fixture("yes_or_no_question.xls"))
def tearDown(self): fixture_path = utils.path_to_text_fixture("how_old_are_you.json") if os.path.exists(fixture_path): os.remove(fixture_path)
def test_create_from_file_object(self): path = utils.path_to_text_fixture('yes_or_no_question.xls') with open(path, 'rb') as f: create_survey_from_xls(f)
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_create_from_file_object(self): path = utils.path_to_text_fixture("yes_or_no_question.xls") with open(path, "rb") as f: create_survey_from_xls(f)
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 = { "name": "another_loop", "id_string": "another_loop", "sms_keyword": "another_loop", "default_language": "default", "title": "another_loop", "type": "survey", "children": [ { "name": "loop_vehicle_types", "type": "group", "children": [ { "label": {"English": "Car", "French": "Voiture"}, "name": "car", "type": "group", "children": [ { "label": { "English": "How many do you have?", "French": "Combien avoir?", }, "name": "total", "type": "integer", }, { "bind": {"constraint": ". <= ../total"}, "label": { "English": "How many are working?", "French": "Combien marcher?", }, "name": "working", "type": "integer", }, ], }, { "label": {"English": "Motorcycle", "French": "Moto"}, "name": "motor_cycle", "type": "group", "children": [ { "label": { "English": "How many do you have?", "French": "Combien avoir?", }, "name": "total", "type": "integer", }, { "bind": {"constraint": ". <= ../total"}, "label": { "English": "How many are working?", "French": "Combien marcher?", }, "name": "working", "type": "integer", }, ], }, ], }, { "children": [ { "bind": { "calculate": "concat('uuid:', uuid())", "readonly": "true()", }, "name": "instanceID", "type": "calculate", } ], "control": {"bodyless": True}, "name": "meta", "type": "group", }, ], } self.assertEquals(survey.to_json_dict(), expected_dict)
def tearDown(self): fixture_path = utils.path_to_text_fixture("how_old_are_you.json") if os.path.exists(fixture_path): os.remove(fixture_path)
def test_create_from_path(self): path = utils.path_to_text_fixture("tutorial.xls") create_survey_from_path(path)