Skip to content

Fix _almost_ all qiita db tests #1099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixing util.py
  • Loading branch information
josenavas committed Apr 23, 2015
commit ff4aae6f48df88f003ee633576ee350e0f9a444b
27 changes: 2 additions & 25 deletions qiita_db/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
check_count, get_processed_params_tables,
params_dict_to_json, insert_filepaths,
get_db_files_base_dir, get_data_types,
get_required_sample_info_status,
get_emp_status, purge_filepaths, get_filepath_id,
purge_filepaths, get_filepath_id,
get_lat_longs, get_mountpoint,
get_mountpoint_path_by_id,
get_files_from_uploads_folders,
Expand Down Expand Up @@ -101,7 +100,7 @@ def test_get_lat_longs(self):
[38.2627021402, 3.48274264219]]

obs = get_lat_longs()
self.assertEqual(obs, exp)
self.assertItemsEqual(obs, exp)

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

def test_get_required_sample_info_status(self):
"""Tests that get_required_sample_info_status works"""
obs = get_required_sample_info_status()
exp = {'received': 1, 'in_preparation': 2, 'running': 3,
'completed': 4}
self.assertEqual(obs, exp)

obs = get_required_sample_info_status(
key='required_sample_info_status_id')
exp = {v: k for k, v in exp.items()}
self.assertEqual(obs, exp)

def test_get_emp_status(self):
"""Tests that get_emp_status works"""
obs = get_emp_status()
exp = {'EMP': 1, 'EMP_Processed': 2, 'NOT_EMP': 3}
self.assertEqual(obs, exp)

obs = get_emp_status(key='emp_status_id')
exp = {v: k for k, v in exp.items()}
self.assertEqual(obs, exp)

def test_get_count(self):
"""Checks that get_count retrieves proper count"""
self.assertEqual(get_count('qiita.study_person'), 3)
Expand Down
72 changes: 13 additions & 59 deletions qiita_db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,62 +212,6 @@ def get_data_types(key='data_type'):
return dict(con.execute_fetchall(sql))


def get_required_sample_info_status(key='status'):
"""Gets the list of possible required sample info status

Parameters
----------
key : {'status', 'required_sample_info_status_id'}, optional
Defaults to 'status'. Determines the format of the returned dict.

Returns
-------
dict
- If `key` is "status", dict is of the form
{status: required_sample_info_status_id}
- If `key` is "required_sample_info_status_id", dict is of the form
{required_sample_info_status_id: status}
"""
con = SQLConnectionHandler()
if key == 'status':
cols = 'status, required_sample_info_status_id'
elif key == 'required_sample_info_status_id':
cols = 'required_sample_info_status_id, status'
else:
raise QiitaDBColumnError("Unknown key. Pass either 'status' or "
"'required_sample_info_status_id'")
sql = 'select {} from qiita.required_sample_info_status'.format(cols)
return dict(con.execute_fetchall(sql))


def get_emp_status(key='emp_status'):
"""Gets the list of possible emp statuses

Parameters
----------
key : {'emp_status', 'emp_status_id'}, optional
Defaults to 'status'. Determines the format of the returned dict.

Returns
-------
dict
- If `key` is "emp_status", dict is of the form
{emp_status: emp_status_id}
- If `key` is "emp_status_id", dict is of the form
{emp_status_id: emp_status}
"""
con = SQLConnectionHandler()
if key == 'emp_status':
cols = 'emp_status, emp_status_id'
elif key == 'emp_status_id':
cols = 'emp_status_id, emp_status'
else:
raise QiitaDBColumnError("Unknown key. Pass either 'emp_status' or "
"'emp_status_id'")
sql = 'select {} from qiita.emp_status'.format(cols)
return dict(con.execute_fetchall(sql))


def create_rand_string(length, punct=True):
"""Returns a string of random ascii characters

Expand Down Expand Up @@ -1008,9 +952,19 @@ def get_processed_params_tables():

def get_lat_longs():
conn = SQLConnectionHandler()
sql = """select latitude, longitude
from qiita.required_sample_info"""
return conn.execute_fetchall(sql)
sql = """SELECT DISTINCT table_name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor and non-blocking, but would you mind adding a docstring in this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

FROM information_schema.columns
WHERE SUBSTR(table_name, 1, 7) = 'sample_'
AND table_schema = 'qiita'
AND column_name IN ('latitude', 'longitude');"""
tables_gen = (t[0] for t in conn.execute_fetchall(sql))

sql = "SELECT latitude, longitude FROM qiita.{0}"
result = []
for table in tables_gen:
result.extend(conn.execute_fetchall(sql.format(table)))

return result


def get_environmental_packages(conn_handler=None):
Expand Down