Skip to content

Spruce - Cecily & Zandra#25

Open
zandra2 wants to merge 15 commits intoAda-C16:mainfrom
zandra2:main
Open

Spruce - Cecily & Zandra#25
zandra2 wants to merge 15 commits intoAda-C16:mainfrom
zandra2:main

Conversation

@zandra2
Copy link
Copy Markdown

@zandra2 zandra2 commented Oct 28, 2021

No description provided.

Copy link
Copy Markdown

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

Comment thread app/__init__.py
db.init_app(app)
migrate.init_app(app, db)

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.

Once we import blueprints (which in turn import the models), we don't need the model import here any more.

@@ -0,0 +1,33 @@
"""empty message
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

try to remember to add a message when you create a migration. It can make it easier to know which migration files to look in if there's a problem going on during setup.

Comment thread tests/test_routes.py

def test_get_response_body_of_fixture_planet(client, two_saved_planets):
#Act
response = client.get("/planets/1")
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 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).

Comment thread tests/conftest.py
saturn = Planet(name = "Saturn", description="Lots of rings!")

db.session.add_all([mars, saturn])
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, saturn] 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.

Comment thread app/routes.py
db.session.add(new_planet)
db.session.commit()

return jsonify(f"Planet {new_planet.name} created successfully"), 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.

Consider returning information about the planet created (its dictionary structure) rather than just a message here. The caller would probably like to at least know the id of the created record. Returning a dictionary with the id and message as two separate parts can make it easy to get the id programmatically, as well as a potentially human friendly message.

Comment thread app/routes.py
Comment on lines +8 to +9
@planets_bp.route("", methods=["POST", "GET"])
def handle_planets():
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 making separate functions for the get and post (the GET method is often called index, while the POST method is often called create). This allows each function to focus on a single piece of functionality, and avoids the need for the if within the route method.

Comment thread app/routes.py
Comment on lines +33 to +37
planets_response.append({
"id": planet.id,
"name": planet.name,
"description": planet.description
})
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 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.

Comment thread app/routes.py
Comment on lines +42 to +43
@planets_bp.route("/<planet_id>", methods=["GET", "PUT", "DELETE"])
def handle_planet(planet_id):
Copy link
Copy Markdown

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.

Comment thread app/routes.py
Comment on lines +51 to +55
return {
"id": planet.id,
"name": planet.name,
"description": planet.description
}
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 moving the code to transform a Planet into a dictionary to a helper function in the Planet class.

Comment thread app/routes.py
Comment on lines +60 to +61
planet.name = form_data["name"]
planet.description = form_data["description"]
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 would want to perform similar error handling as we did for the POST verb here in PUT. We are replacing the record, so any requirements that were required to originally create a record, should really be applied her in the PUT as well.

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