From b05080f9956d67e27e914298f94ccaa6bb4bf49b Mon Sep 17 00:00:00 2001 From: Jose Navas Date: Wed, 18 Jun 2014 20:42:54 -0600 Subject: [PATCH 1/3] Fixing issue 107 --- qiita_db/metadata_template.py | 2 ++ qiita_db/test/test_metadata_template.py | 14 +++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/qiita_db/metadata_template.py b/qiita_db/metadata_template.py index 259629b2d..7d8a3d6b1 100644 --- a/qiita_db/metadata_template.py +++ b/qiita_db/metadata_template.py @@ -582,6 +582,8 @@ def create(cls, md_template, obj): # We are going to modify the md_template. We create a copy so # we don't modify the user one md_template = deepcopy(md_template) + # In the database, all the column headers are lowercase + md_template.columns = [c.lower() for c in md_template.columns] conn_handler = SQLConnectionHandler() # Check that md_template have the required columns diff --git a/qiita_db/test/test_metadata_template.py b/qiita_db/test/test_metadata_template.py index 60c9138f3..713272b98 100644 --- a/qiita_db/test/test_metadata_template.py +++ b/qiita_db/test/test_metadata_template.py @@ -481,7 +481,7 @@ def setUp(self): 'collection_timestamp': datetime(2014, 5, 29, 12, 24, 51), 'host_subject_id': 'NotIdentified', - 'description': 'Test Sample 1', + 'Description': 'Test Sample 1', 'str_column': 'Value for sample 1'}, 'Sample2': {'physical_location': 'location1', 'has_physical_specimen': True, @@ -491,7 +491,7 @@ def setUp(self): 'collection_timestamp': datetime(2014, 5, 29, 12, 24, 51), 'host_subject_id': 'NotIdentified', - 'description': 'Test Sample 2', + 'Description': 'Test Sample 2', 'str_column': 'Value for sample 2'}, 'Sample3': {'physical_location': 'location1', 'has_physical_specimen': True, @@ -501,7 +501,7 @@ def setUp(self): 'collection_timestamp': datetime(2014, 5, 29, 12, 24, 51), 'host_subject_id': 'NotIdentified', - 'description': 'Test Sample 3', + 'Description': 'Test Sample 3', 'str_column': 'Value for sample 3'} } self.metadata = pd.DataFrame.from_dict(metadata_dict, orient='index') @@ -556,7 +556,7 @@ def test_create_duplicate(self): with self.assertRaises(QiitaDBDuplicateError): SampleTemplate.create(self.metadata, self.test_study) - def test_create_(self): + def test_create(self): """Creates a new SampleTemplate""" st = SampleTemplate.create(self.metadata, self.new_study) # The returned object has the correct id @@ -750,21 +750,21 @@ def setUp(self): 'center_project_name': 'Test Project', 'ebi_submission_accession': None, 'ebi_study_accession': None, - 'emp_status_id': 1, + '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, 'ebi_study_accession': None, - 'emp_status_id': 1, + '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, 'ebi_study_accession': None, - 'emp_status_id': 1, + 'EMP_status_id': 1, 'data_type_id': 2, 'str_column': 'Value for sample 3'} } From cb8787c9da6ee596684affcfb55a0819c50b4f35 Mon Sep 17 00:00:00 2001 From: Jose Navas Date: Wed, 18 Jun 2014 22:39:44 -0600 Subject: [PATCH 2/3] Checking column duplicates --- qiita_db/exceptions.py | 8 ++++++++ qiita_db/metadata_template.py | 7 ++++++- qiita_db/test/test_metadata_template.py | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/qiita_db/exceptions.py b/qiita_db/exceptions.py index 6182972ed..015f287b0 100644 --- a/qiita_db/exceptions.py +++ b/qiita_db/exceptions.py @@ -54,3 +54,11 @@ def __init__(self, missing_id, table): super(QiitaDBUnknownIDError, self).__init__() self.args = ("The object with ID '%s' does not exists in table '%s'" % (missing_id, table),) + + +class QiitaDBDuplicateHeaderError(QiitaDBError): + """Exception for error when a MetadataTemplate has duplicate columns""" + def __init__(self): + super(QiitaDBDuplicateHeaderError, self).__init__() + self.args = ("Duplicate headers found in MetadataTemplate. Note " + "that the headers are not case-sensitive",) diff --git a/qiita_db/metadata_template.py b/qiita_db/metadata_template.py index 7d8a3d6b1..eb7010974 100644 --- a/qiita_db/metadata_template.py +++ b/qiita_db/metadata_template.py @@ -38,7 +38,8 @@ from qiita_core.exceptions import IncompetentQiitaDeveloperError from .exceptions import (QiitaDBDuplicateError, QiitaDBColumnError, - QiitaDBUnknownIDError, QiitaDBNotImplementedError) + QiitaDBUnknownIDError, QiitaDBNotImplementedError, + QiitaDBDuplicateHeaderError) from .base import QiitaObject from .sql_connection import SQLConnectionHandler from .util import exists_table, get_table_cols @@ -585,6 +586,10 @@ def create(cls, md_template, obj): # In the database, all the column headers are lowercase md_template.columns = [c.lower() for c in md_template.columns] + # Check that we don't have duplicate columns + if len(set(md_template.columns)) != len(md_template.columns): + raise QiitaDBDuplicateHeaderError() + conn_handler = SQLConnectionHandler() # Check that md_template have the required columns db_cols = get_table_cols(cls._table, conn_handler) diff --git a/qiita_db/test/test_metadata_template.py b/qiita_db/test/test_metadata_template.py index 713272b98..7125a3863 100644 --- a/qiita_db/test/test_metadata_template.py +++ b/qiita_db/test/test_metadata_template.py @@ -19,7 +19,8 @@ from qiita_core.util import qiita_test_checker from qiita_core.exceptions import IncompetentQiitaDeveloperError from qiita_db.exceptions import (QiitaDBDuplicateError, QiitaDBUnknownIDError, - QiitaDBNotImplementedError) + QiitaDBNotImplementedError, + QiitaDBDuplicateHeaderError) from qiita_db.study import Study, StudyPerson from qiita_db.user import User from qiita_db.data import RawData @@ -556,6 +557,13 @@ def test_create_duplicate(self): with self.assertRaises(QiitaDBDuplicateError): SampleTemplate.create(self.metadata, self.test_study) + 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): + SampleTemplate.create(self.metadata, self.new_study) + def test_create(self): """Creates a new SampleTemplate""" st = SampleTemplate.create(self.metadata, self.new_study) @@ -822,6 +830,13 @@ def test_create_duplicate(self): 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) From b766baf72b0eda76b9ae7452d0fc95f82663f21e Mon Sep 17 00:00:00 2001 From: Jose Navas Date: Wed, 18 Jun 2014 23:16:34 -0600 Subject: [PATCH 3/3] cleaning up environment on py3 --- qiita_db/test/test_data.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/qiita_db/test/test_data.py b/qiita_db/test/test_data.py index 03e38c632..0f58f9295 100644 --- a/qiita_db/test/test_data.py +++ b/qiita_db/test/test_data.py @@ -50,7 +50,8 @@ def setUp(self): f.write("\n") def tearDown(self): - map(remove, self._clean_up_files) + for f in self._clean_up_files: + remove(f) def test_create(self): """Correctly creates all the rows in the DB for the raw data""" @@ -144,7 +145,8 @@ def setUp(self): f.write("\n") def tearDown(self): - map(remove, self._clean_up_files) + for f in self._clean_up_files: + remove(f) def test_create(self): """Correctly creates all the rows in the DB for preprocessed data""" @@ -252,7 +254,8 @@ def setUp(self): f.write("\n") def tearDown(self): - map(remove, self._clean_up_files) + for f in self._clean_up_files: + remove(f) def test_create(self): """Correctly creates all the rows in the DB for the processed data"""