-
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
✨ Study resource #175
Conversation
f76e73f
to
f1e7ed7
Compare
dataservice/api/study/resources.py
Outdated
abort(404, 'could not find {} `{}`' | ||
.format('Study', kf_id)) | ||
|
||
st = StudySchema(strict=True).load(body, instance=st).data |
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.
Couple requested changes here..
- Add the
partial=True
kw arg to the load so that marshmallow allows you to deserialize without all required fields. With PATCH we want to allow someone to update one or more fields without having to include all required fields. - Surround the load with a try/except ValidationError.
This is what i did for participant:
# Partial update - validate but allow missing required fields
try:
p = ParticipantSchema(strict=True).load(body, instance=p,
partial=True).data
except ValidationError as err:
abort(400, 'could not update participant: {}'.format(err.messages))
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.
hmm I thought we had a general ValidationError
handler, maybe we should add one?
dataservice/api/study/resources.py
Outdated
|
||
def patch(self, kf_id): | ||
""" | ||
Update an existing study |
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.
Can we update the comment to something like:
Update an existing study. Allows for partial update of resource.
study = resp['results'] | ||
self.assertEqual(kf_id, study['kf_id']) | ||
|
||
def test_patch_study(self): |
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.
dataservice/api/study/resources.py
Outdated
Study | ||
""" | ||
try: | ||
st = Study.query.filter_by(kf_id=kf_id).one() |
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.
Should we change this to use .get
now that kf_id
is the primary key? If we decide to make that change, we'll also have to change the try/except NoResultFound to a if else statement to check if st
is None. I think get() returns None if obj is not found
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.
yeah, I'd like to use get_or_404()
, but you're not able to pass a custom message to it. If we could do that, these 5 lines would be reduced down to one tidy line. We could consider hacking get_or_404()
to take a message arg or not having a specific entity type returned in the not found response.
dataservice/api/study/resources.py
Outdated
""" | ||
body = request.json | ||
try: | ||
st = Study.query.filter_by(kf_id=kf_id).one() |
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.
Similar to above comment, should we change this to use .get
now that kf_id
is the primary key?
dataservice/api/study/resources.py
Outdated
Study | ||
""" | ||
try: | ||
st = Study.query.filter_by(kf_id=kf_id).one() |
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.
Similar to above comment, should we change this to use .get
now that kf_id
is the primary key?
tests/test_api.py
Outdated
@@ -85,7 +94,8 @@ def test_read_only(self, client, entities, endpoint, field): | |||
|
|||
@pytest.mark.parametrize('endpoint,field', [ | |||
('/participants', 'blah'), | |||
('/samples', 'blah') | |||
('/samples', 'blah'), | |||
('/studies', 'blah'), | |||
]) | |||
def test_unknown_field(self, client, entities, endpoint, field): |
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.
dataservice/api/study/resources.py
Outdated
st = StudySchema(strict=True).load(body, instance=st).data | ||
db.session.commit() | ||
|
||
return StudySchema( |
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.
Should return 200
instead of 201
since we are not creating a new resource
tests/study/test_study_resources.py
Outdated
kf_id=kf_id), | ||
headers=self._api_headers(), | ||
data=json.dumps(body)) | ||
self.assertEqual(response.status_code, 201) |
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.
Rember to change to check for 200
after updating patch method to return 200
instead of 201
f1e7ed7
to
cdb42a8
Compare
dataservice/api/study/resources.py
Outdated
st = Study.query.get(kf_id) | ||
if st is None: | ||
abort(404, 'could not find {} `{}`' | ||
.format('Study', kf_id)) |
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.
Change 'Study' to 'study' to keep all error msgs standard
dataservice/api/study/resources.py
Outdated
st = Study.query.get(kf_id) | ||
if st is None: | ||
abort(404, 'could not find {} `{}`' | ||
.format('Study', kf_id)) |
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.
Change Study to study to keep all error msgs standard
dataservice/api/study/resources.py
Outdated
""" | ||
st = Study.query.get(kf_id) | ||
if st is None: | ||
abort(404, 'could not find {} `{}`'.format('Study', kf_id)) |
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.
Change Study to study to keep all error msgs standard
tests/test_api.py
Outdated
('/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`'), |
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.
Remember to change from Study to study after updating resource class
868634b
to
c70cd06
Compare
tests/study/test_study_resources.py
Outdated
data=json.dumps(body)) | ||
return response | ||
|
||
def _test_response_content(self, resp, status_code): |
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.
Remove method and calls to it
tests/study/test_study_resources.py
Outdated
|
||
from flask import url_for | ||
|
||
from dataservice.extensions import db |
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.
Remove unused import: dataservice.extensions.db
dataservice/api/study/resources.py
Outdated
st = (StudySchema(strict=True).load(body, instance=st, | ||
partial=True).data) | ||
except ValidationError as err: | ||
abort(400, 'could not update participant: {}'.format(err.messages)) |
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.
Prob copy/paste error, change could not update participant
to could not update study
dataservice/api/study/resources.py
Outdated
|
||
def patch(self, kf_id): | ||
""" | ||
Update fields in a study and preserve existing fields. |
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.
Nitpicking but can we be consistent w the docstrings for crud resources? All the other resources have patch docstring like: Update an existing demographic. Allows partial update of resource
. If you don't like above, then lets at least create an issue to standardize docstrings for the resource crud methods
5a69450
to
3988371
Compare
3988371
to
d458a98
Compare
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.
👍
Adds endpoints for the study resource.
Addresses #161