Skip to content

Commit 8474ca4

Browse files
committed
addressing @squirrelo's comments
1 parent e46ea59 commit 8474ca4

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ def __setitem__(self, column, value):
324324
------
325325
QiitaDBColumnError
326326
If the column does not exist in the table
327+
ValueError
328+
If the value type does not match the one in the DB
327329
"""
328330
conn_handler = SQLConnectionHandler()
329331

@@ -347,42 +349,42 @@ def __setitem__(self, column, value):
347349
sql, (self._table, column))[0]
348350

349351
if exists_dynamic:
350-
# catching error so we can check if the error is due to different
351-
# column type or something else
352-
try:
353352
sql = """UPDATE qiita.{0}
354353
SET {1}=%s
355354
WHERE sample_id=%s""".format(self._dynamic_table,
356355
column)
357-
conn_handler.execute(sql, (value, self._id))
358-
except Exception as e:
359-
column_type = conn_handler.execute_fetchone(
360-
"""SELECT data_type
361-
FROM information_schema.columns
362-
WHERE column_name=%s AND table_schema='qiita'
363-
""", (column,))[0]
364-
value_type = type(value).__name__
365-
366-
if column_type != value_type:
367-
raise ValueError(
368-
'The new value being added to column: "{0}" is "{1}" '
369-
'(type: "{2}"). However, this column in the DB is of '
370-
'type "{3}". Please change the value in your updated '
371-
'template or reprocess your sample template.'.format(
372-
column, value, value_type, column_type))
373-
else:
374-
raise e
375356
elif exists_required:
376357
# here is not required the type check as the required fields have
377358
# an explicit type check
378359
sql = """UPDATE qiita.{0}
379360
SET {1}=%s
380361
WHERE sample_id=%s""".format(self._table, column)
381-
conn_handler.execute(sql, (value, self._id))
382362
else:
383363
raise QiitaDBColumnError("Column %s does not exist in %s" %
384364
(column, self._dynamic_table))
385365

366+
try:
367+
conn_handler.execute(sql, (value, self._id))
368+
except Exception as e:
369+
# catching error so we can check if the error is due to different
370+
# column type or something else
371+
column_type = conn_handler.execute_fetchone(
372+
"""SELECT data_type
373+
FROM information_schema.columns
374+
WHERE column_name=%s AND table_schema='qiita'
375+
""", (column,))[0]
376+
value_type = type(value).__name__
377+
378+
if column_type != value_type:
379+
raise ValueError(
380+
'The new value being added to column: "{0}" is "{1}" '
381+
'(type: "{2}"). However, this column in the DB is of '
382+
'type "{3}". Please change the value in your updated '
383+
'template or reprocess your sample template.'.format(
384+
column, value, value_type, column_type))
385+
else:
386+
raise e
387+
386388
def __delitem__(self, key):
387389
r"""Removes the sample with sample id `key` from the database
388390

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ class TestPrepSampleReadWrite(BaseTestPrepSample):
222222
def test_setitem(self):
223223
with self.assertRaises(QiitaDBColumnError):
224224
self.tester['column that does not exist'] = 0.3
225+
226+
with self.assertRaises(ValueError):
227+
self.tester['emp_status_id'] = "Error!"
228+
225229
self.assertEqual(self.tester['center_name'], 'ANL')
226230
self.tester['center_name'] = "FOO"
227231
self.assertEqual(self.tester['center_name'], "FOO")

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ class TestSampleReadWrite(BaseTestSample):
208208
def test_setitem(self):
209209
with self.assertRaises(QiitaDBColumnError):
210210
self.tester['column that does not exist'] = 0.30
211+
212+
with self.assertRaises(ValueError):
213+
self.tester['collection_timestamp'] = "Error!"
214+
211215
self.assertEqual(self.tester['tot_nitro'], 1.41)
212216
self.tester['tot_nitro'] = '1234.5'
213217
self.assertEqual(self.tester['tot_nitro'], 1234.5)

0 commit comments

Comments
 (0)