def _update_report_item(self, **update_props): """ Update the text for each element at the configured path if attribute matches """ tree_to_update = update_props['tree_to_update'] prop = update_props['prop'] values = wrap_value(update_props['values']) xroot = self._get_xroot_for(prop) attr_key = 'type' attr_val = u'' if prop == 'attribute_accuracy': attr_val = 'DQQuanAttAcc' elif prop == 'dataset_completeness': attr_val = 'DQCompOm' # Clear (make empty) all elements of the appropriate type for elem in get_elements(tree_to_update, xroot): if get_element_attributes(elem).get(attr_key) == attr_val: clear_element(elem) # Remove all empty elements, including those previously cleared remove_empty_element(tree_to_update, xroot) # Insert elements with correct attributes for each new value attrs = {attr_key: attr_val} updated = [] for idx, value in enumerate(values): elem = insert_element(tree_to_update, idx, xroot, **attrs) updated.append(insert_element(elem, idx, 'measDesc', value)) return updated
def test_iso_parser(self): """ Tests behavior unique to the ISO parser """ # Remove the attribute details href attribute iso_element = get_remote_element(self.iso_file) for citation_element in get_elements(iso_element, _iso_tag_formats["_attr_citation"]): removed = remove_element_attributes(citation_element, "href") # Assert that the href attribute was removed and a different one was read in iso_parser = IsoParser(element_to_string(iso_element)) attribute_href = iso_parser._attr_details_file_url self.assertIsNotNone(removed, "ISO file URL was not removed") self.assertIsNotNone(attribute_href, "ISO href attribute was not read in") self.assertNotEqual(attribute_href, removed, "ISO href attribute is the same as the one removed") # Remove the attribute details linkage attribute iso_element = get_remote_element(self.iso_file) for linkage_element in get_elements(iso_element, _iso_tag_formats["_attr_contact_url"]): removed = get_element_text(linkage_element) clear_element(linkage_element) # Assert that the linkage URL was removed and a different one was read in iso_parser = IsoParser(element_to_string(iso_element)) linkage_url = iso_parser._attr_details_file_url self.assertIsNotNone(removed, "ISO linkage URL was not removed") self.assertIsNotNone(linkage_url, "ISO linkage URL was not read in") self.assertNotEqual(linkage_url, removed, "ISO file URL is the same as the one removed") # Change the href attribute so that it is invalid for citation_element in get_elements(iso_element, _iso_tag_formats["_attr_citation"]): removed = set_element_attributes(citation_element, href="neither url nor file") # Assert that the href attribute was removed and a different one was read in iso_parser = IsoParser(element_to_string(iso_element)) attributes = iso_parser.attributes self.assertIsNone(iso_parser._attr_details_file_url, "Invalid URL stored with parser") self.assertEqual( attributes, TEST_METADATA_VALUES[ATTRIBUTES], "Invalid parsed attributes: {0}".format(attributes) )
def test_fgdc_parser(self): """ Tests behavior unique to the FGDC parser """ # Test dates structure defaults # Remove multiple dates to ensure range is queried fgdc_element = get_remote_element(self.fgdc_file) remove_element(fgdc_element, "idinfo/timeperd/timeinfo/mdattim", True) # Assert that the backup dates are read in successfully fgdc_parser = FgdcParser(element_to_string(fgdc_element)) self.assertEqual(fgdc_parser.dates, {"type": "range", "values": ["Date Range Start", "Date Range End"]}) # Test contact data structure defaults contacts_def = get_complex_definitions()[CONTACTS] # Remove the contact organization completely fgdc_element = get_remote_element(self.fgdc_file) for contact_element in get_elements(fgdc_element, "idinfo/ptcontac"): if element_exists(contact_element, "cntinfo/cntorgp"): clear_element(contact_element) # Assert that the contact organization has been read in fgdc_parser = FgdcParser(element_to_string(fgdc_element)) for key in contacts_def: for contact in fgdc_parser.contacts: self.assertIsNotNone(contact[key], "Failed to read contact.{0}".format(key)) # Remove the contact person completely fgdc_element = get_remote_element(self.fgdc_file) for contact_element in get_elements(fgdc_element, "idinfo/ptcontac"): if element_exists(contact_element, "cntinfo/cntperp"): clear_element(contact_element) # Assert that the contact organization has been read in fgdc_parser = FgdcParser(element_to_string(fgdc_element)) for key in contacts_def: for contact in fgdc_parser.contacts: self.assertIsNotNone(contact[key], "Failed to read updated contact.{0}".format(key))