Skip to content

Commit 220e8df

Browse files
Add unsaved task changes, update test files
1 parent fb463ae commit 220e8df

File tree

6 files changed

+256
-99
lines changed

6 files changed

+256
-99
lines changed

app/routes/task_routes.py

+119-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,119 @@
1-
from flask import Blueprint
1+
from flask import Blueprint, request, abort, make_response, Response
2+
from .route_utilities import validate_model
3+
from app.models.task import Task
4+
from datetime import datetime
5+
from ..db import db
6+
import requests
7+
import os
8+
9+
10+
SLACK_API_URL = os.environ["SLACK_API_URL"]
11+
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
12+
13+
bp = Blueprint("tasks_bp", __name__, url_prefix="/tasks")
14+
15+
@bp.get("")
16+
def get_all_tasks():
17+
query = db.select(Task)
18+
sort_method = request.args.get('sort')
19+
20+
if sort_method and sort_method == "asc":
21+
query = query.order_by(Task.title.asc())
22+
if sort_method and sort_method == "desc":
23+
query = query.order_by(Task.title.desc())
24+
25+
tasks = db.session.scalars(query)
26+
tasks_response = [task.to_dict() for task in tasks]
27+
28+
return tasks_response
29+
30+
31+
@bp.post("")
32+
def create_task():
33+
request_body = request.get_json()
34+
35+
try:
36+
new_task = Task.from_dict(request_body)
37+
38+
except KeyError as error:
39+
response = {"details": f"Invalid data"}
40+
abort(make_response(response, 400))
41+
42+
db.session.add(new_task)
43+
db.session.commit()
44+
45+
return {"task": new_task.to_dict()}, 201
46+
47+
48+
@bp.get("/<task_id>")
49+
def get_one_task(task_id):
50+
task = validate_model(Task, task_id)
51+
return {"task": task.to_dict()}
52+
53+
54+
@bp.put("/<task_id>")
55+
def update_task(task_id):
56+
task = validate_model(Task, task_id)
57+
request_body = request.get_json()
58+
59+
task.title = request_body["title"]
60+
task.description = request_body["description"]
61+
62+
db.session.add(task)
63+
db.session.commit()
64+
65+
return {"task": task.to_dict()}
66+
67+
68+
@bp.delete("/<task_id>")
69+
def delete_task(task_id):
70+
task = validate_model(Task, task_id)
71+
db.session.delete(task)
72+
db.session.commit()
73+
74+
message = f"Task {task_id} \"{task.title}\" successfully deleted"
75+
return {"details": message}
76+
77+
78+
@bp.patch("/<task_id>/mark_complete")
79+
def mark_task_complete(task_id):
80+
task = validate_model(Task, task_id)
81+
task.completed_at = datetime.now()
82+
83+
db.session.add(task)
84+
db.session.commit()
85+
86+
#post_to_slack(task)
87+
88+
return {"task": task.to_dict()}
89+
90+
91+
@bp.patch("/<task_id>/mark_incomplete")
92+
def mark_task_incomplete(task_id):
93+
task = validate_model(Task, task_id)
94+
task.completed_at = None
95+
96+
db.session.add(task)
97+
db.session.commit()
98+
99+
#post_to_slack(task)
100+
101+
return {"task": task.to_dict()}
102+
103+
104+
def post_to_slack(task):
105+
headers = {
106+
"Authorization": f"Bearer {SLACK_BOT_TOKEN}",
107+
}
108+
if task.completed_at:
109+
data = {
110+
"channel": "instructors",
111+
"text": f"Task {task.title} has been marked complete",
112+
}
113+
else:
114+
data = {
115+
"channel": "general",
116+
"text": f"Task {task.title} has been marked incomplete",
117+
}
118+
119+
r = requests.post(SLACK_API_URL, headers=headers, data=data)

tests/test_wave_01.py

+18-27
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44

5-
@pytest.mark.skip(reason="No way to test this feature yet")
5+
# @pytest.mark.skip(reason="No way to test this feature yet")
66
def test_get_tasks_no_saved_tasks(client):
77
# Act
88
response = client.get("/tasks")
@@ -13,7 +13,7 @@ def test_get_tasks_no_saved_tasks(client):
1313
assert response_body == []
1414

1515

16-
@pytest.mark.skip(reason="No way to test this feature yet")
16+
# @pytest.mark.skip(reason="No way to test this feature yet")
1717
def test_get_tasks_one_saved_tasks(client, one_task):
1818
# Act
1919
response = client.get("/tasks")
@@ -32,7 +32,7 @@ def test_get_tasks_one_saved_tasks(client, one_task):
3232
]
3333

3434

35-
@pytest.mark.skip(reason="No way to test this feature yet")
35+
# @pytest.mark.skip(reason="No way to test this feature yet")
3636
def test_get_task(client, one_task):
3737
# Act
3838
response = client.get("/tasks/1")
@@ -51,22 +51,19 @@ def test_get_task(client, one_task):
5151
}
5252

5353

