-
Notifications
You must be signed in to change notification settings - Fork 426
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
Phoenix-Liubov_Davidova-Anh_Tran #63
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.
You're off to a good start. Be sure to add a custom attribute to your Planet class, and there's a small typo in your validate method, but otherwise you're right on track.
app/models/planets.py
Outdated
@@ -0,0 +1,24 @@ | |||
class Planet: | |||
def __init__(self, id, name, description): |
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.
👀 The project specification asks
Define a Planet class with the attributes id, name, and description, and one additional attribute
You have 3 of the attributes, but be sure to add an additional attribute of your choosing as a fourth. It will be easiest to add this before taking on the database work we'll be doing in Wave 03.
app/models/planets.py
Outdated
self.name = name | ||
self.description = description | ||
|
||
def to_dict(self): |
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 job incorporating this refactor from the live code.
app/models/planets.py
Outdated
Planet(4, "Mars", "The Red Planet, known for its volcanoes."), | ||
Planet(5, "Jupiter", "The largest planet, famous for its Great Red Spot."), | ||
Planet(6, "Saturn", "Known for its stunning rings."), | ||
Planet(7, "Uranus", "An ice giant that rotates on its side."), |
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.
😍 Uranus is my favorite planet because of how it rotates on its side.
app/routes/planets_routes.py
Outdated
planets_response.append(dict( | ||
id=planet.id, | ||
name=planet.name, | ||
description=planet.description | ||
|
||
)) |
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.
We could make use of the to_dict
method here, just like you did in the "get one" endpoint.
planets_response.append(planet.to_dict())
app/routes/planets_routes.py
Outdated
planets_response = [] | ||
for planet in planets: | ||
planets_response.append(dict( | ||
id=planet.id, | ||
name=planet.name, | ||
description=planet.description | ||
|
||
)) |
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 a great opportunity to try to apply a list comprehension. It works a little better if we also use the to_dict
method you added.
planets_response = [planet.to_dict() for planet in planets]
app/routes/planets_routes.py
Outdated
return planet.to_dict(), 200 | ||
|
||
|
||
def validate_palnet(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.
👀 Watch out for the typo here. If you haven't, I suggest installing the Code Spell Checker VS Code extension.
app/routes/planets_routes.py
Outdated
def get_one_planet(planet_id): | ||
planet = validate_palnet(planet_id) | ||
|
||
return planet.to_dict(), 200 |
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.
The status code will be 200 by default, so we can leave that off.
return planet.to_dict()
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 making the modifications to connect your API to your database, expanding the endpoints, and adding some tests. Everything here should be able to be applied to your task list implementations.
|
||
|
||
def create_app(test_config=None): | ||
def create_app(config=None): |
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.
👍 Thanks for renaming this. This param could be used for scenarios other than testing (the name was left over from the previous curriculum).
app/__init__.py
Outdated
@@ -1,9 +1,21 @@ | |||
from flask import Flask | |||
from app.db import db, migrate | |||
from app.models import planets |
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.
Note that once we have the Planet
model imported in the routes, and the routes are imported here, we don't technically need to import the planets
module here. If you find the fact that VS Code shows this as not being used, it would now be safe to remove.
app/models/planets.py
Outdated
diameter: Mapped[int] | ||
number_of_moons: Mapped[int] |
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 additional data columns.
For numerical values that require a unit (like diameter
), it can be useful to note the units somewhere. For example, we could call this diameter_in_km
/diameter_in_mi
so that it's explicitly a part of the name. If we feel that's too intrusive, a comment about the expected units can still be helpful. We could even add additional methods for retreiving the diameter in certain units to allow the name to remain simple, but have explicit logic that effectively documents the units.
app/models/planets.py
Outdated
|
||
|
||
# class Planet: |
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.
It can be tempting to keep old code around for reference, but keep in mind that with git, we can always get back to previous versions if necessary. So in general, prefer to remove old code, or at least put very clear comments around why this code is here and commented out.
Even with git, it can be inconvenient to find a previous version that had our original approach. To help with that, we can create a side branch on a commit that still has the old code, so that we can find it more easily, then remove the code from the main branch.
"""adds | ||
Planet model |
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.
Descriptive message for this migration. I'm not sure what caused this to wrap the line like this (generally it should appear as a single line), so try to keep an eye out for what caused this.
|
||
return Response(status=204, mimetype='application/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.
👍 This is how we say there's no content in the response, but even still, we should keep our type consistently as json for all our endpoints.
return app.test_client() | ||
|
||
@pytest.fixture | ||
def two_save_planets(app): |
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 test fixture adding two records.
tests/conftest.py
Outdated
planet_2 = Planet(name="Neptune", description="The farthest planet, known for strong winds", diameter=49244, number_of_moons=14) | ||
|
||
db.session.add_all([planet_1, planet_2]) | ||
db.session.commit() |
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.
Consider returning the list of records. The return value becomes the value passed in for the fixture in a test. By returning the records, we could use their ids in the dependent tests. While our approach of dropping and recreating the database for each test should guarantee that these records are ids 1 and 2, we could use the ids actually assigned by the database to remove that assumption.
tests/test_planets_routes.py
Outdated
|
||
def test_get_one_planet(client, two_save_planets): | ||
#Act | ||
response = client.get("/planets/1") |
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.
If we returned the record list from the fixture, then we could write this line as
response = client.get(f"/planets/{two_save_planets[0].id}")
tests/test_planets_routes.py
Outdated
#Assert | ||
assert response.status_code == 200 | ||
assert response_body == { | ||
"id": 1, |
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.
If we returned the record list from the fixture, then we could check the id as
"id": two_save_planets[0].id,
app/models/planets.py
Outdated
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.
👀 I hadn't noted this before, but prefer to have the name of the model file match the name of the class. So here, since the model is Planet
, the file should be planet.py
rather than planets.py
.
…planets module in __init__.py since we have Planet model imported from the routes
No description provided.