[879b32]: / qiita_db / test / test_portal.py

Download this file

222 lines (179 with data), 8.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -----------------------------------------------------------------------------
# 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
import numpy.testing as npt
from qiita_core.util import qiita_test_checker
from qiita_core.qiita_settings import qiita_config
import qiita_db as qdb
@qiita_test_checker()
class TestPortal(TestCase):
def setUp(self):
self.portal = qiita_config.portal
self.study = qdb.study.Study(1)
self.analysis = qdb.analysis.Analysis(1)
self.qiita_portal = qdb.portal.Portal('QIITA')
self.emp_portal = qdb.portal.Portal('EMP')
def tearDown(self):
qiita_config.portal = self.portal
def test_list_portals(self):
obs = qdb.portal.Portal.list_portals()
exp = ['EMP']
self.assertEqual(obs, exp)
def test_add_portal(self):
obs = qdb.portal.Portal.create("NEWPORTAL", "SOMEDESC")
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.portal_type")
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp = [[1, 'QIITA', 'QIITA portal. Access to all data stored '
'in database.'],
[2, 'EMP', 'EMP portal'],
[4, 'NEWPORTAL', 'SOMEDESC']]
self.assertCountEqual(obs, exp)
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.analysis_portal")
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp = [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 2], [8, 2],
[9, 2], [10, 2], [11, 4], [12, 4], [13, 4], [14, 4],
[15, 4], [16, 4], [17, 4]]
self.assertCountEqual(obs, exp)
with self.assertRaises(qdb.exceptions.QiitaDBDuplicateError):
qdb.portal.Portal.create("EMP", "DOESNTMATTERFORDESC")
qdb.portal.Portal.delete('NEWPORTAL')
def test_remove_portal(self):
qdb.portal.Portal.create("NEWPORTAL", "SOMEDESC")
# Select some samples on a default analysis
qiita_config.portal = "NEWPORTAL"
a = qdb.user.User("test@foo.bar").default_analysis
a.add_samples({1: ['1.SKB8.640193', '1.SKD5.640186']})
qdb.portal.Portal.delete("NEWPORTAL")
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.portal_type")
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp = [[1, 'QIITA', 'QIITA portal. Access to all data stored '
'in database.'],
[2, 'EMP', 'EMP portal']]
self.assertCountEqual(obs, exp)
with qdb.sql_connection.TRN:
qdb.sql_connection.TRN.add("SELECT * FROM qiita.analysis_portal")
obs = qdb.sql_connection.TRN.execute_fetchindex()
exp = [[1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 2], [8, 2],
[9, 2], [10, 2]]
self.assertCountEqual(obs, exp)
with self.assertRaises(qdb.exceptions.QiitaDBLookupError):
qdb.portal.Portal.delete("NOEXISTPORTAL")
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.portal.Portal.delete("QIITA")
qdb.portal.Portal.create("NEWPORTAL2", "SOMEDESC")
# Add study to this new portal and make sure error raised
info = {
"timeseries_type_id": 1,
"metadata_complete": True,
"mixs_compliant": True,
"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",
"principal_investigator_id": qdb.study.StudyPerson(3),
"lab_person_id": qdb.study.StudyPerson(1)
}
qdb.portal.Portal.create("NEWPORTAL3", "SOMEDESC")
qiita_config.portal = "NEWPORTAL3"
qdb.study.Study.create(
qdb.user.User('test@foo.bar'), "Fried chicken microbiome", info)
qiita_config.portal = "QIITA"
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.portal.Portal.delete("NEWPORTAL3")
def test_check_studies(self):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.qiita_portal._check_studies([2000000000000, 122222222222222])
def test_check_analyses(self):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.qiita_portal._check_analyses([2000000000000, 122222222222222])
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.qiita_portal._check_analyses([8, 9])
def test_get_studies_by_portal(self):
obs = self.emp_portal.get_studies()
self.assertEqual(obs, set())
obs = self.qiita_portal.get_studies()
self.assertEqual(obs, {qdb.study.Study(1)})
def test_add_study_portals(self):
obs = qdb.portal.Portal.create("NEWPORTAL4", "SOMEDESC")
obs.add_studies([self.study.id])
self.assertCountEqual(self.study._portals, ['NEWPORTAL4', 'QIITA'])
npt.assert_warns(qdb.exceptions.QiitaDBWarning, obs.add_studies,
[self.study.id])
obs.remove_studies([self.study.id])
qdb.portal.Portal.delete("NEWPORTAL4")
def test_remove_study_portals(self):
with self.assertRaises(ValueError):
self.qiita_portal.remove_studies([self.study.id])
self.emp_portal.add_studies([1])
# Set up the analysis in EMP portal
self.emp_portal.add_analyses([self.analysis.id])
obs = self.analysis._portals
self.assertCountEqual(obs, ['QIITA', 'EMP'])
# Test study removal failure
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.emp_portal.remove_studies([self.study.id])
obs = self.study._portals
self.assertCountEqual(obs, ['QIITA', 'EMP'])
# Test study removal
self.emp_portal.remove_analyses([self.analysis.id])
self.emp_portal.remove_studies([self.study.id])
obs = self.study._portals
self.assertEqual(obs, ['QIITA'])
obs = npt.assert_warns(
qdb.exceptions.QiitaDBWarning, self.emp_portal.remove_studies,
[self.study.id])
def test_get_analyses_by_portal(self):
qiita_config.portal = 'EMP'
exp = {qdb.analysis.Analysis(7), qdb.analysis.Analysis(8),
qdb.analysis.Analysis(9), qdb.analysis.Analysis(10)}
obs = self.emp_portal.get_analyses()
self.assertEqual(obs, exp)
qiita_config.portal = 'QIITA'
exp = {qdb.analysis.Analysis(1), qdb.analysis.Analysis(2),
qdb.analysis.Analysis(3), qdb.analysis.Analysis(4),
qdb.analysis.Analysis(5), qdb.analysis.Analysis(6)}
obs = self.qiita_portal.get_analyses()
self.assertEqual(obs, exp)
def test_add_analysis_portals(self):
obs = self.analysis._portals
self.assertEqual(obs, ['QIITA'])
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.emp_portal.add_analyses([self.analysis.id])
obs = self.analysis._portals
self.assertEqual(obs, ['QIITA'])
self.emp_portal.add_studies([1])
self.emp_portal.add_analyses([self.analysis.id])
obs = self.analysis._portals
self.assertEqual(obs, ['EMP', 'QIITA'])
npt.assert_warns(
qdb.exceptions.QiitaDBWarning, self.emp_portal.add_analyses,
[self.analysis.id])
self.emp_portal.remove_analyses([self.analysis.id])
self.emp_portal.remove_studies([1])
def test_remove_analysis_portals(self):
with self.assertRaises(ValueError):
self.qiita_portal.remove_analyses([self.analysis.id])
# set up the analysis in EMP portal
self.emp_portal.add_studies([1])
self.emp_portal.add_analyses([self.analysis.id])
obs = self.analysis._portals
self.assertCountEqual(obs, ['QIITA', 'EMP'])
# Test removal
self.emp_portal.remove_analyses([self.analysis.id])
obs = self.analysis._portals
self.assertEqual(obs, ['QIITA'])
obs = npt.assert_warns(
qdb.exceptions.QiitaDBWarning, self.emp_portal.remove_analyses,
[self.analysis.id])
self.emp_portal.remove_studies([1])
if __name__ == '__main__':
main()