Conversation
anselrognlie
left a comment
There was a problem hiding this comment.
You're off to a good start. All the Flask-specific code is on the right on track. There are just a few general Python points to review, mostly related to formatting.
There was a problem hiding this comment.
Nice to see that you discussed your coworking needs and preferences!
app/models/planet.py
Outdated
|
|
||
| class Planet: | ||
|
|
||
| def __init__(self,id,name,description,galaxy): |
There was a problem hiding this comment.
Nit: include a space after commas in parameters
def __init__(self, id, name, description, galaxy):There was a problem hiding this comment.
Looking at your data, consider setting a default value for the galaxy parameter.
def __init__(self, id, name, description, galaxy="Milky Way"):| self.description = description | ||
| self.galaxy = galaxy | ||
|
|
||
| def to_dict(self): |
There was a problem hiding this comment.
👍 Nice job incorporating this refactor from the live code.
| return { | ||
| "id": self.id, | ||
| "name": self.name, | ||
| "description": self.description, | ||
| "galaxy": self.galaxy | ||
| } |
There was a problem hiding this comment.
Personally, I prefer to stick with uniform 4-space indented wrapping rather than trying to align to the enclosing character. Generally the closing brace should align with either the first line (pre-indent), or the content lines (1-level indent). As written, it's a bit floaty.
return {
"id": self.id,
"name": self.name,
"description": self.description,
"galaxy": self.galaxy
}
app/models/planet.py
Outdated
| } | ||
|
|
||
|
|
||
| mercury = Planet(1,"Mercury","first planet from the sun","Milkyway") |
There was a problem hiding this comment.
👀 Space after argument commas here and throughout.
mercury = Planet(1, "Mercury", "first planet from the sun", "Milkyway")
app/routes/planet_routes.py
Outdated
| @@ -0,0 +1,31 @@ | |||
| from flask import Blueprint, make_response, abort | |||
| from ..models.planet import planets | |||
| from flask import Blueprint, make_response, abort | ||
| from ..models.planet import planets | ||
|
|
||
| planets_bp = Blueprint("planets_bp",__name__,url_prefix="/planets") |
There was a problem hiding this comment.
👀 Spaces after argument commas.
planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets")| planets_response = [] | ||
| for planet in planets: | ||
| planets_response.append(planet.to_dict()) |
There was a problem hiding this comment.
There's a great opportunity to try to use a list comprehension here.
planets_response = [planet.to_dict() for planet in planets]
app/routes/planet_routes.py
Outdated
| @planets_bp.get("/<planet_id>") | ||
| def get_one_planet(planet_id): | ||
| planet = validate_planet(planet_id) | ||
| return planet.to_dict(),200 |
There was a problem hiding this comment.
The status code will be 200 by default, so we can leave that off.
return planet.to_dict()
anselrognlie
left a comment
There was a problem hiding this comment.
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.
👍 Thanks for renaming this. This param could be used for scenarios other than testing (the name was left over from the previous curriculum).
| app = Flask(__name__) | ||
|
|
||
| app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
| app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI') |
There was a problem hiding this comment.
👍 Loads the connection string from the environment (provided with our .env during development).
| @@ -1,9 +1,21 @@ | |||
| from flask import Flask | |||
| from .db import db, migrate | |||
| from .models import planet | |||
There was a problem hiding this comment.
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 planet module here. If you find the fact that VS Code shows this as not being used, it would now be safe to remove.
| self.name = name | ||
| self.description = description | ||
| self.galaxy = galaxy | ||
| class Planet(db.Model): |
There was a problem hiding this comment.
👍 Nice modifications to associate this model type with our database.
| name = request_body["name"] | ||
| description = request_body["description"] | ||
| galaxy = request_body["galaxy"] |
There was a problem hiding this comment.
We didn't explicitly look at invalid/missing data scenarios for creating a record, but think about what would happen here, and what response we might want to reply with.
| galaxy="Milky Way") | ||
|
|
||
| db.session.add_all([earth,mars]) | ||
| db.session.commit() No newline at end of file |
There was a problem hiding this comment.
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.
| response = client.get("/planets/1") | ||
|
|
||
| #Assert | ||
| assert response.status_code == 404 |
There was a problem hiding this comment.
👀 We should also check that the response body is the expected message.
|
|
||
| def test_get_one_planet_with_records_successful(client, two_saved_planets): | ||
| #Act | ||
| response = client.get("/planets/1") |
There was a problem hiding this comment.
If we returned the record list from the fixture, then we could write this line as
response = client.get(f"/planets/{two_saved_planets[0].id}")| #Assert | ||
| assert response.status_code == 200 | ||
| assert response_body == { | ||
| "id": 1, |
There was a problem hiding this comment.
If we returned the record list from the fixture, then we could check the id as
"id": two_saved_planets[0].id,| count_response = client.get("/planets/count") | ||
| response_body = count_response.get_json() |
There was a problem hiding this comment.
While this will show that the number of planets has decreased, we'd really like to check that planet id 1 is actually the one that's gone. So rather than getting the count, we might try to get planet 1. After deletion, we should get a 404 result.
We could write a separate test for the count endpoint.
Waves 1 and 2 of Solar System API