class TestPrepTemplate(TestCase): """Tests the PrepTemplate class""" def setUp(self): metadata_dict = { 'SKB8.640193': {'center_name': 'ANL', 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 1'}, 'SKD8.640184': {'center_name': 'ANL', 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 2'}, 'SKB7.640196': {'center_name': 'ANL', 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 3'} } self.metadata = pd.DataFrame.from_dict(metadata_dict, orient='index') self.test_raw_data = RawData(1) fd, seqs_fp = mkstemp(suffix='_seqs.fastq') close(fd) fd, barcodes_fp = mkstemp(suffix='_barcodes.fastq') close(fd) filepaths = [(seqs_fp, 1), (barcodes_fp, 2)] with open(seqs_fp, "w") as f: f.write("\n") with open(barcodes_fp, "w") as f: f.write("\n") self.new_raw_data = RawData.create(2, filepaths, [Study(1)]) db_test_raw_dir = join(get_db_files_base_dir(), 'raw_data') db_seqs_fp = join(db_test_raw_dir, "3_%s" % basename(seqs_fp)) db_barcodes_fp = join(db_test_raw_dir, "3_%s" % basename(barcodes_fp)) self._clean_up_files = [db_seqs_fp, db_barcodes_fp] self.tester = PrepTemplate(1) self.exp_sample_ids = {'SKB1.640202', 'SKB2.640194', 'SKB3.640195', 'SKB4.640189', 'SKB5.640181', 'SKB6.640176', 'SKB7.640196', 'SKB8.640193', 'SKB9.640200', 'SKD1.640179', 'SKD2.640178', 'SKD3.640198', 'SKD4.640185', 'SKD5.640186', 'SKD6.640190', 'SKD7.640191', 'SKD8.640184', 'SKD9.640182', 'SKM1.640183', 'SKM2.640199', 'SKM3.640197', 'SKM4.640180', 'SKM5.640177', 'SKM6.640187', 'SKM7.640188', 'SKM8.640201', 'SKM9.640192'} def tearDown(self): for f in self._clean_up_files: remove(f) def test_init_unknown_error(self): """Init raises an error if the id is not known""" with self.assertRaises(QiitaDBUnknownIDError): PrepTemplate(2) def test_init(self): """Init successfully instantiates the object""" st = PrepTemplate(1) self.assertTrue(st.id, 1) def test_table_name(self): """Table name return the correct string""" obs = PrepTemplate._table_name(self.test_raw_data) self.assertEqual(obs, "prep_1") def test_create_duplicate(self): """Create raises an error when creating a duplicated PrepTemplate""" with self.assertRaises(QiitaDBDuplicateError): PrepTemplate.create(self.metadata, self.test_raw_data) def test_create_duplicate_header(self): """Create raises an error when duplicate headers are present""" self.metadata['STR_COLUMN'] = pd.Series(['', '', ''], index=self.metadata.index) with self.assertRaises(QiitaDBDuplicateHeaderError): PrepTemplate.create(self.metadata, self.new_raw_data) def test_create(self): """Creates a new PrepTemplate""" pt = PrepTemplate.create(self.metadata, self.new_raw_data) # The returned object has the correct id self.assertEqual(pt.id, 3) # The relevant rows to common_prep_info have been added. obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.common_prep_info WHERE raw_data_id=3") # raw_data_id, sample_id, center_name, center_project_name, # ebi_submission_accession, ebi_study_accession, emp_status_id, # data_type_id exp = [[3, 'SKB8.640193', 'ANL', 'Test Project', None, None, 1, 2], [3, 'SKD8.640184', 'ANL', 'Test Project', None, None, 1, 2], [3, 'SKB7.640196', 'ANL', 'Test Project', None, None, 1, 2]] self.assertEqual(sorted(obs), sorted(exp)) # The relevant rows have been added to the raw_data_prep_columns obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.raw_data_prep_columns WHERE raw_data_id=3") # raw_data_id, column_name, column_type exp = [[3, "str_column", "varchar"]] self.assertEqual(obs, exp) # The new table exists self.assertTrue(exists_table("prep_3", self.conn_handler)) # The new table hosts the correct values obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.prep_3") # sample_id, str_column exp = [['SKB8.640193', "Value for sample 1"], ['SKD8.640184', "Value for sample 2"], ['SKB7.640196', "Value for sample 3"]] self.assertEqual(sorted(obs), sorted(exp)) def test_exists_true(self): """Exists returns true when the PrepTemplate already exists""" self.assertTrue(PrepTemplate.exists(self.test_raw_data)) def test_exists_false(self): """Exists returns false when the PrepTemplate does not exists""" self.assertFalse(PrepTemplate.exists(self.new_raw_data)) def test_get_sample_ids(self): """get_sample_ids returns the correct set of sample ids""" obs = self.tester._get_sample_ids(self.conn_handler) self.assertEqual(obs, self.exp_sample_ids) def test_len(self): """Len returns the correct number of sample ids""" self.assertEqual(len(self.tester), 27) def test_getitem(self): """Get item returns the correct sample object""" obs = self.tester['SKM7.640188'] exp = PrepSample('SKM7.640188', self.tester) self.assertEqual(obs, exp) def test_getitem_error(self): """Get item raises an error if key does not exists""" with self.assertRaises(KeyError): self.tester['Not_a_Sample'] def test_setitem(self): """setitem raises an error (currently not allowed)""" with self.assertRaises(QiitaDBNotImplementedError): self.tester['SKM7.640188'] = PrepSample('SKM7.640188', self.tester) def test_delitem(self): """delitem raises an error (currently not allowed)""" with self.assertRaises(QiitaDBNotImplementedError): del self.tester['SKM7.640188'] def test_iter(self): """iter returns an iterator over the sample ids""" obs = self.tester.__iter__() self.assertTrue(isinstance(obs, Iterable)) self.assertEqual(set(obs), self.exp_sample_ids) def test_contains_true(self): """contains returns true if the sample id exists""" self.assertTrue('SKM7.640188' in self.tester) def test_contains_false(self): """contains returns false if the sample id does not exists""" self.assertFalse('Not_a_Sample' in self.tester) def test_keys(self): """keys returns an iterator over the sample ids""" obs = self.tester.keys() self.assertTrue(isinstance(obs, Iterable)) self.assertEqual(set(obs), self.exp_sample_ids) def test_values(self): """values returns an iterator over the values""" obs = self.tester.values() self.assertTrue(isinstance(obs, Iterable)) exp = {PrepSample('SKB1.640202', self.tester), PrepSample('SKB2.640194', self.tester), PrepSample('SKB3.640195', self.tester), PrepSample('SKB4.640189', self.tester), PrepSample('SKB5.640181', self.tester), PrepSample('SKB6.640176', self.tester), PrepSample('SKB7.640196', self.tester), PrepSample('SKB8.640193', self.tester), PrepSample('SKB9.640200', self.tester), PrepSample('SKD1.640179', self.tester), PrepSample('SKD2.640178', self.tester), PrepSample('SKD3.640198', self.tester), PrepSample('SKD4.640185', self.tester), PrepSample('SKD5.640186', self.tester), PrepSample('SKD6.640190', self.tester), PrepSample('SKD7.640191', self.tester), PrepSample('SKD8.640184', self.tester), PrepSample('SKD9.640182', self.tester), PrepSample('SKM1.640183', self.tester), PrepSample('SKM2.640199', self.tester), PrepSample('SKM3.640197', self.tester), PrepSample('SKM4.640180', self.tester), PrepSample('SKM5.640177', self.tester), PrepSample('SKM6.640187', self.tester), PrepSample('SKM7.640188', self.tester), PrepSample('SKM8.640201', self.tester), PrepSample('SKM9.640192', self.tester)} # Creating a list and looping over it since unittest does not call # the __eq__ function on the objects for o, e in zip(sorted(list(obs), key=lambda x: x.id), sorted(exp, key=lambda x: x.id)): self.assertEqual(o, e) def test_items(self): """items returns an iterator over the (key, value) tuples""" obs = self.tester.items() self.assertTrue(isinstance(obs, Iterable)) exp = [('SKB1.640202', PrepSample('SKB1.640202', self.tester)), ('SKB2.640194', PrepSample('SKB2.640194', self.tester)), ('SKB3.640195', PrepSample('SKB3.640195', self.tester)), ('SKB4.640189', PrepSample('SKB4.640189', self.tester)), ('SKB5.640181', PrepSample('SKB5.640181', self.tester)), ('SKB6.640176', PrepSample('SKB6.640176', self.tester)), ('SKB7.640196', PrepSample('SKB7.640196', self.tester)), ('SKB8.640193', PrepSample('SKB8.640193', self.tester)), ('SKB9.640200', PrepSample('SKB9.640200', self.tester)), ('SKD1.640179', PrepSample('SKD1.640179', self.tester)), ('SKD2.640178', PrepSample('SKD2.640178', self.tester)), ('SKD3.640198', PrepSample('SKD3.640198', self.tester)), ('SKD4.640185', PrepSample('SKD4.640185', self.tester)), ('SKD5.640186', PrepSample('SKD5.640186', self.tester)), ('SKD6.640190', PrepSample('SKD6.640190', self.tester)), ('SKD7.640191', PrepSample('SKD7.640191', self.tester)), ('SKD8.640184', PrepSample('SKD8.640184', self.tester)), ('SKD9.640182', PrepSample('SKD9.640182', self.tester)), ('SKM1.640183', PrepSample('SKM1.640183', self.tester)), ('SKM2.640199', PrepSample('SKM2.640199', self.tester)), ('SKM3.640197', PrepSample('SKM3.640197', self.tester)), ('SKM4.640180', PrepSample('SKM4.640180', self.tester)), ('SKM5.640177', PrepSample('SKM5.640177', self.tester)), ('SKM6.640187', PrepSample('SKM6.640187', self.tester)), ('SKM7.640188', PrepSample('SKM7.640188', self.tester)), ('SKM8.640201', PrepSample('SKM8.640201', self.tester)), ('SKM9.640192', PrepSample('SKM9.640192', self.tester))] # Creating a list and looping over it since unittest does not call # the __eq__ function on the objects for o, e in zip(sorted(list(obs)), sorted(exp)): self.assertEqual(o, e) def test_get(self): """get returns the correct PrepSample object""" obs = self.tester.get('SKM7.640188') exp = PrepSample('SKM7.640188', self.tester) self.assertEqual(obs, exp) def test_get_none(self): """get returns none if the sample id is not present""" self.assertTrue(self.tester.get('Not_a_Sample') is None) def test_to_file(self): """to file writes a tab delimited file with all the metadata""" fd, fp = mkstemp() close(fd) pt = PrepTemplate.create(self.metadata, self.new_raw_data) pt.to_file(fp) self._clean_up_files.append(fp) with open(fp, 'U') as f: obs = f.read() self.assertEqual(obs, EXP_PREP_TEMPLATE)
class TestPrepTemplate(TestCase): """Tests the PrepTemplate class""" def setUp(self): metadata_dict = { 'SKB8.640193': { 'center_name': 'ANL', 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 1' }, 'SKD8.640184': { 'center_name': 'ANL', 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 2' }, 'SKB7.640196': { 'center_name': 'ANL', 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 3' } } self.metadata = pd.DataFrame.from_dict(metadata_dict, orient='index') self.test_raw_data = RawData(1) fd, seqs_fp = mkstemp(suffix='_seqs.fastq') close(fd) fd, barcodes_fp = mkstemp(suffix='_barcodes.fastq') close(fd) filepaths = [(seqs_fp, 1), (barcodes_fp, 2)] with open(seqs_fp, "w") as f: f.write("\n") with open(barcodes_fp, "w") as f: f.write("\n") self.new_raw_data = RawData.create(2, filepaths, [Study(1)]) db_test_raw_dir = join(get_db_files_base_dir(), 'raw_data') db_seqs_fp = join(db_test_raw_dir, "3_%s" % basename(seqs_fp)) db_barcodes_fp = join(db_test_raw_dir, "3_%s" % basename(barcodes_fp)) self._clean_up_files = [db_seqs_fp, db_barcodes_fp] self.tester = PrepTemplate(1) self.exp_sample_ids = { 'SKB1.640202', 'SKB2.640194', 'SKB3.640195', 'SKB4.640189', 'SKB5.640181', 'SKB6.640176', 'SKB7.640196', 'SKB8.640193', 'SKB9.640200', 'SKD1.640179', 'SKD2.640178', 'SKD3.640198', 'SKD4.640185', 'SKD5.640186', 'SKD6.640190', 'SKD7.640191', 'SKD8.640184', 'SKD9.640182', 'SKM1.640183', 'SKM2.640199', 'SKM3.640197', 'SKM4.640180', 'SKM5.640177', 'SKM6.640187', 'SKM7.640188', 'SKM8.640201', 'SKM9.640192' } def tearDown(self): for f in self._clean_up_files: remove(f) def test_init_unknown_error(self): """Init raises an error if the id is not known""" with self.assertRaises(QiitaDBUnknownIDError): PrepTemplate(2) def test_init(self): """Init successfully instantiates the object""" st = PrepTemplate(1) self.assertTrue(st.id, 1) def test_table_name(self): """Table name return the correct string""" obs = PrepTemplate._table_name(self.test_raw_data) self.assertEqual(obs, "prep_1") def test_create_duplicate(self): """Create raises an error when creating a duplicated PrepTemplate""" with self.assertRaises(QiitaDBDuplicateError): PrepTemplate.create(self.metadata, self.test_raw_data) def test_create_duplicate_header(self): """Create raises an error when duplicate headers are present""" self.metadata['STR_COLUMN'] = pd.Series(['', '', ''], index=self.metadata.index) with self.assertRaises(QiitaDBDuplicateHeaderError): PrepTemplate.create(self.metadata, self.new_raw_data) def test_create(self): """Creates a new PrepTemplate""" pt = PrepTemplate.create(self.metadata, self.new_raw_data) # The returned object has the correct id self.assertEqual(pt.id, 3) # The relevant rows to common_prep_info have been added. obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.common_prep_info WHERE raw_data_id=3") # raw_data_id, sample_id, center_name, center_project_name, # ebi_submission_accession, ebi_study_accession, emp_status_id, # data_type_id exp = [[3, 'SKB8.640193', 'ANL', 'Test Project', 1, 2], [3, 'SKD8.640184', 'ANL', 'Test Project', 1, 2], [3, 'SKB7.640196', 'ANL', 'Test Project', 1, 2]] self.assertEqual(sorted(obs), sorted(exp)) # The relevant rows have been added to the raw_data_prep_columns obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.raw_data_prep_columns WHERE raw_data_id=3") # raw_data_id, column_name, column_type exp = [[3, 'str_column', 'varchar'], [3, 'ebi_submission_accession', 'varchar']] self.assertEqual(obs, exp) # The new table exists self.assertTrue(exists_table("prep_3", self.conn_handler)) # The new table hosts the correct values obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.prep_3") # sample_id, str_column exp = [['SKB8.640193', "Value for sample 1", None], ['SKD8.640184', "Value for sample 2", None], ['SKB7.640196', "Value for sample 3", None]] self.assertEqual(sorted(obs), sorted(exp)) def test_exists_true(self): """Exists returns true when the PrepTemplate already exists""" self.assertTrue(PrepTemplate.exists(self.test_raw_data)) def test_exists_false(self): """Exists returns false when the PrepTemplate does not exists""" self.assertFalse(PrepTemplate.exists(self.new_raw_data)) def test_get_sample_ids(self): """get_sample_ids returns the correct set of sample ids""" obs = self.tester._get_sample_ids(self.conn_handler) self.assertEqual(obs, self.exp_sample_ids) def test_len(self): """Len returns the correct number of sample ids""" self.assertEqual(len(self.tester), 27) def test_getitem(self): """Get item returns the correct sample object""" obs = self.tester['SKM7.640188'] exp = PrepSample('SKM7.640188', self.tester) self.assertEqual(obs, exp) def test_getitem_error(self): """Get item raises an error if key does not exists""" with self.assertRaises(KeyError): self.tester['Not_a_Sample'] def test_setitem(self): """setitem raises an error (currently not allowed)""" with self.assertRaises(QiitaDBNotImplementedError): self.tester['SKM7.640188'] = PrepSample('SKM7.640188', self.tester) def test_delitem(self): """delitem raises an error (currently not allowed)""" with self.assertRaises(QiitaDBNotImplementedError): del self.tester['SKM7.640188'] def test_iter(self): """iter returns an iterator over the sample ids""" obs = self.tester.__iter__() self.assertTrue(isinstance(obs, Iterable)) self.assertEqual(set(obs), self.exp_sample_ids) def test_contains_true(self): """contains returns true if the sample id exists""" self.assertTrue('SKM7.640188' in self.tester) def test_contains_false(self): """contains returns false if the sample id does not exists""" self.assertFalse('Not_a_Sample' in self.tester) def test_keys(self): """keys returns an iterator over the sample ids""" obs = self.tester.keys() self.assertTrue(isinstance(obs, Iterable)) self.assertEqual(set(obs), self.exp_sample_ids) def test_values(self): """values returns an iterator over the values""" obs = self.tester.values() self.assertTrue(isinstance(obs, Iterable)) exp = { PrepSample('SKB1.640202', self.tester), PrepSample('SKB2.640194', self.tester), PrepSample('SKB3.640195', self.tester), PrepSample('SKB4.640189', self.tester), PrepSample('SKB5.640181', self.tester), PrepSample('SKB6.640176', self.tester), PrepSample('SKB7.640196', self.tester), PrepSample('SKB8.640193', self.tester), PrepSample('SKB9.640200', self.tester), PrepSample('SKD1.640179', self.tester), PrepSample('SKD2.640178', self.tester), PrepSample('SKD3.640198', self.tester), PrepSample('SKD4.640185', self.tester), PrepSample('SKD5.640186', self.tester), PrepSample('SKD6.640190', self.tester), PrepSample('SKD7.640191', self.tester), PrepSample('SKD8.640184', self.tester), PrepSample('SKD9.640182', self.tester), PrepSample('SKM1.640183', self.tester), PrepSample('SKM2.640199', self.tester), PrepSample('SKM3.640197', self.tester), PrepSample('SKM4.640180', self.tester), PrepSample('SKM5.640177', self.tester), PrepSample('SKM6.640187', self.tester), PrepSample('SKM7.640188', self.tester), PrepSample('SKM8.640201', self.tester), PrepSample('SKM9.640192', self.tester) } # Creating a list and looping over it since unittest does not call # the __eq__ function on the objects for o, e in zip(sorted(list(obs), key=lambda x: x.id), sorted(exp, key=lambda x: x.id)): self.assertEqual(o, e) def test_items(self): """items returns an iterator over the (key, value) tuples""" obs = self.tester.items() self.assertTrue(isinstance(obs, Iterable)) exp = [('SKB1.640202', PrepSample('SKB1.640202', self.tester)), ('SKB2.640194', PrepSample('SKB2.640194', self.tester)), ('SKB3.640195', PrepSample('SKB3.640195', self.tester)), ('SKB4.640189', PrepSample('SKB4.640189', self.tester)), ('SKB5.640181', PrepSample('SKB5.640181', self.tester)), ('SKB6.640176', PrepSample('SKB6.640176', self.tester)), ('SKB7.640196', PrepSample('SKB7.640196', self.tester)), ('SKB8.640193', PrepSample('SKB8.640193', self.tester)), ('SKB9.640200', PrepSample('SKB9.640200', self.tester)), ('SKD1.640179', PrepSample('SKD1.640179', self.tester)), ('SKD2.640178', PrepSample('SKD2.640178', self.tester)), ('SKD3.640198', PrepSample('SKD3.640198', self.tester)), ('SKD4.640185', PrepSample('SKD4.640185', self.tester)), ('SKD5.640186', PrepSample('SKD5.640186', self.tester)), ('SKD6.640190', PrepSample('SKD6.640190', self.tester)), ('SKD7.640191', PrepSample('SKD7.640191', self.tester)), ('SKD8.640184', PrepSample('SKD8.640184', self.tester)), ('SKD9.640182', PrepSample('SKD9.640182', self.tester)), ('SKM1.640183', PrepSample('SKM1.640183', self.tester)), ('SKM2.640199', PrepSample('SKM2.640199', self.tester)), ('SKM3.640197', PrepSample('SKM3.640197', self.tester)), ('SKM4.640180', PrepSample('SKM4.640180', self.tester)), ('SKM5.640177', PrepSample('SKM5.640177', self.tester)), ('SKM6.640187', PrepSample('SKM6.640187', self.tester)), ('SKM7.640188', PrepSample('SKM7.640188', self.tester)), ('SKM8.640201', PrepSample('SKM8.640201', self.tester)), ('SKM9.640192', PrepSample('SKM9.640192', self.tester))] # Creating a list and looping over it since unittest does not call # the __eq__ function on the objects for o, e in zip(sorted(list(obs)), sorted(exp)): self.assertEqual(o, e) def test_get(self): """get returns the correct PrepSample object""" obs = self.tester.get('SKM7.640188') exp = PrepSample('SKM7.640188', self.tester) self.assertEqual(obs, exp) def test_get_none(self): """get returns none if the sample id is not present""" self.assertTrue(self.tester.get('Not_a_Sample') is None) def test_to_file(self): """to file writes a tab delimited file with all the metadata""" fd, fp = mkstemp() close(fd) pt = PrepTemplate.create(self.metadata, self.new_raw_data) pt.to_file(fp) self._clean_up_files.append(fp) with open(fp, 'U') as f: obs = f.read() self.assertEqual(obs, EXP_PREP_TEMPLATE)