Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Study resource #175

Merged
merged 2 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dataservice/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from dataservice.api.diagnosis import DiagnosisListAPI
from dataservice.api.sample import SampleAPI
from dataservice.api.sample import SampleListAPI
from dataservice.api.study import StudyAPI
from dataservice.api.study import StudyListAPI
from dataservice.api.demographic import DemographicAPI
from dataservice.api.demographic import DemographicListAPI

Expand Down
2 changes: 2 additions & 0 deletions dataservice/api/study/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from dataservice.api.study.resources import StudyAPI
from dataservice.api.study.resources import StudyListAPI
134 changes: 134 additions & 0 deletions dataservice/api/study/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from flask import abort, request
from marshmallow import ValidationError

from dataservice.extensions import db
from dataservice.api.common.pagination import paginated, Pagination
from dataservice.api.study.models import Study
from dataservice.api.study.schemas import StudySchema
from dataservice.api.common.views import CRUDView


class StudyListAPI(CRUDView):
"""
Study API
"""
endpoint = 'studies_list'
rule = '/studies'
schemas = {'Study': StudySchema}

@paginated
def get(self, after, limit):
"""
Get a paginated studies
---
template:
path:
get_list.yml
properties:
resource:
Study
"""
q = Study.query

return (StudySchema(many=True)
.jsonify(Pagination(q, after, limit)))

def post(self):
"""
Create a new study
---
template:
path:
new_resource.yml
properties:
resource:
Study
"""
try:
st = StudySchema(strict=True).load(request.json).data
except ValidationError as err:
abort(400, 'could not create study: {}'.format(err.messages))

db.session.add(st)
db.session.commit()
return StudySchema(
201, 'study {} created'.format(st.kf_id)
).jsonify(st), 201


class StudyAPI(CRUDView):
"""
Study API
"""
endpoint = 'studies'
rule = '/studies/<string:kf_id>'
schemas = {'Study': StudySchema}

def get(self, kf_id):
"""
Get a study by id
---
template:
path:
get_by_id.yml
properties:
resource:
Study
"""
st = Study.query.get(kf_id)
if st is None:
abort(404, 'could not find {} `{}`'
.format('study', kf_id))
return StudySchema().jsonify(st)

def patch(self, kf_id):
"""
Update an existing study. Allows partial update of resource
---
template:
path:
update_by_id.yml
properties:
resource:
Study
"""
body = request.json
st = Study.query.get(kf_id)
if st is None:
abort(404, 'could not find {} `{}`'
.format('study', kf_id))

try:
st = (StudySchema(strict=True).load(body, instance=st,
partial=True).data)
except ValidationError as err:
abort(400, 'could not update study: {}'.format(err.messages))

db.session.add(st)
db.session.commit()

return StudySchema(
200, 'study {} updated'.format(st.kf_id)
).jsonify(st), 200

def delete(self, kf_id):
"""
Delete study by id
---
template:
path:
delete_by_id.yml
properties:
resource:
Study
"""
st = Study.query.get(kf_id)
if st is None:
abort(404, 'could not find {} `{}`'.format('study', kf_id))

db.session.delete(st)
db.session.commit()

return StudySchema(
200, 'study {} deleted'.format(st.kf_id)
).jsonify(st), 200
16 changes: 16 additions & 0 deletions dataservice/api/study/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from dataservice.api.study.models import Study
from dataservice.api.common.schemas import BaseSchema
from dataservice.extensions import ma


class StudySchema(BaseSchema):

class Meta(BaseSchema.Meta):
model = Study
resource_url = 'api.studies'
collection_url = 'api.studies_list'

_links = ma.Hyperlinks({
'self': ma.URLFor(Meta.resource_url, kf_id='<kf_id>'),
'collection': ma.URLFor(Meta.collection_url)
})
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def entities(client):

# Add kf_ids
inputs['kf_ids'] = {'/participants': p.kf_id}
inputs['kf_ids'].update({'/studies': study.kf_id})
inputs['kf_ids'].update({'/demographics': p.demographic.kf_id})
inputs['kf_ids'].update({'/diagnoses': diagnosis.kf_id})
inputs['kf_ids'].update({'/samples': sample.kf_id})
Expand Down
147 changes: 147 additions & 0 deletions tests/study/test_study_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import json

from flask import url_for

from dataservice.api.study.models import Study
from tests.utils import FlaskTestCase

STUDY_URL = 'api.studies'
STUDY_LIST_URL = 'api.studies_list'


class StudyTest(FlaskTestCase):
'''
Test study api endopoints
'''

def test_post_study(self):
'''
Test creating a new study
'''
response = self._make_study(external_id='TEST')
resp = json.loads(response.data.decode('utf-8'))

self.assertEqual(response.status_code, 201)

self.assertIn('study', resp['_status']['message'])
self.assertIn('created', resp['_status']['message'])
self.assertNotIn('_id', resp['results'])

s = Study.query.first()
study = resp['results']
self.assertEqual(s.kf_id, study['kf_id'])
self.assertEqual(s.external_id, study['external_id'])

def test_get_study(self):
'''
Test retrieving a study by id
'''
resp = self._make_study('TEST')
resp = json.loads(resp.data.decode('utf-8'))
kf_id = resp['results']['kf_id']

response = self.client.get(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers())
resp = json.loads(response.data.decode('utf-8'))
self.assertEqual(response.status_code, 200)

