[879b32]: / qiita_pet / test / rest / test_study.py

Download this file

167 lines (146 with data), 7.9 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
# -----------------------------------------------------------------------------
# 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 main
from tornado.escape import json_decode
from qiita_db.study import Study
from qiita_pet.test.rest.test_base import RESTHandlerTestCase
class StudyHandlerTests(RESTHandlerTestCase):
def test_get_valid(self):
exp = {u'title': u'Identification of the Microbiomes for Cannabis '
u'Soils',
u'contacts': {'principal_investigator': [u'PIDude',
u'Wash U',
u'PI_dude@foo.bar'],
'lab_person': [u'LabDude',
u'knight lab',
u'lab_dude@foo.bar']},
u'study_abstract':
(u'This is a preliminary study to examine the '
u'microbiota associated with the Cannabis plant. '
u'Soils samples from the bulk soil, soil '
u'associated with the roots, and the rhizosphere '
u'were extracted and the DNA sequenced. Roots '
u'from three independent plants of different '
u'strains were examined. These roots were '
u'obtained November 11, 2011 from plants that '
u'had been harvested in the summer. Future '
u'studies will attempt to analyze the soils and '
u'rhizospheres from the same location at '
u'different time points in the plant lifecycle.'),
u'study_description': (u'Analysis of the Cannabis Plant '
u'Microbiome'),
u'study_alias': 'Cannabis Soils'}
response = self.get('/api/v1/study/1', headers=self.headers)
self.assertEqual(response.code, 200)
obs = json_decode(response.body)
self.assertEqual(obs, exp)
def test_get_invalid(self):
response = self.get('/api/v1/study/0', headers=self.headers)
self.assertEqual(response.code, 404)
self.assertEqual(json_decode(response.body),
{'message': 'Study not found'})
def test_get_invalid_negative(self):
response = self.get('/api/v1/study/-1', headers=self.headers)
self.assertEqual(response.code, 404)
# not asserting the body content as this is not a valid URI according
# to the regex associating the handler to the webserver
def test_get_invalid_namespace(self):
response = self.get('/api/v1/study/1.11111', headers=self.headers)
self.assertEqual(response.code, 404)
# not asserting the body content as this is not a valid URI according
# to the regex associating the handler to the webserver
class StudyCreatorTests(RESTHandlerTestCase):
def test_post_malformed_study(self):
response = self.post('/api/v1/study', data={'foo': 'bar'},
headers=self.headers, asjson=True)
self.assertEqual(response.code, 400)
def test_post_already_exists(self):
payload = {'title': 'Identification of the Microbiomes for Cannabis '
'Soils',
'study_abstract': 'stuff',
'study_description': 'asdasd',
'owner': 'admin@foo.bar',
'study_alias': 'blah',
'notes': '',
'contacts': {'principal_investigator': [u'PIDude',
u'PI_dude@foo.bar'],
'lab_person': [u'LabDude',
u'lab_dude@foo.bar']}}
response = self.post('/api/v1/study', data=payload, asjson=True,
headers=self.headers)
self.assertEqual(response.code, 409)
obs = json_decode(response.body)
self.assertEqual(obs,
{'message': 'Study title already exists'})
def test_post_valid(self):
payload = {'title': 'foo',
'study_abstract': 'stuff',
'study_description': 'asdasd',
'owner': 'admin@foo.bar',
'study_alias': 'blah',
'notes': '',
'contacts': {'principal_investigator': [u'PIDude',
u'Wash U'],
'lab_person': [u'LabDude',
u'knight lab']}}
response = self.post('/api/v1/study', data=payload,
headers=self.headers, asjson=True)
self.assertEqual(response.code, 201)
study_id = json_decode(response.body)['id']
study = Study(int(study_id))
self.assertEqual(study.title, payload['title'])
self.assertEqual(study.info['study_abstract'],
payload['study_abstract'])
self.assertEqual(study.info['study_description'],
payload['study_description'])
self.assertEqual(study.info['study_alias'], payload['study_alias'])
self.assertEqual(study.owner.email, payload['owner'])
self.assertEqual(study.info['principal_investigator'].name,
payload['contacts']['principal_investigator'][0])
self.assertEqual(study.info['principal_investigator'].affiliation,
payload['contacts']['principal_investigator'][1])
self.assertEqual(study.info['lab_person'].name,
payload['contacts']['lab_person'][0])
self.assertEqual(study.info['lab_person'].affiliation,
payload['contacts']['lab_person'][1])
def test_post_invalid_user(self):
payload = {'title': 'foo',
'study_abstract': 'stuff',
'study_description': 'asdasd',
'owner': 'doesnotexist@foo.bar',
'study_alias': 'blah',
'notes': '',
'contacts': {'principal_investigator': [u'PIDude',
u'Wash U'],
'lab_person': [u'LabDude',
u'knight lab']}}
response = self.post('/api/v1/study', data=payload,
headers=self.headers, asjson=True)
self.assertEqual(response.code, 403)
obs = json_decode(response.body)
self.assertEqual(obs, {'message': 'Unknown user'})
class StudyStatusHandlerTests(RESTHandlerTestCase):
def test_get_no_study(self):
response = self.get('/api/v1/study/0/status', headers=self.headers)
self.assertEqual(response.code, 404)
obs = json_decode(response.body)
exp = {'message': 'Study not found'}
self.assertEqual(obs, exp)
def test_get_valid(self):
response = self.get('/api/v1/study/1/status', headers=self.headers)
self.assertEqual(response.code, 200)
exp = {'is_public': False,
'has_sample_information': True,
'sample_information_has_warnings': False,
'preparations': [{'id': 1, 'has_artifact': True},
{'id': 2, 'has_artifact': True}]
}
obs = json_decode(response.body)
self.assertEqual(obs, exp)
if __name__ == '__main__':
main()