Skip to content

Commit

Permalink
Merge 8f388b0 into d1d64e1
Browse files Browse the repository at this point in the history
  • Loading branch information
squirrelo committed Jun 11, 2014
2 parents d1d64e1 + 8f388b0 commit 895a077
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 140 deletions.
10 changes: 10 additions & 0 deletions qiita_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ class QiitaJobError(QiitaError):
class QiitaStudyError(QiitaError):
"""Exception for error when handling with study objects"""
pass


class IncorrectPasswordError(QiitaError):
"""User passes wrong password"""
pass


class IncorrectEmailError(QiitaError):
"""Email fails validation"""
pass
1 change: 0 additions & 1 deletion qiita_core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from qiita_db.sql_connection import SQLConnectionHandler
from qiita_db.make_environment import (LAYOUT_FP, INITIALIZE_FP, POPULATE_FP)
from qiita_core.qiita_settings import qiita_config
Expand Down
2 changes: 2 additions & 0 deletions qiita_db/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Analysis(QiitaStatusObject):
"""

_table = "analysis"

@staticmethod
def create(owner):
"""Creates a new analysis on the database
Expand Down
14 changes: 11 additions & 3 deletions qiita_db/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
from __future__ import division

# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
Expand All @@ -9,6 +6,7 @@
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from __future__ import division
from qiita_core.exceptions import QiitaError


Expand All @@ -32,6 +30,16 @@ class QiitaDBConnectionError(QiitaDBError):
pass


class QiitaDBDuplicateError(QiitaDBError):
"""Exception when duplicating something in the database"""
pass


class QiitaDBColumnError(QiitaDBError):
"""Exception when database column info missing or incorrect"""
pass


class QiitaDBUnknownIDError(QiitaDBError):
"""Exception for error when an object does not exists in the DB"""
def __init__(self, missing_id, table):
Expand Down
7 changes: 5 additions & 2 deletions qiita_db/support_files/initialize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
INSERT INTO qiita.user_level (name, description) VALUES ('admin', 'Can access and do all the things'), ('dev', 'Can access all data and info about errors'), ('superuser', 'Can see all studies, can run analyses'), ('user', 'Can see own and public data, can run analyses'), ('unverified', 'Email not verified'), ('guest', 'Can view & download public data');

-- Populate analysis_status table
INSERT INTO qiita.analysis_status (status) VALUES ('in_construction'), ('queued'), ('running'), ('completed'), ('error');
INSERT INTO qiita.analysis_status (status) VALUES ('in_construction'), ('queued'), ('running'), ('completed'), ('error'), ('public');

