Skip to content

Commit

Permalink
Merge pull request #110 from josenavas/MetadataBug
Browse files Browse the repository at this point in the history
Metadata bug
  • Loading branch information
teravest committed Jun 19, 2014
2 parents 6b25366 + b766baf commit baf3432
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
8 changes: 8 additions & 0 deletions qiita_db/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",)
9 changes: 8 additions & 1 deletion qiita_db/metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -582,6 +583,12 @@ 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]

# 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
Expand Down
9 changes: 6 additions & 3 deletions qiita_db/test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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"""
Expand Down
31 changes: 23 additions & 8 deletions qiita_db/test/test_metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -481,7 +482,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,
Expand All @@ -491,7 +492,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,
Expand All @@ -501,7 +502,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')
Expand Down Expand Up @@ -556,7 +557,14 @@ def test_create_duplicate(self):
with self.assertRaises(QiitaDBDuplicateError):
SampleTemplate.create(self.metadata, self.test_study)

def test_create_(self):
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)
# The returned object has the correct id
Expand Down Expand Up @@ -750,21 +758,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'}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit baf3432

Please sign in to comment.