Skip to content

Spruce - Reid and Asha#20

Open
ashapa wants to merge 15 commits into
Ada-C16:mainfrom
reidhowdy:main
Open

Spruce - Reid and Asha#20
ashapa wants to merge 15 commits into
Ada-C16:mainfrom
reidhowdy:main

Conversation

@ashapa

@ashapa ashapa commented Oct 27, 2021

Copy link
Copy Markdown

No description provided.

@audreyandoy audreyandoy left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work Reid and Asha! My primary feedback focuses on helper methods and refactoring.

Comment thread app/__init__.py
from .routes import planets_bp
app.register_blueprint(planets_bp)

from app.models.planet import Planet

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The blueprints already import the models so we don't actually need the model import in this file anymore.

Comment thread app/models/planet.py
Comment on lines +5 to +8
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
description = db.Column(db.String)
color = db.Column(db.String) No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Careful! Using the class as it currently is, we'd be able to make a planet with a null name, description, and color. When making classes in the future, consider which columns are required (non-nullable).

Comment thread app/routes.py
Comment on lines +19 to +28
planets_response = []
for planet in planets:
planets_response.append(
{
"id": planet.id,
"name": planet.name,
"description": planet.description,
"color": planet.color
}
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could reduce these lines of code using a helper method to turn the planet object into a dictionary in the Planets class:

Suggested change
planets_response = []
for planet in planets:
planets_response.append(
{
"id": planet.id,
"name": planet.name,
"description": planet.description,
"color": planet.color
}
)
planets_response = []
for planet in planets:
planets_response.append(planet.to_dict())

And further refactor using a list comprehension :D

Suggested change
planets_response = []
for planet in planets:
planets_response.append(
{
"id": planet.id,
"name": planet.name,
"description": planet.description,
"color": planet.color
}
)
planets_response = [ planets_response.append(planet.to_dict()) for planet in planets ]

Comment thread app/routes.py
Comment on lines +31 to +36
elif request.method == "POST":
request_body = request.get_json()
new_planet = Planet(name=request_body["name"],
description=request_body["description"],
color=request_body["color"]
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We should check if request_body contains falsy values so that a planet isn't created with empty data!

Comment thread app/routes.py
Comment on lines +41 to +48
new_planet_response = {
"id": new_planet.id,
"name": new_planet.name,
"description": new_planet.description,
"color": new_planet.color
}

return jsonify(new_planet_response), 201

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

*** repeats broken record about the Planet dictionary helper method ***

Comment thread app/routes.py
"description": planet.description,
"color": planet.color
}, 200 #testing this
elif request.method == "PUT":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread app/routes.py

return jsonify(f"Planet #{planet.id} successfully updated"), 200

elif request.method == "DELETE":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread app/routes.py
return jsonify(new_planet_response), 201


@planets_bp.route("/<planet_id>", methods=["GET", "PUT", "DELETE"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider separating each request method into its different functions. This makes testing the endpoints much easier to do and follows the single responsibility principle.

@audreyandoy audreyandoy Nov 3, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The same can be said about the "/planets" endpoint for GET and POST.

Comment thread tests/conftest.py
saturn_planet = Planet(name="Saturn", description="Has some rings", color="yellow-brown")

db.session.add_all([mars_planet, earth_planet, saturn_planet])
db.session.commit() No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider returning [mars_planet, earth_planet, saturn_planet] after this line, so that tests can make use of the attributes of the records (like the id or name of planet)

Comment thread tests/test_routes.py
Comment on lines +48 to +49
assert response.status_code == 201
assert response_body == {"id" : 1, "name" : "Post", "description" :"Posty", "color" : "Postingggg"}

@audreyandoy audreyandoy Nov 3, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great work! In Hello Books, etc. we've typically returned a short message containing the id of the newly added record. What y'all did (returning the actual record in JSON form) is so much more helpful for tests.

Optionally, we could perform a Planet.query.get call to retrieve the record from the database to confirm that it was actually added.

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