Skip to content

Commit c5ebb32

Browse files
authored
Merge pull request #2028 from antgonza/fix-2026
fix #2026, fix #1854
2 parents 103e0e5 + 4694f15 commit c5ebb32

File tree

3 files changed

+87
-32
lines changed

3 files changed

+87
-32
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,21 +514,9 @@ def _clean_validate_template(cls, md_template, study_id,
514514
md_template.columns = [c.lower() for c in md_template.columns]
515515
# validating pgsql reserved words not to be column headers
516516
current_headers = set(md_template.columns.values)
517-
reserved_words = qdb.metadata_template.util.get_pgsql_reserved_words()
518-
overlap = reserved_words & current_headers
519-
if overlap:
520-
raise qdb.exceptions.QiitaDBColumnError(
521-
"The following column names in the template contain PgSQL "
522-
"reserved words: %s. You need to modify them." % ", ".join(
523-
overlap))
524-
# validating invalid column names
525-
invalid_ids = qdb.metadata_template.util.get_invalid_column_names(
517+
518+
qdb.metadata_template.util.validate_invalid_column_names(
526519
current_headers)
527-
if invalid_ids:
528-
raise qdb.exceptions.QiitaDBColumnError(
529-
"The following column names in the template contain invalid "
530-
"chars: %s. You need to modify them." % ", ".join(
531-
invalid_ids))
532520

533521
# Prefix the sample names with the study_id
534522
qdb.metadata_template.util.prefix_sample_names_with_id(md_template,

qiita_db/metadata_template/test/test_util.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,48 @@ def test_get_get_invalid_sample_names_mixed(self):
207207
obs = qdb.metadata_template.util.get_invalid_sample_names(one_invalid)
208208
self.assertItemsEqual(obs, [' ', ' ', ' '])
209209

210-
def test_get_invalid_column_names(self):
211-
invalid = ['tax on', 'bla.', '.', '{', 'this|is', '4column']
212-
valid = ['fine', 'select']
213-
obs = qdb.metadata_template.util.get_invalid_column_names(
214-
invalid + valid)
215-
self.assertEqual(obs, invalid)
210+
def test_validate_invalid_column_names(self):
211+
# testing just pgsql
212+
pgsql = ['select', 'column', 'just_fine1']
213+
with self.assertRaises(qdb.exceptions.QiitaDBColumnError) as error:
214+
qdb.metadata_template.util.validate_invalid_column_names(pgsql)
215+
self.assertEqual(
216+
str(error.exception),
217+
'The following column names in the template contain PgSQL '
218+
'reserved words: column, select.\nYou need to modify them.')
219+
220+
# testing just wrong chars
221+
invalid = ['tax on', 'bla.', '.', '{', 'this|is',
222+
'4column', 'just_fine2']
223+
with self.assertRaises(qdb.exceptions.QiitaDBColumnError) as error:
224+
qdb.metadata_template.util.validate_invalid_column_names(invalid)
225+
self.assertEqual(
226+
str(error.exception),
227+
'The following column names in the template contain invalid '
228+
'chars: bla., ., tax on, this|is, {, 4column.\nYou need to '
229+
'modify them.')
230+
231+
# testing just forbidden
232+
forbidden = ['sampleid', 'just_fine3']
233+
with self.assertRaises(qdb.exceptions.QiitaDBColumnError) as error:
234+
qdb.metadata_template.util.validate_invalid_column_names(forbidden)
235+
self.assertEqual(
236+
str(error.exception),
237+
'The following column names in the template contain invalid '
238+
'values: sampleid.\nYou need to modify them.')
239+
240+
# testing all
241+
_all = pgsql + invalid + forbidden
242+
with self.assertRaises(qdb.exceptions.QiitaDBColumnError) as error:
243+
qdb.metadata_template.util.validate_invalid_column_names(_all)
244+
self.assertEqual(
245+
str(error.exception),
246+
'The following column names in the template contain PgSQL '
247+
'reserved words: column, select.\n'
248+
'The following column names in the template contain invalid '
249+
'chars: this|is, ., tax on, bla., {, 4column.\n'
250+
'The following column names in the template contain invalid '
251+
'values: sampleid.\nYou need to modify them.')
216252

217253
def test_looks_like_qiime_mapping_file(self):
218254
obs = qdb.metadata_template.util.looks_like_qiime_mapping_file(

qiita_db/metadata_template/util.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,34 +238,65 @@ def get_invalid_sample_names(sample_names):
238238
return inv
239239

240240

241-
def get_invalid_column_names(column_names):
242-
"""Get a list of column names that are not SQL compliant
241+
def validate_invalid_column_names(column_names):
242+
"""Validate a list of column names that are not SQL compliant
243243
244244
Parameters
245245
----------
246246
column_names : iterable
247247
Iterable containing the column names to check.
248248
249-
Returns
250-
-------
251-
list
252-
List of str objects where each object is an invalid column name.
249+
Raises
250+
------
251+
QiitaDBColumnError
252+
If column_name is in get_pgsql_reserved_words or contains invalid
253+
chars or is within the forbidden_values
253254
254255
References
255256
----------
256257
.. [1] postgresql SQL-SYNTAX-IDENTIFIERS: https://goo.gl/EF0cUV.
257258
"""
259+
column_names = set(column_names)
260+
261+
# testing for specific column names that are not included in the other
262+
# tests.
263+
forbidden_values = {
264+
# https://github.com/biocore/qiita/issues/2026
265+
'sampleid'
266+
}
267+
forbidden = forbidden_values & column_names
268+
269+
# pgsql reserved words
270+
pgsql_reserved = (
271+
qdb.metadata_template.util.get_pgsql_reserved_words() & column_names)
272+
273+
# invalid letters in headers
258274
valid_initial_char = letters
259275
valid_rest = set(letters+digits+'_')
260-
inv = []
261-
276+
invalid = []
262277
for s in column_names:
263278
if s[0] not in valid_initial_char:
264-
inv.append(s)
279+
invalid.append(s)
265280
elif set(s) - valid_rest:
266-
inv.append(s)
267-
268-
return inv
281+
invalid.append(s)
282+
283+
error = []
284+
if pgsql_reserved:
285+
error.append(
286+
"The following column names in the template contain PgSQL "
287+
"reserved words: %s." % ", ".join(pgsql_reserved))
288+
if invalid:
289+
error.append(
290+
"The following column names in the template contain invalid "
291+
"chars: %s." % ", ".join(invalid))
292+
if forbidden:
293+
error.append(
294+
"The following column names in the template contain invalid "
295+
"values: %s." % ", ".join(forbidden))
296+
297+
if error:
298+
raise qdb.exceptions.QiitaDBColumnError(
299+
"%s\nYou need to modify them." % '\n'.join(error))
269300

270301

271302
def looks_like_qiime_mapping_file(fp):

0 commit comments

Comments
 (0)