diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..6a9b0576c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -12,11 +12,14 @@ def create_app(test_config=None): app = Flask(__name__) + 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( @@ -30,5 +33,9 @@ def create_app(test_config=None): migrate.init_app(app, db) # 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..e4cae0400 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -1,5 +1,15 @@ 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) + 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 + # 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 c91ab281f..4170f1200 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -1,5 +1,29 @@ 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) + goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id')) + goal = db.relationship('Goal', back_populates='tasks') + + @classmethod + def from_dict(cls, task_data): + new_task = Task(title=task_data["title"], + description=task_data["description"], + completed_at=None) + return new_task + + 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 3aae38d49..f18b2ad94 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1 +1,280 @@ -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 +from sqlalchemy import desc +from datetime import datetime +import requests +import os + +task_bp = Blueprint("tasks", __name__, url_prefix="/tasks") +goal_bp = Blueprint("goals", __name__, url_prefix="/goals") + +def validate_model(cls, model_id): + try: + model_id = int(model_id) + except: + 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) + + if not model: + abort(make_response({"message":f"{cls.__name__.lower()} {model_id} not found"}, 404)) + + return model + + +# 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.from_dict(request_body) + + # 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 = [] + + 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(task.to_dict()) + return jsonify(task_response) + + +@task_bp.route("/", methods=["GET"]) +def read_one_task(task_id): + task = validate_model(Task, task_id) + 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"]) +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.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_model(Task, task_id) + + db.session.delete(task) + db.session.commit() + + 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_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 + + db.session.commit() + + return { + "task": { + "id": task.task_id, + "title": task.title, + "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'}) + + +@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/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 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={ 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*************** # ***************************************************************** 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..4f3de5c53 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={ @@ -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") @@ -51,13 +51,14 @@ 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 == {"message":"goal 1 not found"} # ***************************************************************** # **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_get_tasks_for_specific_goal_no_tasks(client, one_goal): # Act response = client.get("/goals/1/tasks") @@ -74,7 +75,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 +100,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()