Skip to content

Rebecca Z and Sarah O in Spruce#22

Open
rzuick wants to merge 21 commits intoAda-C16:mainfrom
rzuick:main
Open

Rebecca Z and Sarah O in Spruce#22
rzuick wants to merge 21 commits intoAda-C16:mainfrom
rzuick:main

Conversation

@rzuick
Copy link
Copy Markdown

@rzuick rzuick commented Oct 27, 2021

No description provided.

Copy link
Copy Markdown

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

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

Great work Rebecca and Sarah! My feedback primarily focuses on refactoring :)

Comment thread app/__init__.py
app.config['SQLALCHEMY_ECHO'] = True

# import models here
from app.models.planet import 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.

The blueprints already import the models so we don't actually need the model import in this file anymore.

Comment thread app/models/planet.py
Comment on lines +5 to +8
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
description = db.Column(db.String)
distance = db.Column(db.String)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Careful! Using the class as it currently is, we'd be able to make a planet with a null name, description, and color. I do see in the POST request that y'all checked for falsy values for name, description, and color, however, null values can still be inserted manually using SQL. When making classes in the future, consider which columns are required (non-nullable).

Comment thread app/models/planet.py
description = db.Column(db.String)
distance = db.Column(db.String)

def planet_dict(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yay!! Nice helper method :D

Comment thread app/routes.py
Comment on lines +11 to +23
if request.method == "POST":
request_body = request.get_json()
if ("name" or "description" or "distance") not in request_body:
return jsonify("Invalid Request"), 404

new_planet = Planet(
name=request_body["name"],
description=request_body["description"],
distance=request_body["distance"]
)
db.session.add(new_planet)
db.session.commit()
return jsonify(new_planet.planet_dict()), 201
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 +24 to +34
elif request.method == "GET":
planet_name_query = request.args.get("name")
if planet_name_query:
planets = Planet.query.filter_by(name=planet_name_query)
else:
planets = Planet.query.all()

planets_response = [
planet.planet_dict() for planet in planets
]
return jsonify(planets_response)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 great use of a nested dictionary!

Comment thread app/routes.py
return jsonify(f"{planet.name} was successfully updated"), 200

elif request.method == "PATCH":
response_body = request.get_json()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adding a check for an empty response_body can also be handy here

Comment thread app/routes.py
db.session.commit()
return jsonify(f"{planet.name} was successfully updated"), 200

elif request.method == "DELETE":
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 tests/conftest.py
name="Earth", description="Blue green marble", distance="Right here")
planet2 = Planet(name="Mars", description="red", distance="next door")
db.session.add_all([planet1, planet2])
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.

Consider returning [planet1, planet2] after this line, so that tests can make use of the attributes of the records (like the id or name of planet)

Comment thread tests/test_routes.py
response = client.post("/planets", data=json.dumps(data),
headers={"Content-Type": "application/json"})

assert response.status_code == 201
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We could tweak the POST requests in routes.py so that we can return a dictionary of the actual record that was added (just to ensure that all the attributes were valid and added).

We could perform a Planet.query.get call to retrieve the record from the database to confirm that it was actually added.

Comment thread tests/test_routes.py
def test_get_planet_returns_404(client):
response = client.get("/planets/1")

assert response.status_code == 404 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.

Otherwise, your tests looks great!

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