54-
@pytest.mark.skip(reason="No way to test this feature yet")
54+
# @pytest.mark.skip(reason="No way to test this feature yet")
5555
def test_get_task_not_found(client):
5656
# Act
5757
response = client.get("/tasks/1")
5858
response_body = response.get_json()
5959

6060
# Assert
6161
assert response.status_code == 404
62+
assert "message" in response_body
63+
assert response_body["message"] == "Task 1 not found"
6264

63-
raise Exception("Complete test with assertion about response body")
64-
# *****************************************************************
65-
# **Complete test with assertion about response body***************
66-
# *****************************************************************
6765

68-
69-
@pytest.mark.skip(reason="No way to test this feature yet")
66+
# @pytest.mark.skip(reason="No way to test this feature yet")
7067
def test_create_task(client):
7168
# Act
7269
response = client.post("/tasks", json={
@@ -93,7 +90,7 @@ def test_create_task(client):
9390
assert new_task.completed_at == None
9491

9592

96-
@pytest.mark.skip(reason="No way to test this feature yet")
93+
# @pytest.mark.skip(reason="No way to test this feature yet")
9794
def test_update_task(client, one_task):
9895
# Act
9996
response = client.put("/tasks/1", json={
@@ -119,7 +116,7 @@ def test_update_task(client, one_task):
119116
assert task.completed_at == None
120117

121118

122-
@pytest.mark.skip(reason="No way to test this feature yet")
119+
# @pytest.mark.skip(reason="No way to test this feature yet")
123120
def test_update_task_not_found(client):
124121
# Act
125122
response = client.put("/tasks/1", json={
@@ -130,14 +127,11 @@ def test_update_task_not_found(client):
130127

131128
# Assert
132129
assert response.status_code == 404
133-
134-
raise Exception("Complete test with assertion about response body")
135-
# *****************************************************************
136-
# **Complete test with assertion about response body***************
137-
# *****************************************************************
130+
assert "message" in response_body
131+
assert response_body["message"] == "Task 1 not found"
138132

139133

140-
@pytest.mark.skip(reason="No way to test this feature yet")
134+
# @pytest.mark.skip(reason="No way to test this feature yet")
141135
def test_delete_task(client, one_task):
142136
# Act
143137
response = client.delete("/tasks/1")
@@ -152,7 +146,7 @@ def test_delete_task(client, one_task):
152146
assert Task.query.get(1) == None
153147

154148

155-
@pytest.mark.skip(reason="No way to test this feature yet")
149+
# @pytest.mark.skip(reason="No way to test this feature yet")
156150
def test_delete_task_not_found(client):
157151
# Act
158152
response = client.delete("/tasks/1")
@@ -161,15 +155,12 @@ def test_delete_task_not_found(client):
161155
# Assert
162156
assert response.status_code == 404
163157

164-
raise Exception("Complete test with assertion about response body")
165-
# *****************************************************************
166-
# **Complete test with assertion about response body***************
167-
# *****************************************************************
168-
158+
assert "message" in response_body
159+
assert response_body["message"] == "Task 1 not found"
169160
assert Task.query.all() == []
170161

171162

172-
@pytest.mark.skip(reason="No way to test this feature yet")
163+
# @pytest.mark.skip(reason="No way to test this feature yet")
173164
def test_create_task_must_contain_title(client):
174165
# Act
175166
response = client.post("/tasks", json={
@@ -186,7 +177,7 @@ def test_create_task_must_contain_title(client):
186177
assert Task.query.all() == []
187178

188179

189-
@pytest.mark.skip(reason="No way to test this feature yet")
180+
# @pytest.mark.skip(reason="No way to test this feature yet")
190181
def test_create_task_must_contain_description(client):
191182
# Act
192183
response = client.post("/tasks", json={
@@ -200,4 +191,4 @@ def test_create_task_must_contain_description(client):
200191
assert response_body == {
201192
"details": "Invalid data"
202193
}
203-
assert Task.query.all() == []
194+
assert Task.query.all() == []

tests/test_wave_02.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33

4-
@pytest.mark.skip(reason="No way to test this feature yet")
4+
#@pytest.mark.skip(reason="No way to test this feature yet")
55
def test_get_tasks_sorted_asc(client, three_tasks):
66
# Act
77
response = client.get("/tasks?sort=asc")
@@ -29,7 +29,7 @@ def test_get_tasks_sorted_asc(client, three_tasks):
2929
]
3030

3131

32-
@pytest.mark.skip(reason="No way to test this feature yet")
32+
#@pytest.mark.skip(reason="No way to test this feature yet")
3333
def test_get_tasks_sorted_desc(client, three_tasks):
3434
# Act
3535
response = client.get("/tasks?sort=desc")
@@ -54,4 +54,4 @@ def test_get_tasks_sorted_desc(client, three_tasks):
5454
"id": 2,
5555
"is_complete": False,
5656
"title": "Answer forgotten email 📧"},
57-
]
57+
]

0 commit comments

Comments
 (0)