Skip to content

Commit 16af20e

Browse files
authored
Study.from_title (#3161)
1 parent bfacd73 commit 16af20e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

qiita_db/study.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,39 @@ def _lock_non_sandbox(self):
8787
raise qdb.exceptions.QiitaDBStatusError(
8888
"Illegal operation on non-sandbox study!")
8989

90+
@classmethod
91+
def from_title(cls, title):
92+
"""Instantiate Study from title
93+
94+
Parameters
95+
----------
96+
title : str
97+
Tht title to search for
98+
99+
Returns
100+
-------
101+
Study
102+
The study with the given title
103+
104+
Raises
105+
------
106+
QiitaDBUnknownIDError
107+
If the title doesn't exist
108+
"""
109+
with qdb.sql_connection.TRN:
110+
sql = """SELECT study_id
111+
FROM qiita.{}
112+
WHERE study_title = %s""".format(cls._table)
113+
114+
qdb.sql_connection.TRN.add(sql, [title])
115+
sid = qdb.sql_connection.TRN.execute_fetchflatten()
116+
117+
if not sid:
118+
raise qdb.exceptions.QiitaDBUnknownIDError(
119+
cls._table, f'"{title}" does not exist')
120+
121+
return qdb.study.Study(sid[0])
122+
90123
@classmethod
91124
def iter(cls):
92125
"""Iterate over all studies in the database

qiita_db/test/test_study.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ def _change_processed_data_status(self, new_status):
207207
qdb.sql_connection.perform_as_transaction(
208208
"UPDATE qiita.artifact SET visibility_id = %s", (id_status,))
209209

210+
def test_from_title(self):
211+
study = qdb.study.Study.from_title(
212+
'Identification of the Microbiomes for Cannabis Soils')
213+
self.assertEqual(study, qdb.study.Study(1))
214+
215+
with self.assertRaises(qdb.exceptions.QiitaDBUnknownIDError):
216+
qdb.study.Study.from_title('Study title')
217+
210218
def test_get_info(self):
211219
# Test get all info for single study
212220
qiita_config.portal = 'QIITA'

0 commit comments

Comments
 (0)