def test_arcgis_parser(self): """ Tests behavior unique to the FGDC parser """ # Test dates structure defaults # Remove multiple dates to ensure range is queried arcgis_element = get_remote_element(self.arcgis_file) remove_element(arcgis_element, "dataIdInfo/dataExt/tempEle/TempExtent/exTemp/TM_Instant", True) # Assert that the backup dates are read in successfully arcgis_parser = ArcGISParser(element_to_string(arcgis_element)) self.assertEqual(arcgis_parser.dates, {"type": "range", "values": ["Date Range Start", "Date Range End"]}) # Remove one of the date range values and assert that only the end date is read in as a single remove_element(arcgis_element, "dataIdInfo/dataExt/tempEle/TempExtent/exTemp/TM_Period/tmBegin", True) arcgis_parser = ArcGISParser(element_to_string(arcgis_element)) self.assertEqual(arcgis_parser.dates, {"type": "single", "values": ["Date Range End"]}) # Remove the last of the date range values and assert that no dates are read in remove_element(arcgis_element, "dataIdInfo/dataExt/tempEle/TempExtent/exTemp/TM_Period", True) arcgis_parser = ArcGISParser(element_to_string(arcgis_element)) self.assertEqual(arcgis_parser.dates, {}) # Insert a single date value and assert that only it is read in single_path = "dataIdInfo/dataExt/tempEle/TempExtent/exTemp/TM_Instant/tmPosition" single_text = "Single Date" insert_element(arcgis_element, 0, single_path, single_text) arcgis_parser = ArcGISParser(element_to_string(arcgis_element)) self.assertEqual(arcgis_parser.dates, {"type": "single", "values": [single_text]})
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))
def test_parser_values(self): """ Tests that parsers are populated with the expected values """ arcgis_element = get_remote_element(self.arcgis_file) arcgis_parser = ArcGISParser(element_to_string(arcgis_element)) arcgis_new = ArcGISParser(**TEST_METADATA_VALUES) # Test that the two ArcGIS parsers have the same data given the same input file self.assert_parsers_are_equal(arcgis_parser, arcgis_new) fgdc_element = get_remote_element(self.fgdc_file) fgdc_parser = FgdcParser(element_to_string(fgdc_element)) fgdc_new = FgdcParser(**TEST_METADATA_VALUES) # Test that the two FGDC parsers have the same data given the same input file self.assert_parsers_are_equal(fgdc_parser, fgdc_new) iso_element = get_remote_element(self.iso_file) remove_element(iso_element, _iso_tag_formats["_attr_citation"], True) iso_parser = IsoParser(element_to_string(iso_element)) iso_new = IsoParser(**TEST_METADATA_VALUES) # Test that the two ISO parsers have the same data given the same input file self.assert_parsers_are_equal(iso_parser, iso_new) # Test that all distinct parsers have the same data given equivalent input files self.assert_parsers_are_equal(arcgis_parser, fgdc_parser) self.assert_parsers_are_equal(fgdc_parser, iso_parser) self.assert_parsers_are_equal(iso_parser, arcgis_parser) # Test that each parser's values correspond to the target values for parser in (arcgis_parser, fgdc_parser, iso_parser): parser_type = type(parser) for prop, target in TEST_METADATA_VALUES.items(): self.assert_equal_for(parser_type, prop, getattr(parser, prop), target)
def _parse_attribute_details_file(self, prop=ATTRIBUTES): """ Concatenates a list of Attribute Details data structures parsed from a remote file """ # Parse content from remote file URL, which may be stored in one of two places: # Starting at: contentInfo/MD_FeatureCatalogueDescription/featureCatalogueCitation # ATTRIBUTE: href # ELEMENT TEXT: CI_Citation/.../CI_Contact/onlineResource/CI_OnlineResource/linkage self._attr_details_file_url = parse_property( self._xml_tree, None, self._data_map, '_attributes_file' ) if not self._attr_details_file_url: return None try: tree_to_parse = get_remote_element(self._attr_details_file_url) except Exception: self._attr_details_file_url = None return None xpath_map = self._data_structures[ATTRIBUTES] xpath_root = self._get_xroot_for(prop) return parse_complex_list(tree_to_parse, xpath_root, xpath_map, prop)
def _parse_attribute_details_file(self, prop=ATTRIBUTES): """ Concatenates a list of Attribute Details data structures parsed from a remote file """ # Parse content from remote file URL, which may be stored in one of two places: # Starting at: contentInfo/MD_FeatureCatalogueDescription/featureCatalogueCitation # ATTRIBUTE: href # ELEMENT TEXT: CI_Citation/.../CI_Contact/onlineResource/CI_OnlineResource/linkage self._attr_details_file_url = parse_property( self._xml_tree, None, self._data_map, '_attributes_file' ) if not self._attr_details_file_url: return None try: tree_to_parse = get_remote_element(self._attr_details_file_url) except: self._attr_details_file_url = None return None xpath_map = self._data_structures[ATTRIBUTES] xpath_root = self._get_xroot_for(prop) return parse_complex_list(tree_to_parse, xpath_root, xpath_map, prop)