def test_process_project_data_and_account(self): fa=Find_and_register_new_project_data(projet_info_path=os.path.join('.','data/check_project_data'),\ dbconfig=self.dbconfig,\ user_account_template='template/email_notification/send_new_account_info.txt',\ log_slack=False,\ setup_irods=False,\ notify_user=False,\ check_hpc_user=False,\ ) fa.process_project_data_and_account() dbparam = None with open(self.dbconfig, 'r') as json_data: dbparam = json.load(json_data) base = BaseAdaptor(**dbparam) base.start_session() pa=ProjectAdaptor(**{'session':base.session}) project_exists=pa.check_project_records_igf_id(project_igf_id='IGFP0002_test_23-5-2017_rna') self.assertTrue(project_exists) ua=UserAdaptor(**{'session':base.session}) user_exists=ua.check_user_records_email_id(email_id='*****@*****.**') self.assertTrue(user_exists) user1=ua.fetch_user_records_email_id(user_email_id='*****@*****.**') self.assertEqual(user1.name,'User2') sa=SampleAdaptor(**{'session':base.session}) sample_exists=sa.check_sample_records_igf_id(sample_igf_id='IGF00006') self.assertTrue(sample_exists) project_user_exists=pa.check_existing_project_user(project_igf_id='IGFP0002_test_23-5-2017_rna',\ email_id='*****@*****.**') self.assertTrue(project_user_exists) project_user_exists=pa.check_existing_project_user(project_igf_id='IGFP0002_test_23-5-2017_rna',\ email_id='*****@*****.**') self.assertTrue(project_user_exists) base.close_session()
def _check_existing_data(self, data, dbsession, table_name, check_column='EXISTS'): ''' An internal function for checking and registering project info :param data: A pandas data series :param dbsession: A sqlalchemy database session object :param table_name: A database table name :param check_column: Column name for existing data ''' try: if not isinstance(data, pd.Series): raise ValueError('Expecting a data series and got {0}'.format( type(data))) if table_name == 'project': if self.project_lookup_column in data and \ not pd.isnull(data[self.project_lookup_column]): project_igf_id = data[self.project_lookup_column] pa = ProjectAdaptor(**{'session': dbsession }) # connect to project adaptor project_exists = pa.check_project_records_igf_id( project_igf_id) if project_exists: # store data only if project is not existing data[check_column] = True else: data[check_column] = False return data else: raise ValueError('Missing or empty required column {0}'.\ format(self.project_lookup_column)) elif table_name == 'user': if self.user_lookup_column in data and \ not pd.isnull(data[self.user_lookup_column]): user_email = data[self.user_lookup_column] ua = UserAdaptor(**{'session': dbsession}) # connect to user adaptor user_exists = ua.check_user_records_email_id( email_id=user_email) if user_exists: # store data only if user is not existing data[check_column] = True else: data[check_column] = False return data else: raise ValueError('Missing or empty required column {0}'.\ format(self.user_lookup_column)) elif table_name == 'sample': if self.sample_lookup_column in data and \ not pd.isnull(data[self.sample_lookup_column]): project_igf_id = data[self.project_lookup_column] sample_igf_id = data[self.sample_lookup_column] sa = SampleAdaptor(**{'session': dbsession }) # connect to sample adaptor sample_project_exists=sa.check_project_and_sample(project_igf_id=project_igf_id,\ sample_igf_id=sample_igf_id) # check for existing sample_id and project-id combination if sample_project_exists: # store data only if sample is not existing data[check_column] = True else: sample_exists = sa.check_sample_records_igf_id( sample_igf_id) # check for existing sample if sample_exists: raise ValueError('Sample {0} exists in database but not associated with project {1}'.\ format(sample_igf_id,project_igf_id)) # inconsistency in sample project combination data[check_column] = False return data else: raise ValueError('Missing or empty required column {0}'.\ format(self.sample_lookup_column)) elif table_name == 'project_user': if self.user_lookup_column in data and \ not pd.isnull(data[self.user_lookup_column]) and \ self.project_lookup_column in data and \ not pd.isnull(data[self.project_lookup_column]): project_igf_id = data[self.project_lookup_column] user_email = data[self.user_lookup_column] pa = ProjectAdaptor(**{'session': dbsession }) # connect to project adaptor project_user_exists=pa.check_existing_project_user(project_igf_id,\ email_id=user_email) if user_email != self.default_user_email and \ (self.data_authority_column not in data or \ pd.isnull(data[self.data_authority_column])): data[ self. data_authority_column] = True # set user as data authority, filter default user if project_user_exists: # store data only if sample is not existing data[check_column] = True else: data[check_column] = False return data else: raise ValueError('Missing or empty required column {0}, {1}'.\ format(self.project_lookup_column,\ self.user_lookup_column)) else: raise ValueError('table {0} not supported'.format(table_name)) except: raise