-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add participants to hacknight endpoint with tests (fixes #93) #166
Add participants to hacknight endpoint with tests (fixes #93) #166
Conversation
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.
Overal seems okay, just have some questions - to be asked on next meeting.
backend/resources/hacknight.py
Outdated
return {'message': 'No new participant has been provided'}, \ | ||
HTTPStatus.BAD_REQUEST | ||
|
||
for new_particpiant in new_participants: |
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.
new_participant
- just typo
backend/resources/hacknight.py
Outdated
|
||
for new_particpiant in new_participants: | ||
hacknight.participants.append( | ||
Participant.query.get_or_404(new_particpiant) |
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.
ditto
backend/tests/test_hacknight.py
Outdated
def test_add_participants_to_nonexistent_hacknight( | ||
access_token, add_participants, client | ||
): | ||
"""Test add participants to non-existent hacknight.""" |
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.
non-existing
backend/tests/test_hacknight.py
Outdated
def test_duplicate_participant_in_hacknight( | ||
access_token, add_hacknights, add_participants, client, _db | ||
): | ||
"""Test add participant who is in hacknight already.""" |
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.
Add participant who is already in hacknight.
It's not perfect, but sounds better. ;-)
backend/tests/test_hacknight.py
Outdated
assert response['msg'] == 'Missing Authorization Header' | ||
|
||
|
||
def test_add_nonexistent_participants_to_hacknight( |
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.
non-existing
@@ -18,3 +18,10 @@ class Meta: | |||
many=True | |||
) | |||
phone = fields.Str(validate=[validate.Length(max=13)]) | |||
|
|||
|
|||
class ParticipantIdSchema(Schema): |
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.
I'd like to discuss this approach.
backend/tests/test_hacknight.py
Outdated
def test_duplicate_participant_in_hacknight( | ||
access_token, add_hacknights, add_participants, client, _db | ||
): | ||
"""Test add participant who is already in hacknight..""" |
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.
Redundant space at the end.
backend/tests/test_hacknight.py
Outdated
assert rv.status_code == HTTPStatus.OK | ||
|
||
hacknight_db = Hacknight.query.get(1) | ||
ids_from_db = [part.id for part in hacknight_db.participants] |
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.
Why using part
instead of participant
here? I think it won't collide with further code (you're using ids_from_db
).
backend/tests/test_hacknight.py
Outdated
data=json.dumps(payload) | ||
) | ||
resp = rv.get_json() | ||
participants_ids = [ |
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.
That's never used
backend/tests/test_hacknight.py
Outdated
headers={'Authorization': 'Bearer {}'.format(access_token)}, | ||
data=json.dumps(payload) | ||
) | ||
response = rv.get_json() |
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.
That's never used.
backend/tests/test_hacknight.py
Outdated
headers={'Authorization': 'Bearer {}'.format(access_token)}, | ||
data=json.dumps(payload) | ||
) | ||
response = rv.get_json() |
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.
That's never used.
@@ -4,10 +4,12 @@ | |||
class ParticipantSchema(Schema): | |||
class Meta: | |||
fields = ( | |||
'id', 'name', 'lastname', 'email', 'github', 'hacknights', 'phone' | |||
'id', 'participants_ids', 'name', 'lastname', |
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.
I am not sure, if that's a good wat to do that with participant_ids
.
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.
At first I delegated it to separate schema, but as we discussed, it wasn't best solution. So I pull it into ParticipantSchema. Do you have some suggestions. I certenly want serialize somehow that payload.
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.
@w1stler please solve this discussion and merge
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.
I could make it inline inside Resource:
ids_schema = Schema.from_dict({"participants_ids": fields.List(fields.Int())})
try:
data = ids_schema().load(json_data)
and remove from participant serializer.
What do you think about that solution?
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.
Seems that is a better solution. We can find some examples with good practices for similar problem, as I am still not 100% confident if that's the best approach. :)
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.
There's one merge conflict to be resolved, but I also sincerily believe there's a better place to put these participants ids.
) | ||
dump_only = ('id', 'hacknights') | ||
|
||
participants_ids = fields.List(fields.Int()) |
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.
Wait, how the participants_ids
as a list can be part of a ParticipantSchema
? I can agree, that list in such shape will be used to attach participants to a hacknight, but it should be defined in probably different place.
Ok, I see that you're working on that @stanislawK. Just, please, notify me when it will be ready. Also if you will feel that you're struggling with that we can have a conversation about that next hacknight. |
backend/resources/hacknight.py
Outdated
db.session.commit() | ||
|
||
hacknight_schema = HacknightSchema() | ||
return {"hacknight": hacknight_schema.dump(hacknight)}, \ |
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.
I think we can remove "hacknight"
.
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.
No reason for hacknight
as a key here, we should return it directly.
Do we have an agreement here @stanislawK & @magul ? |
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.
A few questions to be addressed here.
backend/resources/hacknight.py
Outdated
try: | ||
data = ids_schema().load(json_data) | ||
except ValidationError as err: | ||
return (err.messages), HTTPStatus.BAD_REQUEST |
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.
no reason for ()
here, also 422 could be applied here.
backend/resources/hacknight.py
Outdated
db.session.commit() | ||
|
||
hacknight_schema = HacknightSchema() | ||
return {"hacknight": hacknight_schema.dump(hacknight)}, \ |
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.
No reason for hacknight
as a key here, we should return it directly.
backend/resources/hacknight.py
Outdated
@@ -54,3 +54,38 @@ def get(self, id): | |||
return {'hacknights': hacknight_schema.dump(Hacknight.query.get_or_404( | |||
id)) | |||
}, HTTPStatus.OK | |||
|
|||
@jwt_required | |||
def patch(self, 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.
didn't we agree to have separate url for that (like /hacknights/<id>/participants
)?
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.
I hear it for first time. No one discussed it with me, although there was plenty of time - it is 4 months since I proposed this pr.
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.
changed
except ValidationError as err: | ||
return (err.messages), HTTPStatus.BAD_REQUEST | ||
|
||
new_participants = [ |
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.
As I understand that this endpoint should be able to change the list of participants for particular hacknight.
I see that you implemented only adding new participants? How the plan for removing a participant from hacknight looks like?
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.
Title of these ticket is "Create add participant to hacknight endpoint", and I believe that delete participant from hacknight, is great topic for next 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.
Chenged to post on /hacknights//participants @ngiersz going to do delete method
Testing:
Headers: Authorization: Bearer access_token; Content-Type: application/json
Body: {
"participants": [1]
}
With pytest: