Skip to content

Commit b26a867

Browse files
committed
Merge pull request #1099 from josenavas/fix-qiita-db-tests
Fix _almost_ all qiita db tests
2 parents 79256ff + d777469 commit b26a867

File tree

6 files changed

+80
-143
lines changed

6 files changed

+80
-143
lines changed

qiita_db/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ def delete(cls, raw_data_id, study_id):
369369
"""
370370
SELECT EXISTS(
371371
SELECT * FROM qiita.prep_template AS pt
372-
LEFT JOIN qiita.common_prep_info AS cpi ON
372+
LEFT JOIN qiita.prep_template_sample AS cpi ON
373373
(pt.prep_template_id=cpi.prep_template_id)
374-
LEFT JOIN qiita.required_sample_info AS rsi ON
374+
LEFT JOIN qiita.study_sample AS rsi ON
375375
(cpi.sample_id=rsi.sample_id)
376376
WHERE raw_data_id = {0} and study_id = {1}
377377
)

qiita_db/search.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ def __repr__(self):
116116

117117

118118
class SearchTerm(object):
119-
# column names from required_sample_info table
120-
required_cols = set(get_table_cols("required_sample_info"))
121119
# column names from study table
122120
study_cols = set(get_table_cols("study"))
123121

@@ -128,7 +126,7 @@ def __init__(self, tokens):
128126
self.term[pos] = scrub_data(term)
129127

130128
def generate_sql(self):
131-
# we can assume that the metadata is either in required_sample_info
129+
# we can assume that the metadata is either in study_sample
132130
# or the study-specific table
133131
column_name, operator, argument = self.term
134132
argument_type = type(convert_type(argument))
@@ -140,9 +138,7 @@ def generate_sql(self):
140138
if operator not in allowable_types[argument_type]:
141139
raise QiitaDBIncompatibleDatatypeError(operator, argument_type)
142140

143-
if column_name in self.required_cols:
144-
column_name = "r.%s" % column_name.lower()
145-
elif column_name in self.study_cols:
141+
if column_name in self.study_cols:
146142
column_name = "st.%s" % column_name.lower()
147143
else:
148144
column_name = "sa.%s" % column_name.lower()
@@ -167,8 +163,6 @@ def __repr__(self):
167163
class QiitaStudySearch(object):
168164
"""QiitaStudySearch object to parse and run searches on studies."""
169165

170-
# column names from required_sample_info table
171-
required_cols = set(get_table_cols("required_sample_info"))
172166
# column names from study table
173167
study_cols = set(get_table_cols("study"))
174168

@@ -310,9 +304,9 @@ def _parse_study_search_string(self, searchstr,
310304
meta_header_type_lookup[header] = 'varchar'
311305

312306
# create the study finding SQL
313-
# remove metadata headers that are in required_sample_info table
314-
meta_headers = tuple(meta_headers.difference(
315-
self.required_cols).difference(self.study_cols))
307+
# remove metadata headers that are in study table
308+
meta_headers.discard('sample_id')
309+
meta_headers = tuple(meta_headers.difference(self.study_cols))
316310

317311
# get all study ids that contain all metadata categories searched for
318312
sql = []
@@ -341,17 +335,17 @@ def _parse_study_search_string(self, searchstr,
341335
# build the sql formatted list of metadata headers
342336
header_info = []
343337
for meta in meta_header_type_lookup:
344-
if meta in self.required_cols:
345-
header_info.append("r.%s" % meta)
346-
elif meta in self.study_cols:
338+
if meta in self.study_cols:
347339
header_info.append("st.%s" % meta)
348340
else:
349341
header_info.append("sa.%s" % meta)
350342
# build the SQL query
351-
sample_sql = ("SELECT r.sample_id,%s FROM qiita.required_sample_info "
352-
"r JOIN qiita.sample_{0} sa ON sa.sample_id = "
353-
"r.sample_id JOIN qiita.study st ON st.study_id = "
354-
"r.study_id WHERE %s" %
343+
344+
sample_sql = ("SELECT ss.sample_id,%s "
345+
"FROM qiita.study_sample ss "
346+
"JOIN qiita.sample_{0} sa USING (sample_id) "
347+
"JOIN qiita.study st USING (study_id) "
348+
"WHERE %s" %
355349
(','.join(header_info), sql_where))
356350
return study_sql, sample_sql, meta_header_type_lookup.keys()
357351

qiita_db/test/test_search.py

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ def test_parse_study_search_string(self):
2727
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
2828
"lower(column_name) = lower('altitude') and column_type "
2929
"in ('integer', 'float8')")
30-
exp_samp_sql = ("SELECT r.sample_id,sa.altitude FROM "
31-
"qiita.required_sample_info r JOIN qiita.sample_{0} sa"
32-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
33-
" st.study_id = r.study_id WHERE sa.altitude > 0")
30+
exp_samp_sql = ("SELECT ss.sample_id,sa.altitude "
31+
"FROM qiita.study_sample ss "
32+
"JOIN qiita.sample_{0} sa USING (sample_id) "
33+
"JOIN qiita.study st USING (study_id) "
34+
"WHERE sa.altitude > 0")
3435
self.assertEqual(st_sql, exp_st_sql)
3536
self.assertEqual(samp_sql, exp_samp_sql)
3637
self.assertEqual(meta, ["altitude"])
@@ -41,11 +42,11 @@ def test_parse_study_search_string(self):
4142
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
4243
"lower(column_name) = lower('altitude') and column_type "
4344
"in ('integer', 'float8')")
44-
exp_samp_sql = ("SELECT r.sample_id,sa.altitude FROM "
45-
"qiita.required_sample_info r JOIN qiita.sample_{0} sa"
46-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
47-
" st.study_id = r.study_id WHERE NOT "
48-
"sa.altitude > 0")
45+
exp_samp_sql = ("SELECT ss.sample_id,sa.altitude "
46+
"FROM qiita.study_sample ss "
47+
"JOIN qiita.sample_{0} sa USING (sample_id) "
48+
"JOIN qiita.study st USING (study_id) "
49+
"WHERE NOT sa.altitude > 0")
4950
self.assertEqual(st_sql, exp_st_sql)
5051
self.assertEqual(samp_sql, exp_samp_sql)
5152
self.assertEqual(meta, ["altitude"])
@@ -56,11 +57,11 @@ def test_parse_study_search_string(self):
5657
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
5758
"lower(column_name) = lower('ph') and column_type in "
5859
"('integer', 'float8')")
59-
exp_samp_sql = ("SELECT r.sample_id,sa.ph FROM "
60-
"qiita.required_sample_info r JOIN qiita.sample_{0} sa"
61-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
62-
" st.study_id = r.study_id WHERE (sa.ph > 7 AND "
63-
"sa.ph < 9)")
60+
exp_samp_sql = ("SELECT ss.sample_id,sa.ph "
61+
"FROM qiita.study_sample ss "
62+
"JOIN qiita.sample_{0} sa USING (sample_id) "
63+
"JOIN qiita.study st USING (study_id) "
64+
"WHERE (sa.ph > 7 AND sa.ph < 9)")
6465
self.assertEqual(st_sql, exp_st_sql)
6566
self.assertEqual(samp_sql, exp_samp_sql)
6667
self.assertEqual(meta, ["ph"])
@@ -71,11 +72,11 @@ def test_parse_study_search_string(self):
7172
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns WHERE "
7273
"lower(column_name) = lower('ph') and column_type in "
7374
"('integer', 'float8')")
74-
exp_samp_sql = ("SELECT r.sample_id,sa.ph FROM "
75-
"qiita.required_sample_info r JOIN qiita.sample_{0} sa"
76-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
77-
" st.study_id = r.study_id WHERE (sa.ph > 7 OR "
78-
"sa.ph < 9)")
75+
exp_samp_sql = ("SELECT ss.sample_id,sa.ph "
76+
"FROM qiita.study_sample ss "
77+
"JOIN qiita.sample_{0} sa USING (sample_id) "
78+
"JOIN qiita.study st USING (study_id) "
79+
"WHERE (sa.ph > 7 OR sa.ph < 9)")
7980
self.assertEqual(st_sql, exp_st_sql)
8081
self.assertEqual(samp_sql, exp_samp_sql)
8182
self.assertEqual(meta, ["ph"])
@@ -84,12 +85,15 @@ def test_parse_study_search_string(self):
8485
st_sql, samp_sql, meta = \
8586
self.search._parse_study_search_string(
8687
'host_subject_id includes "Chicken little"')
87-
exp_st_sql = "SELECT study_id FROM qiita.study_sample_columns"
88-
exp_samp_sql = ("SELECT r.sample_id,r.host_subject_id FROM "
89-
"qiita.required_sample_info r JOIN qiita.sample_{0} sa"
90-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
91-
" st.study_id = r.study_id WHERE "
92-
"LOWER(r.host_subject_id) LIKE '%chicken little%'")
88+
exp_st_sql = ("SELECT study_id FROM qiita.study_sample_columns "
89+
"WHERE lower(column_name) = lower('host_subject_id') "
90+
"and column_type in ('varchar')")
91+
exp_samp_sql = ("SELECT ss.sample_id,sa.host_subject_id "
92+
"FROM qiita.study_sample ss "
93+
"JOIN qiita.sample_{0} sa USING (sample_id) "
94+
"JOIN qiita.study st USING (study_id) "
95+
"WHERE LOWER(sa.host_subject_id) "
96+
"LIKE '%chicken little%'")
9397
self.assertEqual(st_sql, exp_st_sql)
9498
self.assertEqual(samp_sql, exp_samp_sql)
9599
self.assertEqual(meta, ["host_subject_id"])
@@ -104,11 +108,12 @@ def test_parse_study_search_string(self):
104108
"lower(column_name) = lower('name') and column_type in "
105109
"('varchar')")
106110
exp_samp_sql = (
107-
"SELECT r.sample_id,sa.name FROM qiita.required_sample_info r JOIN"
108-
" qiita.sample_{0} sa ON sa.sample_id = r.sample_id JOIN "
109-
"qiita.study st ON st.study_id = r.study_id WHERE (sa.name = "
110-
"'Billy Bob' OR sa.name = 'Timmy' OR (sa.name = 'Jimbo' AND "
111-
"sa.name > 25) OR sa.name < 5)")
111+
"SELECT ss.sample_id,sa.name "
112+
"FROM qiita.study_sample ss "
113+
"JOIN qiita.sample_{0} sa USING (sample_id) "
114+
"JOIN qiita.study st USING (study_id) "
115+
"WHERE (sa.name = 'Billy Bob' OR sa.name = 'Timmy' OR "
116+
"(sa.name = 'Jimbo' AND sa.name > 25) OR sa.name < 5)")
112117
self.assertEqual(st_sql, exp_st_sql)
113118
self.assertEqual(samp_sql, exp_samp_sql)
114119
self.assertEqual(meta, ['name'])
@@ -124,11 +129,11 @@ def test_parse_study_search_string(self):
124129
"('integer', 'float8')", "SELECT study_id FROM "
125130
"qiita.study_sample_columns WHERE lower(column_name) = "
126131
"lower('ph') and column_type in ('integer', 'float8')"]
127-
exp_samp_sql = ("SELECT r.sample_id,sa.pH,sa.ph FROM "
128-
"qiita.required_sample_info r JOIN qiita.sample_{0} sa"
129-
" ON sa.sample_id = r.sample_id JOIN qiita.study st ON"
130-
" st.study_id = r.study_id WHERE (sa.ph > 7 OR "
131-
"sa.ph < 9)")
132+
exp_samp_sql = ("SELECT ss.sample_id,sa.pH,sa.ph "
133+
"FROM qiita.study_sample ss "
134+
"JOIN qiita.sample_{0} sa USING (sample_id) "
135+
"JOIN qiita.study st USING (study_id) "
136+
"WHERE (sa.ph > 7 OR sa.ph < 9)")
132137
# use the split list to make sure the SQL is properly formed
133138
self.assertEqual(len(st_sql), 2)
134139
pos = exp_st_sql.index(st_sql[0])

qiita_db/test/test_setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ def test_study_raw_data(self):
5454
self.assertEqual(get_count("qiita.study_raw_data"), 4)
5555

5656
def test_required_sample_info(self):
57-
self.assertEqual(get_count("qiita.required_sample_info"), 27)
57+
self.assertEqual(get_count("qiita.study_sample"), 27)
5858

5959
def test_study_sample_columns(self):
60-
self.assertEqual(get_count("qiita.study_sample_columns"), 21)
60+
self.assertEqual(get_count("qiita.study_sample_columns"), 30)
6161

6262
def test_sample_1(self):
6363
self.assertEqual(get_count("qiita.sample_1"), 27)
@@ -66,10 +66,10 @@ def test_prep_template(self):
6666
self.assertEqual(get_count("qiita.prep_template"), 1)
6767

6868
def test_common_prep_info(self):
69-
self.assertEqual(get_count("qiita.common_prep_info"), 27)
69+
self.assertEqual(get_count("qiita.prep_template_sample"), 27)
7070

7171
def test_prep_columns(self):
72-
self.assertEqual(get_count("qiita.prep_columns"), 19)
72+
self.assertEqual(get_count("qiita.prep_columns"), 22)
7373

7474
def test_prep_1(self):
7575
self.assertEqual(get_count("qiita.prep_1"), 27)

qiita_db/test/test_util.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
check_count, get_processed_params_tables,
2727
params_dict_to_json, insert_filepaths,
2828
get_db_files_base_dir, get_data_types,
29-
get_required_sample_info_status,
30-
get_emp_status, purge_filepaths, get_filepath_id,
29+
purge_filepaths, get_filepath_id,
3130
get_lat_longs, get_mountpoint,
3231
get_mountpoint_path_by_id,
3332
get_files_from_uploads_folders,
@@ -101,7 +100,7 @@ def test_get_lat_longs(self):
101100
[38.2627021402, 3.48274264219]]
102101

103102
obs = get_lat_longs()
104-
self.assertEqual(obs, exp)
103+
self.assertItemsEqual(obs, exp)
105104

106105
def test_check_table_cols(self):
107106
# Doesn't do anything if correct info passed, only errors if wrong info
@@ -236,28 +235,6 @@ def test_get_data_types(self):
236235
exp = {v: k for k, v in exp.items()}
237236
self.assertEqual(obs, exp)
238237

239-
def test_get_required_sample_info_status(self):
240-
"""Tests that get_required_sample_info_status works"""
241-
obs = get_required_sample_info_status()
242-
exp = {'received': 1, 'in_preparation': 2, 'running': 3,
243-
'completed': 4}
244-
self.assertEqual(obs, exp)
245-
246-
obs = get_required_sample_info_status(
247-
key='required_sample_info_status_id')
248-
exp = {v: k for k, v in exp.items()}
249-
self.assertEqual(obs, exp)
250-
251-
def test_get_emp_status(self):
252-
"""Tests that get_emp_status works"""
253-
obs = get_emp_status()
254-
exp = {'EMP': 1, 'EMP_Processed': 2, 'NOT_EMP': 3}
255-
self.assertEqual(obs, exp)
256-
257-
obs = get_emp_status(key='emp_status_id')
258-
exp = {v: k for k, v in exp.items()}
259-
self.assertEqual(obs, exp)
260-
261238
def test_get_count(self):
262239
"""Checks that get_count retrieves proper count"""
263240
self.assertEqual(get_count('qiita.study_person'), 3)

qiita_db/util.py

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -212,62 +212,6 @@ def get_data_types(key='data_type'):
212212
return dict(con.execute_fetchall(sql))
213213

214214

215-
def get_required_sample_info_status(key='status'):
216-
"""Gets the list of possible required sample info status
217-
218-
Parameters
219-
----------
220-
key : {'status', 'required_sample_info_status_id'}, optional
221-
Defaults to 'status'. Determines the format of the returned dict.
222-
223-
Returns
224-
-------
225-
dict
226-
- If `key` is "status", dict is of the form
227-
{status: required_sample_info_status_id}
228-
- If `key` is "required_sample_info_status_id", dict is of the form
229-
{required_sample_info_status_id: status}
230-
"""
231-
con = SQLConnectionHandler()
232-
if key == 'status':
233-
cols = 'status, required_sample_info_status_id'
234-
elif key == 'required_sample_info_status_id':
235-
cols = 'required_sample_info_status_id, status'
236-
else:
237-
raise QiitaDBColumnError("Unknown key. Pass either 'status' or "
238-
"'required_sample_info_status_id'")
239-
sql = 'select {} from qiita.required_sample_info_status'.format(cols)
240-
return dict(con.execute_fetchall(sql))
241-
242-
243-
def get_emp_status(key='emp_status'):
244-
"""Gets the list of possible emp statuses
245-
246-
Parameters
247-
----------
248-
key : {'emp_status', 'emp_status_id'}, optional
249-
Defaults to 'status'. Determines the format of the returned dict.
250-
251-
Returns
252-
-------
253-
dict
254-
- If `key` is "emp_status", dict is of the form
255-
{emp_status: emp_status_id}
256-
- If `key` is "emp_status_id", dict is of the form
257-
{emp_status_id: emp_status}
258-
"""
259-
con = SQLConnectionHandler()
260-
if key == 'emp_status':
261-
cols = 'emp_status, emp_status_id'
262-
elif key == 'emp_status_id':
263-
cols = 'emp_status_id, emp_status'
264-
else:
265-
raise QiitaDBColumnError("Unknown key. Pass either 'emp_status' or "
266-
"'emp_status_id'")
267-
sql = 'select {} from qiita.emp_status'.format(cols)
268-
return dict(con.execute_fetchall(sql))
269-
270-
271215
def create_rand_string(length, punct=True):
272216
"""Returns a string of random ascii characters
273217
@@ -1007,10 +951,27 @@ def get_processed_params_tables():
1007951

1008952

1009953
def get_lat_longs():
954+
"""Retrieve the latitude and longitude of all the samples in the DB
955+
956+
Returns
957+
-------
958+
list of [float, float]
959+
The latitude and longitude for each sample in the database
960+
"""
1010961
conn = SQLConnectionHandler()
1011-
sql = """select latitude, longitude
1012-
from qiita.required_sample_info"""
1013-
return conn.execute_fetchall(sql)
962+
sql = """SELECT DISTINCT table_name
963+
FROM information_schema.columns
964+
WHERE SUBSTR(table_name, 1, 7) = 'sample_'
965+
AND table_schema = 'qiita'
966+
AND column_name IN ('latitude', 'longitude');"""
967+
tables_gen = (t[0] for t in conn.execute_fetchall(sql))
968+
969+
sql = "SELECT latitude, longitude FROM qiita.{0}"
970+
result = []
971+
for table in tables_gen:
972+
result.extend(conn.execute_fetchall(sql.format(table)))
973+
974+
return result
1014975

1015976

1016977
def get_environmental_packages(conn_handler=None):

0 commit comments

Comments
 (0)