def add_surveys(self): if not hasattr(self, "_dict_organizer"): self._dict_organizer = DictOrganizer() for i in self._data_dictionary.xform.surveys.iterator(): d = xform_instance_to_dict(i.xml) obs = self._dict_organizer.get_observation_from_dict(d) self.add_obs(obs)
def test_repeat_child_name_matches_repeat(self): """ ParsedInstance.to_dict creates a list within a repeat if a child has the same name as the repeat This test makes sure that doesnt happen """ self.maxDiff = None fixture = "repeat_child_name_matches_repeat" # publish form so we have a dd to pass to xform inst. parser self._publish_xls_fixture_set_xform(fixture) submission_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "fixtures", fixture, fixture + ".xml") # get submission xml str with open(submission_path, "r") as f: xml_str = f.read() dict = xform_instance_to_dict(xml_str, self.xform.data_dictionary()) expected_dict = { u'test_item_name_matches_repeat': { u'formhub': { u'uuid': u'c911d71ce1ac48478e5f8bac99addc4e' }, u'gps': [{ u'info': u'Yo', u'gps': u'-1.2625149 36.7924478 0.0 30.0' }, { u'info': u'What', u'gps': u'-1.2625072 36.7924328 0.0 30.0' }] } } self.assertEqual(dict, expected_dict)
def _test_parse_xform_instance(self): # todo: need to test id string as well for d in self.inputs_and_outputs: self.assertEqual(xform_instance_to_dict(d[XML]), d[DICT]) self.assertEqual(xform_instance_to_flat_dict(d[XML]), d[FLAT_DICT]) flat_dict_with_id = {ID: d[ID]} flat_dict_with_id.update(d[FLAT_DICT]) self.assertEqual(parse_xform_instance(d[XML]), flat_dict_with_id)
def test_parse_xform_instance(self): # todo: need to test id string as well for d in self.inputs_and_outputs: self.assertEqual(xform_instance_to_dict(d[XML]), d[DICT]) self.assertEqual(xform_instance_to_flat_dict(d[XML]), d[FLAT_DICT]) flat_dict_with_id = {ID: d[ID]} flat_dict_with_id.update(d[FLAT_DICT]) self.assertEqual(parse_xform_instance(d[XML]), flat_dict_with_id)
def test_repeat_child_name_matches_repeat(self): """ ParsedInstance.to_dict creates a list within a repeat if a child has the same name as the repeat This test makes sure that doesnt happen """ self.maxDiff = None fixture = "repeat_child_name_matches_repeat" # publish form so we have a dd to pass to xform inst. parser self._publish_xls_fixture_set_xform(fixture) submission_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "fixtures", fixture, fixture + ".xml" ) # get submission xml str with open(submission_path, "r") as f: xml_str = f.read() dict = xform_instance_to_dict(xml_str, self.xform.data_dictionary()) expected_dict = { u'test_item_name_matches_repeat': { u'formhub': { u'uuid': u'c911d71ce1ac48478e5f8bac99addc4e' }, u'gps': [ { u'info': u'Yo', u'gps': u'-1.2625149 36.7924478 0.0 30.0' }, { u'info': u'What', u'gps': u'-1.2625072 36.7924328 0.0 30.0' } ] } } self.assertEqual(dict, expected_dict)
def test_dict_organizer(self): serious_xml = u''' <?xml version=\'1.0\' ?> <household> <number_of_members>10</number_of_members> <man><name>Alex</name></man> <man><name>Bob</name></man> <woman> <name>Carla</name> <child><name>Danny</name></child> <child><name>Ed</name></child> </woman> <woman> <name>Fran</name> <child><name>Greg</name></child> </woman> </household>''' serious_xml = re.sub(r">\s+<", "><", serious_xml) d = xform_instance_to_dict(serious_xml) dict_organizer = DictOrganizer() expected_dict = { u'household': [{ u'_parent_table_name': u'', u'_parent_index': -1, u'number_of_members': u'10', u'_index': 0 }], u'woman': [{ u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Carla', u'_index': 0 }, { u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Fran', u'_index': 1 }], u'man': [{ u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Alex', u'_index': 0 }, { u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Bob', u'_index': 1 }], u'child': [{ u'_parent_table_name': u'woman', u'_parent_index': 0, u'name': u'Danny', u'_index': 0 }, { u'_parent_table_name': u'woman', u'_parent_index': 0, u'name': u'Ed', u'_index': 1 }, { u'_parent_table_name': u'woman', u'_parent_index': 1, u'name': u'Greg', u'_index': 2 }] } self.assertEqual(dict_organizer.get_observation_from_dict(d), expected_dict)
def test_dict_organizer(self): serious_xml = u''' <?xml version=\'1.0\' ?> <household> <number_of_members>10</number_of_members> <man><name>Alex</name></man> <man><name>Bob</name></man> <woman> <name>Carla</name> <child><name>Danny</name></child> <child><name>Ed</name></child> </woman> <woman> <name>Fran</name> <child><name>Greg</name></child> </woman> </household>''' serious_xml = re.sub(r">\s+<", "><", serious_xml) d = xform_instance_to_dict(serious_xml) dict_organizer = DictOrganizer() expected_dict = { u'household': [ {u'_parent_table_name': u'', u'_parent_index': -1, u'number_of_members': u'10', u'_index': 0} ], u'woman': [ {u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Carla', u'_index': 0}, {u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Fran', u'_index': 1} ], u'man': [ {u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Alex', u'_index': 0}, {u'_parent_table_name': u'household', u'_parent_index': 0, u'name': u'Bob', u'_index': 1}], u'child': [ {u'_parent_table_name': u'woman', u'_parent_index': 0, u'name': u'Danny', u'_index': 0}, {u'_parent_table_name': u'woman', u'_parent_index': 0, u'name': u'Ed', u'_index': 1}, {u'_parent_table_name': u'woman', u'_parent_index': 1, u'name': u'Greg', u'_index': 2}]} self.assertEqual( dict_organizer.get_observation_from_dict(d), expected_dict )
def test_dict_organizer(self): self.survey = create_survey_from_xls("odk_viewer/tests/household.xls") json_str = json.dumps(self.survey.to_json_dict()) self.data_dictionary = DataDictionary.objects.create( xml=self.survey.to_xml(), json=json_str, user = self.user) serious_xml = u''' <?xml version='1.0' ?> <household id="serious_survey"> <number_of_members>7</number_of_members> <man> <name>Alex</name> </man> <man> <name>Bob</name> </man> <woman> <name>Carla</name> <child> <name>Danny</name> </child> <child> <name>Ed</name> </child> </woman> <woman> <name>Fran</name> <child> <name>Greg</name> </child> </woman> <ice_cream_flavors> <number_for_vanilla>5</number_for_vanilla> <number_for_strawberry>2</number_for_strawberry> </ice_cream_flavors> </household> ''' serious_xml = re.sub(r">\s+<", "><", serious_xml) d = xform_instance_to_dict(serious_xml, self.data_dictionary) dict_organizer = DictOrganizer() expected_dict = { u'household': [ { u'_parent_index': -1, u'number_of_members': u'7', u'_parent_table_name': u'', u'_index': 0 } ], u'woman': [ { u'_parent_index': 0, u'name': u'Carla', u'_parent_table_name': u'household', u'_index': 0 }, { u'_parent_index': 0, u'name': u'Fran', u'_parent_table_name': u'household', u'_index': 1 } ], u'ice_cream_flavors': [ { u'_parent_index': 0, u'number_for_strawberry': u'2', u'number_for_vanilla': u'5', u'_parent_table_name': u'household', u'_index': 0 } ], u'man': [ { u'_parent_index': 0, u'name': u'Alex', u'_parent_table_name': u'household', u'_index': 0 }, { u'_parent_index': 0, u'name': u'Bob', u'_parent_table_name': u'household', u'_index': 1 } ], u'child': [ { u'_parent_index': 0, u'name': u'Danny', u'_parent_table_name': u'woman', u'_index': 0 }, { u'_parent_index': 0, u'name': u'Ed', u'_parent_table_name': u'woman', u'_index': 1 }, { u'_parent_index': 1, u'name': u'Greg', u'_parent_table_name': u'woman', u'_index': 2 } ] } self.assertEqual( dict_organizer.get_observation_from_dict(d), expected_dict )
def test_dict_organizer(self): self.survey = create_survey_from_xls("odk_viewer/tests/household.xls") json_str = json.dumps(self.survey.to_json_dict()) self.data_dictionary = DataDictionary.objects.create( xml=self.survey.to_xml(), json=json_str, user=self.user) serious_xml = u''' <?xml version='1.0' ?> <household id="serious_survey"> <number_of_members>7</number_of_members> <man> <name>Alex</name> </man> <man> <name>Bob</name> </man> <woman> <name>Carla</name> <child> <name>Danny</name> </child> <child> <name>Ed</name> </child> </woman> <woman> <name>Fran</name> <child> <name>Greg</name> </child> </woman> <ice_cream_flavors> <number_for_vanilla>5</number_for_vanilla> <number_for_strawberry>2</number_for_strawberry> </ice_cream_flavors> </household> ''' serious_xml = re.sub(r">\s+<", "><", serious_xml) d = xform_instance_to_dict(serious_xml, self.data_dictionary) dict_organizer = DictOrganizer() expected_dict = { u'household': [{ u'_parent_index': -1, u'number_of_members': u'7', u'_parent_table_name': u'', u'_index': 0 }], u'woman': [{ u'_parent_index': 0, u'name': u'Carla', u'_parent_table_name': u'household', u'_index': 0 }, { u'_parent_index': 0, u'name': u'Fran', u'_parent_table_name': u'household', u'_index': 1 }], u'ice_cream_flavors': [{ u'_parent_index': 0, u'number_for_strawberry': u'2', u'number_for_vanilla': u'5', u'_parent_table_name': u'household', u'_index': 0 }], u'man': [{ u'_parent_index': 0, u'name': u'Alex', u'_parent_table_name': u'household', u'_index': 0 }, { u'_parent_index': 0, u'name': u'Bob', u'_parent_table_name': u'household', u'_index': 1 }], u'child': [{ u'_parent_index': 0, u'name': u'Danny', u'_parent_table_name': u'woman', u'_index': 0 }, { u'_parent_index': 0, u'name': u'Ed', u'_parent_table_name': u'woman', u'_index': 1 }, { u'_parent_index': 1, u'name': u'Greg', u'_parent_table_name': u'woman', u'_index': 2 }] } self.assertEqual(dict_organizer.get_observation_from_dict(d), expected_dict)