diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..62e430aca --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn 'app:create_app()' \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..d2ff42be4 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,12 +4,10 @@ import os from dotenv import load_dotenv - db = SQLAlchemy() migrate = Migrate() load_dotenv() - def create_app(test_config=None): app = Flask(__name__) app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False @@ -30,5 +28,13 @@ def create_app(test_config=None): migrate.init_app(app, db) # Register Blueprints here + from .routes.task_routes import task_bp + from .routes.goal_routes import bp + + app.register_blueprint(task_bp) + app.register_blueprint(bp) + from app.models.task import Task + from app.models.goal import Goal + return app diff --git a/app/models/goal.py b/app/models/goal.py index b0ed11dd8..376899dfd 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -1,5 +1,19 @@ 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, nullable=False) + tasks = db.relationship("Task", back_populates="goal", lazy=True) + + def to_dict_goal(self): + return { + "id": self.goal_id, + "title": self.title + } + + @classmethod + def from_dict(cls, data_dict): + if "title" in data_dict: + new_obj = cls(title=data_dict["title"]) + + return new_obj \ No newline at end of file diff --git a/app/models/task.py b/app/models/task.py index c91ab281f..d1d928134 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -1,5 +1,31 @@ from app import db - +from datetime import datetime 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, nullable=False) + description = db.Column(db.String) + completed_at = db.Column(db.DateTime, nullable=True) + + goal_id = db.Column(db.Integer, db.ForeignKey('goal.goal_id'), nullable=True) + goal = db.relationship("Goal", back_populates="tasks") + + def to_dict(self): + completed = True if self.completed_at else False + + task_dict = { + "id": self.task_id, + "title": self.title, + "description": self.description, + "is_complete": completed, + } + + if self.goal_id: + task_dict["goal_id"] = self.goal_id + + return task_dict + + @classmethod + def from_dict(cls, task_data): + + return Task(title=task_data["title"], description=task_data["description"], completed_at=None) \ No newline at end of file diff --git a/app/routes.py b/app/routes.py deleted file mode 100644 index 3aae38d49..000000000 --- a/app/routes.py +++ /dev/null @@ -1 +0,0 @@ -from flask import Blueprint \ No newline at end of file diff --git a/app/routes/goal_routes.py b/app/routes/goal_routes.py new file mode 100644 index 000000000..b8549a332 --- /dev/null +++ b/app/routes/goal_routes.py @@ -0,0 +1,120 @@ +from app import db +from app.models.task import Task +from app.models.goal import Goal +from flask import request, Blueprint, jsonify, make_response, abort +from sqlalchemy import desc +from datetime import datetime +import requests +import os +from .validate_model import validate_model + + +bp = Blueprint("bp", __name__, url_prefix="/goals") + +@bp.route("", methods=["GET"]) +def read_goals(): + goals = Goal.query.all() + + goals_list = [goal.to_dict_goal() for goal in goals] + + return make_response(jsonify(goals_list), 200) + +@bp.route("/", methods=["GET"]) +def read_one_goal(goal_id): + goal = validate_model(Goal, goal_id) + if goal: + return { + "goal": goal.to_dict_goal()}, 200 + else: + response_body = { + "details": "Invalid data"} + return make_response(jsonify(response_body)) + +@bp.route("", methods=["POST"]) +def create_goal(): + request_body = request.get_json() + + if not "title" in request_body: + return jsonify({ + "details": "Invalid data" + }), 400 + + + new_goal = Goal.from_dict(request_body) + + db.session.add(new_goal) + db.session.commit() + + return jsonify({ + "goal": new_goal.to_dict_goal() + }), 201 + + +@bp.route("/", methods=["PUT"]) +def update_goal(goal_id): + goal = validate_model(Goal, goal_id) + request_body = request.get_json() + + request_body = request.get_json() + + if "title" in request_body: + goal.title = request_body["title"] + db.session.commit() + response_body = { + "goal": goal.to_dict_goal() + } + return make_response(jsonify(response_body, 200)) + +@bp.route("/", methods=["DELETE"]) +def delete_goal(goal_id): + goal = validate_model(Goal, goal_id) + if goal: + + goal_dict = { + "details": f"Goal {goal_id} \"{goal.title }\" successfully deleted"} + + db.session.delete(goal) + db.session.commit() + return jsonify(goal_dict), 200 + + +@bp.route("//tasks", methods=["GET"]) +def goal_tasks(goal_id): + goal = validate_model(Goal, goal_id) + + goal_dict = { + "id": goal.goal_id, + "title": goal.title, + "tasks": [] + } + + for task in goal.tasks: + goal_dict["tasks"].append(task.to_dict()) + + return jsonify(goal_dict), 200 + +@bp.route("//tasks", methods=["POST"]) +def sending_list_of_task_ids_to_goal(goal_id): + goal= validate_model(Goal, goal_id) + request_body = request.get_json() + + goal.tasks = [] + + for task_id in request_body["task_ids"]: + task = validate_model(Task, task_id) + goal.tasks.append(task) + + db.session.commit() + + response = { + "id": goal.goal_id, + "task_ids": request_body["task_ids"] + } + return make_response(jsonify(response), 200) + +@bp.route("//tasks", methods=["GET"]) +def getting_tasks_of_one_goal(goal_id): + goal=validate_model(Goal, goal_id) + + goals_list = [goal.to_dict_goal() for goal in goal.tasks] + return jsonify(goals_list), 200 \ No newline at end of file diff --git a/app/routes/task_routes.py b/app/routes/task_routes.py new file mode 100644 index 000000000..31eda8f24 --- /dev/null +++ b/app/routes/task_routes.py @@ -0,0 +1,103 @@ +from flask import Blueprint, abort, jsonify, make_response, request +from app import db +from app.models.task import Task +import datetime +from app import os +from .validate_model import validate_model +import os +import requests + +task_bp = Blueprint("tasks", __name__, url_prefix="/tasks") + +"""Wave 1""" + +@task_bp.route("", methods=["POST"]) +def create_task(): + request_body = request.get_json() + + if "title" not in request_body or "description" not in request_body: + return {"details": "Invalid data"}, 400 + + new_task = Task.from_dict(request_body) + db.session.add(new_task) + db.session.commit() + + return jsonify({ + "task": new_task.to_dict() + }), 201 + +@task_bp.route("", methods=["GET"]) +def read_all_tasks(): + title_query = request.args.get("title") + 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(Task.title.desc()).all() + elif title_query: + tasks = Task.query.get(title=title_query) + else: + tasks = Task.query.all() + + task_list = [task.to_dict() for task in tasks] + + return make_response(jsonify(task_list), 200) + + +@task_bp.route("/", methods=["GET"]) +def read_one_task(task_id): + task = validate_model(Task, task_id) + + return make_response(jsonify({"task":task.to_dict()})) + +@task_bp.route("/", methods=["DELETE"]) +def delete_task(task_id): + task = validate_model(Task, task_id) + + task_dict = { + "details": f"Task {task_id} \"{task.title}\" successfully deleted" + } + + db.session.delete(task) + db.session.commit() + + return jsonify(task_dict), 200 + +@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"] + + response_body = {"task": task.to_dict()} + db.session.commit() + return jsonify(response_body), 200 + +"""WAVE 3 & 4""" +SLACK_TOKEN = os.environ.get('SLACK_TOKEN') +SLACK_URL = os.environ.get('SLACK_URL') + +@task_bp.route("//mark_incomplete", methods=["PATCH"]) +def mark_task_incomplete(task_id): + task = validate_model(Task, task_id) + task.completed_at = None + db.session.commit() + return jsonify(task=task.to_dict()), 200 + +@task_bp.route("//mark_complete", methods=["PATCH"]) +def mark_complete_on_completed_task_and_incomplete_task(task_id): + task = validate_model(Task, task_id) + validated_task = task.query.get(task_id) + task.completed_at = datetime.datetime.utcnow() + + headers = {"Authorization":f"Bearer {SLACK_TOKEN}"} + data = { + "channel":"task-notifications", + "text": f"Someone just completed the task {task.title}." + } + res = requests.post(SLACK_URL, headers=headers, data=data) + db.session.commit() + return jsonify({"task":task.to_dict()}), 200 \ No newline at end of file diff --git a/app/routes/validate_model.py b/app/routes/validate_model.py new file mode 100644 index 000000000..eb6c2c7b6 --- /dev/null +++ b/app/routes/validate_model.py @@ -0,0 +1,15 @@ +from flask import abort, make_response, jsonify + +def validate_model(cls, model_id): + try: + model_id = int(model_id) + except: + abort(make_response({"message":f"{cls.__name__} {model_id} invalid"}, 400)) + + model = cls.query.get(model_id) + + if not model: + response = f"{cls.__name__} #{model_id} was not found" + abort(make_response({"message": response}, 404)) + + return model \ No newline at end of file diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..f8ed4801f --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 000000000..8b3fb3353 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from flask import current_app + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option( + 'sqlalchemy.url', + str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 000000000..2c0156303 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/2ebaf05a6720_adding_goals_and_task_models.py b/migrations/versions/2ebaf05a6720_adding_goals_and_task_models.py new file mode 100644 index 000000000..06ed4753a --- /dev/null +++ b/migrations/versions/2ebaf05a6720_adding_goals_and_task_models.py @@ -0,0 +1,42 @@ +"""Adding goals and task models + +Revision ID: 2ebaf05a6720 +Revises: +Create Date: 2022-11-14 16:12:19.878354 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2ebaf05a6720' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('goal', + sa.Column('goal_id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('title', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('goal_id') + ) + op.create_table('task', + sa.Column('task_id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('title', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('completed_at', sa.DateTime(), nullable=True), + sa.Column('goal_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['goal_id'], ['goal.goal_id'], ), + sa.PrimaryKeyConstraint('task_id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('task') + op.drop_table('goal') + # ### end Alembic commands ### diff --git a/requirements.txt b/requirements.txt index cacdbc36e..b2b419106 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ autopep8==1.5.5 blinker==1.4 certifi==2020.12.5 chardet==4.0.0 +charset-normalizer==2.1.1 click==7.1.2 Flask==1.1.2 Flask-Migrate==2.6.0 @@ -22,7 +23,6 @@ py==1.10.0 pycodestyle==2.6.0 pyparsing==2.4.7 pytest==7.1.1 -pytest-cov==2.12.1 python-dateutil==2.8.1 python-dotenv==0.15.0 python-editor==1.0.4 @@ -30,5 +30,6 @@ requests==2.25.1 six==1.15.0 SQLAlchemy==1.3.23 toml==0.10.2 -urllib3==1.26.5 +tomli==2.0.1 +urllib3==1.26.4 Werkzeug==1.0.1 diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index dca626d78..ef932ac92 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -1,8 +1,7 @@ from app.models.task import Task 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") @@ -12,8 +11,7 @@ def test_get_tasks_no_saved_tasks(client): assert response.status_code == 200 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") @@ -31,8 +29,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") @@ -50,8 +47,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") @@ -60,13 +56,14 @@ def test_get_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*************** - # ***************************************************************** - + # Added assert statement + assert "message" in response_body + assert Task.query.get(1)== None + assert Task.query.all() == [] + assert "message" in response_body + assert response_body == {"message": "Task #1 was 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_create_task(client): # Act response = client.post("/tasks", json={ @@ -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={ @@ -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={ @@ -131,13 +128,13 @@ 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*************** - # ***************************************************************** + #Added assertions + assert Task.query.all() == [] + assert "message" in response_body + assert response_body == {"message": "Task #1 was 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") @@ -152,7 +149,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 +158,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*************** - # ***************************************************************** - + #Added assertions assert Task.query.all() == [] + assert "message" in response_body + assert response_body == {"message": "Task #1 was 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_create_task_must_contain_title(client): # Act response = client.post("/tasks", json={ @@ -185,8 +179,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..f545ba76c 100644 --- a/tests/test_wave_02.py +++ b/tests/test_wave_02.py @@ -1,7 +1,6 @@ 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 +28,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") @@ -54,4 +53,4 @@ def test_get_tasks_sorted_desc(client, three_tasks): "id": 2, "is_complete": False, "title": "Answer forgotten email 📧"}, - ] + ] \ No newline at end of file diff --git a/tests/test_wave_03.py b/tests/test_wave_03.py index 32d379822..e2b6a7986 100644 --- a/tests/test_wave_03.py +++ b/tests/test_wave_03.py @@ -4,8 +4,7 @@ from app.models.task import Task 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 +41,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") @@ -61,8 +60,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 +97,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 +117,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 +125,12 @@ def test_mark_complete_missing_task(client): # Assert assert response.status_code == 404 + + # Added assertion + assert "'message': 'Task #1 was not found' in response_body" + assert "message" in response_body - 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 +139,6 @@ def test_mark_incomplete_missing_task(client): # Assert assert response.status_code == 404 - raise Exception("Complete test with assertion about response body") - # ***************************************************************** - # **Complete test with assertion about response body*************** - # ***************************************************************** + # Added assertions + assert "'message': 'Task #1 was not found' in response_body" + assert "message" in response_body \ No newline at end of file diff --git a/tests/test_wave_05.py b/tests/test_wave_05.py index aee7c52a1..6ae5bc6ca 100644 --- a/tests/test_wave_05.py +++ b/tests/test_wave_05.py @@ -1,7 +1,6 @@ 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 +11,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 +28,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 +45,19 @@ 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 - # ---- Complete Test ---- - + assert response.status_code == 404 + #Added assertions + assert "message" in response_body + assert response_body == {"message": "Goal #1 was 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_create_goal(client): # Act response = client.post("/goals", json={ @@ -79,35 +75,39 @@ def test_create_goal(client): } } - -@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") + # Arrange + test_data = {"goal": + { + "id": 1, + "title": "Updated Goal Title" + }} + # Act - # ---- Complete Act Here ---- + response = client.put("/goals/1", json=test_data) + response_body = response.get_json() # Assert - # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here - # assertion 3 goes here - # ---- Complete Assertions Here ---- + assert response.status_code == 200 + """Added Asserts""" + f"'goal': 'id': 1, 'title': 'Build a habit of going outside daily'" -@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") - # Act - # ---- Complete Act Here ---- + response = client.delete("/goals/3") + response_body = response.get_json() # Assert - # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here - # ---- Complete Assertions Here ---- + assert response.status_code == 404 + #Added assertions + assert "message" in response_body + assert response_body == {"message": "Goal #3 was 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_goal(client, one_goal): # Act response = client.delete("/goals/1") @@ -120,31 +120,21 @@ def test_delete_goal(client, one_goal): "details": 'Goal 1 "Build a habit of going outside daily" successfully deleted' } - # Check that the goal was deleted - response = client.get("/goals/1") - assert response.status_code == 404 - - raise Exception("Complete test with assertion about response body") - # ***************************************************************** - # **Complete test with assertion about response body*************** - # ***************************************************************** - -@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.delete("/goals/1") + response_body = response.get_json() # Assert - # ---- Complete Assertions Here ---- - # assertion 1 goes here - # assertion 2 goes here - # ---- Complete Assertions Here ---- + assert response.status_code == 404 + #Added assertions + assert "message" in response_body + assert response_body == {"message": "Goal #1 was 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_create_goal_missing_title(client): # Act response = client.post("/goals", json={}) @@ -152,6 +142,8 @@ def test_create_goal_missing_title(client): # Assert assert response.status_code == 400 + + #Added assertion assert response_body == { "details": "Invalid data" - } + } \ No newline at end of file diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index 8afa4325e..4af5581fa 100644 --- a/tests/test_wave_06.py +++ b/tests/test_wave_06.py @@ -1,8 +1,7 @@ from app.models.goal import Goal 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={ @@ -22,8 +21,7 @@ def test_post_task_ids_to_goal(client, one_goal, three_tasks): # Check that Goal was updated in the db 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={ @@ -41,8 +39,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 +48,11 @@ 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") - # ***************************************************************** - # **Complete test with assertion about response body*************** - # ***************************************************************** - + #Add Assert statement + assert "message" in response_body + assert response_body == {"message": "Goal #1 was 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_get_tasks_for_specific_goal_no_tasks(client, one_goal): # Act response = client.get("/goals/1/tasks") @@ -73,8 +68,7 @@ def test_get_tasks_for_specific_goal_no_tasks(client, one_goal): "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_for_specific_goal(client, one_task_belongs_to_one_goal): # Act response = client.get("/goals/1/tasks") @@ -98,8 +92,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() @@ -115,4 +108,4 @@ def test_get_task_includes_goal_id(client, one_task_belongs_to_one_goal): "description": "Notice something new every day", "is_complete": False } - } + } \ No newline at end of file