Skip to content

C22 - Tatiana Snook (Sphinx) and Jen Nguyen (Phoenix)#4

Open
ItsJenOClock wants to merge 19 commits intoAda-C22:mainfrom
tatianasnook:main
Open

C22 - Tatiana Snook (Sphinx) and Jen Nguyen (Phoenix)#4
ItsJenOClock wants to merge 19 commits intoAda-C22:mainfrom
tatianasnook:main

Conversation

@ItsJenOClock
Copy link

@ItsJenOClock ItsJenOClock commented Oct 21, 2024

No description provided.

Copy link
Collaborator

@apradoada apradoada left a comment

Choose a reason for hiding this comment

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

Well done, y'all! I can tell y'all have a good grasp of building Flask APIs! Overall most of my comments are simply potential extension questions that you could think about, but no changes or updates are required!

@@ -1,7 +1,21 @@
from flask import Flask
from .routes.planet_routes import bp as planet_bp
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love that y'all aliased the bp as planet_bp!

name=planet_data['name'],
description=planet_data['description'],
num_of_moons=planet_data['num_of_moons']
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great job on the to_dict and from_dict!

Comment on lines +30 to +41
if name_param:
query = query.where(Planet.name.ilike(f"%{name_param}%"))

description_param = request.args.get("description")
if description_param:
query = query.where(Planet.description.ilike(f"%{description_param}%"))

num_of_moons_param = request.args.get("num_of_moons")
if num_of_moons_param:
query = query.where(Planet.num_of_moons >= num_of_moons_param)

query = query.order_by(Planet.num_of_moons)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't something you need to refactor, but I could definitely see the name_param and the description_param being similar across models. I wonder if there is a way these two queries could be extracted to a helper function in your route_utilities.py!

query = query.order_by(Planet.num_of_moons)
planets = db.session.scalars(query)

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

Choose a reason for hiding this comment

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

Great use of a list comprehension!

Comment on lines +59 to +61
planet.name = request_body["name"]
planet.description = request_body["description"]
planet.num_of_moons = request_body["num_of_moons"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

One thing to think about here is the idea that we might not need to update all the attributes in Planet. Refactoring this wasn't a requirement, but one thing to think about as we continue to work with APIs is to how can we modify our update_planet in a way that allows us to only update certain parts of the planet?


db.session.add_all([planet_1, planet_2])

db.session.commit()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This conftest looks great!

response_body = response.get_json()

assert response.status_code == 404
assert response_body == {"message": f"Planet 1 not found"}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small note here! The f-string you are using as the message is not necessary! It is true that the response we are returning from our route uses and f-string, but in the response itself, it it just a string. In fact, f-strings are just strings that we format differently. We would only need to use an f-string in this assert if we were bringing in a value. Because we aren't, you should be able to remove the f and it will still work!

response_body = response.get_json()

assert response.status_code == 404
assert response_body == {"message": f"Planet 3 not found"}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same note as before with the f-string!

"name": "Pluto",
"description": "dwarf planet",
"num_of_moons": 5
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These two create tests are great! One extension question to think about: How could we ensure the planets that have been created made it to the database? Hint: It might require a second API call!

def test_delete_one_planet_with_planets_already_in_db(client, two_saved_planets):
response = client.delete("planets/1")
assert response.status_code == 204
assert response.content_length is None
Copy link
Collaborator

Choose a reason for hiding this comment

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

These look good! I'm not sure we necessarily need to assert that the content_length is None, but it can't hurt!

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