-
Notifications
You must be signed in to change notification settings - Fork 79
Tiger - Tanil&Stacy #29
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
base: main
Are you sure you want to change the base?
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.
Nice work Tanil & Stacy, you hit the learning goals here. Nice work testing and writing validations. I left some minor comments. Let me know if you have questions.
def to_dict(self): | ||
return({ | ||
"id": self.id, | ||
"name": self.name, | ||
"color": self.color, | ||
"description": self.description | ||
}) | ||
@classmethod | ||
def from_json(cls, req_body): | ||
return cls( | ||
name = req_body["name"], | ||
color = req_body["color"], | ||
description = req_body["description"]) | ||
|
||
def update(self, req_body): | ||
try: | ||
self.name = req_body["name"] | ||
self.color = req_body["color"] | ||
self.description = req_body["description"] | ||
except KeyError as error: | ||
raise error |
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 methods
planets_bp = Blueprint("planets_bp", __name__, url_prefix = "/planets") | ||
|
||
# HELPER FUNCTION | ||
def validate_id(class_obj, 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.
Nice helper method
# CREATE RESOURCE | ||
@planets_bp.route("", methods = ["POST"]) | ||
def create_planet(): | ||
request_body = request.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.
Doing some validation on the request body here would be nice, just to check if it has the required fields.
try: | ||
planet.update(request_body) | ||
except KeyError as error: | ||
return make_response({'message': f"Missing attribute: {error}"}, 400) |
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.
This is good validation, something similar could be done in the post request. It should also be tested
assert response_body[0]["name"] == "foo1" | ||
assert response_body[1]["name"] == "foo2" | ||
assert response_body[2]["name"] == "foo3" | ||
assert response_body[0]["color"] == "bar1" | ||
assert response_body[1]["color"] == "bar2" | ||
assert response_body[2]["color"] == "bar3" | ||
assert response_body[0]["description"] == "foobar1" | ||
assert response_body[1]["description"] == "foobar2" | ||
assert response_body[2]["description"] == "foobar3" |
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 something like:
assert response_body[0]["name"] == "foo1" | |
assert response_body[1]["name"] == "foo2" | |
assert response_body[2]["name"] == "foo3" | |
assert response_body[0]["color"] == "bar1" | |
assert response_body[1]["color"] == "bar2" | |
assert response_body[2]["color"] == "bar3" | |
assert response_body[0]["description"] == "foobar1" | |
assert response_body[1]["description"] == "foobar2" | |
assert response_body[2]["description"] == "foobar3" | |
assert response_body == [ { | |
"name": "foo1", | |
"color": "bar1", | |
"description": "foobar1", | |
}, | |
{ | |
"name": "foo2", | |
"color": "bar2", | |
"description": "foobar2", | |
} | |
... |
|
||
|
||
|
||
def test_create_planet_can_create_planet_in_empty_db(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 test an invalid create planet request.
|
||
# DELETE RESOURCE | ||
@planets_bp.route("/<planet_id>", methods = ["DELETE"]) | ||
def delete_planet(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 a note that this is untested.
solar-system-api part 1