From 494a30ad96a5d99bec6ad646ccafa7570cedb73f Mon Sep 17 00:00:00 2001 From: an2084 Date: Tue, 16 May 2023 15:28:12 -0400 Subject: [PATCH 01/10] Post method working --- app/__init__.py | 4 ++ app/models/task.py | 15 ++++- app/routes.py | 46 ++++++++++++- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++++++ migrations/env.py | 96 ++++++++++++++++++++++++++++ migrations/script.py.mako | 24 +++++++ migrations/versions/ea67644817cc_.py | 39 +++++++++++ tests/test_wave_01.py | 10 +-- 9 files changed, 272 insertions(+), 8 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/ea67644817cc_.py diff --git a/app/__init__.py b/app/__init__.py index 2764c4cc8..6461110ea 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -30,5 +30,9 @@ def create_app(test_config=None): migrate.init_app(app, db) # Register Blueprints here + from .routes import goal_bp + app.register_blueprint(goal_bp) + 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..229dfc168 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -1,5 +1,16 @@ from app import db - class Task(db.Model): - task_id = db.Column(db.Integer, primary_key=True) + id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String) + description = db.Column(db.String) + completed_at = db.Column(db.DateTime, nullable=True) + +def to_dict(self): + task_as_dict = {} + task_as_dict["id"] = self.id + task_as_dict["title"] = self.title + task_as_dict["description"] = self.description + task_as_dict["is_complete"] = self.is_complete + + return task_as_dict \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 3aae38d49..56d7c8eae 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1 +1,45 @@ -from flask import Blueprint \ No newline at end of file +from flask import Blueprint, jsonify, abort, make_response, request +from app import db +from app.models.goal import Goal +from app.models.task import Task + +goal_bp = Blueprint("goal", __name__, url_prefix="/goal") +task_bp = Blueprint("task", __name__, url_prefix="/task") + +@task_bp.route("", methods=['POST']) +def create_task(): + request_body = request.get_json() + + task = Task(title=request_body["title"], + description=request_body["description"], + completed_at=request_body["completed_at"]) + + + + db.session.add(task) + db.session.commit() + + return jsonify({ + "task": { + "id": task.id, + "title": task.title, + "description": task.description, + "is_complete": True if task.completed_at else False + } + }), 201 + +@task_bp.route("", methods = ["GET"]) +def read_all_tasks(): + title_query = request.args.get("title") + + if title_query: + tasks = Task.query.filter_by(title=title_query) + else: + tasks = Task.query.all() + + tasks_response = [] + + for task in tasks: + tasks_response.append(task.to_dict()) + + return jsonify(tasks_response) \ 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/ea67644817cc_.py b/migrations/versions/ea67644817cc_.py new file mode 100644 index 000000000..0b23987c8 --- /dev/null +++ b/migrations/versions/ea67644817cc_.py @@ -0,0 +1,39 @@ +"""empty message + +Revision ID: ea67644817cc +Revises: +Create Date: 2023-05-16 13:37:35.739721 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ea67644817cc' +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(), 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..6cc6f9771 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") @@ -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 855de522d04ccb43fc54616a756df450ee98ade2 Mon Sep 17 00:00:00 2001 From: an2084 Date: Wed, 17 May 2023 06:42:25 -0400 Subject: [PATCH 02/10] Read one task --- app/__init__.py | 3 +- app/routes.py | 43 ++++++++++++++++--- ...5353c32c0b_started_from_beginning_again.py | 30 +++++++++++++ tests/test_wave_01.py | 10 ++--- 4 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 migrations/versions/615353c32c0b_started_from_beginning_again.py diff --git a/app/__init__.py b/app/__init__.py index 6461110ea..19bc2a0b0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,14 +4,15 @@ 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 if test_config is None: diff --git a/app/routes.py b/app/routes.py index 56d7c8eae..a69dec251 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify, abort, make_response, request +from flask import Flask, Blueprint, jsonify, abort, make_response, request from app import db from app.models.goal import Goal from app.models.task import Task @@ -6,16 +6,19 @@ goal_bp = Blueprint("goal", __name__, url_prefix="/goal") task_bp = Blueprint("task", __name__, url_prefix="/task") +#TASK ROUTES + @task_bp.route("", methods=['POST']) + def create_task(): + request_body = request.get_json() + task = Task(title=request_body["title"], - description=request_body["description"], - completed_at=request_body["completed_at"]) + description=request_body["description"], + completed_at=request_body["completed_at"]) - - db.session.add(task) db.session.commit() @@ -30,10 +33,17 @@ def create_task(): @task_bp.route("", methods = ["GET"]) def read_all_tasks(): + title_query = request.args.get("title") + description_query = request.args.get("description") + is_complete_query = request.args.get("is_complete") if title_query: tasks = Task.query.filter_by(title=title_query) + elif description_query: + tasks = Task.query.filter_by(description=description_query) + elif completed_query: + tasks = Task.query.filter_by(description=is_complete_query) else: tasks = Task.query.all() @@ -42,4 +52,25 @@ def read_all_tasks(): for task in tasks: tasks_response.append(task.to_dict()) - return jsonify(tasks_response) \ No newline at end of file + return jsonify(tasks_response), 200 + +@task_bp.route("/", methods = ["GET"]) +def read_one_task(task_id): + + task = validate_model(Task, task_id) + + return task.to_dict(), 200 + +def validate_model(cls, model_id): + try: + model_id = int(model_id) + except: + abort(make_response({"message": f"{model_id} is not a valid type ({type(model_id)}). Must be an integer)"}, 400)) + + model = cls.query.get(model_id) + + if not model: + abort(make_response({"message": f"{cls.__name__} {model_id} does not exist"}, 404)) + + return model + diff --git a/migrations/versions/615353c32c0b_started_from_beginning_again.py b/migrations/versions/615353c32c0b_started_from_beginning_again.py new file mode 100644 index 000000000..219609f65 --- /dev/null +++ b/migrations/versions/615353c32c0b_started_from_beginning_again.py @@ -0,0 +1,30 @@ +"""Started from beginning again + +Revision ID: 615353c32c0b +Revises: ea67644817cc +Create Date: 2023-05-17 06:24:59.088577 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '615353c32c0b' +down_revision = 'ea67644817cc' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('id', sa.Integer(), nullable=False)) + op.drop_column('task', 'task_id') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('task_id', sa.INTEGER(), autoincrement=True, nullable=False)) + op.drop_column('task', 'id') + # ### end Alembic commands ### diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index 6cc6f9771..dca626d78 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") @@ -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 11b64be15063b9ec5933be19dcad7b6a745127a1 Mon Sep 17 00:00:00 2001 From: an2084 Date: Wed, 17 May 2023 06:53:20 -0400 Subject: [PATCH 03/10] Update task --- app/models/task.py | 1 + app/routes.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/models/task.py b/app/models/task.py index 229dfc168..9fbdc9dae 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -5,6 +5,7 @@ class Task(db.Model): title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable=True) + is_complete = db.Column(db.Boolean, default = False) def to_dict(self): task_as_dict = {} diff --git a/app/routes.py b/app/routes.py index a69dec251..59d52e762 100644 --- a/app/routes.py +++ b/app/routes.py @@ -61,6 +61,26 @@ def read_one_task(task_id): return task.to_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"] + + db.session.commit() + + return task.to_dict(), 200 + + + + + + def validate_model(cls, model_id): try: model_id = int(model_id) From 17a07f92dbc9667326e14a91f43a3bc865ae8245 Mon Sep 17 00:00:00 2001 From: an2084 Date: Wed, 17 May 2023 07:03:51 -0400 Subject: [PATCH 04/10] Delete task --- app/routes.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 59d52e762..1b1366530 100644 --- a/app/routes.py +++ b/app/routes.py @@ -59,7 +59,7 @@ def read_one_task(task_id): task = validate_model(Task, task_id) - return task.to_dict(), 200 + return jsonify({"task":task.to_dict()}), 200 @task_bp.route("/", methods=["PUT"]) @@ -74,7 +74,16 @@ def update_task(task_id): db.session.commit() - return task.to_dict(), 200 + return jsonify({"task":task.to_dict()}), 200 + +@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_id} \"{task.title}\" successfully deleted"}) From daf2560e119e2373126a9d47b5822ae26f0536f2 Mon Sep 17 00:00:00 2001 From: an2084 Date: Wed, 17 May 2023 16:02:03 -0400 Subject: [PATCH 05/10] Wave 1 completed --- app/__init__.py | 8 +- app/models/task.py | 20 ++--- app/routes.py | 80 +++++++++---------- ...py => 16588ec20c55_updating_task_model.py} | 8 +- ...5353c32c0b_started_from_beginning_again.py | 30 ------- tests/test_wave_01.py | 44 ++++------ 6 files changed, 76 insertions(+), 114 deletions(-) rename migrations/versions/{ea67644817cc_.py => 16588ec20c55_updating_task_model.py} (88%) delete mode 100644 migrations/versions/615353c32c0b_started_from_beginning_again.py diff --git a/app/__init__.py b/app/__init__.py index 19bc2a0b0..3ea3a2269 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -31,9 +31,9 @@ def create_app(test_config=None): migrate.init_app(app, db) # Register Blueprints here - from .routes import goal_bp - app.register_blueprint(goal_bp) - from .routes import task_bp - app.register_blueprint(task_bp) + from .routes import goals_bp + app.register_blueprint(goals_bp) + 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 9fbdc9dae..780d89204 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -1,17 +1,19 @@ from app import db class Task(db.Model): - id = db.Column(db.Integer, primary_key=True) + task_id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) description = db.Column(db.String) completed_at = db.Column(db.DateTime, nullable=True) - is_complete = db.Column(db.Boolean, default = False) -def to_dict(self): - task_as_dict = {} - task_as_dict["id"] = self.id - task_as_dict["title"] = self.title - task_as_dict["description"] = self.description - task_as_dict["is_complete"] = self.is_complete + def to_dict(self): + task_as_dict = { + "id": self.task_id, + "title": self.title, + "description": self.description, + "is_complete": True if self.completed_at else False + } + + return task_as_dict - return task_as_dict \ No newline at end of file + \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 1b1366530..0fbec92ce 100644 --- a/app/routes.py +++ b/app/routes.py @@ -3,47 +3,57 @@ from app.models.goal import Goal from app.models.task import Task -goal_bp = Blueprint("goal", __name__, url_prefix="/goal") -task_bp = Blueprint("task", __name__, url_prefix="/task") +goals_bp = Blueprint("goals", __name__, url_prefix="/goals") +tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") #TASK ROUTES -@task_bp.route("", methods=['POST']) +def validate_model(cls, model_id): + try: + model_id = int(model_id) + except: + abort(make_response({"message": f"{model_id} is not a valid type ({type(model_id)}). Must be an integer)"}, 400)) + + model = cls.query.get(model_id) + + if not model: + abort(make_response({"message": f"{cls.__name__} {model_id} does not exist"}, 404)) + + return model + +### + +@tasks_bp.route("", methods=['POST']) def create_task(): request_body = request.get_json() + if "title" not in request_body or "description" not in request_body or "is_complete" == False: + abort(make_response({"details": "Invalid data"}, 400)) - task = Task(title=request_body["title"], - description=request_body["description"], - completed_at=request_body["completed_at"]) + new_task = Task(title=request_body["title"], + description=request_body["description"]) + + if "completed_at" in request_body: + new_task.completed_at=request_body["completed_at"] - db.session.add(task) + db.session.add(new_task) db.session.commit() - return jsonify({ - "task": { - "id": task.id, - "title": task.title, - "description": task.description, - "is_complete": True if task.completed_at else False - } - }), 201 - -@task_bp.route("", methods = ["GET"]) + return { + "task": new_task.to_dict() + }, 201 + +@tasks_bp.route("", methods = ["GET"]) def read_all_tasks(): - title_query = request.args.get("title") - description_query = request.args.get("description") - is_complete_query = request.args.get("is_complete") + sort_query = request.args.get("sort") - if title_query: - tasks = Task.query.filter_by(title=title_query) - elif description_query: - tasks = Task.query.filter_by(description=description_query) - elif completed_query: - tasks = Task.query.filter_by(description=is_complete_query) + if sort_query == "asc": + tasks = Task.query.order_by(Task.title.asc()).all() + elif sort_query == "desc": + tasks = Task.query.order_by(Task.title.desc()).all() else: tasks = Task.query.all() @@ -54,7 +64,7 @@ def read_all_tasks(): return jsonify(tasks_response), 200 -@task_bp.route("/", methods = ["GET"]) +@tasks_bp.route("/", methods = ["GET"]) def read_one_task(task_id): task = validate_model(Task, task_id) @@ -62,7 +72,7 @@ def read_one_task(task_id): return jsonify({"task":task.to_dict()}), 200 -@task_bp.route("/", methods=["PUT"]) +@tasks_bp.route("/", methods=["PUT"]) def update_task(task_id): task = validate_model(Task, task_id) @@ -72,11 +82,12 @@ def update_task(task_id): task.title = request_body["title"] task.description = request_body["description"] + db.session.add(task) db.session.commit() return jsonify({"task":task.to_dict()}), 200 -@task_bp.route("/", methods=["DELETE"]) +@tasks_bp.route("/", methods=["DELETE"]) def delete_task(task_id): task = validate_model(Task, task_id) @@ -90,16 +101,5 @@ def delete_task(task_id): -def validate_model(cls, model_id): - try: - model_id = int(model_id) - except: - abort(make_response({"message": f"{model_id} is not a valid type ({type(model_id)}). Must be an integer)"}, 400)) - model = cls.query.get(model_id) - - if not model: - abort(make_response({"message": f"{cls.__name__} {model_id} does not exist"}, 404)) - - return model diff --git a/migrations/versions/ea67644817cc_.py b/migrations/versions/16588ec20c55_updating_task_model.py similarity index 88% rename from migrations/versions/ea67644817cc_.py rename to migrations/versions/16588ec20c55_updating_task_model.py index 0b23987c8..af02c2630 100644 --- a/migrations/versions/ea67644817cc_.py +++ b/migrations/versions/16588ec20c55_updating_task_model.py @@ -1,8 +1,8 @@ -"""empty message +"""Updating task model -Revision ID: ea67644817cc +Revision ID: 16588ec20c55 Revises: -Create Date: 2023-05-16 13:37:35.739721 +Create Date: 2023-05-17 15:14:03.704277 """ from alembic import op @@ -10,7 +10,7 @@ # revision identifiers, used by Alembic. -revision = 'ea67644817cc' +revision = '16588ec20c55' down_revision = None branch_labels = None depends_on = None diff --git a/migrations/versions/615353c32c0b_started_from_beginning_again.py b/migrations/versions/615353c32c0b_started_from_beginning_again.py deleted file mode 100644 index 219609f65..000000000 --- a/migrations/versions/615353c32c0b_started_from_beginning_again.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Started from beginning again - -Revision ID: 615353c32c0b -Revises: ea67644817cc -Create Date: 2023-05-17 06:24:59.088577 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '615353c32c0b' -down_revision = 'ea67644817cc' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('id', sa.Integer(), nullable=False)) - op.drop_column('task', 'task_id') - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('task_id', sa.INTEGER(), autoincrement=True, nullable=False)) - op.drop_column('task', 'id') - # ### end Alembic commands ### diff --git a/tests/test_wave_01.py b/tests/test_wave_01.py index dca626d78..450bbfc1f 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,11 @@ def test_get_task_not_found(client): # Assert assert response.status_code == 404 + assert "message" in response_body + assert response_body["message"] == "Task 1 does not exist" - 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 +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={ @@ -130,14 +127,11 @@ def test_update_task_not_found(client): # Assert assert response.status_code == 404 - - raise Exception("Complete test with assertion about response body") - # ***************************************************************** - # **Complete test with assertion about response body*************** - # ***************************************************************** + assert "message" in response_body + assert response_body["message"] == "Task 1 does not exist" -@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 +146,7 @@ def test_delete_task(client, one_task): assert Task.query.get(1) == None -@pytest.mark.skip(reason="No way to test this feature yet") +# @pytest.mark.skip(reason="No way to test this feature yet") def test_delete_task_not_found(client): # Act response = client.delete("/tasks/1") @@ -160,16 +154,12 @@ def test_delete_task_not_found(client): # Assert assert response.status_code == 404 - - raise Exception("Complete test with assertion about response body") - # ***************************************************************** - # **Complete test with assertion about response body*************** - # ***************************************************************** - + assert "message" in response_body + assert response_body["message"] == "Task 1 does not exist" 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 fb22ab7b3f4d68cd982ce49e1c2d30395f3aec5b Mon Sep 17 00:00:00 2001 From: an2084 Date: Wed, 17 May 2023 16:27:18 -0400 Subject: [PATCH 06/10] Completed waves 2 and 3 --- app/models/goal.py | 1 + app/routes.py | 26 ++++++++++++++++++++++++++ tests/test_wave_02.py | 4 ++-- tests/test_wave_03.py | 26 ++++++++++---------------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/app/models/goal.py b/app/models/goal.py index b0ed11dd8..de44a76f3 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -3,3 +3,4 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True) + title = db.Column(db.String) diff --git a/app/routes.py b/app/routes.py index 0fbec92ce..8028ec4b6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,6 +2,7 @@ from app import db from app.models.goal import Goal from app.models.task import Task +from datetime import date goals_bp = Blueprint("goals", __name__, url_prefix="/goals") tasks_bp = Blueprint("tasks", __name__, url_prefix="/tasks") @@ -98,6 +99,31 @@ def delete_task(task_id): +@tasks_bp.route("//mark_complete", methods=["PATCH"]) +def mark_task_complete(task_id): + + task = validate_model(Task, task_id) + + task.completed_at = date.today() + + db.session.add(task) + db.session.commit() + + return jsonify({"task":task.to_dict()}), 200 + +@tasks_bp.route("//mark_incomplete", methods=["PATCH"]) +def mark_task_incomplete(task_id): + + task = validate_model(Task, task_id) + + task.completed_at = None + + db.session.add(task) + db.session.commit() + + return jsonify({"task":task.to_dict()}), 200 + + 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..d67c5ee82 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,11 @@ def test_mark_complete_missing_task(client): # Assert assert response.status_code == 404 + assert "message" in response_body + assert response_body["message"] == "Task 1 does not exist" - 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") @@ -142,8 +139,5 @@ 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*************** - # ***************************************************************** + assert "message" in response_body + assert response_body["message"] == "Task 1 does not exist" From f46514e8d48ad2197217ea5827ea59196091b160 Mon Sep 17 00:00:00 2001 From: an2084 Date: Wed, 17 May 2023 17:32:25 -0400 Subject: [PATCH 07/10] Almost completed wave 06 --- app/models/goal.py | 8 ++ app/routes.py | 65 +++++++++++++ ...df_updated_goal_model_with_to_dict_func.py | 28 ++++++ tests/test_wave_05.py | 91 +++++++++++-------- 4 files changed, 152 insertions(+), 40 deletions(-) create mode 100644 migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py diff --git a/app/models/goal.py b/app/models/goal.py index de44a76f3..a37445ba5 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -4,3 +4,11 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) + + def to_dict(self): + goal_as_dict = { + "id": self.goal_id, + "title": self.title + } + + return goal_as_dict \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 8028ec4b6..f87385ad6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -123,9 +123,74 @@ def mark_task_incomplete(task_id): return jsonify({"task":task.to_dict()}), 200 +#################################GOAL ROUTES ############################################# +@goals_bp.route("", methods=['POST']) +def create_goal(): + request_body = request.get_json() + + if "title" not in request_body: + abort(make_response({"details": "Invalid data"}, 400)) + + new_goal = Goal(title=request_body["title"]) + + db.session.add(new_goal) + db.session.commit() + + return { + "goal": new_goal.to_dict() + }, 201 + + + + +@goals_bp.route("", methods = ["GET"]) + +def read_all_goals(): + goals = Goal.query.all() + + goals_response = [] + + for goal in goals: + goals_response.append(goal.to_dict()) + + return jsonify(goals_response), 200 + + + +@goals_bp.route("/", methods = ["GET"]) +def read_one_goal(goal_id): + + goal = validate_model(Goal, goal_id) + + return jsonify({"goal":goal.to_dict()}), 200 + + + +@goals_bp.route("/", methods=["PUT"]) +def update_goal(goal_id): + + task = validate_model(Goal, goal_id) + + request_body = request.get_json() + + goal.title = request_body["title"] + + db.session.add(goal) + db.session.commit() + + return jsonify({"goal":goal.to_dict()}), 200 + + +@goals_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_id} \"{goal.title}\" successfully deleted"}) diff --git a/migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py b/migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py new file mode 100644 index 000000000..ce61cf23e --- /dev/null +++ b/migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py @@ -0,0 +1,28 @@ +"""Updated goal model with to_dict func + +Revision ID: a5775a219ddf +Revises: 16588ec20c55 +Create Date: 2023-05-17 16:29:31.468947 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a5775a219ddf' +down_revision = '16588ec20c55' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('goal', sa.Column('title', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('goal', 'title') + # ### end Alembic commands ### diff --git a/tests/test_wave_05.py b/tests/test_wave_05.py index aee7c52a1..75fb48353 100644 --- a/tests/test_wave_05.py +++ b/tests/test_wave_05.py @@ -1,7 +1,8 @@ +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_get_goals_no_saved_goals(client): # Act response = client.get("/goals") @@ -12,7 +13,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 +30,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 +47,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 "message" in response_body + assert response_body["message"] == "Goal 1 does not exist" -@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={ @@ -80,35 +78,48 @@ 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") +###Completed 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 - # ---- Complete Assertions Here ---- + assert response.status_code == 200 + assert "goal" in response_body + assert response_body == { + "goal": { + "id": 1, + "title": "Updated Task Title" + } + } + goal = Goal.query.get(1) + assert goal.title == "Updated Task Title" -@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") +###Completed Test### # Act - # ---- Complete Act Here ---- + response = client.put("/goals/1", json={ + "title": "Updated Task Title" + }) + 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 + assert "message" in response_body + assert response_body["message"] == "Goal 1 does not exist" -@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): +###Completed Test### # Act response = client.delete("/goals/1") response_body = response.get_json() @@ -123,28 +134,28 @@ def test_delete_goal(client, one_goal): # 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*************** - # ***************************************************************** + assert "details" in response_body + assert response_body == { + "details": 'Goal 1 "Build a habit of going outside daily" successfully deleted' + } + assert Goal.query.get(1) == None -@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") - +###Completed 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 + assert "message" in response_body + assert response_body["message"] == "Goal 1 does not exist" + assert Goal.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_goal_missing_title(client): # Act response = client.post("/goals", json={}) From d6abbbd4bdfba68b3c737cc800d61adea2e895ba Mon Sep 17 00:00:00 2001 From: an2084 Date: Thu, 18 May 2023 14:03:08 -0400 Subject: [PATCH 08/10] All waves completed --- app/models/goal.py | 1 + app/models/task.py | 5 ++- app/routes.py | 43 ++++++++++++++++++- ...8728ed_added_relationship_to_task_model.py | 30 +++++++++++++ requirements.txt | 2 + tests/test_wave_05.py | 4 +- tests/test_wave_06.py | 21 ++++----- 7 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 migrations/versions/66a2998728ed_added_relationship_to_task_model.py diff --git a/app/models/goal.py b/app/models/goal.py index a37445ba5..31249023a 100644 --- a/app/models/goal.py +++ b/app/models/goal.py @@ -4,6 +4,7 @@ class Goal(db.Model): goal_id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String) + tasks = db.relationship("Task", back_populates="goal") def to_dict(self): goal_as_dict = { diff --git a/app/models/task.py b/app/models/task.py index 780d89204..f31d50c0b 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -5,13 +5,16 @@ class Task(db.Model): 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'), nullable=True) + goal = db.relationship("Goal", back_populates="tasks") def to_dict(self): task_as_dict = { "id": self.task_id, "title": self.title, "description": self.description, - "is_complete": True if self.completed_at else False + "is_complete": True if self.completed_at else False, + "goal_id":self.goal_id } return task_as_dict diff --git a/app/routes.py b/app/routes.py index f87385ad6..380fe37bb 100644 --- a/app/routes.py +++ b/app/routes.py @@ -123,7 +123,7 @@ def mark_task_incomplete(task_id): return jsonify({"task":task.to_dict()}), 200 -#################################GOAL ROUTES ############################################# +################################# GOAL ROUTES ####################################### @goals_bp.route("", methods=['POST']) @@ -172,7 +172,7 @@ def read_one_goal(goal_id): @goals_bp.route("/", methods=["PUT"]) def update_goal(goal_id): - task = validate_model(Goal, goal_id) + goal = validate_model(Goal, goal_id) request_body = request.get_json() @@ -193,4 +193,43 @@ def delete_goal(goal_id): return make_response({"details": f"Goal {goal_id} \"{goal.title}\" successfully deleted"}) +############################## MANY TO MANY RELATIONSHIP ####################### +@goals_bp.route("//tasks", methods=["POST"]) +def create_list_of_tasks(goal_id): + print("PRINT") + goal = validate_model(Goal, goal_id) + + request_body = request.get_json() + + for task_id in request_body["task_ids"]: + task = validate_model(Task, task_id) + if task not in goal.tasks: + goal.tasks.append(task) + + + task_ids=[] + for task in goal.tasks: + task_ids.append(task.task_id) + + db.session.commit() + + return { + "id": int(goal_id), + "task_ids": task_ids }, 200 +@goals_bp.route("//tasks", methods=["GET"]) + +def read_task_of_one_goal(goal_id): + + goal = validate_model(Goal, goal_id) + + tasks_response = [] + + for task in goal.tasks: + tasks_response.append(task.to_dict()) + + return { + "id": int(goal_id), + "title": goal.title, + "tasks": tasks_response + }, 200 \ No newline at end of file diff --git a/migrations/versions/66a2998728ed_added_relationship_to_task_model.py b/migrations/versions/66a2998728ed_added_relationship_to_task_model.py new file mode 100644 index 000000000..1be32088c --- /dev/null +++ b/migrations/versions/66a2998728ed_added_relationship_to_task_model.py @@ -0,0 +1,30 @@ +"""Added relationship to task model + +Revision ID: 66a2998728ed +Revises: a5775a219ddf +Create Date: 2023-05-17 17:42:05.317367 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '66a2998728ed' +down_revision = 'a5775a219ddf' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('task', sa.Column('goal_id', sa.Integer(), nullable=True)) + op.create_foreign_key(None, 'task', 'goal', ['goal_id'], ['goal_id']) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'task', type_='foreignkey') + op.drop_column('task', 'goal_id') + # ### end Alembic commands ### 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_05.py b/tests/test_wave_05.py index 75fb48353..9319a2ef4 100644 --- a/tests/test_wave_05.py +++ b/tests/test_wave_05.py @@ -93,12 +93,12 @@ def test_update_goal(client, one_goal): assert response_body == { "goal": { "id": 1, - "title": "Updated Task Title" + "title": "Updated Goal Title" } } goal = Goal.query.get(1) - assert goal.title == "Updated Task Title" + assert goal.title == "Updated Goal Title" diff --git a/tests/test_wave_06.py b/tests/test_wave_06.py index 8afa4325e..05837c547 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,22 +42,19 @@ 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") response_body = response.get_json() - + # Assert assert response.status_code == 404 - - raise Exception("Complete test with assertion about response body") - # ***************************************************************** - # **Complete test with assertion about response body*************** - # ***************************************************************** + assert "message" in response_body + assert response_body["message"] == "Goal 1 does not exist" -@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 +71,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 +96,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 cf1d1c107f3d804510a3fcaab366b12456ab2b1a Mon Sep 17 00:00:00 2001 From: an2084 Date: Thu, 18 May 2023 14:50:17 -0400 Subject: [PATCH 09/10] All tests passing --- app/models/task.py | 5 +++- ...8728ed_added_relationship_to_task_model.py | 30 ------------------- ...y => 7c8d5ebfb554_completed_task_model.py} | 11 ++++--- ...df_updated_goal_model_with_to_dict_func.py | 28 ----------------- 4 files changed, 11 insertions(+), 63 deletions(-) delete mode 100644 migrations/versions/66a2998728ed_added_relationship_to_task_model.py rename migrations/versions/{16588ec20c55_updating_task_model.py => 7c8d5ebfb554_completed_task_model.py} (75%) delete mode 100644 migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py diff --git a/app/models/task.py b/app/models/task.py index f31d50c0b..07cdcc294 100644 --- a/app/models/task.py +++ b/app/models/task.py @@ -14,9 +14,12 @@ def to_dict(self): "title": self.title, "description": self.description, "is_complete": True if self.completed_at else False, - "goal_id":self.goal_id + } + 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/migrations/versions/66a2998728ed_added_relationship_to_task_model.py b/migrations/versions/66a2998728ed_added_relationship_to_task_model.py deleted file mode 100644 index 1be32088c..000000000 --- a/migrations/versions/66a2998728ed_added_relationship_to_task_model.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Added relationship to task model - -Revision ID: 66a2998728ed -Revises: a5775a219ddf -Create Date: 2023-05-17 17:42:05.317367 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '66a2998728ed' -down_revision = 'a5775a219ddf' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('task', sa.Column('goal_id', sa.Integer(), nullable=True)) - op.create_foreign_key(None, 'task', 'goal', ['goal_id'], ['goal_id']) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint(None, 'task', type_='foreignkey') - op.drop_column('task', 'goal_id') - # ### end Alembic commands ### diff --git a/migrations/versions/16588ec20c55_updating_task_model.py b/migrations/versions/7c8d5ebfb554_completed_task_model.py similarity index 75% rename from migrations/versions/16588ec20c55_updating_task_model.py rename to migrations/versions/7c8d5ebfb554_completed_task_model.py index af02c2630..b48437236 100644 --- a/migrations/versions/16588ec20c55_updating_task_model.py +++ b/migrations/versions/7c8d5ebfb554_completed_task_model.py @@ -1,8 +1,8 @@ -"""Updating task model +"""Completed task model -Revision ID: 16588ec20c55 +Revision ID: 7c8d5ebfb554 Revises: -Create Date: 2023-05-17 15:14:03.704277 +Create Date: 2023-05-18 14:49:49.634211 """ from alembic import op @@ -10,7 +10,7 @@ # revision identifiers, used by Alembic. -revision = '16588ec20c55' +revision = '7c8d5ebfb554' down_revision = None branch_labels = None depends_on = None @@ -20,6 +20,7 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('goal', sa.Column('goal_id', sa.Integer(), nullable=False), + sa.Column('title', sa.String(), nullable=True), sa.PrimaryKeyConstraint('goal_id') ) op.create_table('task', @@ -27,6 +28,8 @@ def upgrade(): 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 ### diff --git a/migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py b/migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py deleted file mode 100644 index ce61cf23e..000000000 --- a/migrations/versions/a5775a219ddf_updated_goal_model_with_to_dict_func.py +++ /dev/null @@ -1,28 +0,0 @@ -"""Updated goal model with to_dict func - -Revision ID: a5775a219ddf -Revises: 16588ec20c55 -Create Date: 2023-05-17 16:29:31.468947 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'a5775a219ddf' -down_revision = '16588ec20c55' -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.add_column('goal', sa.Column('title', sa.String(), nullable=True)) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column('goal', 'title') - # ### end Alembic commands ### From 43a69a27d9cd77a5989e2be399440bdf6220ac6b Mon Sep 17 00:00:00 2001 From: an2084 Date: Thu, 18 May 2023 16:16:02 -0400 Subject: [PATCH 10/10] Configure Render database --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 3ea3a2269..2b83e1713 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -17,7 +17,7 @@ def create_app(test_config=None): if test_config is None: 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(