Skip to content

Commit c6e7a13

Browse files
committed
mark_test_complete returns same in prod and dev environment,using helper function and remove all jsonify
1 parent eb9c199 commit c6e7a13

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

app/routes/goal_routes.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from flask import Blueprint, jsonify, request, abort, make_response
1+
from flask import Blueprint, request, abort, make_response,Response
22
from ..db import db
33
from ..models.goal import Goal
44
from ..models.task import Task
5-
from .route_utilities import validate_model
5+
from .route_utilities import validate_model,create_model,get_models_with_filters
66

77

88
bp = Blueprint("goals_bp", __name__, url_prefix="/goals")
@@ -12,27 +12,27 @@
1212
@bp.post("")
1313
def create_goal():
1414
request_body = request.get_json()
15-
try:
16-
new_goal = Goal.from_dict(request_body)
17-
except KeyError:
18-
return jsonify({"details": "Invalid data"}), 400
15+
# try:
16+
# new_goal = Goal.from_dict(request_body)
17+
# except KeyError:
18+
# return jsonify({"details": "Invalid data"}), 400
1919

20-
db.session.add(new_goal)
21-
db.session.commit()
20+
# db.session.add(new_goal)
21+
# db.session.commit()
2222

23-
return jsonify(new_goal.to_dict()), 201
23+
#return jsonify(new_goal.to_dict()), 201
24+
return create_model(Goal,request_body)
2425

2526

2627
@bp.get("")
2728
def get_all_goals():
28-
goals = Goal.query.order_by(Goal.id).all()
29-
return jsonify([goal.to_dict() for goal in goals]), 200
30-
29+
filters = request.args.to_dict()
30+
return get_models_with_filters(Goal,filters)
3131

3232
@bp.get("/<id>")
3333
def get_one_goal(id):
3434
goal = validate_model(Goal, id)
35-
return jsonify(goal.to_dict()), 200
35+
return goal.to_dict()
3636

3737

3838

@@ -45,7 +45,7 @@ def update_goal(id):
4545
goal.title = request_body["title"]
4646

4747
db.session.commit()
48-
return jsonify(goal.to_dict()), 200
48+
return goal.to_dict()
4949

5050

5151

@@ -56,8 +56,8 @@ def delete_goal(id):
5656
db.session.delete(goal)
5757
db.session.commit()
5858

59-
return jsonify({"message": f'Goal {goal.id} successfully deleted'}), 204
60-
59+
#return jsonify({"message": f'Goal {goal.id} successfully deleted'}), 204
60+
return Response(status=204, mimetype="application/json")
6161

6262
#nested
6363
@bp.post("/<goal_id>/tasks")
@@ -70,18 +70,16 @@ def add_tasks_to_goal(goal_id):
7070
for task in goal.tasks:
7171
task.goal_id = None
7272

73-
74-
7573
for task_id in task_ids:
7674
task = validate_model(Task, task_id)
7775
task.goal_id = goal.id
7876

7977
db.session.commit()
8078

81-
return jsonify({
79+
return {
8280
"id": goal.id,
8381
"task_ids": task_ids
84-
}), 200
82+
}, 200
8583

8684

8785

@@ -91,10 +89,13 @@ def get_tasks_for_goal(goal_id):
9189

9290
tasks_response = [task.to_dict() for task in goal.tasks]
9391

94-
goal_dict = goal.to_dict()
95-
goal_dict["tasks"] = tasks_response
96-
97-
return jsonify(goal_dict), 200
92+
response_body = {
93+
"id": goal.id,
94+
"title": goal.title,
95+
"tasks": tasks_response
96+
}
97+
98+
return response_body,200
9899

99100

100101

app/routes/task_routes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def get_all_tasks():
6363

6464
result_list =[task.to_dict()for task in tasks]
6565

66-
return jsonify(result_list or []),200
66+
return result_list or []
6767

6868

6969
@bp.get("/<id>")
@@ -144,8 +144,8 @@ def mark_task_complete(id):
144144

145145
json={"channel": slack_channel, "text": message}
146146
)
147-
if current_app.config.get("TESTING"):
148-
return Response(status=204, mimetype="application/json")
147+
#if current_app.config.get("TESTING"):
148+
return Response(status=204, mimetype="application/json")
149149

150150

151-
return jsonify({"task": task.to_dict()}), 200
151+

tests/test_wave_07.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,15 @@ def test_route_utilities_create_model_with_goal_missing_title(client):
163163
# *****************************************************************************
164164
# **Complete test with assertion about status code response body***************
165165
# *****************************************************************************
166+
167+
168+
169+
def test_mark_complete_response_consistency(client, one_task):
170+
response = client.patch("/tasks/1/mark_complete")
171+
172+
#print("Test mode response status:", response.status_code)
173+
#print("Test mode response mimetype:", response.mimetype)
174+
#print("Test mode response data:", response.get_data(as_text=True))
175+
176+
assert response.status_code == 204
177+
assert response.mimetype == "application/json"

0 commit comments

Comments
 (0)