From 40fa594379687229eb7e9d585a73927b576b3784 Mon Sep 17 00:00:00 2001 From: Evelyn Date: Sun, 6 Nov 2022 21:07:43 -0800 Subject: [PATCH 1/8] made get and post routes --- app/__init__.py | 4 +- app/models/task.py | 18 +++- app/routes.py | 27 +++++- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++ migrations/env.py | 96 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../versions/1f9c65321a1c_adds_task_model.py | 39 ++++++++ tests/test_wave_01.py | 2 +- 9 files changed, 252 insertions(+), 4 deletions(-) create mode 100644 migrations/README create mode 100644 migrations/alembic.ini create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/1f9c65321a1c_adds_task_model.py diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..90f13d662 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 tasks_bp + app.register_blueprint(tasks_bp) + return app diff --git a/app/models/task.py b/app/models/task.py index c91ab281f..b6c0e7eed 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -2,4 +2,20 @@ 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) + + def to_dict(self): + task_dict = { + "id": self.task_id, + "title": self.title, + "description": self.description, + } + + if self.completed_at == None: + task_dict["is_complete"] = False + + return task_dict + diff --git a/app/routes.py b/app/routes.py index 3aae38d49..146c86456 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1 +1,26 @@ -from flask import Blueprint \ No newline at end of file +from flask import Blueprint, jsonify, make_response, request +from app import db +from app.models.task import Task + +tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") + +@tasks_bp.route("", methods=["POST", "GET"]) +def handle_tasks(): + if request.method == "POST": + request_body = request.get_json() + + task = Task(title = request_body["title"], description = request_body["description"]) + + db.session.add(task) + db.session.commit() + + response_body = {"task": task.to_dict()} + + return make_response(jsonify(response_body), 201) + + elif request.method == "GET": + tasks = Task.query.all() + response_body = [] + for task in tasks: + response_body.append(task.to_dict()) + return make_response(jsonify(response_body), 200) \ 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/1f9c65321a1c_adds_task_model.py b/migrations/versions/1f9c65321a1c_adds_task_model.py new file mode 100644 index 000000000..b256c468a --- /dev/null +++ b/migrations/versions/1f9c65321a1c_adds_task_model.py @@ -0,0 +1,39 @@ +"""adds Task model + +Revision ID: 1f9c65321a1c +Revises: +Create Date: 2022-11-05 11:02:52.252878 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '1f9c65321a1c' +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(), nullable=False), + 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.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/tests/test_wave_01.py b/tests/test_wave_01.py index dca626d78..e055d3713 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -66,7 +66,7 @@ def test_get_task_not_found(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_create_task(client): # Act response = client.post("/tasks", json={ From 6387aa5b3d5d4f39e981bd7c31f11e9a76e4e83f Mon Sep 17 00:00:00 2001 From: Evelyn Date: Sun, 6 Nov 2022 21:16:57 -0800 Subject: [PATCH 2/8] added get one task route --- app/routes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 146c86456..e38fcd6aa 100644 --- a/app/routes.py +++ b/app/routes.py @@ -23,4 +23,10 @@ def handle_tasks(): response_body = [] for task in tasks: response_body.append(task.to_dict()) - return make_response(jsonify(response_body), 200) \ No newline at end of file + return make_response(jsonify(response_body), 200) + +@tasks_bp.route("/", methods=["GET"]) +def handle_task(task_id): + task = Task.query.get(task_id) + + return make_response(jsonify(task.to_dict())) \ No newline at end of file From 1cba073807724c9e6df9f713e819f2202f17c05a Mon Sep 17 00:00:00 2001 From: Evelyn Date: Sun, 6 Nov 2022 21:31:37 -0800 Subject: [PATCH 3/8] made put route --- app/routes.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index e38fcd6aa..63e3deec2 100644 --- a/app/routes.py +++ b/app/routes.py @@ -25,8 +25,21 @@ def handle_tasks(): response_body.append(task.to_dict()) return make_response(jsonify(response_body), 200) -@tasks_bp.route("/", methods=["GET"]) +@tasks_bp.route("/", methods=["GET", "PUT"]) def handle_task(task_id): - task = Task.query.get(task_id) + if request.method == "GET": + task = Task.query.get(task_id) - return make_response(jsonify(task.to_dict())) \ No newline at end of file + return make_response(jsonify(task.to_dict()), 200) + + elif request.method == "PUT": + task = Task.query.get(task_id) + + request_body = request.get_json() + + task.title = request_body["title"] + task.description = request_body["description"] + + db.session.commit() + + return make_response(jsonify(task.to_dict()), 200) From df18080b555b45749b52840bc31688b0592cb93e Mon Sep 17 00:00:00 2001 From: Evelyn Date: Thu, 10 Nov 2022 11:16:55 -0800 Subject: [PATCH 4/8] mostly done with wave one. fixed sytax and response body in delete route to pass tests --- app/models/task.py | 7 ++++++ app/routes.py | 57 +++++++++++++++++++++++++++++++++++++------ tests/test_wave_01.py | 21 ++++++++-------- 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/app/models/task.py b/app/models/task.py index b6c0e7eed..bc3e1d4f1 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -19,3 +19,10 @@ def to_dict(self): return task_dict + @classmethod + def from_dict(cls, task_dict): + return cls( + title = task_dict["title"], + description = task_dict["description"] + ) + diff --git a/app/routes.py b/app/routes.py index 63e3deec2..b99be8ba9 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify, make_response, request +from flask import Blueprint, jsonify, make_response, request, abort from app import db from app.models.task import Task @@ -9,7 +9,11 @@ def handle_tasks(): if request.method == "POST": request_body = request.get_json() - task = Task(title = request_body["title"], description = request_body["description"]) + try: + task = Task(title = request_body['title'], description = request_body['description']) + except KeyError: + invalid_dict = {"details": "invalid data"} + return make_response(jsonify(invalid_dict),400) db.session.add(task) db.session.commit() @@ -25,16 +29,19 @@ def handle_tasks(): response_body.append(task.to_dict()) return make_response(jsonify(response_body), 200) -@tasks_bp.route("/", methods=["GET", "PUT"]) +@tasks_bp.route("/", methods=["GET", "PUT", "DELETE"]) + def handle_task(task_id): if request.method == "GET": - task = Task.query.get(task_id) + task = validate_model_id(Task, task_id) + #task = Task.query.get(task_to_get) - return make_response(jsonify(task.to_dict()), 200) + return make_response(jsonify({"task": task.to_dict()}), 200) elif request.method == "PUT": - task = Task.query.get(task_id) - + task = validate_model_id(Task, task_id) + #task = Task.query.get(task_to_edit) + request_body = request.get_json() task.title = request_body["title"] @@ -42,4 +49,38 @@ def handle_task(task_id): db.session.commit() - return make_response(jsonify(task.to_dict()), 200) + return make_response(jsonify({"task": task.to_dict()}), 200) + + elif request.method == "DELETE": + task = validate_model_id(Task, task_id) + #task = Task.query.get(task_to_delete) + + db.session.delete(task) + db.session.commit() + + return make_response({"details": f'Task {task.task_id} ' f'"{task.title}"' ' successfully deleted'}, 200) + +def validate_model_id(cls,task_id): + try: + task_id = int(task_id) + except ValueError: + return abort(make_response({"message":f"{cls.__name__} id {task_id} is invalid"},400)) + + chosen_object = cls.query.get(task_id) + + if chosen_object is None: + return abort(make_response({"message":f"{cls.__name__} {task_id} not found"}, 404)) + + return chosen_object + +# #def check_for_missing_info(task_id): +# task = validate_model_id(Task, task_id) + +# if "title" not in task: +# return make_response("Missing title", 400) + +# if "description" not in task: +# return make_response("Missing description", 400) + +# if "completed_at" not in task: +# return make_response("Missing completed_at value", 400) \ No newline at end of file diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index e055d3713..2a430df5e 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,8 +59,7 @@ def test_get_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"} # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** @@ -93,7 +92,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 +118,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 +130,13 @@ 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"} + # ************************************e 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") From fff0f2b7ec4d5a110f6f8e6543d4deacb91e1a2c Mon Sep 17 00:00:00 2001 From: Evelyn Date: Thu, 10 Nov 2022 11:20:03 -0800 Subject: [PATCH 5/8] fixed syntax in post route to pass test --- app/routes.py | 2 +- tests/test_wave_01.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index b99be8ba9..b43a7cd34 100644 --- a/app/routes.py +++ b/app/routes.py @@ -12,7 +12,7 @@ def handle_tasks(): try: task = Task(title = request_body['title'], description = request_body['description']) except KeyError: - invalid_dict = {"details": "invalid data"} + invalid_dict = {"details": "Invalid data"} return make_response(jsonify(invalid_dict),400) db.session.add(task) diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index 2a430df5e..84a5a2bba 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -151,7 +151,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") @@ -160,7 +160,7 @@ def test_delete_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"} # ***************************************************************** # **Complete test with assertion about response body*************** # ***************************************************************** @@ -168,7 +168,7 @@ def test_delete_task_not_found(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_title(client): # Act response = client.post("/tasks", json={ From 85f79ba2497f46b0a801de7d551f3fc96a8e553c Mon Sep 17 00:00:00 2001 From: Evelyn Date: Thu, 10 Nov 2022 11:20:44 -0800 Subject: [PATCH 6/8] no new changes. Passes all Wave 1 tests --- tests/test_wave_01.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index 84a5a2bba..5b5564a0a 100644 --- a/tests/test_wave_01.py +++ b/tests/test_wave_01.py @@ -185,7 +185,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 ce7bc09e6a3f2410ba3a4761b82bb53bfa76e459 Mon Sep 17 00:00:00 2001 From: Evelyn Date: Sat, 12 Nov 2022 10:56:59 -0800 Subject: [PATCH 7/8] made asc and desc query params. Passes all Wave 2 tests. --- app/routes.py | 10 ++++++++++ tests/test_wave_02.py | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index b43a7cd34..d3613661c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,7 @@ from flask import Blueprint, jsonify, make_response, request, abort from app import db from app.models.task import Task +from sqlalchemy import desc tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") @@ -23,10 +24,19 @@ def handle_tasks(): return make_response(jsonify(response_body), 201) elif request.method == "GET": + sort_by_query = request.args.get('sort') + tasks = Task.query.all() response_body = [] + + if sort_by_query == 'asc': + tasks = Task.query.order_by(Task.title) + elif sort_by_query == 'desc': + tasks = Task.query.order_by(desc(Task.title)) + for task in tasks: response_body.append(task.to_dict()) + return make_response(jsonify(response_body), 200) @tasks_bp.route("/", methods=["GET", "PUT", "DELETE"]) diff --git a/tests/test_wave_02.py b/tests/test_wave_02.py index a087e0909..c9a76e6b1 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") From af1f42739ad764f9862bb3ec4bd94a6c69a54fdb Mon Sep 17 00:00:00 2001 From: Evelyn Date: Sat, 12 Nov 2022 15:32:23 -0800 Subject: [PATCH 8/8] added mark_complete and mark_incomplete routes. Passes all Wave 3 tests --- app/models/task.py | 2 ++ app/routes.py | 37 +++++++++++++++++++++---------------- tests/test_wave_03.py | 16 ++++++++-------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/models/task.py b/app/models/task.py index bc3e1d4f1..fc97d8c58 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -16,6 +16,8 @@ def to_dict(self): if self.completed_at == None: task_dict["is_complete"] = False + else: + task_dict["is_complete"] = True return task_dict diff --git a/app/routes.py b/app/routes.py index d3613661c..fb27f3336 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,6 +2,7 @@ from app import db from app.models.task import Task from sqlalchemy import desc +import datetime tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") @@ -42,16 +43,12 @@ def handle_tasks(): @tasks_bp.route("/", methods=["GET", "PUT", "DELETE"]) def handle_task(task_id): - if request.method == "GET": - task = validate_model_id(Task, task_id) - #task = Task.query.get(task_to_get) + task = validate_model_id(Task, task_id) + if request.method == "GET": return make_response(jsonify({"task": task.to_dict()}), 200) elif request.method == "PUT": - task = validate_model_id(Task, task_id) - #task = Task.query.get(task_to_edit) - request_body = request.get_json() task.title = request_body["title"] @@ -62,8 +59,6 @@ def handle_task(task_id): return make_response(jsonify({"task": task.to_dict()}), 200) elif request.method == "DELETE": - task = validate_model_id(Task, task_id) - #task = Task.query.get(task_to_delete) db.session.delete(task) db.session.commit() @@ -83,14 +78,24 @@ def validate_model_id(cls,task_id): return chosen_object -# #def check_for_missing_info(task_id): -# task = validate_model_id(Task, task_id) +@tasks_bp.route("//mark_complete", methods = ["PATCH"]) +def mark_complete(task_id): + task = validate_model_id(Task, task_id) + + task.completed_at = datetime.date.today() + task.is_complete = True -# if "title" not in task: -# return make_response("Missing title", 400) + db.session.commit() + + return make_response(jsonify({"task": task.to_dict()}), 200) + +@tasks_bp.route("//mark_incomplete", methods=["PATCH"]) +def mark_incomplete(task_id): + task = validate_model_id(Task, task_id) + + task.completed_at = None + task.is_complete = False -# if "description" not in task: -# return make_response("Missing description", 400) + db.session.commit() -# if "completed_at" not in task: -# return make_response("Missing completed_at value", 400) \ No newline at end of file + return make_response(jsonify({"task": task.to_dict()}), 200) \ No newline at end of file diff --git a/tests/test_wave_03.py b/tests/test_wave_03.py index 32d379822..f221e3dbc 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") @@ -128,13 +128,13 @@ 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"} # ***************************************************************** # **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 +143,7 @@ def test_mark_incomplete_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"} # ***************************************************************** # **Complete test with assertion about response body*************** # *****************************************************************