Skip to content

Zoisite - Johanna C. and Yvett J.#16

Open
johanna-j-c wants to merge 18 commits intoAda-C19:mainfrom
johanna-j-c:main
Open

Zoisite - Johanna C. and Yvett J.#16
johanna-j-c wants to merge 18 commits intoAda-C19:mainfrom
johanna-j-c:main

Conversation

@johanna-j-c
Copy link
Copy Markdown

Part 1

Comment thread app/routes.py Outdated
Comment on lines +41 to +42
for planet in planets:
result.append(planet.make_planet_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:

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

Comment thread app/routes.py Outdated
Comment on lines +58 to +61
@planet_bp.route("/<id>", methods=["GET"])
def get_planet(id):
planet = validate_planet(id)
return planet.make_planet_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.

It's not a rule and the code still executes just fine, but usually we'd see all the routes grouped together and all the helpers methods grouped together in a file.

@yangashley
Copy link
Copy Markdown

@johanna-j-c @yvett-codes 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!

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! I left a couple of small comments. Y'all seem to be in good shape wrt writing backend code using Flask and SQLAlchemy 🥳

Comment thread app/__init__.py
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_DATABASE_URI")
else:
app.config["TESTING"] = True
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.

Notice that line 15 and 19 are the same. Since this line of code needs to execute for both conditions, you can bring it outside the if/else block.

Comment thread app/routes.py
Comment on lines +19 to +20
except KeyError as e:
abort(make_response({"message": f"Missing required value {e}"}, 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 try/except to handle missing data when creating a Planet.

Comment thread app/routes.py

@planet_bp.route("/<id>", methods=["GET"])
def get_planet(id):
planet = validate_model(Planet, id)
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 work using the updated validate_model method and passing in the class of the object we want to validate

Comment thread app/routes.py

return jsonify(results)

def validate_model(cls, model_id):
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 +64 to +66
planet_to_update.name = planet_data["name"]
planet_to_update.description = planet_data["description"]
planet_to_update.radius = planet_data["radius"]
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 you had error handling in your POST request, you want to add try/except to handle missing data when updating a Planet. Right now, if the request body sent with this PUT request didn't have a key "description", then line 65 would throw an exception that doesn't get handled. We should anticipate that a request body might have missing or bad data and try to handle it our route.

Comment thread tests/conftest.py

db.session.add_all(planets)
db.session.commit()
return planets No newline at end of file
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 work writing up the boiler plate code needed for the app and client fixtures and also writing a couple of custom fixtures for saving some planets to the db!

Comment thread tests/test_routes.py
response_body = response.get_json()

assert response.status_code == 201
assert response_body == "Planet Yoshi successfully created."
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A way to make this test more robust would to check the response_body against the request body that you create for the post request.

The dictionary literal you create on lines 42-26 should be pulled into a variable like:

EXPECTED_PLANET = {
        "name": "Yoshi",
        "description": "Adorable and egg filled planet.",
        "radius": "7,156.9 mi"
    }

 response = client.post("/planets", json=EXPECTED_PLANET)

Then you could write some assertions like:

actual_planet = Planet.query.get(1) # since we create new table at the start of each test, we can assume that when your test makes a POST request to the DB that there will only be one planet in there so we can query for a record with id == 1
assert actual_planet.name == EXPECTED_PLANET["name"]
assert actual_planet.description == EXPECTED_PLANET["description"]
assert actual_planet.radius == EXPECTED_PLANET["radius"]

Comment thread tests/test_routes.py

assert response.status_code == 404

def test_get_all_planets_with_valid_data(client, multiple_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.

This is a thorough test! Nice work 👍

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