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

Zoisite Jamie McGraner and Lu D19 #11

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

Conversation

jdanielle12
Copy link

No description provided.

app/routes.py Outdated
Comment on lines 36 to 38
all_planets_response = []
for planet in planets:
all_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.

This would be a good candidate for list comprehension, something like:

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

@yangashley
Copy link

@jdanielle12 @lulusde1210 Great work on waves 1 and 2 for Solar System, team! It looks like you've got a handle on creating a blueprint, registering it with your Flask app and writing routes. Nice job refactoring your code so that the routes can stay short and sweet!

@jdanielle12 jdanielle12 changed the title Zoisite Jamie McGraner and Lu D19 Waves 1 & 2 Zoisite Jamie McGraner and Lu D19 May 4, 2023
Copy link

@yangashley yangashley 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 on Part 2 of solar-system. Left a couple of minor comments. Hope they help with task-list. Let me know if you have any questions!


def create_app(test_config=None):
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

Choose a reason for hiding this comment

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

👍

@@ -0,0 +1,27 @@
from app import db

Choose a reason for hiding this comment

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

Nicely done!

Comment on lines +21 to +23
except KeyError as e:
message = jsonify(f"missing required value: {e}")
abort(make_response(message, 400))

Choose a reason for hiding this comment

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

Nice job using try/except to add in error handling for a request that relies on (possibly incorrect) client data.

Comment on lines +32 to +37
if name_query:
planets = Planet.query.filter_by(name=name_query)
else:
planets = Planet.query.all()

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

Choose a reason for hiding this comment

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

🥳

Comment on lines +73 to +81
planet_to_update = validate_model(Planet, id)

planet_to_update.name = planet_data["name"]
planet_to_update.description = planet_data["description"]
planet_to_update.distance_from_sun = planet_data["distance_from_sun"]
db.session.commit()

message = jsonify(f"Planet {planet_to_update.name} updated")
return make_response(message, 200)

Choose a reason for hiding this comment

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

Just like your POST request had error handling, your PUT request also needs it too. If a client sends a PUT req, but the request body had "planet_name" instead of "name", line 75 will throw an error (500 status code), but what we want our route to do is respond with an error message that gives the client feedback about what went wrong.

)
db.session.add(Jupiter)
db.session.add(Mercury)
db.session.commit()

Choose a reason for hiding this comment

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

Something you could do here is to create a list and add the 2 planets to it, then return the list. That way when a unit test brings in this fixture two_planets you actually get the list of 2 saved planets. Right now this fixture doesn't return anything.

So that would look like

list_of_planets = [jupiter, mercury]

Comment on lines +30 to +35
Jupiter = Planet(
name="Jupiter",
description="Jupiter has sixty-seven moons",
distance_from_sun=48.4
)
Mercury = Planet(

Choose a reason for hiding this comment

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

variable names should begin with lowercase letters

Comment on lines +15 to +26
assert response_body == [{
"id": 1,
"name": "Jupiter",
"description": "Jupiter has sixty-seven moons",
"distance_from_sun": "48.4"
},
{
"id": 2,
"name": "Mercury",
"description": "Mercury has zero moons",
"distance_from_sun": "3.5"
}]

Choose a reason for hiding this comment

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

Per my comment above in the two_planets fixture, if that fixture returned a list of your 2 saved planets then you could check the response_body against your fixture (where you saved the planets when this test first runs)!

saved_planet_1 = two_planetes[0] 
assert response_body["id"] == saved_planet_1.id 
assert response_body["name"] == saved_planet_1.name
assert response_body["description"] == saved_planet_1.description 
assert response_body["distance_from_sun"] == saved_planet_1.distance_from_sun

This would make the test more robust

Comment on lines +31 to +38
response = client.get("/planets/1")
response_body = response.get_json()

assert response.status_code == 200
assert response_body["id"] == 1
assert response_body["name"] == "Jupiter"
assert response_body["description"] == "Jupiter has sixty-seven moons"
assert response_body["distance_from_sun"] == "48.4"

Choose a reason for hiding this comment

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

Would be good to compare the response_body with what was saved in the database when the fixture ran instead of comparing the response_body to string literals.

If you refactored two_planets to return a list of saved planet instances, then you can refactor the test. See my comment in test_all_planets_return_a_list_of_planets as an example.

Comment on lines +58 to +62
assert response.status_code == 201
assert response_body == f"Planet {EXPECTED_PLANET['name']} successfully created"
assert actual_planet.name == EXPECTED_PLANET["name"]
assert actual_planet.description == EXPECTED_PLANET["description"]
assert actual_planet.distance_from_sun == EXPECTED_PLANET["distance_from_sun"]

Choose a reason for hiding this comment

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

👍

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.

2 participants