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

Spruce: Asya and Gabe #23

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Spruce: Asya and Gabe #23

wants to merge 7 commits into from

Conversation

GabeKelemen
Copy link

No description provided.

Copy link

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

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

Overall, this looks good! The main thing to consider going forward is looking for ways to move shared code into helper methods, and to reduce the complexity of the individual route functions by splitting them into their own functions, following the single responsibility principle.

Also, make sure that you are consistently having your endpoints return json. Messages and errors should be done carefully, to make sure that their responses are still building a response which is valid JSON.

requests==2.25.1
six==1.15.0
SQLAlchemy==1.3.23
alembic==1.7.4

Choose a reason for hiding this comment

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

What prompted you to regenerate your requirements? There's nothing wrong with doing this (and in fact it gets you more up-to-date libraries), but it shouldn't have been strictly necessary.

# Alternatively, we could do
# db.session.add(ocean_book)
# db.session.add(mountain_book)
db.session.commit()

Choose a reason for hiding this comment

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

Consider returning [nine_planet, pluto_planet] after this line, so that tests can make use of the ids of the records, and anything else that's part of the record that might be useful in the test.


def test_get_one_planet(client, two_saved_planets):
# Act
response = client.get("/planets/1")

Choose a reason for hiding this comment

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

We could made this test a little less brittle, though perhaps a little tougher to read, if we write two_saved_planets to return the created models as well. We could then access the id of the model here, using the id the database gave the record. This can help us future-proof our tests (in case we change the number or order of records created, directly or indirectly).

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
description = db.Column(db.String)
circum = db.Column(db.Integer)

Choose a reason for hiding this comment

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

Keep in mind that the name we use here is what will literally be used for the db column name. So I'd suggest using the full circumference here.

Comment on lines +5 to +7
name = db.Column(db.String)
description = db.Column(db.String)
circum = db.Column(db.Integer)

Choose a reason for hiding this comment

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

Consider which columns we might want to make "required" (non-nullable). As it is, with this definition, we could make a Planet with a NULL name, description, and circumference. Would we want to allow this to happen?

planets = Planet.query.all()
planets_response = []
for planet in planets:
planets_response.append({"id": planet.id, "name": planet.name, "description": planet.description, "circumference in mkm": planet.circum})

Choose a reason for hiding this comment

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

We strongly suggest making a helper method on the Planet class to take care of converting the model instance to a dictionary for the purpose of being returned.

Choose a reason for hiding this comment

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

For the keys in our json dictionaries, prefer a name that can be a valid variable name (generally, prefer _ to spaces). While it doesn't impact our python handling that much, it can make the JSON handling for other languages (esp. JavaScript) if the keys can be treated as variables.

Comment on lines +25 to +26
@planets_bp.route("/<planet_name>", methods=["GET", "PUT", "DELETE"])
def handle_one_planet(planet_name):

Choose a reason for hiding this comment

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

As with the previous endpoint, here too consider splitting the various verbs into separate functions.

planet = Planet.query.get(planet_name)

if planet is None:
return make_response(f"Error: Planet {planet_name} not found", 404)

Choose a reason for hiding this comment

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

Be sure to jsonify any of the responses throughout.

return make_response(f"Error: Planet {planet_name} not found", 404)

if request.method == "GET":
return {"id": planet.id, "name": planet.name, "description": planet.description, "circumference": planet.circum}

Choose a reason for hiding this comment

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

Consider moving the code to transform a Planet into a dictionary to a helper function in the Planet class.

Comment on lines +38 to +42
if request_body is None:
return make_response(f"Error: Request requires name, description, and circumference", 400)

elif "name" not in request_body or "description" not in request_body or "circum" not in request_body:
return make_response(f"Error: Request requires name, description, and circumference", 400)

Choose a reason for hiding this comment

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

Great! These are essentially the same as the checks we'd like to have for the POST method.

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.

3 participants