Skip to content
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

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open

Conversation

momofAnAl
Copy link

No description provided.

Copy link
Contributor

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

@@ -0,0 +1,24 @@
class Planet:
def __init__(self, id, name, description):
Copy link
Contributor

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.

self.name = name
self.description = description

def to_dict(self):
Copy link
Contributor

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.

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."),
Copy link
Contributor

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.

Comment on lines 10 to 15
planets_response.append(dict(
id=planet.id,
name=planet.name,
description=planet.description

))
Copy link
Contributor

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

Comment on lines 8 to 15
planets_response = []
for planet in planets:
planets_response.append(dict(
id=planet.id,
name=planet.name,
description=planet.description

))
Copy link
Contributor

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]

return planet.to_dict(), 200


def validate_palnet(planet_id):
Copy link
Contributor

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.

def get_one_planet(planet_id):
planet = validate_palnet(planet_id)

return planet.to_dict(), 200
Copy link
Contributor

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
Contributor

@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):
Copy link
Contributor

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
Copy link
Contributor

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.

Comment on lines 8 to 9
diameter: Mapped[int]
number_of_moons: Mapped[int]
Copy link
Contributor

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.



# class Planet:
Copy link
Contributor

@anselrognlie anselrognlie Nov 3, 2024

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.

Comment on lines +1 to +2
"""adds
Planet model
Copy link
Contributor

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')
Copy link
Contributor

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):
Copy link
Contributor

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.

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()
Copy link
Contributor

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.


def test_get_one_planet(client, two_save_planets):
#Act
response = client.get("/planets/1")
Copy link
Contributor

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

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

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,

Copy link
Contributor

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.

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