Skip to content

C22 Wei and Beenish#10

Open
beenishali693 wants to merge 31 commits intoAda-C22:mainfrom
beenishali693:main
Open

C22 Wei and Beenish#10
beenishali693 wants to merge 31 commits intoAda-C22:mainfrom
beenishali693:main

Conversation

@beenishali693
Copy link

Waves 1 and 2 of Solar System API

Copy link

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

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.

Choose a reason for hiding this comment

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

Nice to see that you discussed your coworking needs and preferences!


class Planet:

def __init__(self,id,name,description,galaxy):

Choose a reason for hiding this comment

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

Nit: include a space after commas in parameters

    def __init__(self, id, name, description, galaxy):

Choose a reason for hiding this comment

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

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):

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.

Comment on lines +12 to +17
return {
"id": self.id,
"name": self.name,
"description": self.description,
"galaxy": self.galaxy
}

Choose a reason for hiding this comment

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

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
        }

}


mercury = Planet(1,"Mercury","first planet from the sun","Milkyway")

Choose a reason for hiding this comment

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

👀 Space after argument commas here and throughout.

mercury = Planet(1, "Mercury", "first planet from the sun", "Milkyway")

@@ -0,0 +1,31 @@
from flask import Blueprint, make_response, abort
from ..models.planet import planets

Choose a reason for hiding this comment

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

👍 Nice relative import.

from flask import Blueprint, make_response, abort
from ..models.planet import planets

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

Choose a reason for hiding this comment

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

👀 Spaces after argument commas.

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

Comment on lines +8 to +10
planets_response = []
for planet in planets:
planets_response.append(planet.to_dict())

Choose a reason for hiding this comment

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

There's a great opportunity to try to use a list comprehension here.

    planets_response = [planet.to_dict() for planet in planets]

@planets_bp.get("/<planet_id>")
def get_one_planet(planet_id):
planet = validate_planet(planet_id)
return planet.to_dict(),200

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()

Copy link

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

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):

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 = Flask(__name__)

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')

Choose a reason for hiding this comment

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

👍 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

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 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):

Choose a reason for hiding this comment

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

👍 Nice modifications to associate this model type with our database.

Comment on lines +10 to +12
name = request_body["name"]
description = request_body["description"]
galaxy = request_body["galaxy"]

Choose a reason for hiding this comment

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

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

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.

response = client.get("/planets/1")

#Assert
assert response.status_code == 404

Choose a reason for hiding this comment

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

👀 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")

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_saved_planets[0].id}")

#Assert
assert response.status_code == 200
assert response_body == {
"id": 1,

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_saved_planets[0].id,

Comment on lines +91 to +92
count_response = client.get("/planets/count")
response_body = count_response.get_json()

Choose a reason for hiding this comment

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

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.

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