-
Notifications
You must be signed in to change notification settings - Fork 79
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
wave 1 and 2 completed #32
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Maria Silva
moons in assert statement
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.
Overall nice work. I left some comments and suggestions. Let me know if you have any questions via slack.
def to_dict(self): | ||
planet_as_dict = {} | ||
planet_as_dict["id"] = self.id | ||
planet_as_dict["name"] = self.name | ||
planet_as_dict["description"] = self.description | ||
planet_as_dict["moons"]=self.moons | ||
|
||
return planet_as_dict | ||
|
||
def from_json(cls, req_body): | ||
return cls( | ||
name = req_body["name"], | ||
description = req_body["description"], | ||
moons = req_body["moons"] | ||
) |
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.
Good helper methods
def validate_model(cls, model_id): | ||
try: | ||
model_id = int(model_id) | ||
except: | ||
abort(make_response({"message":f"{cls.__name__} {model_id} invalid"}, 400)) | ||
|
||
model = cls.query.get(model_id) | ||
|
||
if not model: | ||
abort(make_response({"message":f"{cls.__name__} {model_id} not found"}, 404)) | ||
|
||
return model |
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.
Great helper function
|
||
|
||
@planets_bp.route("", methods=["POST"]) | ||
def handle_planets(): |
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 name this function handle_planets
and not create_planet
?
planet.name = request_body["name"] | ||
planet.description = request_body["description"] | ||
planet.moons = request_body["moons"] | ||
|
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.
Doing some validation on the request body here would be good. i.e. make sure it has the given fields and they are valid.
assert response_body[0] == { | ||
'id': 1, | ||
'name': 'Mars', | ||
'description': 'Too hot', | ||
'moons': 2} | ||
|
||
assert response_body[1] == { | ||
'id': 2, | ||
'name' : 'Earth', | ||
'description' : 'Home Sweet Home', | ||
'moons': 1} |
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.
You could also do:
assert response_body[0] == { | |
'id': 1, | |
'name': 'Mars', | |
'description': 'Too hot', | |
'moons': 2} | |
assert response_body[1] == { | |
'id': 2, | |
'name' : 'Earth', | |
'description' : 'Home Sweet Home', | |
'moons': 1} | |
assert response_body == [{ | |
'id': 1, | |
'name': 'Mars', | |
'description': 'Too hot', | |
'moons': 2 | |
}, | |
{ | |
'id': 2, | |
'name' : 'Earth', | |
'description' : 'Home Sweet Home', | |
'moons': 1} | |
] |
|
||
|
||
# 4.POST /books with a JSON request body returns a 201 | ||
def test_create_one_book(client): |
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.
You should also have a test that checks what happens when the request body is invalid.
|
||
# DELETE RESOURCE | ||
@planets_bp.route("/<id>", methods=["DELETE"]) | ||
def delete_planet(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.
Just note this route is untested.
|
||
# UPDATE RESOURCE | ||
@planets_bp.route("/<id>", methods=["PUT"]) | ||
def update_planet(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.
Just note this route is untested.
Tigers - Neema and Maria