Skip to content

Zoisite Jamie McGraner and Lu D19 #11

Open
jdanielle12 wants to merge 9 commits intoAda-C19:mainfrom
jdanielle12:main
Open

Zoisite Jamie McGraner and Lu D19 #11
jdanielle12 wants to merge 9 commits intoAda-C19:mainfrom
jdanielle12:main

Conversation

@jdanielle12
Copy link
Copy Markdown

No description provided.

Comment thread app/routes.py Outdated
Comment on lines +36 to +38
all_planets_response = []
for planet in planets:
all_planets_response.append(planet.to_dict())
Copy link
Copy Markdown

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
Copy Markdown

@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
Copy Markdown

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

Comment thread app/__init__.py

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

Choose a reason for hiding this comment

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

👍

Comment thread app/models/planet.py
@@ -0,0 +1,27 @@
from app import db
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nicely done!

Comment thread app/routes.py
Comment on lines +21 to +23
except KeyError as e:
message = jsonify(f"missing required value: {e}")
abort(make_response(message, 400))
Copy link
Copy Markdown

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 thread app/routes.py
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]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🥳

Comment thread app/routes.py
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)
Copy link
Copy Markdown

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.

Comment thread tests/conftest.py
)
db.session.add(Jupiter)
db.session.add(Mercury)
db.session.commit()
Copy link
Copy Markdown

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 thread tests/conftest.py
Comment on lines +30 to +35
Jupiter = Planet(
name="Jupiter",
description="Jupiter has sixty-seven moons",
distance_from_sun=48.4
)
Mercury = Planet(
Copy link
Copy Markdown

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 thread tests/test_routes.py
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"
}]
Copy link
Copy Markdown

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 thread tests/test_routes.py
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"
Copy link
Copy Markdown

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 thread tests/test_routes.py
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"]
Copy link
Copy Markdown

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