Skip to content

Commit baf3432

Browse files
committed
Merge pull request #110 from josenavas/MetadataBug
Metadata bug
2 parents 6b25366 + b766baf commit baf3432

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

qiita_db/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ def __init__(self, missing_id, table):
5454
super(QiitaDBUnknownIDError, self).__init__()
5555
self.args = ("The object with ID '%s' does not exists in table '%s'"
5656
% (missing_id, table),)
57+
58+
59+
class QiitaDBDuplicateHeaderError(QiitaDBError):
60+
"""Exception for error when a MetadataTemplate has duplicate columns"""
61+
def __init__(self):
62+
super(QiitaDBDuplicateHeaderError, self).__init__()
63+
self.args = ("Duplicate headers found in MetadataTemplate. Note "
64+
"that the headers are not case-sensitive",)

qiita_db/metadata_template.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838

3939
from qiita_core.exceptions import IncompetentQiitaDeveloperError
4040
from .exceptions import (QiitaDBDuplicateError, QiitaDBColumnError,
41-
QiitaDBUnknownIDError, QiitaDBNotImplementedError)
41+
QiitaDBUnknownIDError, QiitaDBNotImplementedError,
42+
QiitaDBDuplicateHeaderError)
4243
from .base import QiitaObject
4344
from .sql_connection import SQLConnectionHandler
4445
from .util import exists_table, get_table_cols
@@ -582,6 +583,12 @@ def create(cls, md_template, obj):
582583
# We are going to modify the md_template. We create a copy so
583584
# we don't modify the user one
584585
md_template = deepcopy(md_template)
586+
# In the database, all the column headers are lowercase
587+
md_template.columns = [c.lower() for c in md_template.columns]
588+
589+
# Check that we don't have duplicate columns
590+
if len(set(md_template.columns)) != len(md_template.columns):
591+
raise QiitaDBDuplicateHeaderError()
585592

586593
conn_handler = SQLConnectionHandler()
587594
# Check that md_template have the required columns

qiita_db/test/test_data.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def setUp(self):
5050
f.write("\n")
5151

5252
def tearDown(self):
53-
map(remove, self._clean_up_files)
53+
for f in self._clean_up_files:
54+
remove(f)
5455

5556
def test_create(self):
5657
"""Correctly creates all the rows in the DB for the raw data"""
@@ -144,7 +145,8 @@ def setUp(self):
144145
f.write("\n")
145146

146147
def tearDown(self):
147-
map(remove, self._clean_up_files)
148+
for f in self._clean_up_files:
149+
remove(f)
148150

149151
def test_create(self):
150152
"""Correctly creates all the rows in the DB for preprocessed data"""
@@ -252,7 +254,8 @@ def setUp(self):
252254
f.write("\n")
253255

254256
def tearDown(self):
255-
map(remove, self._clean_up_files)
257+
for f in self._clean_up_files:
258+
remove(f)
256259

257260
def test_create(self):
258261
"""Correctly creates all the rows in the DB for the processed data"""

qiita_db/test/test_metadata_template.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from qiita_core.util import qiita_test_checker
2020
from qiita_core.exceptions import IncompetentQiitaDeveloperError
2121
from qiita_db.exceptions import (QiitaDBDuplicateError, QiitaDBUnknownIDError,
22-
QiitaDBNotImplementedError)
22+
QiitaDBNotImplementedError,
23+
QiitaDBDuplicateHeaderError)
2324
from qiita_db.study import Study, StudyPerson
2425
from qiita_db.user import User
2526
from qiita_db.data import RawData
@@ -481,7 +482,7 @@ def setUp(self):
481482
'collection_timestamp':
482483
datetime(2014, 5, 29, 12, 24, 51),
483484
'host_subject_id': 'NotIdentified',
484-
'description': 'Test Sample 1',
485+
'Description': 'Test Sample 1',
485486
'str_column': 'Value for sample 1'},
486487
'Sample2': {'physical_location': 'location1',
487488
'has_physical_specimen': True,
@@ -491,7 +492,7 @@ def setUp(self):
491492
'collection_timestamp':
492493
datetime(2014, 5, 29, 12, 24, 51),
493494
'host_subject_id': 'NotIdentified',
494-
'description': 'Test Sample 2',
495+
'Description': 'Test Sample 2',
495496
'str_column': 'Value for sample 2'},
496497
'Sample3': {'physical_location': 'location1',
497498
'has_physical_specimen': True,
@@ -501,7 +502,7 @@ def setUp(self):
501502
'collection_timestamp':
502503
datetime(2014, 5, 29, 12, 24, 51),
503504
'host_subject_id': 'NotIdentified',
504-
'description': 'Test Sample 3',
505+
'Description': 'Test Sample 3',
505506
'str_column': 'Value for sample 3'}
506507
}
507508
self.metadata = pd.DataFrame.from_dict(metadata_dict, orient='index')
@@ -556,7 +557,14 @@ def test_create_duplicate(self):
556557
with self.assertRaises(QiitaDBDuplicateError):
557558
SampleTemplate.create(self.metadata, self.test_study)
558559

559-
def test_create_(self):
560+
def test_create_duplicate_header(self):
561+
"""Create raises an error when duplicate headers are present"""
562+
self.metadata['STR_COLUMN'] = pd.Series(['', '', ''],
563+
index=self.metadata.index)
564+
with self.assertRaises(QiitaDBDuplicateHeaderError):
565+
SampleTemplate.create(self.metadata, self.new_study)
566+
567+
def test_create(self):
560568
"""Creates a new SampleTemplate"""
561569
st = SampleTemplate.create(self.metadata, self.new_study)
562570
# The returned object has the correct id
@@ -750,21 +758,21 @@ def setUp(self):
750758
'center_project_name': 'Test Project',
751759
'ebi_submission_accession': None,
752760
'ebi_study_accession': None,
753-
'emp_status_id': 1,
761+
'EMP_status_id': 1,
754762
'data_type_id': 2,
755763
'str_column': 'Value for sample 1'},
756764
'SKD8.640184': {'center_name': 'ANL',
757765
'center_project_name': 'Test Project',
758766
'ebi_submission_accession': None,
759767
'ebi_study_accession': None,
760-
'emp_status_id': 1,
768+
'EMP_status_id': 1,
761769
'data_type_id': 2,
762770
'str_column': 'Value for sample 2'},
763771
'SKB7.640196': {'center_name': 'ANL',
764772
'center_project_name': 'Test Project',
765773
'ebi_submission_accession': None,
766774
'ebi_study_accession': None,
767-
'emp_status_id': 1,
775+
'EMP_status_id': 1,
768776
'data_type_id': 2,
769777
'str_column': 'Value for sample 3'}
770778
}
@@ -822,6 +830,13 @@ def test_create_duplicate(self):
822830
with self.assertRaises(QiitaDBDuplicateError):
823831
PrepTemplate.create(self.metadata, self.test_raw_data)
824832

833+
def test_create_duplicate_header(self):
834+
"""Create raises an error when duplicate headers are present"""
835+
self.metadata['STR_COLUMN'] = pd.Series(['', '', ''],
836+
index=self.metadata.index)
837+
with self.assertRaises(QiitaDBDuplicateHeaderError):
838+
PrepTemplate.create(self.metadata, self.new_raw_data)
839+
825840
def test_create(self):
826841
"""Creates a new PrepTemplate"""
827842
pt = PrepTemplate.create(self.metadata, self.new_raw_data)

0 commit comments

Comments
 (0)