Skip to content

Commit cd4f66e

Browse files
committed
alter/add tests for handlers to reflect changes
1 parent cf6ca77 commit cd4f66e

File tree

2 files changed

+113
-46
lines changed

2 files changed

+113
-46
lines changed

qiita_pet/handlers/study_handlers/listing_handlers.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,18 @@ def get(self, ignore):
181181
try:
182182
res, meta = search(query, User(user))
183183
except ParseException:
184-
print ">>>>>>ParseException"
185184
self.clear()
186185
self.set_status(400)
187186
self.write('Malformed search query. Please read "search help" '
188187
'and try again.')
189188
return
190189
except QiitaDBIncompatibleDatatypeError as e:
191-
print ">>>>>>QiitaDBIncompatibleDatatypeError"
192190
self.clear()
193191
self.set_status(400)
194-
searchmsg = 'BUUUUUTS'
192+
searchmsg = ''.join(e)
195193
self.write(searchmsg)
196194
return
197195
except:
198-
print ">>>>>>GENERIC ERROR"
199196
# catch any other error as generic server error
200197
self.set_status(500)
201198
self.write("Server error during search. Please try again "

qiita_pet/test/test_study_handlers.py

Lines changed: 112 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest import main
22
from collections import namedtuple
3+
from json import loads
34

45
from qiita_pet.test.tornado_test_base import TestHandlerBase
56
from qiita_db.study import StudyPerson, Study
@@ -12,19 +13,13 @@
1213
class TestHelpers(TestHandlerBase):
1314
database = True
1415

15-
def test_get_shared_links_for_study(self):
16-
obs = _get_shared_links_for_study(Study(1))
17-
exp = '<a target="_blank" href="mailto:shared@foo.bar">Shared</a>'
18-
self.assertEqual(obs, exp)
19-
20-
def test_build_study_info(self):
21-
Study(1).status = 'public'
22-
obs = _build_study_info('public', User('test@foo.bar'))
23-
StudyTuple = namedtuple('StudyInfo', 'id title meta_complete '
24-
'num_samples_collected shared num_raw_data pi '
25-
'pmids owner status, abstract')
26-
exp = [
27-
StudyTuple(
16+
def setUp(self):
17+
self.StudyTuple = namedtuple('StudyInfo', 'id title meta_complete '
18+
'num_samples_collected shared '
19+
'num_raw_data pi pmids owner status '
20+
'abstract')
21+
self.exp_studies = {
22+
self.StudyTuple(
2823
id=1,
2924
title='Identification of the Microbiomes for Cannabis Soils',
3025
meta_complete=True, num_samples_collected=27,
@@ -48,9 +43,19 @@ def test_build_study_info(self):
4843
'These roots were obtained November 11, 2011 from plants that '
4944
'had been harvested in the summer. Future studies will '
5045
'attempt to analyze the soils and rhizospheres from the same '
51-
'location at different time points in the plant lifecycle.')]
46+
'location at different time points in the plant lifecycle.')}
47+
super(TestHelpers, self).setUp()
48+
49+
def test_get_shared_links_for_study(self):
50+
obs = _get_shared_links_for_study(Study(1))
51+
exp = '<a target="_blank" href="mailto:shared@foo.bar">Shared</a>'
5252
self.assertEqual(obs, exp)
5353

54+
def test_build_study_info(self):
55+
Study(1).status = 'public'
56+
obs = _build_study_info('standard', User('test@foo.bar'))
57+
self.assertItemsEqual(obs, self.exp_studies)
58+
5459
def test_build_study_info_new_study(self):
5560
info = {
5661
'timeseries_type_id': 1,
@@ -62,30 +67,20 @@ def test_build_study_info_new_study(self):
6267
'study_description': 'desc',
6368
'study_alias': 'alias',
6469
'study_abstract': 'abstract'}
65-
user = User('shared@foo.bar')
70+
user = User('test@foo.bar')
6671

6772
Study.create(user, 'test_study_1', efo=[1], info=info)
6873

69-
obs = _build_study_info('private', user)
70-
71-
StudyTuple = namedtuple('StudyInfo', 'id title meta_complete '
72-
'num_samples_collected shared num_raw_data pi '
73-
'pmids owner status, abstract')
74-
exp = [
75-
StudyTuple(
76-
id=2,
77-
title='test_study_1',
78-
meta_complete=False, num_samples_collected=None,
79-
shared='',
80-
num_raw_data=0,
74+
obs = _build_study_info('standard', user)
75+
self.exp_studies.add(
76+
self.StudyTuple(
77+
id=2, title='test_study_1', meta_complete=False,
78+
num_samples_collected=None, shared='', num_raw_data=0,
8179
pi='<a target="_blank" href="mailto:PI_dude@foo.bar">'
82-
'PIDude</a>',
83-
pmids='',
84-
owner='<a target="_blank" href="mailto:shared@foo.bar">'
85-
'shared@foo.bar</a>',
86-
status='sandbox',
87-
abstract='abstract')]
88-
self.assertEqual(obs, exp)
80+
'PIDude</a>', pmids='', owner='<a target="_blank" '
81+
'href="mailto:test@foo.bar">test@foo.bar</a>',
82+
status='sandbox', abstract='abstract'))
83+
self.assertItemsEqual(obs, self.exp_studies)
8984

9085

9186
class TestStudyEditorForm(TestHandlerBase):
@@ -98,15 +93,9 @@ class TestStudyEditorExtendedForm(TestHandlerBase):
9893
pass
9994

10095

101-
class TestPrivateStudiesHandler(TestHandlerBase):
96+
class TestListStudiesHandler(TestHandlerBase):
10297
def test_get(self):
103-
response = self.get('/study/private/')
104-
self.assertEqual(response.code, 200)
105-
106-
107-
class TestPublicStudiesHandler(TestHandlerBase):
108-
def test_get(self):
109-
response = self.get('/study/public/')
98+
response = self.get('/study/list/')
11099
self.assertEqual(response.code, 200)
111100

112101

@@ -274,6 +263,87 @@ def test_get(self):
274263
self.assertEqual(response.body, 'False')
275264

276265

266+
class TestSearchStudiesAJAX(TestHandlerBase):
267+
database = False
268+
269+
json = {
270+
'iTotalRecords': 1,
271+
'aaData': [[
272+
"<input type='checkbox' value='1'>",
273+
'<a href=\'#\'\' data-toggle=\'modal\' data-target=\'#study-'
274+
'abstract-modal\' onclick=\'fillAbstract("user-studies-table", 0)'
275+
'\'><span class=\'glyphicon glyphicon-file\' aria-hidden=\'true\''
276+
'></span></a> | <a href=\'/study/description/1\' id=\'study0-title'
277+
'\'>Identification of the Microbiomes for Cannabis Soils</a>',
278+
'This is a preliminary study to examine the microbiota associated '
279+
'with the Cannabis plant. Soils samples from the bulk soil, soil '
280+
'associated with the roots, and the rhizosphere were extracted and'
281+
' the DNA sequenced. Roots from three independent plants of '
282+
'different strains were examined. These roots were obtained '
283+
'November 11, 2011 from plants that had been harvested in the '
284+
'summer. Future studies will attempt to analyze the soils and '
285+
'rhizospheres from the same location at different time points in '
286+
'the plant lifecycle.', 1,
287+
"<span class='glyphicon glyphicon-ok'></span>", 27, 4,
288+
'<span id=\'shared_html_1\'><a target="_blank" href="mailto:shared'
289+
'@foo.bar">Shared</a></span><br/><a class=\'btn btn-primary\' '
290+
'data-toggle=\'modal\' data-target=\'#share-study-modal-view\' '
291+
'onclick=\'modify_sharing(1);\'>Modify</a>',
292+
'<a target="_blank" href="mailto:PI_dude@foo.bar">PIDude</a>',
293+
'<a target="_blank" href="http://www.ncbi.nlm.nih.gov/pubmed/'
294+
'123456">123456</a>, <a target="_blank" href="http://www.ncbi.nlm.'
295+
'nih.gov/pubmed/7891011">7891011</a>', 'private']],
296+
'sEcho': 1021, 'iTotalDisplayRecords': 1}
297+
298+
empty = {'aaData': [],
299+
'iTotalDisplayRecords': 0,
300+
'iTotalRecords': 0,
301+
'sEcho': 1021}
302+
303+
def test_get(self):
304+
response = self.get('/study/search/', {
305+
'type': 'standard',
306+
'user': 'test@foo.bar',
307+
'query': '',
308+
'sEcho': '1021'
309+
})
310+
self.assertEqual(response.code, 200)
311+
# make sure responds properly
312+
self.assertEqual(loads(response.body), self.json)
313+
314+
response = self.get('/study/search/', {
315+
'type': 'shared',
316+
'user': 'test@foo.bar',
317+
'query': '',
318+
'sEcho': '1021'
319+
})
320+
self.assertEqual(response.code, 200)
321+
# make sure responds properly
322+
self.assertEqual(loads(response.body), self.empty)
323+
324+
response = self.get('/study/search/', {
325+
'type': 'standard',
326+
'user': 'test@foo.bar',
327+
'query': 'ph > 50',
328+
'sEcho': '1021'
329+
})
330+
self.assertEqual(response.code, 200)
331+
# make sure responds properly
332+
self.assertEqual(loads(response.body), self.empty)
333+
334+
def test_get_failure(self):
335+
response = self.get('/study/search/', {
336+
'type': 'standard',
337+
'user': 'test@foo.bar',
338+
'query': 'ph',
339+
'sEcho': '1021'
340+
})
341+
self.assertEqual(response.code, 400)
342+
# make sure responds properly
343+
self.assertEqual(response.body, 'Malformed search query. '
344+
'Please read "search help" and try again.')
345+
346+
277347
class TestMetadataSummaryHandler(TestHandlerBase):
278348
def test_error_prep_and_sample(self):
279349
response = self.get('/metadata_summary/', {'sample_template': 1,

0 commit comments

Comments
 (0)