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

Download this file

103 lines (83 with data), 4.2 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
# -----------------------------------------------------------------------------
# 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 StudyPerson
from qiita_pet.test.rest.test_base import RESTHandlerTestCase
class StudyPersonHandlerTests(RESTHandlerTestCase):
def test_get_list(self):
exp = [{'name': 'LabDude', 'affiliation': 'knight lab'},
{'name': 'empDude', 'affiliation': 'broad'},
{'name': 'PIDude', 'affiliation': 'Wash U'}]
response = self.get('/api/v1/person', headers=self.headers)
self.assertEqual(response.code, 200)
obs = json_decode(response.body)
self.assertCountEqual(obs, exp)
def test_exist(self):
exp = {'email': 'lab_dude@foo.bar', 'phone': '121-222-3333',
'address': '123 lab street', 'id': 1}
response = self.get('/api/v1/person?name=LabDude&'
'affiliation=knight%20lab', headers=self.headers)
self.assertEqual(response.code, 200)
obs = json_decode(response.body)
self.assertEqual(obs, exp)
def test_get_does_not_exist(self):
exp = {'message': 'Person not found'}
response = self.get('/api/v1/person?name=Boaty%20McBoatFace&'
'affiliation=UCSD', headers=self.headers)
self.assertEqual(response.code, 404)
obs = json_decode(response.body)
self.assertEqual(obs, exp)
def test_get_does_not_exist_affiliation(self):
exp = {'message': 'Person not found'}
response = self.get('/api/v1/person?name=LabDude%20&affiliation=UCSD',
headers=self.headers)
self.assertEqual(response.code, 404)
obs = json_decode(response.body)
self.assertEqual(obs, exp)
def test_get_invalid_query_string(self):
response = self.get('/api/v1/person?name=LabDude',
headers=self.headers)
self.assertEqual(response.code, 400)
def test_get_invalid_query_string_2(self):
response = self.get('/api/v1/person?affiliation=knight%20lab',
headers=self.headers)
self.assertEqual(response.code, 400)
def test_get_valid_extra_arguments(self):
exp = {'email': 'lab_dude@foo.bar', 'phone': '121-222-3333',
'address': '123 lab street', 'id': 1}
response = self.get('/api/v1/person?name=LabDude&'
'affiliation=knight%20lab&foo=bar',
headers=self.headers)
self.assertEqual(response.code, 200)
obs = json_decode(response.body)
self.assertEqual(obs, exp)
def test_post_new_person(self):
body = {'name': 'Boaty McBoatFace', 'affiliation': 'UCSD',
'email': 'boat@ucsd.edu', 'phone': '720-876-5309'}
response = self.post('/api/v1/person', data=body, headers=self.headers)
self.assertEqual(response.code, 201)
obs = json_decode(response.body)
exp = StudyPerson.from_name_and_affiliation(body['name'],
body['affiliation']).id
self.assertEqual(exp, obs['id'])
def test_post_existing(self):
body = {'name': 'LabDude', 'affiliation': 'knight lab',
'email': 'lab_dude@foo.bar', 'phone': '121-222-3333'}
response = self.post('/api/v1/person', data=body, headers=self.headers)
self.assertEqual(response.code, 409)
obs = json_decode(response.body)
exp = {'message': 'Person already exists'}
self.assertEqual(exp, obs)
def test_post_incomplete_details(self):
body = {'affiliation': 'knight lab',
'email': 'lab_dude@foo.bar', 'phone': '121-222-3333'}
response = self.post('/api/v1/person', data=body, headers=self.headers)
self.assertEqual(response.code, 400)
if __name__ == '__main__':
main()