def setUp(self): """ Sets up necessary files for the tests. """ self.file_path = os.path.join(os.path.dirname(__file__), self.TO_BE_READ_FILE) self.meta_reader = XMLReader(self.file_path)
def __build_operation_from_file(self, project, operation_file): """ Create Operation entity from metadata file. """ reader = XMLReader(operation_file) operation_dict = reader.read_metadata() operation_entity = manager_of_class(model.Operation).new_instance() return operation_entity.from_dict(operation_dict, dao, self.user_id, project.gid)
def test_write_metadata(self): """ Test that an XML file is created and correct data is written in it. """ self.assertFalse(os.path.exists(self.result_path)) self.meta_writer.write(self.result_path) self.assertTrue(os.path.exists(self.result_path)) reader = XMLReader(self.result_path) meta_data = reader.read_metadata() for key, value in MetaDataReadXMLTest.EXPECTED_DICTIONARY.iteritems(): found_value = meta_data[key] self.assertEqual(value, found_value)
class MetaDataReadXMLTest(unittest.TestCase): """ Tests for tvb.core.entities.file.metadatahandler.XMLReader class. """ TO_BE_READ_FILE = "test_read.xml" #Values expected to be read from file EXPECTED_DICTIONARY = { 'status': 'FINISHED', 'gid': '497b3d59-b3c1-11e1-b2e4-68a86d1bd4fa', 'method_name': 'launch', 'user_group': 'cff_74', 'fk_from_algo': json.dumps({ 'classname': 'CFF_Importer', 'identifier': None, 'module': 'tvb.adapters.uploaders.cff_importer' }) } def setUp(self): """ Sets up necessary files for the tests. """ self.file_path = os.path.join(os.path.dirname(__file__), self.TO_BE_READ_FILE) self.meta_reader = XMLReader(self.file_path) def test_read_metadata(self): """ Test that content return by read_metadata matches the actual content of the XML. """ meta_data = self.meta_reader.read_metadata() self.assertTrue(isinstance(meta_data, GenericMetaData)) for key, value in self.EXPECTED_DICTIONARY.iteritems(): found_value = meta_data[key] self.assertEqual(value, found_value) def test_read_gid(self): """ Test that value returned by read_only_element matches the actual value from the XML file. """ read_value = self.meta_reader.read_only_element('gid') self.assertTrue(isinstance(read_value, str)) self.assertEqual(read_value, self.EXPECTED_DICTIONARY['gid'])
def update_operation_metadata(self, project_name, new_group_name, operation_id, is_group=False): """ Update operation meta data. :param is_group: when FALSE, use parameter 'new_group_name' for direct assignment on operation.user_group when TRUE, update operation.operation_group.name = parameter 'new_group_name' """ op_path = self.get_operation_meta_file_path(project_name, operation_id) if not os.path.exists(op_path): self.logger.warning( "Trying to update an operation-meta file which does not exist." " It could happen in a group where partial entities have errors!" ) return op_meta_data = XMLReader(op_path).read_metadata() if is_group: group_meta_str = op_meta_data[ DataTypeMetaData.KEY_FK_OPERATION_GROUP] group_meta = json.loads(group_meta_str) group_meta[ DataTypeMetaData.KEY_OPERATION_GROUP_NAME] = new_group_name op_meta_data[DataTypeMetaData.KEY_FK_OPERATION_GROUP] = json.dumps( group_meta) else: op_meta_data[DataTypeMetaData.KEY_OPERATION_TAG] = new_group_name XMLWriter(op_meta_data).write(op_path)
def test_write_operation_metadata(self): """ Test that a correct XML is created for an operation. """ operation = TestFactory.create_operation(test_user=self.test_user, test_project=self.test_project) expected_file = self.files_helper.get_operation_meta_file_path(self.PROJECT_NAME, operation.id) self.assertFalse(os.path.exists(expected_file)) self.files_helper.write_operation_metadata(operation) self.assertTrue(os.path.exists(expected_file)) operation_meta = XMLReader(expected_file).read_metadata() loaded_operation = model.Operation(None, None, None, None) loaded_operation.from_dict(operation_meta, dao) expected_dict = operation.to_dict()[1] found_dict = loaded_operation.to_dict()[1] for key, value in expected_dict.iteritems(): self.assertEqual(str(value), str(found_dict[key])) # Now validate that operation metaData can be also updated self.assertNotEqual("new_group_name", found_dict['user_group']) self.files_helper.update_operation_metadata(self.PROJECT_NAME, "new_group_name", operation.id) found_dict = XMLReader(expected_file).read_metadata() self.assertEqual("new_group_name", found_dict['user_group'])
def __populate_project(self, project_path): """ Create and store a Project entity. """ self.logger.debug("Creating project from path: %s" % project_path) project_cfg_file = os.path.join(project_path, FilesHelper.TVB_PROJECT_FILE) reader = XMLReader(project_cfg_file) project_dict = reader.read_metadata() project_entity = manager_of_class(model.Project).new_instance() project_entity = project_entity.from_dict(project_dict, self.user_id) try: self.logger.debug("Storing imported project") return dao.store_entity(project_entity) except IntegrityError, excep: self.logger.exception(excep) error_msg = ( "Could not import project: %s with gid: %s. There is already a " "project with the same name or gid.") % (project_entity.name, project_entity.gid) raise ProjectImportException(error_msg)
def test_write_project_metadata(self): """ Write XML for test-project. """ self.files_helper.write_project_metadata(self.test_project) expected_file = self.files_helper.get_project_meta_file_path(self.PROJECT_NAME) self.assertTrue(os.path.exists(expected_file)) project_meta = XMLReader(expected_file).read_metadata() loaded_project = model.Project(None, None) loaded_project.from_dict(project_meta, self.test_user.id) self.assertEqual(self.test_project.name, loaded_project.name) self.assertEqual(self.test_project.description, loaded_project.description) self.assertEqual(self.test_project.gid, loaded_project.gid) expected_dict = self.test_project.to_dict()[1] del expected_dict['last_updated'] found_dict = loaded_project.to_dict()[1] del found_dict['last_updated'] self.assertEqual(expected_dict, found_dict)
def __populate_image(self, file_name, project_id, op_id): """ Create and store a image entity. """ figure_dict = XMLReader(file_name).read_metadata() new_path = os.path.join( os.path.split(file_name)[0], os.path.split(figure_dict['file_path'])[1]) if os.path.exists(new_path): figure_dict['fk_op_id'] = op_id figure_dict['fk_user_id'] = self.user_id figure_dict['fk_project_id'] = project_id figure_entity = manager_of_class(model.ResultFigure).new_instance() figure_entity = figure_entity.from_dict(figure_dict) stored_entity = dao.store_entity(figure_entity) # Update image meta-data with the new details after import figure = dao.load_figure(stored_entity.id) self.logger.debug("Store imported figure") self.files_helper.write_image_metadata(figure)
def test_update_meta_data_simple(self): """ Test the new update metaData for a simple data that is not part of a group. """ inserted_project, gid, _ = self._create_value_wrapper(self.test_user) new_meta_data = { DataTypeOverlayDetails.DATA_SUBJECT: "new subject", DataTypeOverlayDetails.DATA_STATE: "second_state", DataTypeOverlayDetails.CODE_GID: gid, DataTypeOverlayDetails.CODE_OPERATION_TAG: 'new user group' } self.project_service.update_metadata(new_meta_data) new_datatype = dao.get_datatype_by_gid(gid) self.__check_meta_data(new_meta_data, new_datatype) op_path = FilesHelper().get_operation_meta_file_path( inserted_project.name, new_datatype.parent_operation.id) op_meta = XMLReader(op_path).read_metadata() self.assertEqual(op_meta['user_group'], 'new user group', 'UserGroup not updated!')
def parse_xml_content(xml_content): """ Delegate reading of some XML content. Will parse the XMl and return a dictionary of elements with max 2 levels. """ return XMLReader(None).parse_xml_content_to_dict(xml_content)