def test_check_project_and_sample(self): sa = SampleAdaptor(**{'session_class': self.session_class}) sample_data = [ { 'sample_igf_id': 'IGFS001', 'library_id': 'IGFS001', 'project_igf_id': 'IGFP0001_test_22-8-2017_rna', }, { 'sample_igf_id': 'IGFS002', 'library_id': 'IGFS002', 'project_igf_id': 'IGFP0001_test_22-8-2017_rna', }, { 'sample_igf_id': 'IGFS003', 'library_id': 'IGFS003', 'project_igf_id': 'IGFP0001_test_22-8-2017_rna', }, { 'sample_igf_id': 'IGFS004', 'library_id': 'IGFS004', 'project_igf_id': 'IGFP0001_test_22-8-2017_rna', }, ] sa.start_session() sa.store_sample_and_attribute_data(data=sample_data) sa1=sa.check_project_and_sample(project_igf_id='IGFP0001_test_22-8-2017_rna',\ sample_igf_id='IGFS001') self.assertEqual(sa1, True) sa2=sa.check_project_and_sample(project_igf_id='IGFP0001_test_22-8-2017_rna',\ sample_igf_id='IGFS0011') self.assertEqual(sa2, False) sa.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