Skip to content

Commit

Permalink
Merge pull request #1322 from squirrelo/portals-remove-fix
Browse files Browse the repository at this point in the history
Portals object removal fix
  • Loading branch information
ElDeveloper committed Jul 2, 2015
2 parents 0891923 + 33e8522 commit 7704a5e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
12 changes: 7 additions & 5 deletions qiita_db/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,35 +134,37 @@ def delete(portal):
JOIN qiita.analysis USING (analysis_id)
WHERE portal_type_id = %s and dflt = FALSE"""
analyses = conn_handler.execute_fetchall(sql, [portal_id])
if studies:
if analyses:
raise QiitaDBError(
" Cannot delete portal '%s', analyses still attached: %s" %
(portal, ', '.join([str(a[0]) for a in analyses])))

# Remove portal and default analyses for all users
sql = """DO $do$
DECLARE
pid bigint;
aid bigint;
BEGIN
FOR aid IN
SELECT analysis_id FROM qiita.analysis_portal
WHERE portal_type_id = %s
JOIN qiita.analysis USING (analysis_id)
WHERE portal_type_id = %s and dflt = True
LOOP
DELETE FROM qiita.analysis_portal
WHERE analysis_id = aid;
DELETE FROM qiita.analysis_workflow
WHERE analysis_id = aid;
DELETE FROM qiita.analysis_sample
WHERE analysis_id = aid;
DELETE FROM qiita.analysis
WHERE analysis_id = aid;
END LOOP;
DELETE FROM qiita.portal_type WHERE portal_type_id = %s;
END $do$;"""
conn_handler = SQLConnectionHandler()
conn_handler.execute(sql, [portal_id, portal_id])
conn_handler.execute(sql, [portal_id] * 2)

@staticmethod
def exists(portal):
Expand Down
44 changes: 42 additions & 2 deletions qiita_db/test/test_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from qiita_core.util import qiita_test_checker
from qiita_core.exceptions import IncompetentQiitaDeveloperError
from qiita_db.portal import Portal
from qiita_db.study import Study
from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_db.analysis import Analysis
from qiita_db.exceptions import (QiitaDBError, QiitaDBDuplicateError,
QiitaDBWarning)
Expand All @@ -14,14 +15,16 @@

@qiita_test_checker()
class TestPortal(TestCase):
portal = qiita_config.portal

def setUp(self):
self.study = Study(1)
self.analysis = Analysis(1)
self.qiita_portal = Portal('QIITA')
self.emp_portal = Portal('EMP')

def tearDown(self):
qiita_config.portal = 'QIITA'
qiita_config.portal = self.portal

def test_list_portals(self):
obs = Portal.list_portals()
Expand Down Expand Up @@ -49,6 +52,11 @@ def test_add_portal(self):

def test_remove_portal(self):
Portal.create("NEWPORTAL", "SOMEDESC")
# Select some samples on a default analysis
qiita_config.portal = "NEWPORTAL"
a = Analysis(User("test@foo.bar").default_analysis)
a.add_samples({1: ['1.SKB8.640193', '1.SKD5.640186']})

Portal.delete("NEWPORTAL")
obs = self.conn_handler.execute_fetchall(
"SELECT * FROM qiita.portal_type")
Expand All @@ -68,6 +76,38 @@ def test_remove_portal(self):
with self.assertRaises(QiitaDBError):
Portal.delete("QIITA")

Portal.create("NEWPORTAL2", "SOMEDESC")
# Add analysis to this new portal and make sure error raised
qiita_config.portal = "NEWPORTAL2"
Analysis.create(User("test@foo.bar"), "newportal analysis", "desc")
qiita_config.portal = "QIITA"
with self.assertRaises(QiitaDBError):
Portal.delete("NEWPORTAL2")

# Add study to this new portal and make sure error raised
info = {
"timeseries_type_id": 1,
"metadata_complete": True,
"mixs_compliant": True,
"number_samples_collected": 25,
"number_samples_promised": 28,
"study_alias": "FCM",
"study_description": "Microbiome of people who eat nothing but "
"fried chicken",
"study_abstract": "Exploring how a high fat diet changes the "
"gut microbiome",
"emp_person_id": StudyPerson(2),
"principal_investigator_id": StudyPerson(3),
"lab_person_id": StudyPerson(1)
}
Portal.create("NEWPORTAL3", "SOMEDESC")
qiita_config.portal = "NEWPORTAL3"
Study.create(User('test@foo.bar'), "Fried chicken microbiome",
[1], info)
qiita_config.portal = "QIITA"
with self.assertRaises(QiitaDBError):
Portal.delete("NEWPORTAL3")

def test_check_studies(self):
with self.assertRaises(QiitaDBError):
self.qiita_portal._check_studies([2000000000000, 122222222222222])
Expand Down

0 comments on commit 7704a5e

Please sign in to comment.