def test_no_file(self): not_file_path = os.path.join(self.tmpdir, 'a.notfile') with self.assertRaises(OSError) as error_catcher: mcp.MCP(path = not_file_path) self.assertEqual('No such file or directory: {}'.format(not_file_path), str(error_catcher.exception))
def test_return_all_uuids(self): mcp_filename = 'a.mcp' mcp_path = os.path.join(self.tmpdir, mcp_filename) mcp_tree = generate_fake_mcp(MCP_LEVEL1, MCP_LEVEL2, MCP_LEVEL3, MCP_LEVEL4A, MCP_LEVEL4B) mcp_tree.write(mcp_path) valid_mcp = mcp.MCP(path = mcp_path) uuids = valid_mcp.get_uuids() self.assertEqual(len(uuids), 3) def is_uuid(value): try: val = uuid.UUID(value) except ValueError: return False return True for choice in uuids: self.assertTrue(len(choice.keys()), 2) for uuid_string in choice.values(): self.assertTrue(is_uuid(uuid_string))
def test_not_xml(self): not_xml_filename = 'a.notxml' not_xml_path = os.path.join(self.tmpdir, not_xml_filename) with open(not_xml_path, 'w+') as f: f.write('"i_am_json": "lol"') with self.assertRaises(mcp.MCPError) as error_catcher: mcp.MCP(path = not_xml_path) self.assertEqual('{} does not contain valid XML'.format(not_xml_filename), str(error_catcher.exception))
def test_not_mcp_third_level(self): not_mcp_filename = 'a.notmcp' not_mcp_path = os.path.join(self.tmpdir, not_mcp_filename) mcp_tree = generate_fake_mcp(MCP_LEVEL1, MCP_LEVEL2, MCP_LEVEL3[:-1], MCP_LEVEL4A, MCP_LEVEL4B) mcp_tree.write(not_mcp_path) with self.assertRaises(mcp.MCPError) as error_catcher: mcp.MCP(path = not_mcp_path) self.assertEqual('{} does not appear to be a valid Archivematica MCP'.format(not_mcp_filename), str(error_catcher.exception))
def test_write_xml(self): mcp_filename = 'a.mcp' mcp_path = os.path.join(self.tmpdir, mcp_filename) mcp_tree = generate_fake_mcp(MCP_LEVEL1, MCP_LEVEL2, MCP_LEVEL3, MCP_LEVEL4A, MCP_LEVEL4B) mcp_tree.write(mcp_path) valid_mcp = mcp.MCP(path = mcp_path) new_path = os.path.join(self.tmpdir, 'a/bunch/of/fake/directories/', mcp_filename) valid_mcp.write(new_path) self.assertTrue(os.path.exists(new_path))
def test_valid_mcp(self): mcp_filename = 'a.mcp' mcp_path = os.path.join(self.tmpdir, mcp_filename) mcp_tree = generate_fake_mcp(MCP_LEVEL1, MCP_LEVEL2, MCP_LEVEL3, MCP_LEVEL4A, MCP_LEVEL4B) mcp_tree.write(mcp_path) valid_mcp = mcp.MCP(path = mcp_path) self.assertEqual(valid_mcp.filename, mcp_filename) self.assertEqual(valid_mcp.path, mcp_path) self.assertTrue(valid_mcp.validate_mcp())
def test_translated_xml_still_valid(self): mcp_filename = 'a.mcp' mcp_path = os.path.join(self.tmpdir, mcp_filename) mcp_tree = generate_fake_mcp(MCP_LEVEL1, MCP_LEVEL2, MCP_LEVEL3, MCP_LEVEL4A, MCP_LEVEL4B) mcp_tree.write(mcp_path) valid_mcp = mcp.MCP(path = mcp_path) uuids = valid_mcp.get_uuids() replacement_dict = {uuids[0]['goToChain']: 'test_label'} valid_mcp.set_uuid_labels(replacement_dict) self.assertTrue(valid_mcp.validate_mcp())
def test_translate_uuids(self): mcp_filename = 'a.mcp' mcp_path = os.path.join(self.tmpdir, mcp_filename) mcp_tree = generate_fake_mcp(MCP_LEVEL1, MCP_LEVEL2, MCP_LEVEL3, MCP_LEVEL4A, MCP_LEVEL4B) mcp_tree.write(mcp_path) valid_mcp = mcp.MCP(path = mcp_path) uuids = valid_mcp.get_uuids() replacement_dict = {uuids[0]['goToChain']: 'test_label'} valid_mcp.set_uuid_labels(replacement_dict) count_labels = etree.XPath("count(//preconfiguredChoice/*/@label)") self.assertTrue(count_labels(valid_mcp.tree), 1) all_labels = etree.XPath("//preconfiguredChoice/goToChain/@label") self.assertTrue(all_labels(valid_mcp.tree), ['test_label'])
parser.add_argument("-o", "--output", help="directory to save output", default='.') return parser if __name__ == '__main__': parser = _make_parser() args = parser.parse_args() mcps = [] if args.dir: directory_path = os.path.abspath(args.dir) else: directory_path = PROCESSING_MCP_PATH for path in glob.glob(os.path.join(directory_path, '*ProcessingMCP.xml')): mcps.append(path) if args.file: mcps.append(os.path.abspath(args.file)) configParser = _parse_db_config() mcp_dict = _collect_db_data(configParser) for path in set(mcps): mcp_data = mcp.MCP(path) mcp_data.set_uuid_labels(mcp_dict) mcp_data.write(args.output)