Skip to content

Commit

Permalink
Add unsaved task changes, update test files
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsey-steven-ada committed Oct 4, 2024
1 parent fb463ae commit 220e8df
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 99 deletions.
120 changes: 119 additions & 1 deletion app/routes/task_routes.py
Original file line number Diff line number Diff line change
@@ -1 +1,119 @@
from flask import Blueprint
from flask import Blueprint, request, abort, make_response, Response
from .route_utilities import validate_model
from app.models.task import Task
from datetime import datetime
from ..db import db
import requests
import os


SLACK_API_URL = os.environ["SLACK_API_URL"]
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]

bp = Blueprint("tasks_bp", __name__, url_prefix="/tasks")

@bp.get("")
def get_all_tasks():
query = db.select(Task)
sort_method = request.args.get('sort')

if sort_method and sort_method == "asc":
query = query.order_by(Task.title.asc())
if sort_method and sort_method == "desc":
query = query.order_by(Task.title.desc())

tasks = db.session.scalars(query)
tasks_response = [task.to_dict() for task in tasks]

return tasks_response


@bp.post("")
def create_task():
request_body = request.get_json()

try:
new_task = Task.from_dict(request_body)

except KeyError as error:
response = {"details": f"Invalid data"}
abort(make_response(response, 400))

db.session.add(new_task)
db.session.commit()

return {"task": new_task.to_dict()}, 201


@bp.get("/<task_id>")
def get_one_task(task_id):
task = validate_model(Task, task_id)
return {"task": task.to_dict()}


@bp.put("/<task_id>")
def update_task(task_id):
task = validate_model(Task, task_id)
request_body = request.get_json()

task.title = request_body["title"]
task.description = request_body["description"]

db.session.add(task)
db.session.commit()

return {"task": task.to_dict()}


@bp.delete("/<task_id>")
def delete_task(task_id):
task = validate_model(Task, task_id)
db.session.delete(task)
db.session.commit()

message = f"Task {task_id} \"{task.title}\" successfully deleted"
return {"details": message}


@bp.patch("/<task_id>/mark_complete")
def mark_task_complete(task_id):
task = validate_model(Task, task_id)
task.completed_at = datetime.now()

db.session.add(task)
db.session.commit()

#post_to_slack(task)

return {"task": task.to_dict()}


@bp.patch("/<task_id>/mark_incomplete")
def mark_task_incomplete(task_id):
task = validate_model(Task, task_id)
task.completed_at = None

db.session.add(task)
db.session.commit()

#post_to_slack(task)

return {"task": task.to_dict()}


def post_to_slack(task):
headers = {
"Authorization": f"Bearer {SLACK_BOT_TOKEN}",
}
if task.completed_at:
data = {
"channel": "instructors",
"text": f"Task {task.title} has been marked complete",
}
else:
data = {
"channel": "general",
"text": f"Task {task.title} has been marked incomplete",
}

r = requests.post(SLACK_API_URL, headers=headers, data=data)
45 changes: 18 additions & 27 deletions tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest


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


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


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


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_get_task_not_found(client):
# Act
response = client.get("/tasks/1")
response_body = response.get_json()

# Assert
assert response.status_code == 404
assert "message" in response_body
assert response_body["message"] == "Task 1 not found"

raise Exception("Complete test with assertion about response body")
# *****************************************************************
# **Complete test with assertion about response body***************
# *****************************************************************


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_create_task(client):
# Act
response = client.post("/tasks", json={
Expand All @@ -93,7 +90,7 @@ def test_create_task(client):
assert new_task.completed_at == None


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


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

# Assert
assert response.status_code == 404

raise Exception("Complete test with assertion about response body")
# *****************************************************************
# **Complete test with assertion about response body***************
# *****************************************************************
assert "message" in response_body
assert response_body["message"] == "Task 1 not found"


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


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

raise Exception("Complete test with assertion about response body")
# *****************************************************************
# **Complete test with assertion about response body***************
# *****************************************************************

assert "message" in response_body
assert response_body["message"] == "Task 1 not found"
assert Task.query.all() == []


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


@pytest.mark.skip(reason="No way to test this feature yet")
# @pytest.mark.skip(reason="No way to test this feature yet")
def test_create_task_must_contain_description(client):
# Act
response = client.post("/tasks", json={
Expand All @@ -200,4 +191,4 @@ def test_create_task_must_contain_description(client):
assert response_body == {
"details": "Invalid data"
}
assert Task.query.all() == []
assert Task.query.all() == []
6 changes: 3 additions & 3 deletions tests/test_wave_02.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest


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


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

0 comments on commit 220e8df

Please sign in to comment.