Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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, default_name=root_filename)
            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_equivalency(self):
     equivalent_fixtures = [
         "group",
         "loop",
         "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)
         xlsx_path = utils.path_to_text_fixture("%s.xlsx" % fixture)
         xls_inp = xls_to_dict(xls_path)
         xlsx_inp = xlsx_to_dict(xlsx_path)
         self.maxDiff = None
         self.assertEqual(xls_inp, xlsx_inp)
Esempio n. 5
0
 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))
Esempio n. 6
0
 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")
     )
Esempio n. 7
0
    def test_equality_of_to_dict(self):
        x = SurveyReader(utils.path_to_text_fixture("group.xls"),
                         default_name="group")
        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)
Esempio n. 8
0
 def setUp(self):
     self.excel_files = [
         "gps.xls",
         # "include.xls",
         "specify_other.xls",
         "group.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",
     ]
     self.surveys = {}
     self.this_directory = os.path.dirname(__file__)
     for filename in self.excel_files:
         path = utils.path_to_text_fixture(filename)
         self.surveys[filename] = create_survey_from_path(path)
Esempio n. 9
0
    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)
Esempio n. 10
0
 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)
Esempio n. 11
0
 def test_create_from_path(self):
     path = utils.path_to_text_fixture("tutorial.xls")
     create_survey_from_path(path)
Esempio n. 12
0
 def test_json(self):
     x = SurveyReader(utils.path_to_text_fixture("group.xls"),
                      default_name="group")
     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)
Esempio n. 13
0
 def test_loop(self):
     path = utils.path_to_text_fixture("another_loop.xls")
     survey = create_survey_from_xls(path, "another_loop")
     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)
Esempio n. 14
0
 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)
Esempio n. 15
0
 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)
Esempio n. 16
0
 def setUp(self):
     self.path = utils.path_to_text_fixture("settings.xls")
Esempio n. 17
0
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)
Esempio n. 18
0
 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)
     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))
Esempio n. 19
0
    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)
Esempio n. 20
0
 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)
     self.assertEqual(converted_csv, converted_xls)