study = resp['results']
self.assertEqual(kf_id, study['kf_id'])

def test_patch_study(self):
Copy link
Member

@znatty22 znatty22 Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea to check the database as well. Make sure that only the fields in the body were modified and others remained the same.

'''
Test updating an existing study
'''
response = self._make_study(external_id='TEST')
resp = json.loads(response.data.decode('utf-8'))
study = resp['results']
kf_id = study.get('kf_id')
external_id = study.get('external_id')

# Update the study via http api
body = {
'external_id': 'new_id'
}
response = self.client.patch(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers(),
data=json.dumps(body))
self.assertEqual(response.status_code, 200)

self.assertEqual(Study.query.get(kf_id).external_id,
body['external_id'])

resp = json.loads(response.data.decode('utf-8'))
self.assertIn('study', resp['_status']['message'])
self.assertIn('updated', resp['_status']['message'])

study = resp['results']
self.assertEqual(study['kf_id'], kf_id)
self.assertEqual(study['external_id'], body['external_id'])

def test_patch_study_no_required_field(self):
'''
Test that we may update the study without a required field
'''
response = self._make_study(external_id='TEST')
resp = json.loads(response.data.decode('utf-8'))
study = resp['results']
kf_id = study.get('kf_id')
external_id = study.get('external_id')

# Update the study via http api
body = {
'version': '2.0'
}
response = self.client.patch(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers(),
data=json.dumps(body))
self.assertEqual(response.status_code, 200)

self.assertEqual(Study.query.get(kf_id).version, '2.0')

resp = json.loads(response.data.decode('utf-8'))
self.assertIn('study', resp['_status']['message'])
self.assertIn('updated', resp['_status']['message'])

study = resp['results']
self.assertEqual(study['kf_id'], kf_id)
self.assertEqual(study['external_id'], external_id)
self.assertEqual(study['version'], body['version'])

def test_delete_study(self):
'''
Test deleting a study by id
'''
resp = self._make_study('TEST')
resp = json.loads(resp.data.decode('utf-8'))
kf_id = resp['results']['kf_id']

response = self.client.delete(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers())

resp = json.loads(response.data.decode('utf-8'))
self.assertEqual(response.status_code, 200)

response = self.client.get(url_for(STUDY_URL,
kf_id=kf_id),
headers=self._api_headers())

resp = json.loads(response.data.decode('utf-8'))
self.assertEqual(response.status_code, 404)

def _make_study(self, external_id='TEST-0001'):
'''
Convenience method to create a study with a given source name
'''
body = {
'external_id': external_id,
'version': '1.0'
}
response = self.client.post(url_for(STUDY_LIST_URL),
headers=self._api_headers(),
data=json.dumps(body))
return response
20 changes: 15 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class TestAPI:
('/demographics/123', 'GET', 404),
('/participants', 'GET', 200),
('/persons', 'GET', 404),
('/participants/123', 'GET', 404)
('/participants/123', 'GET', 404),
('/studies', 'GET', 200),
('/studies/123', 'GET', 404)
])
def test_status_codes(self, client, endpoint, method, status_code):
""" Test endpoint response codes """
Expand Down Expand Up @@ -47,7 +49,11 @@ def test_status_codes(self, client, endpoint, method, status_code):
('/participants', 'GET', 'success'),
('/participants/123', 'GET', 'could not find participant `123`'),
('/participants/123', 'PATCH', 'could not find participant `123`'),
('/participants/123', 'DELETE', 'could not find participant `123`')
('/participants/123', 'DELETE', 'could not find participant `123`'),
('/studies', 'GET', 'success'),
('/studies/123', 'GET', 'could not find study `123`'),
('/studies/123', 'PATCH', 'could not find study `123`'),
('/studies/123', 'DELETE', 'could not find study `123`'),
])
def test_status_messages(self, client, endpoint, method, status_message):
"""
Expand All @@ -63,7 +69,8 @@ def test_status_messages(self, client, endpoint, method, status_message):
('/participants', 'GET'),
('/samples', 'GET'),
('/diagnoses', 'GET'),
('/demographics', 'GET')
('/demographics', 'GET'),
('/studies', 'GET')
])
def test_status_format(self, client, endpoint, method):
""" Test that the _response field is consistent """
Expand All @@ -80,7 +87,9 @@ def test_status_format(self, client, endpoint, method):
('/participants', 'PATCH', ['created_at', 'modified_at']),
('/demographics', 'PATCH', ['created_at', 'modified_at']),
('/diagnoses', 'PATCH', ['created_at', 'modified_at']),
('/samples', 'PATCH', ['created_at', 'modified_at'])
('/samples', 'PATCH', ['created_at', 'modified_at']),
('/studies', 'PATCH', ['created_at', 'modified_at']),
('/studies', 'PATCH', ['created_at', 'modified_at'])
])
def test_read_only(self, client, entities, endpoint, method, fields):
""" Test that given fields can not be written or modified """
Expand All @@ -103,7 +112,8 @@ def test_read_only(self, client, entities, endpoint, method, fields):
@pytest.mark.parametrize('endpoint', ['/participants',
'/demographics',
'/diagnoses',
'/samples'])
'/samples',
'/studies'])
def test_unknown_field(self, client, entities, endpoint, method):
""" Test that unknown fields are rejected when trying to create """
inputs = entities[endpoint]
Expand Down
Loading