def testCreateMets(self): metsgen = MetsGenerator(TestMetsCreation.test_ip_dir) mets_data = {'packageid': '996ed635-3e13-4ee5-8e5b-e9661e1d9a93', 'type': 'AIP', 'schemas': 'schemas', 'parent': None} metsgen.createMets(mets_data, TestMetsCreation.mets_file_path) self.assertTrue(os.path.exists(TestMetsCreation.mets_file_path)) pm = ParsedMets(TestMetsCreation.test_ip_dir) pm.load_mets(TestMetsCreation.mets_file_path) obj_id = pm.get_obj_id() self.assertEqual("996ed635-3e13-4ee5-8e5b-e9661e1d9a93", obj_id)
def get_mets_obj_id(mets_file_path): """ Get identifier from mets file @type mets_file_path: str @param mets_file_path: METS file path @rtype: str @return: Object identifier """ package_path, file_name = os.path.split(mets_file_path) pm = ParsedMets(package_path) pm.load_mets(mets_file_path) return str(pm.get_obj_id())
class TestParsedMets(unittest.TestCase): test_dir = 'tests/test_resources/metadata/mets/' test_file = test_dir + 'METS_filesec.xml' pmets = ParsedMets(test_dir) pmets.load_mets(test_file) def test_validate_files_size(self): """ Must not validate if the files listed in the METS file does not match the actual file size """ actual = self.pmets.mets_tree.getroot().tag self.assertEqual(actual, "{http://www.loc.gov/METS/}mets", "Root tag 'mets' not found") def test_get_file_elements(self): """ File elements list must have one file element """ file_elements = self.pmets.get_file_elements() self.assertIsNotNone(file_elements, "File elements must not be None") self.assertEqual(len(file_elements), 1, "File elements list must have one file element") def test_get_first_file_element(self): """ Must return first file element """ file_element = self.pmets.get_first_file_element() self.assertEqual(file_element.tag, "{http://www.loc.gov/METS/}file", "Must return a file element") def test_get_package_type(self): """ Must return first file element """ self.assertEqual("SIP", self.pmets.get_package_type())
def getFileElements(self, deliveryDir, delivery_xml_file, schema_file): log = [] log.append("Validating delivery: %s using schema: %s" % (delivery_xml_file, schema_file)) try: # Parse the XML file, get the root element parsed_mets = ParsedMets(deliveryDir) parsed_mets.load_mets(delivery_xml_file) # If the XSD file wasn't found, extract location from the XML if schema_file == None: schema_file = parsed_mets.get_mets_schema_from_schema_location() # Parse the XSD file parsed_sfile = etree.parse(schema_file) # Validate the delivery XML file xmlVal = XmlValidation() validation_result = xmlVal.validate_XML(parsed_mets.mets_tree, parsed_sfile) if validation_result: return parsed_mets.get_file_elements() except etree.XMLSyntaxError as why: logger.error('Error validating delivery %s, why: %s' % (delivery_xml_file, str(why))) return None