Skip to content

Commit 6b3e23d

Browse files
committed
Add raw data setter and tests, also improving test for the raw data property
1 parent e66a5b3 commit 6b3e23d

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

qiita_db/metadata_template/prep_template.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,30 @@ def data_type(self, ret_id=False):
279279
@property
280280
def raw_data(self):
281281
conn_handler = SQLConnectionHandler()
282-
return conn_handler.execute_fetchone(
282+
result = conn_handler.execute_fetchone(
283283
"SELECT raw_data_id FROM qiita.prep_template "
284-
"WHERE prep_template_id=%s", (self.id,))[0]
284+
"WHERE prep_template_id=%s", (self.id,))
285+
if result:
286+
return result[0]
287+
return None
288+
289+
@raw_data.setter
290+
def raw_data(self, raw_data):
291+
conn_handler = SQLConnectionHandler()
292+
sql = """SELECT (
293+
SELECT raw_data_id
294+
FROM qiita.prep_template
295+
WHERE prep_template_id=%s)
296+
IS NOT NULL"""
297+
exists = conn_handler.execute_fetchone(sql, (self.id,))[0]
298+
if exists:
299+
raise QiitaDBError(
300+
"Prep template %d already has a raw data associated"
301+
% self.id)
302+
sql = """UPDATE qiita.prep_template
303+
SET raw_data_id = %s
304+
WHERE prep_template_id = %s"""
305+
conn_handler.execute(sql, (raw_data.id, self.id))
285306

286307
@property
287308
def preprocessed_data(self):

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
QiitaDBDuplicateHeaderError,
2525
QiitaDBExecutionError,
2626
QiitaDBColumnError,
27-
QiitaDBWarning)
27+
QiitaDBWarning,
28+
QiitaDBError)
2829
from qiita_db.sql_connection import SQLConnectionHandler
2930
from qiita_db.study import Study
3031
from qiita_db.data import RawData, ProcessedData
@@ -535,10 +536,6 @@ def test_data_type_id(self):
535536
"""data_type returns the int with the data_type_id"""
536537
self.assertTrue(self.tester.data_type(ret_id=True), 2)
537538

538-
def test_raw_data(self):
539-
"""Returns the raw_data associated with the prep template"""
540-
self.assertEqual(self.tester.raw_data, 1)
541-
542539
def test_preprocessed_data(self):
543540
"""Returns the preprocessed data list generated from this template"""
544541
self.assertEqual(self.tester.preprocessed_data, [1, 2])
@@ -1318,6 +1315,27 @@ def test_check_restrictions(self):
13181315
PREP_TEMPLATE_COLUMNS_TARGET_GENE['demultiplex']])
13191316
self.assertEqual(obs, {'primer'})
13201317

1318+
def test_raw_data(self):
1319+
"""Returns the raw_data associated with the prep template"""
1320+
self.assertEqual(self.tester.raw_data, 1)
1321+
1322+
pt = PrepTemplate.create(self.metadata, self.test_study,
1323+
self.data_type_id)
1324+
self.assertEqual(pt.raw_data, None)
1325+
1326+
def test_raw_data_setter_error(self):
1327+
rd = RawData(1)
1328+
with self.assertRaises(QiitaDBError):
1329+
self.tester.raw_data = rd
1330+
1331+
def test_raw_data_setter(self):
1332+
rd = RawData(1)
1333+
pt = PrepTemplate.create(self.metadata, self.test_study,
1334+
self.data_type_id)
1335+
self.assertEqual(pt.raw_data, None)
1336+
pt.raw_data = rd
1337+
self.assertEqual(pt.raw_data, 1)
1338+
13211339

13221340
EXP_PREP_TEMPLATE = (
13231341
'sample_name\tbarcode\tcenter_name\tcenter_project_name\t'

0 commit comments

Comments
 (0)