-
Notifications
You must be signed in to change notification settings - Fork 3
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
✨ Study resource #175
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
''' | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.