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

Add participants to hacknight endpoint with tests (fixes #93) #166

Conversation

stanislawK
Copy link
Contributor

Testing:

  • make populate_database
  • make start
  • login in postman with given username: "/auth/login"
  • copy access_token
  • send patch request to "hacknights/1/":
    Headers: Authorization: Bearer access_token; Content-Type: application/json
    Body: {
    "participants": [1]
    }

With pytest:

  • make bash
  • type pytest and press enter

@w1stler w1stler requested a review from a team September 4, 2019 18:09
Copy link
Member

@w1stler w1stler left a 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.

return {'message': 'No new participant has been provided'}, \
HTTPStatus.BAD_REQUEST

for new_particpiant in new_participants:
Copy link
Member

Choose a reason for hiding this comment

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

new_participant - just typo


for new_particpiant in new_participants:
hacknight.participants.append(
Participant.query.get_or_404(new_particpiant)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

def test_add_participants_to_nonexistent_hacknight(
access_token, add_participants, client
):
"""Test add participants to non-existent hacknight."""
Copy link
Member

Choose a reason for hiding this comment

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

non-existing

def test_duplicate_participant_in_hacknight(
access_token, add_hacknights, add_participants, client, _db
):
"""Test add participant who is in hacknight already."""
Copy link
Member

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. ;-)

assert response['msg'] == 'Missing Authorization Header'


def test_add_nonexistent_participants_to_hacknight(
Copy link
Member

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):
Copy link
Member

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.

def test_duplicate_participant_in_hacknight(
access_token, add_hacknights, add_participants, client, _db
):
"""Test add participant who is already in hacknight.."""
Copy link
Member

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.

assert rv.status_code == HTTPStatus.OK

hacknight_db = Hacknight.query.get(1)
ids_from_db = [part.id for part in hacknight_db.participants]
Copy link
Member

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).

data=json.dumps(payload)
)
resp = rv.get_json()
participants_ids = [
Copy link
Member

Choose a reason for hiding this comment

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

That's never used

headers={'Authorization': 'Bearer {}'.format(access_token)},
data=json.dumps(payload)
)
response = rv.get_json()
Copy link
Member

Choose a reason for hiding this comment

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

That's never used.

headers={'Authorization': 'Bearer {}'.format(access_token)},
data=json.dumps(payload)
)
response = rv.get_json()
Copy link
Member

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',
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Member

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. :)

Copy link
Member

@magul magul left a 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())
Copy link
Member

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.

@magul
Copy link
Member

magul commented Nov 9, 2019

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.

db.session.commit()

hacknight_schema = HacknightSchema()
return {"hacknight": hacknight_schema.dump(hacknight)}, \
Copy link
Member

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".

Copy link
Member

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.

@w1stler
Copy link
Member

w1stler commented Nov 27, 2019

Do we have an agreement here @stanislawK & @magul ?

magul
magul previously requested changes Nov 29, 2019
Copy link
Member

@magul magul left a 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.

try:
data = ids_schema().load(json_data)
except ValidationError as err:
return (err.messages), HTTPStatus.BAD_REQUEST
Copy link
Member

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.

db.session.commit()

hacknight_schema = HacknightSchema()
return {"hacknight": hacknight_schema.dump(hacknight)}, \
Copy link
Member

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.

@@ -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):
Copy link
Member

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)?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 = [
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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

@w1stler w1stler merged commit bfcf68f into CodeForPoznan:master Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants