From 22923b5d0f0bd98c9f930346a0003faffd11b9fa Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Fri, 5 May 2023 20:22:31 -0400 Subject: [PATCH 1/7] add wave 1 crud operations and tests --- app/__init__.py | 4 +- app/models/task.py | 6 ++- app/routes.py | 111 +++++++++++++++++++++++++++++++++++++++++- tests/test_wave_01.py | 42 ++++++---------- 4 files changed, 133 insertions(+), 30 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..f0ff4e61b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -30,5 +30,7 @@ def create_app(test_config=None): migrate.init_app(app, db) # Register Blueprints here - + from .routes import task_bp + app.register_blueprint(task_bp) + return app diff --git a/app/models/task.py b/app/models/task.py index c91ab281f..3c3763a25 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -1,5 +1,7 @@ from app import db - class Task(db.Model): - task_id = db.Column(db.Integer, primary_key=True) + task_id = db.Column(db.Integer, primary_key=True, autoincrement=True) + title = db.Column(db.String) + description = db.Column(db.String) + completed_at = db.Column(db.DateTime, nullable=True) diff --git a/app/routes.py b/app/routes.py index 3aae38d49..8f793ed01 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1 +1,110 @@ -from flask import Blueprint \ No newline at end of file +from flask import Blueprint, jsonify, request, abort, make_response +from app import db +from app.models.task import Task +# from app.models.goal import Goal + +task_bp = Blueprint("tasks", __name__, url_prefix="/tasks") + +def validate_task(task_id): + try: + task_id = int(task_id) + except: + abort(make_response({"message": f"task {task_id} invalid"}, 400)) + + task = Task.query.get(task_id) + + if not task: + abort(make_response({"message": f"task {task_id} not found"}, 404)) + + return task + + +def validate_task_details(): + pass + +# Create a Task: Valid Task with 'null' 'completed at' +@task_bp.route("", methods=["POST"]) +def handle_tasks(): + # handle the HTTP request body - pasrses JSON body into a Python dict + request_body = request.get_json() + + # create a new task instance from the request + if len(request_body) < 2: + return {"details": "Invalid data"}, 400 + else: + new_task = Task( + title = request_body["title"], + description = request_body["description"] + ) + + # database collects new changes - adding new_task as a record + db.session.add(new_task) + # database saves and commits the new changes + db.session.commit() + + return { + "task": + { + "id": new_task.task_id, + "title": new_task.title, + "description": new_task.description, + "is_complete": bool(new_task.completed_at) + } + }, 201 + +# Get Saved Tasks from the database +@task_bp.route("", methods=["GET"]) +def read_all_tasks(): + task_response = [] + + tasks = Task.query.all() + if tasks: + for task in tasks: + task_response.append({ + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + }) + return jsonify(task_response) + + +@task_bp.route("/", methods=["GET"]) +def read_one_task(task_id): + task = validate_task(task_id) + return { + "task": + { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + } + } + +@task_bp.route("/", methods=["PUT"]) +def update_task(task_id): + task = validate_task(task_id) + request_body = request.get_json() + + task.title = request_body["title"] + task.description = request_body["description"] + + db.session.commit() + + return { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + }} + +@task_bp.route("/", methods=["DELETE"]) +def delete_task(task_id): + task = validate_task(task_id) + + db.session.delete(task) + db.session.commit() + + return make_response({"details": f'Task {task.task_id} "{task.title}" successfully deleted'}) diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index dca626d78..a6a5879ad 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -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") @@ -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") @@ -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") @@ -51,7 +51,7 @@ 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") @@ -59,14 +59,10 @@ def test_get_task_not_found(client): # Assert assert response.status_code == 404 + 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={ @@ -93,7 +89,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={ @@ -119,7 +115,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={ @@ -130,14 +126,10 @@ def test_update_task_not_found(client): # Assert assert response.status_code == 404 + # raise Exception("Complete test with assertion about 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_delete_task(client, one_task): # Act response = client.delete("/tasks/1") @@ -152,7 +144,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") @@ -161,15 +153,13 @@ 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*************** - # ***************************************************************** + # raise Exception("Complete test with assertion about 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={ @@ -186,7 +176,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={ From 4707ed3bd8ac60a07bb5a6a103c0f22c2075c90d Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Sun, 7 May 2023 20:00:01 -0400 Subject: [PATCH 2/7] Added patch request handling to update a task record --- app/routes.py | 48 ++++++++++++++++++++++++++++++++++--------- tests/test_wave_02.py | 4 ++-- tests/test_wave_03.py | 21 +++++++++---------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/app/routes.py b/app/routes.py index 8f793ed01..519878d3a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,8 @@ from flask import Blueprint, jsonify, request, abort, make_response from app import db from app.models.task import Task +from sqlalchemy import desc +from datetime import datetime # from app.models.goal import Goal task_bp = Blueprint("tasks", __name__, url_prefix="/tasks") @@ -19,9 +21,6 @@ def validate_task(task_id): return task -def validate_task_details(): - pass - # Create a Task: Valid Task with 'null' 'completed at' @task_bp.route("", methods=["POST"]) def handle_tasks(): @@ -52,12 +51,21 @@ def handle_tasks(): } }, 201 + # Get Saved Tasks from the database @task_bp.route("", methods=["GET"]) def read_all_tasks(): task_response = [] - tasks = Task.query.all() + sort_query = request.args.get("sort") + + if sort_query == "asc": + tasks = Task.query.order_by(Task.title).all() + elif sort_query == "desc": + tasks = Task.query.order_by(desc(Task.title)).all() + else: + tasks = Task.query.all() + if tasks: for task in tasks: task_response.append({ @@ -74,12 +82,12 @@ def read_one_task(task_id): task = validate_task(task_id) return { "task": - { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": bool(task.completed_at) - } + { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + } } @task_bp.route("/", methods=["PUT"]) @@ -108,3 +116,23 @@ def delete_task(task_id): db.session.commit() return make_response({"details": f'Task {task.task_id} "{task.title}" successfully deleted'}) + + +@task_bp.route("//", methods=["PATCH"]) +def update_completed_task(task_id, task_status): + task = validate_task(task_id) + if task: + if task_status == "mark_complete": + task.completed_at = datetime.now() + elif task_status == "mark_incomplete": + task.completed_at = None + + db.session.commit() + + return { + "task": { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + }} diff --git a/tests/test_wave_02.py b/tests/test_wave_02.py index a087e0909..651e3aebd 100644 --- a/tests/test_wave_02.py +++ b/tests/test_wave_02.py @@ -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") @@ -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") diff --git a/tests/test_wave_03.py b/tests/test_wave_03.py index 32d379822..29c8b036a 100644 --- a/tests/test_wave_03.py +++ b/tests/test_wave_03.py @@ -5,7 +5,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_mark_complete_on_incomplete_task(client, one_task): # Arrange """ @@ -42,7 +42,7 @@ def test_mark_complete_on_incomplete_task(client, one_task): assert Task.query.get(1).completed_at -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_incomplete_on_complete_task(client, completed_task): # Act response = client.patch("/tasks/1/mark_incomplete") @@ -62,7 +62,7 @@ def test_mark_incomplete_on_complete_task(client, completed_task): assert Task.query.get(1).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_mark_complete_on_completed_task(client, completed_task): # Arrange """ @@ -99,7 +99,7 @@ def test_mark_complete_on_completed_task(client, completed_task): assert Task.query.get(1).completed_at -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_mark_incomplete_on_incomplete_task(client, one_task): # Act response = client.patch("/tasks/1/mark_incomplete") @@ -119,7 +119,7 @@ def test_mark_incomplete_on_incomplete_task(client, one_task): assert Task.query.get(1).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_mark_complete_missing_task(client): # Act response = client.patch("/tasks/1/mark_complete") @@ -127,14 +127,12 @@ def test_mark_complete_missing_task(client): # Assert assert response.status_code == 404 + # raise Exception("Complete test with assertion about 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_mark_incomplete_missing_task(client): # Act response = client.patch("/tasks/1/mark_incomplete") @@ -143,7 +141,8 @@ def test_mark_incomplete_missing_task(client): # Assert assert response.status_code == 404 - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") + assert response_body == {"message": "task 1 not found"} # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** From b36dba20e9a8c1c3e053f8d13b3a2a3577eec421 Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Thu, 11 May 2023 15:51:48 -0400 Subject: [PATCH 3/7] added goal model --- app/__init__.py | 3 + app/models/goal.py | 13 ++++- app/models/task.py | 11 ++++ app/routes.py | 128 +++++++++++++++++++++++++++++++++++++----- tests/test_wave_05.py | 62 +++++++++++--------- tests/test_wave_06.py | 10 ++-- 6 files changed, 180 insertions(+), 47 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index f0ff4e61b..48edd320c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -12,6 +12,7 @@ def create_app(test_config=None): app = Flask(__name__) + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False if test_config is None: @@ -32,5 +33,7 @@ def create_app(test_config=None): # Register Blueprints here from .routes import task_bp app.register_blueprint(task_bp) + from .routes import goal_bp + app.register_blueprint(goal_bp) return app diff --git a/app/models/goal.py b/app/models/goal.py index b0ed11dd8..510c6b6c4 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -1,5 +1,14 @@ from app import db - class Goal(db.Model): - goal_id = db.Column(db.Integer, primary_key=True) + goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) + title = db.Column(db.String) + + # @classmethod + # # in class methods, cls must come first. it's a reference to the class itself + # def from_dict(cls, task_data): + # new_goal = goal( + # title=goal_data["title"] + # ) + + # return new_goal \ No newline at end of file diff --git a/app/models/task.py b/app/models/task.py index 3c3763a25..15daac184 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -5,3 +5,14 @@ class Task(db.Model): title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable=True) + + # @classmethod + # # in class methods, cls must come first. it's a reference to the class itself + # def from_dict(cls, task_data): + # new_task = Task( + # title=task_data["title"], + # description=task_data["description"], + # completed_at=task_data["completed_at"] + # ) + + # return new_task \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 519878d3a..26efce1bf 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,24 +1,28 @@ from flask import Blueprint, jsonify, request, abort, make_response from app import db from app.models.task import Task +from app.models.goal import Goal from sqlalchemy import desc from datetime import datetime -# from app.models.goal import Goal +import requests +import os task_bp = Blueprint("tasks", __name__, url_prefix="/tasks") +goal_bp = Blueprint("goals", __name__, url_prefix="/goals") -def validate_task(task_id): +def validate_model(cls, model_id): try: - task_id = int(task_id) + model_id = int(model_id) except: - abort(make_response({"message": f"task {task_id} invalid"}, 400)) + abort(make_response({"message":f"{model_id} invalid type ({type(model_id)})"}, 400)) + # abort(make_response({"message":f"Invalid data"}, 400)) + + model = cls.query.get(model_id) - task = Task.query.get(task_id) + if not model: + abort(make_response({"message":f"{cls.__name__.lower()} {model_id} not found"}, 404)) - if not task: - abort(make_response({"message": f"task {task_id} not found"}, 404)) - - return task + return model # Create a Task: Valid Task with 'null' 'completed at' @@ -79,7 +83,7 @@ def read_all_tasks(): @task_bp.route("/", methods=["GET"]) def read_one_task(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) return { "task": { @@ -90,9 +94,10 @@ def read_one_task(task_id): } } + @task_bp.route("/", methods=["PUT"]) def update_task(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) request_body = request.get_json() task.title = request_body["title"] @@ -110,7 +115,7 @@ def update_task(task_id): @task_bp.route("/", methods=["DELETE"]) def delete_task(task_id): - task = validate_task(task_id) + task = validate_model(Task, task_id) db.session.delete(task) db.session.commit() @@ -118,12 +123,33 @@ def delete_task(task_id): return make_response({"details": f'Task {task.task_id} "{task.title}" successfully deleted'}) + + +def handle_slack_api(task_title): + url = "https://slack.com/api/chat.postMessage?channel=task-notifications&text=beep%20boop&pretty=1" + + payload = { + "channel": "C0574HS4KL2", + "text": task_title + } + + auth_key = {"Authorization": f"Bearer " + os.environ.get('AUTHORIZATION')} + + response = requests.post(url, headers=auth_key, data=payload) + + print(response.text) + + + + @task_bp.route("//", methods=["PATCH"]) def update_completed_task(task_id, task_status): - task = validate_task(task_id) + task = validate_model(Task, task_id) if task: if task_status == "mark_complete": task.completed_at = datetime.now() + # call to API helper fuction here + handle_slack_api(task.title) elif task_status == "mark_incomplete": task.completed_at = None @@ -136,3 +162,79 @@ def update_completed_task(task_id, task_status): "description": task.description, "is_complete": bool(task.completed_at) }} + +# Wave 5: Creating a Second Model Goal +# Make a POST request +# Create a Valid Goal +@goal_bp.route("", methods=["POST"]) +def handle_goals(): + request_body = request.get_json() + + if len(request_body) < 1: + return {"details": "Invalid data"}, 400 + else: + new_goal = Goal( + title = request_body["title"] + ) + + db.session.add(new_goal) + db.session.commit() + + return { + "goal": + { + "id": new_goal.goal_id, + "title": new_goal.title + } + }, 201 + +@goal_bp.route("", methods=["GET"]) +def read_all_goals(): + goal_response = [] + + goals = Goal.query.all() + + if goals: + for goal in goals: + goal_response.append({ + "id": goal.goal_id, + "title": goal.title + }) + return jsonify(goal_response), 200 + +@goal_bp.route("/", methods=["GET"]) +def read_goal(goal_id): + goal = validate_model(Goal, goal_id) + return { + "goal": { + "id": goal.goal_id, + "title": goal.title + } + }, 200 + + +@goal_bp.route("/", methods=["PUT"]) +def update_goal(goal_id): + goal = validate_model(Goal, goal_id) + request_body = request.get_json() + + goal.title = request_body["title"] + + db.session.add(goal) + db.session.commit() + + return { + "goal": { + "id": goal.goal_id, + "title": goal.title + }}, 200 + + +@goal_bp.route("/", methods=["DELETE"]) +def delete_goal(goal_id): + goal = validate_model(Goal, goal_id) + + db.session.delete(goal) + db.session.commit() + + return make_response({"details": f'Goal {goal.goal_id} "{goal.title}" successfully deleted'}) diff --git a/tests/test_wave_05.py b/tests/test_wave_05.py index aee7c52a1..4e23fdfa7 100644 --- a/tests/test_wave_05.py +++ b/tests/test_wave_05.py @@ -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_goals_no_saved_goals(client): # Act response = client.get("/goals") @@ -12,7 +12,7 @@ def test_get_goals_no_saved_goals(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_goals_one_saved_goal(client, one_goal): # Act response = client.get("/goals") @@ -29,7 +29,7 @@ def test_get_goals_one_saved_goal(client, one_goal): ] -@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_goal(client, one_goal): # Act response = client.get("/goals/1") @@ -46,22 +46,20 @@ def test_get_goal(client, one_goal): } -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_get_goal_not_found(client): - pass # Act response = client.get("/goals/1") response_body = response.get_json() - raise Exception("Complete test") # Assert # ---- Complete Test ---- - # assertion 1 goes here - # assertion 2 goes here + assert response.status_code == 404 + assert response_body == {"message": "goal 1 not found"} # ---- Complete Test ---- -@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_goal(client): # Act response = client.post("/goals", json={ @@ -75,39 +73,48 @@ def test_create_goal(client): assert response_body == { "goal": { "id": 1, - "title": "My New Goal" + "title": 'My New Goal' } } -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_update_goal(client, one_goal): - raise Exception("Complete test") # Act # ---- Complete Act Here ---- + response = client.put("/goals/1", json={"title": 'Updated Goal Title'}) + response_body = response.get_json() # Assert # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here - # assertion 3 goes here + assert response.status_code == 200 + assert "goal" in response_body + assert response_body == { + "goal": { + "id": 1, + "title": 'Updated Goal Title' + } + } # ---- Complete Assertions Here ---- -@pytest.mark.skip(reason="test to be completed by student") + +# @pytest.mark.skip(reason="test to be completed by student") def test_update_goal_not_found(client): - raise Exception("Complete test") + # raise Exception("Complete test") # Act # ---- Complete Act Here ---- + response = client.put("/goals/100") + response_body = response.get_json() # Assert # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here + assert response.status_code == 404 + assert response_body == {"message": "goal 100 not found"} # ---- Complete Assertions Here ---- -@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_goal(client, one_goal): # Act response = client.delete("/goals/1") @@ -124,27 +131,28 @@ def test_delete_goal(client, one_goal): response = client.get("/goals/1") assert response.status_code == 404 - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** + assert response_body == {"details": 'Goal 1 "Build a habit of going outside daily" successfully deleted'} -@pytest.mark.skip(reason="test to be completed by student") +# @pytest.mark.skip(reason="test to be completed by student") def test_delete_goal_not_found(client): - raise Exception("Complete test") - # Act # ---- Complete Act Here ---- + response = client.put("/goals/100") + response_body = response.get_json() # Assert # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here + assert response.status_code == 404 + assert response_body == {"message": "goal 100 not found"} # ---- Complete Assertions Here ---- -@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_goal_missing_title(client): # Act response = client.post("/goals", json={}) diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index 8afa4325e..ebfcbac67 100644 --- a/tests/test_wave_06.py +++ b/tests/test_wave_06.py @@ -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_post_task_ids_to_goal(client, one_goal, three_tasks): # Act response = client.post("/goals/1/tasks", json={ @@ -23,7 +23,7 @@ def test_post_task_ids_to_goal(client, one_goal, three_tasks): assert len(Goal.query.get(1).tasks) == 3 -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_post_task_ids_to_goal_already_with_goals(client, one_task_belongs_to_one_goal, three_tasks): # Act response = client.post("/goals/1/tasks", json={ @@ -57,7 +57,7 @@ def test_get_tasks_for_specific_goal_no_goal(client): # ***************************************************************** -@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_for_specific_goal_no_tasks(client, one_goal): # Act response = client.get("/goals/1/tasks") @@ -74,7 +74,7 @@ def test_get_tasks_for_specific_goal_no_tasks(client, one_goal): } -@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_for_specific_goal(client, one_task_belongs_to_one_goal): # Act response = client.get("/goals/1/tasks") @@ -99,7 +99,7 @@ def test_get_tasks_for_specific_goal(client, one_task_belongs_to_one_goal): } -@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_includes_goal_id(client, one_task_belongs_to_one_goal): response = client.get("/tasks/1") response_body = response.get_json() From 2d6a37118e72362ddb964137d2199fd9e7a79389 Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Fri, 12 May 2023 11:19:13 -0400 Subject: [PATCH 4/7] updated goals routes to return goal tasks --- app/models/goal.py | 1 + app/models/task.py | 31 +++++++++++------ app/routes.py | 78 ++++++++++++++++++++++++++++++++----------- tests/test_wave_06.py | 3 +- 4 files changed, 83 insertions(+), 30 deletions(-) diff --git a/app/models/goal.py b/app/models/goal.py index 510c6b6c4..e4cae0400 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -3,6 +3,7 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) + tasks = db.relationship("Task", back_populates="goal", lazy='select') # @classmethod # # in class methods, cls must come first. it's a reference to the class itself diff --git a/app/models/task.py b/app/models/task.py index 15daac184..4170f1200 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -4,15 +4,26 @@ class Task(db.Model): task_id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String) description = db.Column(db.String) - completed_at = db.Column(db.DateTime, nullable=True) + completed_at = db.Column(db.DateTime, nullable=True) + goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id')) + goal = db.relationship('Goal', back_populates='tasks') - # @classmethod - # # in class methods, cls must come first. it's a reference to the class itself - # def from_dict(cls, task_data): - # new_task = Task( - # title=task_data["title"], - # description=task_data["description"], - # completed_at=task_data["completed_at"] - # ) + @classmethod + def from_dict(cls, task_data): + new_task = Task(title=task_data["title"], + description=task_data["description"], + completed_at=None) + return new_task - # return new_task \ No newline at end of file + def to_dict(self): + task_as_dict = { + "id": self.task_id, + "title": self.title, + "description": self.description, + "is_complete": bool(self.completed_at) + } + + if self.goal_id: + task_as_dict["goal_id"] = self.goal_id + + return task_as_dict \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 26efce1bf..13f1e6f04 100644 --- a/app/routes.py +++ b/app/routes.py @@ -35,10 +35,7 @@ def handle_tasks(): if len(request_body) < 2: return {"details": "Invalid data"}, 400 else: - new_task = Task( - title = request_body["title"], - description = request_body["description"] - ) + new_task = Task.from_dict(request_body) # database collects new changes - adding new_task as a record db.session.add(new_task) @@ -72,27 +69,35 @@ def read_all_tasks(): if tasks: for task in tasks: - task_response.append({ - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": bool(task.completed_at) - }) + task_response.append(task.to_dict()) return jsonify(task_response) @task_bp.route("/", methods=["GET"]) def read_one_task(task_id): task = validate_model(Task, task_id) - return { - "task": - { - "id": task.task_id, - "title": task.title, - "description": task.description, - "is_complete": bool(task.completed_at) - } - } + if not task.goal_id: + return { + "task": + { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at) + } + } + else: + return { + "task": + { + "id": task.task_id, + "title": task.title, + "description": task.description, + "is_complete": bool(task.completed_at), + "goal_id": task.goal_id + } + } + @task_bp.route("/", methods=["PUT"]) @@ -238,3 +243,38 @@ def delete_goal(goal_id): db.session.commit() return make_response({"details": f'Goal {goal.goal_id} "{goal.title}" successfully deleted'}) + + +@goal_bp.route("//tasks", methods=["POST"]) +def create_tasks_for_goal(goal_id): + goal = validate_model(Goal, goal_id) + request_body = request.get_json() + + for task in request_body["task_ids"]: + task = validate_model(Task, task) + goal.tasks.append(task) + + + db.session.commit() + return { + "id": goal.goal_id, + "task_ids": request_body["task_ids"] + } + + + +@goal_bp.route("//tasks", methods=["GET"]) +def read_tasks_from_goal(goal_id): + goal = validate_model(Goal, goal_id) + + goals_tasks = { + "id": goal.goal_id, + "title": goal.title, + "tasks": [] + } + + for task in goal.tasks: + + goals_tasks["tasks"].append(task.to_dict()) + + return goals_tasks, 200 \ No newline at end of file diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index ebfcbac67..f2cec656d 100644 --- a/tests/test_wave_06.py +++ b/tests/test_wave_06.py @@ -51,7 +51,8 @@ def test_get_tasks_for_specific_goal_no_goal(client): # Assert assert response.status_code == 404 - raise Exception("Complete test with assertion about response body") + # raise Exception("Complete test with assertion about response body") + assert response_body # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** From ae1103c697ed95de1786f1c01502ef0a7ee5e33a Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Fri, 12 May 2023 16:02:48 -0400 Subject: [PATCH 5/7] completed wave 6 tests for tasks in goal --- tests/test_wave_06.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index f2cec656d..4f3de5c53 100644 --- a/tests/test_wave_06.py +++ b/tests/test_wave_06.py @@ -42,7 +42,7 @@ def test_post_task_ids_to_goal_already_with_goals(client, one_task_belongs_to_on assert len(Goal.query.get(1).tasks) == 2 -@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_for_specific_goal_no_goal(client): # Act response = client.get("/goals/1/tasks") @@ -52,7 +52,7 @@ def test_get_tasks_for_specific_goal_no_goal(client): assert response.status_code == 404 # raise Exception("Complete test with assertion about response body") - assert response_body + assert response_body == {"message":"goal 1 not found"} # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** From dd35decad1cf4f59c0c8c14f69233dfe83e171d4 Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Fri, 12 May 2023 16:55:09 -0400 Subject: [PATCH 6/7] configure render database --- app/__init__.py | 4 +++- requirements.txt | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 48edd320c..6a9b0576c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -16,8 +16,10 @@ def create_app(test_config=None): app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False if test_config is None: + # app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( + # "SQLALCHEMY_DATABASE_URI") app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( - "SQLALCHEMY_DATABASE_URI") + "RENDER_DATABASE_URI") else: app.config["TESTING"] = True app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( diff --git a/requirements.txt b/requirements.txt index 453f0ef6a..552c57235 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ blinker==1.4 certifi==2020.12.5 chardet==4.0.0 click==7.1.2 +coverage==7.2.5 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 @@ -30,5 +31,6 @@ requests==2.25.1 six==1.15.0 SQLAlchemy==1.3.23 toml==0.10.2 +tomli==2.0.1 urllib3==1.26.5 Werkzeug==1.0.1 From 33001e262cd2777da5899ebb96455ed98df47c35 Mon Sep 17 00:00:00 2001 From: Elaine Watkins Date: Fri, 12 May 2023 20:02:33 -0400 Subject: [PATCH 7/7] fixed str concatenation for auth_key --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 13f1e6f04..f18b2ad94 100644 --- a/app/routes.py +++ b/app/routes.py @@ -138,7 +138,7 @@ def handle_slack_api(task_title): "text": task_title } - auth_key = {"Authorization": f"Bearer " + os.environ.get('AUTHORIZATION')} + auth_key = {"Authorization": f"Bearer {os.environ.get('AUTHORIZATION')}"} response = requests.post(url, headers=auth_key, data=payload)