-- Populate job_status table
INSERT INTO qiita.job_status (status) VALUES ('queued'), ('running'), ('completed'), ('error');
Expand Down Expand Up @@ -35,4 +35,7 @@ INSERT INTO qiita.required_sample_info_status (status) VALUES ('received'), ('in
INSERT INTO qiita.filepath_type (filepath_type) VALUES ('raw_sequences'), ('raw_barcodes'), ('raw_spectra'), ('preprocessed_sequences'), ('preprocessed_sequences_qual'), ('biom');

-- Populate checksum_algorithm table
INSERT INTO qiita.checksum_algorithm (name) VALUES ('crc32')
INSERT INTO qiita.checksum_algorithm (name) VALUES ('crc32');

-- Populate commands available
INSERT INTO qiita.command (name, command) VALUES ('Summarize taxa through plots', 'summarize_taxa_through_plots.py'), ('Beta diversity through plots', 'beta_diversity_through_plots.py');
24 changes: 24 additions & 0 deletions qiita_db/support_files/populate_test_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,27 @@ INSERT INTO qiita.filepath (filepath, filepath_type_id, checksum, checksum_algor

-- Insert (link) the processed data with the processed filepath
INSERT INTO qiita.processed_filepath (processed_data_id, filepath_id) VALUES (1, 5);

-- Insert filepath for job results files
INSERT INTO qiita.filepath (filepath, filepath_type_id, checksum, checksum_algorithm_id) VALUES ('job1result.txt', 1, '852952723', 1), ('job2result.txt', 1, '852952723', 1);

-- Insert jobs
INSERT INTO qiita.job (data_type_id, job_status_id, command_id, options) VALUES (1, 3, 1, 'options1'), (1, 3, 2, 'options2');

-- Insert Analysis
INSERT INTO qiita.analysis (email, name, description, analysis_status_id, pmid) VALUES ('test@foo.bar', 'SomeAnalysis', 'A test analysis', 4, '121112');

-- Attach jobs to analysis
INSERT INTO qiita.analysis_job (analysis_id, job_id) VALUES (1, 1), (1, 2);

-- Attach filepath to analysis
INSERT INTO qiita.analysis_filepath (analysis_id, filepath_id) VALUES (1, 7);

-- Attach samples to analysis
INSERT INTO qiita.analysis_sample (analysis_id, processed_data_id, sample_id) VALUES (1,1,'SKB8.640193'), (1,1,'SKD8.640184'), (1,1,'SKB7.640196'), (1,1,'SKM9.640192'), (1,1,'SKM4.640180');

--Share analysis with shared user
INSERT INTO qiita.analysis_users (analysis_id, email) VALUES (1, 'shared@foo.bar');

-- Add job results
INSERT INTO qiita.job_results_filepath (job_id, filepath_id) VALUES (1, 8), (2, 9);
22 changes: 21 additions & 1 deletion qiita_db/test/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_study_experimental_factor(self):
self._check_count("qiita.study_experimental_factor", 1)

def test_filepath(self):
self._check_count("qiita.filepath", 7)
self._check_count("qiita.filepath", 9)

def test_raw_data(self):
self._check_count("qiita.raw_data", 2)
Expand Down Expand Up @@ -100,6 +100,26 @@ def test_processed_params_uclust(self):
def test_processed_filepath(self):
self._check_count("qiita.processed_filepath", 1)

def test_job(self):
self._check_count("qiita.job", 2)

def test_analysis(self):
self._check_count("qiita.analysis", 1)

def test_analysis_job(self):
self._check_count("qiita.analysis_job", 2)

def test_analysis_filepath(self):
self._check_count("qiita.analysis_filepath", 1)

def test_analysis_sample(self):
self._check_count("qiita.analysis_sample", 5)

def test_analysis_users(self):
self._check_count("qiita.analysis_users", 1)

def test_job_results_filepath(self):
self._check_count("qiita.job_results_filepath", 2)

if __name__ == '__main__':
main()
183 changes: 183 additions & 0 deletions qiita_db/test/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from unittest import TestCase, main

from qiita_core.exceptions import (IncompetentQiitaDeveloperError,
IncorrectEmailError, IncorrectPasswordError)
from qiita_core.util import qiita_test_checker
from qiita_db.user import User
from qiita_db.study import Study
from qiita_db.analysis import Analysis
from qiita_db.sql_connection import SQLConnectionHandler
from qiita_db.exceptions import QiitaDBDuplicateError, QiitaDBColumnError


@qiita_test_checker()
class UserTest(TestCase):
"""Tests the User object and all properties/methods"""

def setUp(self):
self.conn = SQLConnectionHandler()
self.user = User('admin@foo.bar')

self.userinfo = {
'name': 'Dude',
'affiliation': 'Nowhere University',
'address': '123 fake st, Apt 0, Faketown, CO 80302',
'phone': '111-222-3344'
}

def _check_correct_info(self, obs, exp):
self.assertEqual(set(exp.keys()), set(obs.keys()))
for key in exp:
# user_verify_code and password seed randomly generated so just
# making sure they exist and is correct length
if key == 'user_verify_code':
self.assertEqual(len(obs[key]), 20)
elif key == "password":
self.assertEqual(len(obs[key]), 60)
else:
self.assertEqual(obs[key], exp[key])

def test_create_user(self):
user = User.create('new@test.bar', 'password')
self.assertEqual(user.id, 'new@test.bar')
sql = "SELECT * from qiita.qiita_user WHERE email = 'new@test.bar'"
obs = self.conn.execute_fetchall(sql)
self.assertEqual(len(obs), 1)
obs = dict(obs[0])
exp = {
'password': '',
'name': None,
'pass_reset_timestamp': None,
'affiliation': None,
'pass_reset_code': None,
'phone': None,
'user_verify_code': '',
'address': None,
'user_level_id': 5,
'email': 'new@test.bar'}
self._check_correct_info(obs, exp)

def test_create_user_info(self):
user = User.create('new@test.bar', 'password', self.userinfo)
self.assertEqual(user.id, 'new@test.bar')
sql = "SELECT * from qiita.qiita_user WHERE email = 'new@test.bar'"
obs = self.conn.execute_fetchall(sql)
self.assertEqual(len(obs), 1)
obs = dict(obs[0])
exp = {
'password': '',
'name': 'Dude',
'affiliation': 'Nowhere University',
'address': '123 fake st, Apt 0, Faketown, CO 80302',
'phone': '111-222-3344',
'pass_reset_timestamp': None,
'pass_reset_code': None,
'user_verify_code': '',
'user_level_id': 5,
'email': 'new@test.bar'}
self._check_correct_info(obs, exp)

def test_create_user_column_not_allowed(self):
self.userinfo["pass_reset_code"] = "FAIL"
with self.assertRaises(QiitaDBColumnError):
User.create('new@test.bar', 'password', self.userinfo)

def test_create_user_non_existent_column(self):
self.userinfo["BADTHING"] = "FAIL"
with self.assertRaises(QiitaDBColumnError):
User.create('new@test.bar', 'password', self.userinfo)

def test_create_user_duplicate(self):
with self.assertRaises(QiitaDBDuplicateError):
User.create('test@foo.bar', 'password')

def test_create_user_bad_email(self):
with self.assertRaises(IncorrectEmailError):
User.create('notanemail', 'password')

def test_create_user_bad_password(self):
with self.assertRaises(IncorrectPasswordError):
User.create('new@test.com', '')

def test_login(self):
self.assertEqual(User.login("test@foo.bar", "password"),
User("test@foo.bar"))

def test_login_incorrect_user(self):
with self.assertRaises(IncorrectEmailError):
User.login("notexist@foo.bar", "password")

def test_login_incorrect_password(self):
with self.assertRaises(IncorrectPasswordError):
User.login("test@foo.bar", "WRONGPASSWORD")

def test_login_invalid_password(self):
with self.assertRaises(IncorrectPasswordError):
User.login("test@foo.bar", "SHORT")

def test_exists(self):
self.assertTrue(User.exists("test@foo.bar"))

def test_exists_notindb(self):
self.assertFalse(User.exists("notexist@foo.bar"))

def test_exists_invaid_email(self):
with self.assertRaises(IncorrectEmailError):
User.exists("notanemail@badformat")

def test_get_email(self):
self.assertEqual(self.user.email, 'admin@foo.bar')

def test_get_level(self):
self.assertEqual(self.user.level, "user")

def test_get_info(self):
expinfo = {
'name': 'Admin',
'affiliation': 'Owner University',
'address': '312 noname st, Apt K, Nonexistantown, CO 80302',
'phone': '222-444-6789'
}
self.assertEqual(self.user.info, expinfo)

def test_set_info(self):
self.user.info = self.userinfo
self.assertEqual(self.user.info, self.userinfo)

def test_set_info_not_info(self):
"""Tests setting info with a non-allowed column"""
self.userinfo["email"] = "FAIL"
with self.assertRaises(QiitaDBColumnError):
self.user.info = self.userinfo

def test_set_info_bad_info(self):
"""Test setting info with a key not in the table"""
self.userinfo["BADTHING"] = "FAIL"
with self.assertRaises(QiitaDBColumnError):
self.user.info = self.userinfo

def test_get_private_studies(self):
user = User('test@foo.bar')
self.assertEqual(user.private_studies, [Study(1)])

def test_get_shared_studies(self):
user = User('shared@foo.bar')
self.assertEqual(user.shared_studies, [Study(1)])

def test_get_private_analyses(self):
self.assertEqual(self.user.private_analyses, [])

def test_get_shared_analyses(self):
self.assertEqual(self.user.shared_analyses, [])


if __name__ == "__main__":
main()
50 changes: 50 additions & 0 deletions qiita_db/test/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from unittest import TestCase, main

from qiita_core.util import qiita_test_checker
from qiita_db.util import check_table_cols, check_required_columns
from qiita_db.sql_connection import SQLConnectionHandler
from qiita_db.exceptions import QiitaDBColumnError


@qiita_test_checker()
class DBUtilTests(TestCase):
def setUp(self):
self.conn_handler = SQLConnectionHandler()
self.table = 'study'
self.required = [
'number_samples_promised', 'study_title', 'mixs_compliant',
'metadata_complete', 'study_description', 'first_contact',
'reprocess', 'study_status_id', 'portal_type_id',
'timeseries_type_id', 'study_alias', 'study_abstract',
'principal_investigator_id', 'email', 'number_samples_collected']

def test_check_required_columns(self):
# Doesn't do anything if correct info passed, only errors if wrong info
check_required_columns(self.conn_handler, self.required, self.table)

def test_check_required_columns_fail(self):
self.required.remove('study_title')
with self.assertRaises(QiitaDBColumnError):
check_required_columns(self.conn_handler, self.required,
self.table)

def test_check_table_cols(self):
# Doesn't do anything if correct info passed, only errors if wrong info
check_table_cols(self.conn_handler, self.required, self.table)

def test_check_table_cols_fail(self):
self.required.append('BADTHINGNOINHERE')
with self.assertRaises(QiitaDBColumnError):
check_table_cols(self.conn_handler, self.required,
self.table)

if __name__ == "__main__":
main()

0 comments on commit 895a077

Please sign in to comment.