Skip to content

Spruce - Veronica Osei#28

Open
VeronicaO27 wants to merge 4 commits intoAda-C16:mainfrom
VeronicaO27:main
Open

Spruce - Veronica Osei#28
VeronicaO27 wants to merge 4 commits intoAda-C16:mainfrom
VeronicaO27:main

Conversation

@VeronicaO27
Copy link
Copy Markdown

No description provided.

@VeronicaO27 VeronicaO27 changed the title Solar System - Veronica Osei Spruce - Veronica Osei Oct 28, 2021
Copy link
Copy Markdown

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

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

Hi Veronica and Cassandra (?) Great work! I have added feedback on how to use helper methods and refactor.

Comment thread app/models/solar.py
Comment on lines +4 to +7
class Planet(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
description = db.Column(db.String)
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 and description. When making classes in the future, consider which columns are required (non-nullable).

Comment thread app/models/solar.py
Comment on lines +9 to +10
def to_string(self):
return f'{self.id}: {self.name} Description: {self.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.

Yay helper methods!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, I highly recommend adding a helper method to turn planet instances into a dictionary (this will definitely save you from writing a few lines of code down the road)!

Comment thread app/routes.py

planets_bp = Blueprint("planets_py", __name__, url_prefix= "/planets")

@planets_bp.route("", methods=["GET","POST"])
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.

Comment thread app/routes.py
Comment on lines +12 to +19
planets_response = []
for planet in planets:
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 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
})
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
})
planets_response = [ planets_response.append(planet.to_dict()) for planet in planets ]

Comment thread app/routes.py
Comment on lines +22 to +24
request_body = request.get_json()
new_planet = Planet(name=request_body["name"],
description=request_body["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 should check if request_body contains falsy values so that a planet isn't created with empty data!

Comment thread app/routes.py
}

elif request.method == "PUT":
request_body = request.get_json()
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 always check if the request_body is empty

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

return make_response(f"Planet #{planet.id} successfully updated")
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 test_fixtures.py
def test_so_fancy(so_fancy):
assert so_fancy.fancy
so_fancy.or_is_it()
assert not so_fancy.fancy 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.

V Fancy 💅

Comment thread tests/conftest.py
# db.session.add(ocean_book)
# db.session.add(mountain_book)
db.session.commit()

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 [saturn_planet, pluto_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
# 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.

The test for the post request is missing

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.

2 participants