From 2826bdda4dbc2c37d39f8c962bccb4b3c32e4621 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 18 Oct 2021 14:00:00 -0700 Subject: [PATCH 01/25] Created planet class and objects --- app/routes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..a5cc7e7f2 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,20 @@ from flask import Blueprint +class Planet: + def __init__(self, id, name, description, size_rank): + self.id = id + self.name = name + self.description = description + self.size_rank = size_rank + +planets = [ + Planet(20, "Jupiter", "Planet in the Milky Way", 1), + Planet(21, "Saturn", "Planet in the Milky Way", 2), + Planet(22, "Uranus", "Planet in the Milky Way", 3), + Planet(23, "Neptune", "Planet in the Milky Way", 4), + Planet(24, "Earth", "Planet in the Milky Way", 5), + Planet(25, "Venus", "Planet in the Milky Way", 6), + Planet(26, "Mars", "Planet in the Milky Way", 7), + Planet(27, "Mercury", "Planet in the Milky Way", 8), + Planet(28, "Pluto", "Planet in the Milky Way", 9), +] From cff53193dcbe34aec9f3ec2da920251fe865c316 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 18 Oct 2021 14:20:56 -0700 Subject: [PATCH 02/25] Added /planets route to return all planets. --- app/__init__.py | 3 +++ app/routes.py | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..ab9eee40e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,7 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import planets_bp + app.register_blueprint(planets_bp) + return app diff --git a/app/routes.py b/app/routes.py index a5cc7e7f2..c041134b9 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint +from flask import Blueprint, jsonify class Planet: def __init__(self, id, name, description, size_rank): @@ -18,3 +18,20 @@ def __init__(self, id, name, description, size_rank): Planet(27, "Mercury", "Planet in the Milky Way", 8), Planet(28, "Pluto", "Planet in the Milky Way", 9), ] + +planets_bp = Blueprint("planets", __name__) + +@planets_bp.route("/planets", methods=["GET"]) +def get_all_planets(): + response_list = [] + for planet in planets: + response_list.append( + { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "size_rank": planet.size_rank + } + ) + return jsonify(response_list) + # return response_list From bbdc9b309a9926eafe72ec50787398b68f9f38cf Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 18 Oct 2021 14:33:09 -0700 Subject: [PATCH 03/25] Create route for individual planets '/' --- app/routes.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index c041134b9..d6e66d3c0 100644 --- a/app/routes.py +++ b/app/routes.py @@ -19,9 +19,9 @@ def __init__(self, id, name, description, size_rank): Planet(28, "Pluto", "Planet in the Milky Way", 9), ] -planets_bp = Blueprint("planets", __name__) +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("/planets", methods=["GET"]) +@planets_bp.route("/", methods=["GET"]) def get_all_planets(): response_list = [] for planet in planets: @@ -34,4 +34,14 @@ def get_all_planets(): } ) return jsonify(response_list) - # return response_list + +@planets_bp.route("/", methods=["GET"],) +def get_one_planet_by_id(planet_id): + for planet in planets: + if int(planet_id) == planet.id: + return { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "size_rank": planet.size_rank + } From b2d27c1717558b3b333d7e95735326b1ba088716 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 18 Oct 2021 14:45:47 -0700 Subject: [PATCH 04/25] Create instance method that returns object as dictionary --- app/routes.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/app/routes.py b/app/routes.py index d6e66d3c0..09b7482a5 100644 --- a/app/routes.py +++ b/app/routes.py @@ -7,6 +7,14 @@ def __init__(self, id, name, description, size_rank): self.description = description self.size_rank = size_rank + def to_dictionary(self): + return { + "id": self.id, + "name": self.name, + "description": self.description, + "size_rank": self.size_rank + } + planets = [ Planet(20, "Jupiter", "Planet in the Milky Way", 1), Planet(21, "Saturn", "Planet in the Milky Way", 2), @@ -25,23 +33,12 @@ def __init__(self, id, name, description, size_rank): def get_all_planets(): response_list = [] for planet in planets: - response_list.append( - { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size_rank": planet.size_rank - } - ) + response_list.append(planet.to_dictionary()) return jsonify(response_list) @planets_bp.route("/", methods=["GET"],) def get_one_planet_by_id(planet_id): for planet in planets: if int(planet_id) == planet.id: - return { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size_rank": planet.size_rank - } + return planet.to_dictionary() + From c6c8f809f55e9bfd355d2a5ffaa2f98f0421cb0f Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 18 Oct 2021 15:00:58 -0700 Subject: [PATCH 05/25] Add error handling. --- app/routes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 09b7482a5..f6024a1c4 100644 --- a/app/routes.py +++ b/app/routes.py @@ -38,7 +38,13 @@ def get_all_planets(): @planets_bp.route("/", methods=["GET"],) def get_one_planet_by_id(planet_id): + if not planet_id.isdigit(): + return { + "error": "Invalid planet id.", + }, 400 for planet in planets: if int(planet_id) == planet.id: return planet.to_dictionary() - + return { + "error": "Planet not found.", + }, 404 From 6c9ccf3c0ad540c3e47bf2aa4c02bf0c9cd3b80f Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 18 Oct 2021 15:02:27 -0700 Subject: [PATCH 06/25] Fix spacing error in line 42. --- app/routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index f6024a1c4..a5cd30bfe 100644 --- a/app/routes.py +++ b/app/routes.py @@ -39,7 +39,7 @@ def get_all_planets(): @planets_bp.route("/", methods=["GET"],) def get_one_planet_by_id(planet_id): if not planet_id.isdigit(): - return { + return { "error": "Invalid planet id.", }, 400 for planet in planets: From bdd2d54ca67e2265fff863046070708d9dfb4b3d Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 25 Oct 2021 13:43:51 -0700 Subject: [PATCH 07/25] Update create_app to access database and SQLAlchemy --- app/__init__.py | 10 ++++++++++ app/models/planet.py | 1 + 2 files changed, 11 insertions(+) create mode 100644 app/models/planet.py diff --git a/app/__init__.py b/app/__init__.py index ab9eee40e..3be0f32c8 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,9 +1,19 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +db = SQLAlchemy() +migrate = Migrate() def create_app(test_config=None): app = Flask(__name__) + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + db.init_app(app) + migrate.init_app(app, db) + from .routes import planets_bp app.register_blueprint(planets_bp) diff --git a/app/models/planet.py b/app/models/planet.py new file mode 100644 index 000000000..030763662 --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1 @@ +from app import db \ No newline at end of file From c83adb31ad7453f9611004291438fe78aebb8fa9 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 25 Oct 2021 13:53:00 -0700 Subject: [PATCH 08/25] Create Planet model and import into create_app --- app/__init__.py | 4 +++- app/models/planet.py | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 3be0f32c8..440d92159 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -13,8 +13,10 @@ def create_app(test_config=None): db.init_app(app) migrate.init_app(app, db) - + from .routes import planets_bp app.register_blueprint(planets_bp) + from app.models.planet import Planet + return app diff --git a/app/models/planet.py b/app/models/planet.py index 030763662..3576274b4 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -1 +1,7 @@ -from app import db \ No newline at end of file +from app import db + +class Planet(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String) + description = db.Column(db.String) + size_rank = db.Column(db.Integer) \ No newline at end of file From f9dca04205ff308b9a285544e4662b1c26d7112d Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 25 Oct 2021 14:03:20 -0700 Subject: [PATCH 09/25] Applies migration --- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++ migrations/env.py | 96 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../9129fcb5000b_adds_planet_model.py | 34 +++++++ 5 files changed, 200 insertions(+) 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/9129fcb5000b_adds_planet_model.py 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/9129fcb5000b_adds_planet_model.py b/migrations/versions/9129fcb5000b_adds_planet_model.py new file mode 100644 index 000000000..1ae08fcf7 --- /dev/null +++ b/migrations/versions/9129fcb5000b_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds Planet model + +Revision ID: 9129fcb5000b +Revises: +Create Date: 2021-10-25 13:59:20.565157 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '9129fcb5000b' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('planet', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('size_rank', sa.Integer(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('planet') + # ### end Alembic commands ### From d52149d6d13764660b883a5a46999adf3fc54529 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 25 Oct 2021 14:38:14 -0700 Subject: [PATCH 10/25] Creates /planets POST endpoint --- app/routes.py | 77 +++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/app/routes.py b/app/routes.py index a5cd30bfe..85875da61 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,50 +1,37 @@ -from flask import Blueprint, jsonify +from app import db +from app.models.planet import Planet +from flask import Blueprint, jsonify, request, make_response -class Planet: - def __init__(self, id, name, description, size_rank): - self.id = id - self.name = name - self.description = description - self.size_rank = size_rank - - def to_dictionary(self): - return { - "id": self.id, - "name": self.name, - "description": self.description, - "size_rank": self.size_rank - } +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -planets = [ - Planet(20, "Jupiter", "Planet in the Milky Way", 1), - Planet(21, "Saturn", "Planet in the Milky Way", 2), - Planet(22, "Uranus", "Planet in the Milky Way", 3), - Planet(23, "Neptune", "Planet in the Milky Way", 4), - Planet(24, "Earth", "Planet in the Milky Way", 5), - Planet(25, "Venus", "Planet in the Milky Way", 6), - Planet(26, "Mars", "Planet in the Milky Way", 7), - Planet(27, "Mercury", "Planet in the Milky Way", 8), - Planet(28, "Pluto", "Planet in the Milky Way", 9), -] +@planets_bp.route("", methods=["POST"], strict_slashes=False) +def handle_planets(): + request_body = request.get_json() + new_planet = Planet( + name=request_body["name"], + description=request_body["description"], + size_rank=request_body["size_rank"] + ) + db.session.add(new_planet) + db.session.commit() -planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + return make_response(f"Planet {new_planet.name} successfully created", 201) -@planets_bp.route("/", methods=["GET"]) -def get_all_planets(): - response_list = [] - for planet in planets: - response_list.append(planet.to_dictionary()) - return jsonify(response_list) +# def get_all_planets(): +# response_list = [] +# for planet in planets: +# response_list.append(planet.to_dictionary()) +# return jsonify(response_list) -@planets_bp.route("/", methods=["GET"],) -def get_one_planet_by_id(planet_id): - if not planet_id.isdigit(): - return { - "error": "Invalid planet id.", - }, 400 - for planet in planets: - if int(planet_id) == planet.id: - return planet.to_dictionary() - return { - "error": "Planet not found.", - }, 404 +# @planets_bp.route("/", methods=["GET"],) +# def get_one_planet_by_id(planet_id): +# if not planet_id.isdigit(): +# return { +# "error": "Invalid planet id.", +# }, 400 +# for planet in planets: +# if int(planet_id) == planet.id: +# return planet.to_dictionary() +# return { +# "error": "Planet not found.", +# }, 404 From a0bf00c12c209f7fbbfdb051ff6861916231c5fc Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 25 Oct 2021 14:56:01 -0700 Subject: [PATCH 11/25] Adds /planets GET endpoint --- app/routes.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/app/routes.py b/app/routes.py index 85875da61..84c156ba3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,18 +4,32 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("", methods=["POST"], strict_slashes=False) +@planets_bp.route("", methods=["POST", "GET"], strict_slashes=False) def handle_planets(): - request_body = request.get_json() - new_planet = Planet( - name=request_body["name"], - description=request_body["description"], - size_rank=request_body["size_rank"] - ) - db.session.add(new_planet) - db.session.commit() + if request.method == "POST": + request_body = request.get_json() + new_planet = Planet( + name=request_body["name"], + description=request_body["description"], + size_rank=request_body["size_rank"] + ) + db.session.add(new_planet) + db.session.commit() - return make_response(f"Planet {new_planet.name} successfully created", 201) + return make_response(f"Planet {new_planet.name} successfully created", 201) + + elif request.method == "GET": + planet_objects = Planet.query.all() # returning list of objects + response_list = [] + for planet in planet_objects: + response_list.append( + { + "name": planet.name, + "description": planet.description, + "size_rank": planet.size_rank + } + ) + return jsonify(response_list), 200 # def get_all_planets(): # response_list = [] From 04ad8f074acf917abd7b643fac88ff20c7c93f84 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Mon, 25 Oct 2021 15:15:04 -0700 Subject: [PATCH 12/25] Create / GET endpoint --- app/routes.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/app/routes.py b/app/routes.py index 84c156ba3..828fde86c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -29,23 +29,16 @@ def handle_planets(): "size_rank": planet.size_rank } ) - return jsonify(response_list), 200 + return make_response(jsonify(response_list), 200) -# def get_all_planets(): -# response_list = [] -# for planet in planets: -# response_list.append(planet.to_dictionary()) -# return jsonify(response_list) -# @planets_bp.route("/", methods=["GET"],) -# def get_one_planet_by_id(planet_id): -# if not planet_id.isdigit(): -# return { -# "error": "Invalid planet id.", -# }, 400 -# for planet in planets: -# if int(planet_id) == planet.id: -# return planet.to_dictionary() -# return { -# "error": "Planet not found.", -# }, 404 +@planets_bp.route("/", methods=["GET"],) +def get_one_planet_by_id(planet_id): + # requested_planet = Planet.query.filter_by(id=planet_id).first() + requested_planet = Planet.query.get(planet_id) + return make_response({ + "name": requested_planet.name, + "description": requested_planet.description, + "size_rank": requested_planet.size_rank + }, 200) + From 8c72e852dd9f43089e404fc1b92e6d7c1766f776 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Tue, 26 Oct 2021 11:05:44 -0700 Subject: [PATCH 13/25] Add PATCH endpoint for updating a planet --- app/routes.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/app/routes.py b/app/routes.py index 828fde86c..35f0784b8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -24,6 +24,7 @@ def handle_planets(): for planet in planet_objects: response_list.append( { + "id": planet.id, "name": planet.name, "description": planet.description, "size_rank": planet.size_rank @@ -32,13 +33,26 @@ def handle_planets(): return make_response(jsonify(response_list), 200) -@planets_bp.route("/", methods=["GET"],) -def get_one_planet_by_id(planet_id): - # requested_planet = Planet.query.filter_by(id=planet_id).first() - requested_planet = Planet.query.get(planet_id) - return make_response({ - "name": requested_planet.name, - "description": requested_planet.description, - "size_rank": requested_planet.size_rank - }, 200) +@planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) +def handle_single_planet(planet_id): + selected_planet = Planet.query.get_or_404(planet_id) + if request.method == "GET": + return make_response({ + "id": selected_planet.id, + "name": selected_planet.name, + "description": selected_planet.description, + "size_rank": selected_planet.size_rank + }, 200) + + elif request.method == "PATCH": + request_body = request.get_json() + if "name" in request_body: + selected_planet.name = request_body["name"] + if "description" in request_body: + selected_planet.description = request_body["description"] + if "size_rank" in request_body: + selected_planet.size_rank = request_body["size_rank"] + db.session.commit() + return make_response(f"{selected_planet.name} updated", 200) + From 8a417e857fd94aa71b851e3a45c29172a5606b04 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Tue, 26 Oct 2021 11:08:25 -0700 Subject: [PATCH 14/25] Add DELETE endpoint for single planet --- app/routes.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/routes.py b/app/routes.py index 35f0784b8..3eeddea28 100644 --- a/app/routes.py +++ b/app/routes.py @@ -54,5 +54,10 @@ def handle_single_planet(planet_id): selected_planet.size_rank = request_body["size_rank"] db.session.commit() return make_response(f"{selected_planet.name} updated", 200) + + elif request.method == "DELETE": + db.session.delete(selected_planet) + db.session.commit() + return make_response(f"{selected_planet.name} deleted", 200) From 2b7a69169e308abd3a1589211e848f5234bbdc4e Mon Sep 17 00:00:00 2001 From: Laurel S Date: Tue, 26 Oct 2021 11:13:02 -0700 Subject: [PATCH 15/25] Add error handling for non-int planet id in url --- app/routes.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 3eeddea28..f8e4292d3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,6 @@ from app import db from app.models.planet import Planet -from flask import Blueprint, jsonify, request, make_response +from flask import Blueprint, jsonify, request, make_response, abort planets_bp = Blueprint("planets", __name__, url_prefix="/planets") @@ -35,7 +35,13 @@ def handle_planets(): @planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) def handle_single_planet(planet_id): + try: + planet_id = int(planet_id) + except: + abort(make_response({"error": "planet_id must be an int"}, 400)) + selected_planet = Planet.query.get_or_404(planet_id) + if request.method == "GET": return make_response({ "id": selected_planet.id, From 09626e085be1fd1164c09d912aa05ba9cb08a01e Mon Sep 17 00:00:00 2001 From: Laurel S Date: Tue, 26 Oct 2021 11:25:54 -0700 Subject: [PATCH 16/25] Add to_dict method to Planet model. Implement in GET endpoints. --- app/models/planet.py | 10 +++++++++- app/routes.py | 16 ++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index 3576274b4..fa6f002d3 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -4,4 +4,12 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) - size_rank = db.Column(db.Integer) \ No newline at end of file + size_rank = db.Column(db.Integer) + + def to_dict(self): + return { + "id": self.id, + "name": self.name, + "description": self.description, + "size_rank": self.size_rank + } \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index f8e4292d3..abb6b2d8e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -22,14 +22,7 @@ def handle_planets(): planet_objects = Planet.query.all() # returning list of objects response_list = [] for planet in planet_objects: - response_list.append( - { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "size_rank": planet.size_rank - } - ) + response_list.append(planet.to_dict()) return make_response(jsonify(response_list), 200) @@ -43,12 +36,7 @@ def handle_single_planet(planet_id): selected_planet = Planet.query.get_or_404(planet_id) if request.method == "GET": - return make_response({ - "id": selected_planet.id, - "name": selected_planet.name, - "description": selected_planet.description, - "size_rank": selected_planet.size_rank - }, 200) + return selected_planet.to_dict() elif request.method == "PATCH": request_body = request.get_json() From 88fa5641c6501fc3703437bc47d08d11fb80d753 Mon Sep 17 00:00:00 2001 From: Maria Obregon Garcia Date: Wed, 27 Oct 2021 14:55:04 -0400 Subject: [PATCH 17/25] Refactored routes to have single method --- app/routes.py | 101 ++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/app/routes.py b/app/routes.py index abb6b2d8e..c6098c1b7 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,54 +4,65 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("", methods=["POST", "GET"], strict_slashes=False) -def handle_planets(): - if request.method == "POST": - request_body = request.get_json() - new_planet = Planet( - name=request_body["name"], - description=request_body["description"], - size_rank=request_body["size_rank"] - ) - db.session.add(new_planet) - db.session.commit() - - return make_response(f"Planet {new_planet.name} successfully created", 201) - - elif request.method == "GET": - planet_objects = Planet.query.all() # returning list of objects - response_list = [] - for planet in planet_objects: - response_list.append(planet.to_dict()) - return make_response(jsonify(response_list), 200) - - -@planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) -def handle_single_planet(planet_id): +#Helper functions +def valid_planet(planet_id): try: - planet_id = int(planet_id) + int(planet_id) except: abort(make_response({"error": "planet_id must be an int"}, 400)) - + +def get_planet_from_id(planet_id): + valid_planet(planet_id) selected_planet = Planet.query.get_or_404(planet_id) - - if request.method == "GET": - return selected_planet.to_dict() - - elif request.method == "PATCH": - request_body = request.get_json() - if "name" in request_body: - selected_planet.name = request_body["name"] - if "description" in request_body: - selected_planet.description = request_body["description"] - if "size_rank" in request_body: - selected_planet.size_rank = request_body["size_rank"] - db.session.commit() - return make_response(f"{selected_planet.name} updated", 200) - - elif request.method == "DELETE": - db.session.delete(selected_planet) - db.session.commit() - return make_response(f"{selected_planet.name} deleted", 200) + return selected_planet + +# Routes +@planets_bp.route("", methods=["POST"], strict_slashes=False) +# def handle_planets(): +def add_planet(): + request_body = request.get_json() + new_planet = Planet( + name=request_body["name"], + description=request_body["description"], + size_rank=request_body["size_rank"] + ) + db.session.add(new_planet) + db.session.commit() + return make_response(f"Planet {new_planet.name} successfully created", 201) + +@planets_bp.route("", methods=["GET"], strict_slashes=False) +def get_all_planets(): + planet_objects = Planet.query.all() # returning list of objects + response_list = [] + for planet in planet_objects: + response_list.append(planet.to_dict()) + return make_response(jsonify(response_list), 200) + +# @planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) + +@planets_bp.route("/", methods=["GET"]) +def get_planet(planet_id): + selected_planet = get_planet_from_id(planet_id) + return selected_planet.to_dict() + +@planets_bp.route("/", methods=["PATCH"]) +def update_planet(planet_id): + selected_planet = get_planet_from_id(planet_id) + request_body = request.get_json() + if "name" in request_body: + selected_planet.name = request_body["name"] + if "description" in request_body: + selected_planet.description = request_body["description"] + if "size_rank" in request_body: + selected_planet.size_rank = request_body["size_rank"] + db.session.commit() + return make_response(f"{selected_planet.name} updated", 200) + +@planets_bp.route("/", methods=["DELETE"]) +def delete_planet(planet_id): + selected_planet = get_planet_from_id(planet_id) + db.session.delete(selected_planet) + db.session.commit() + return make_response(f"{selected_planet.name} deleted", 200) From 055f1a044ef453f4b868e74761e95bb16e82aaa2 Mon Sep 17 00:00:00 2001 From: Maria Obregon Garcia Date: Wed, 27 Oct 2021 14:55:04 -0400 Subject: [PATCH 18/25] Refactor routes to have single method --- app/routes.py | 101 ++++++++++++++++++++++++++++---------------------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/app/routes.py b/app/routes.py index abb6b2d8e..c6098c1b7 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,54 +4,65 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -@planets_bp.route("", methods=["POST", "GET"], strict_slashes=False) -def handle_planets(): - if request.method == "POST": - request_body = request.get_json() - new_planet = Planet( - name=request_body["name"], - description=request_body["description"], - size_rank=request_body["size_rank"] - ) - db.session.add(new_planet) - db.session.commit() - - return make_response(f"Planet {new_planet.name} successfully created", 201) - - elif request.method == "GET": - planet_objects = Planet.query.all() # returning list of objects - response_list = [] - for planet in planet_objects: - response_list.append(planet.to_dict()) - return make_response(jsonify(response_list), 200) - - -@planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) -def handle_single_planet(planet_id): +#Helper functions +def valid_planet(planet_id): try: - planet_id = int(planet_id) + int(planet_id) except: abort(make_response({"error": "planet_id must be an int"}, 400)) - + +def get_planet_from_id(planet_id): + valid_planet(planet_id) selected_planet = Planet.query.get_or_404(planet_id) - - if request.method == "GET": - return selected_planet.to_dict() - - elif request.method == "PATCH": - request_body = request.get_json() - if "name" in request_body: - selected_planet.name = request_body["name"] - if "description" in request_body: - selected_planet.description = request_body["description"] - if "size_rank" in request_body: - selected_planet.size_rank = request_body["size_rank"] - db.session.commit() - return make_response(f"{selected_planet.name} updated", 200) - - elif request.method == "DELETE": - db.session.delete(selected_planet) - db.session.commit() - return make_response(f"{selected_planet.name} deleted", 200) + return selected_planet + +# Routes +@planets_bp.route("", methods=["POST"], strict_slashes=False) +# def handle_planets(): +def add_planet(): + request_body = request.get_json() + new_planet = Planet( + name=request_body["name"], + description=request_body["description"], + size_rank=request_body["size_rank"] + ) + db.session.add(new_planet) + db.session.commit() + return make_response(f"Planet {new_planet.name} successfully created", 201) + +@planets_bp.route("", methods=["GET"], strict_slashes=False) +def get_all_planets(): + planet_objects = Planet.query.all() # returning list of objects + response_list = [] + for planet in planet_objects: + response_list.append(planet.to_dict()) + return make_response(jsonify(response_list), 200) + +# @planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) + +@planets_bp.route("/", methods=["GET"]) +def get_planet(planet_id): + selected_planet = get_planet_from_id(planet_id) + return selected_planet.to_dict() + +@planets_bp.route("/", methods=["PATCH"]) +def update_planet(planet_id): + selected_planet = get_planet_from_id(planet_id) + request_body = request.get_json() + if "name" in request_body: + selected_planet.name = request_body["name"] + if "description" in request_body: + selected_planet.description = request_body["description"] + if "size_rank" in request_body: + selected_planet.size_rank = request_body["size_rank"] + db.session.commit() + return make_response(f"{selected_planet.name} updated", 200) + +@planets_bp.route("/", methods=["DELETE"]) +def delete_planet(planet_id): + selected_planet = get_planet_from_id(planet_id) + db.session.delete(selected_planet) + db.session.commit() + return make_response(f"{selected_planet.name} deleted", 200) From 8b49c49236e2279be42acfd171709020423e6c18 Mon Sep 17 00:00:00 2001 From: Maria Obregon Garcia Date: Thu, 28 Oct 2021 14:44:06 -0400 Subject: [PATCH 19/25] Add connection strings as environment variables, refactor create_app to access test database --- .env | 2 ++ .gitignore | 10 +++++----- app/__init__.py | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 000000000..19080225d --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development +SQLALCHEMY_TEST_DATABASE_URI=postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_test diff --git a/.gitignore b/.gitignore index 4e9b18359..e2e92a9c5 100644 --- a/.gitignore +++ b/.gitignore @@ -107,13 +107,13 @@ celerybeat.pid # SageMath parsed files *.sage.py -# Environments -.env +# # Environments +# .env .venv -env/ +# env/ venv/ -ENV/ -env.bak/ +# ENV/ +# env.bak/ venv.bak/ # Spyder project settings diff --git a/app/__init__.py b/app/__init__.py index 440d92159..1e5bb9ed2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,15 +1,25 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate +from dotenv import load_dotenv +import os db = SQLAlchemy() migrate = Migrate() +load_dotenv() def create_app(test_config=None): app = Flask(__name__) - + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + if not test_config: + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( + "SQLALCHEMY_DATABASE_URI") + else: + app.config["TESTING"] = True + app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( + "SQLALCHEMY_TEST_DATABASE_URI") db.init_app(app) migrate.init_app(app, db) From 30d6c91cfeaf5202c894ae5b66dc3338b32dd132 Mon Sep 17 00:00:00 2001 From: Maria Obregon Garcia Date: Thu, 28 Oct 2021 16:06:36 -0400 Subject: [PATCH 20/25] Set up files and configurations needed for testing --- requirements.txt | 10 +++++++- tests/__init__.py | 0 tests/conftest.py | 39 +++++++++++++++++++++++++++++ tests/test_routes.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_routes.py diff --git a/requirements.txt b/requirements.txt index fd90fffa8..ffcfcb2eb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ alembic==1.5.4 +attrs==20.3.0 autopep8==1.5.5 certifi==2020.12.5 chardet==4.0.0 @@ -6,13 +7,20 @@ click==7.1.2 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 +gunicorn==20.1.0 idna==2.10 +iniconfig==1.1.1 itsdangerous==1.1.0 Jinja2==2.11.3 Mako==1.1.4 MarkupSafe==1.1.1 +packaging==20.9 +pluggy==0.13.1 psycopg2-binary==2.8.6 +py==1.10.0 pycodestyle==2.6.0 +pyparsing==2.4.7 +pytest==6.2.3 python-dateutil==2.8.1 python-dotenv==0.15.0 python-editor==1.0.4 @@ -21,4 +29,4 @@ six==1.15.0 SQLAlchemy==1.3.23 toml==0.10.2 urllib3==1.26.4 -Werkzeug==1.0.1 +Werkzeug==1.0.1 \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..111ba3895 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,39 @@ +import pytest + +from app import create_app, db +from app.models.planet import Planet + +#app +@pytest.fixture +def app(): + app = create_app({"Testing": True}) + + with app.app_context(): + db.create_all() + yield app + + with app.app_context(): + db.drop_all() + +#client +@pytest.fixture +def client(app): + return app.test_client() + +#data +@pytest.fixture +def one_planet(app): + new_planet = Planet( + name="Jupiter", description="A planet in the Milky Way", size_rank=1) + db.session.add(new_planet) + db.session.commit() + +@pytest.fixture +def multiple_planets(app): + planet_one = Planet( + name="Jupiter", description="A planet in the Milky Way", size_rank=1) + planet_two = Planet( + name="Saturn", description="A planet in the Milky Way", size_rank=2) + db.session.add(planet_one) + db.session.add(planet_two) + db.session.commit() diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..c2eb20607 --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,58 @@ +def test_get_planets_returns_200_empty_array_when_db_is_empty(client): + # act + response = client.get("/planets") + + # assert + assert response.status_code == 200 + assert response.get_json() == [] + +# def test_get_single_planet_returns_one_saved_planet(client, one_planet): +# # act +# response = client.get("/planets/1") + +# # assert +# assert response.status_code == 200 +# assert response.get_json() == { +# "id": 1, +# "name": "Jupiter", +# "description": "A planet in the Milky Way", +# "size_rank": 1 +# } + +# def test_get_single_planet_with_empty_db_returns_404(client): +# # act +# response = client.get("/planets/1") + +# # assert +# assert response.status_code == 404 +# assert response.get_json() == [] + + +# def test_get_planets_with_valid_data_returns_correct_array(client, multiple_planets): +# # act +# response = client.get("/planets") + +# # assert +# assert response.status_code == 200 +# assert response.get_json() == [ +# { +# "id": 1, +# "name": "Jupiter", +# "description": "A planet in the Milky Way", +# "size_rank": 1 +# }, +# { +# "id": 2, +# "name": "Saturn", +# "description": "A planet in the Milky Way", +# "size_rank": 2 +# } +# ] + +# def test_post_planets_with_JSON_request_body_returns_201(client): +# # act +# response = client.get("/planets/1") + +# # assert +# assert response.status_code == 201 +# assert response.get_json() == [] \ No newline at end of file From 8b38a3156df4272927422482f582dd107a526d8b Mon Sep 17 00:00:00 2001 From: Maria Obregon Garcia Date: Thu, 28 Oct 2021 16:30:01 -0400 Subject: [PATCH 21/25] Adds tests 0-3 --- tests/test_routes.py | 104 ++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index c2eb20607..d6d74c866 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -6,53 +6,65 @@ def test_get_planets_returns_200_empty_array_when_db_is_empty(client): assert response.status_code == 200 assert response.get_json() == [] -# def test_get_single_planet_returns_one_saved_planet(client, one_planet): -# # act -# response = client.get("/planets/1") - -# # assert -# assert response.status_code == 200 -# assert response.get_json() == { -# "id": 1, -# "name": "Jupiter", -# "description": "A planet in the Milky Way", -# "size_rank": 1 -# } - -# def test_get_single_planet_with_empty_db_returns_404(client): -# # act -# response = client.get("/planets/1") - -# # assert -# assert response.status_code == 404 -# assert response.get_json() == [] - - -# def test_get_planets_with_valid_data_returns_correct_array(client, multiple_planets): -# # act -# response = client.get("/planets") - -# # assert -# assert response.status_code == 200 -# assert response.get_json() == [ -# { -# "id": 1, -# "name": "Jupiter", -# "description": "A planet in the Milky Way", -# "size_rank": 1 -# }, -# { -# "id": 2, -# "name": "Saturn", -# "description": "A planet in the Milky Way", -# "size_rank": 2 -# } -# ] +def test_get_single_planet_returns_one_saved_planet(client, one_planet): + # act + response = client.get("/planets/1") + + # assert + assert response.status_code == 200 + assert response.get_json() == { + "id": 1, + "name": "Jupiter", + "description": "A planet in the Milky Way", + "size_rank": 1 + } + +def test_get_single_planet_with_empty_db_returns_404(client): + # act + response = client.get("/planets/1") + response_body = response.get_json() + + # assert + assert response.status_code == 404 + assert response.get_json() == None + + +def test_get_planets_with_valid_data_returns_correct_array(client, multiple_planets): + # act + response = client.get("/planets") + + # assert + assert response.status_code == 200 + assert response.get_json() == [ + { + "id": 1, + "name": "Jupiter", + "description": "A planet in the Milky Way", + "size_rank": 1 + }, + { + "id": 2, + "name": "Saturn", + "description": "A planet in the Milky Way", + "size_rank": 2 + } + ] # def test_post_planets_with_JSON_request_body_returns_201(client): -# # act -# response = client.get("/planets/1") +# # Act +# response = client.post("/planets", json={ +# # "id": 1, +# "name": "Jupiter", +# "description": "A planet in the Milky Way", +# "size_rank": 1 +# }) +# response_body = response.get_json() -# # assert +# # Assert # assert response.status_code == 201 -# assert response.get_json() == [] \ No newline at end of file +# assert response_body == { +# "id": 1, +# "name": "Jupiter", +# "description": "A planet in the Milky Way", +# "size_rank": 1 +# } \ No newline at end of file From 684cfa4ecbba8ac6278cab7edcc9e8f7c490edf9 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Thu, 28 Oct 2021 14:11:09 -0700 Subject: [PATCH 22/25] Add test for creating new planet with POST to /planets --- app/routes.py | 3 ++- tests/test_routes.py | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/routes.py b/app/routes.py index c6098c1b7..0f057786b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -28,7 +28,8 @@ def add_planet(): ) db.session.add(new_planet) db.session.commit() - return make_response(f"Planet {new_planet.name} successfully created", 201) + # return make_response(f"Planet {new_planet.name} successfully created", 201) + return make_response({"planet": new_planet.to_dict()}, 201) @planets_bp.route("", methods=["GET"], strict_slashes=False) def get_all_planets(): diff --git a/tests/test_routes.py b/tests/test_routes.py index d6d74c866..1d4544c78 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -1,3 +1,5 @@ +from app.models.planet import Planet + def test_get_planets_returns_200_empty_array_when_db_is_empty(client): # act response = client.get("/planets") @@ -50,21 +52,28 @@ def test_get_planets_with_valid_data_returns_correct_array(client, multiple_plan } ] -# def test_post_planets_with_JSON_request_body_returns_201(client): -# # Act -# response = client.post("/planets", json={ -# # "id": 1, -# "name": "Jupiter", -# "description": "A planet in the Milky Way", -# "size_rank": 1 -# }) -# response_body = response.get_json() +def test_post_planet_with_JSON_request_body_returns_201(client): + # Act + response = client.post("/planets", json={ + "name": "Jupiter", + "description": "A planet in the Milky Way", + "size_rank": 1 + }) + response_body = response.get_json() -# # Assert -# assert response.status_code == 201 -# assert response_body == { -# "id": 1, -# "name": "Jupiter", -# "description": "A planet in the Milky Way", -# "size_rank": 1 -# } \ No newline at end of file + # Assert + assert response.status_code == 201 + assert response_body == { + "planet": { + "id": 1, + "name": "Jupiter", + "description": "A planet in the Milky Way", + "size_rank": 1 + } + } + new_planet = Planet.query.get(1) + assert new_planet + assert new_planet.id == 1 + assert new_planet.name == "Jupiter" + assert new_planet.description == "A planet in the Milky Way" + assert new_planet.size_rank == 1 \ No newline at end of file From 8515ca28571a7b9d98327a199b5332a92e012837 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Thu, 28 Oct 2021 14:12:28 -0700 Subject: [PATCH 23/25] Refactor create planet POST /planets test --- tests/test_routes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index 1d4544c78..9ae5f3fec 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -63,6 +63,7 @@ def test_post_planet_with_JSON_request_body_returns_201(client): # Assert assert response.status_code == 201 + assert "planet" in response_body assert response_body == { "planet": { "id": 1, From 870caca2072c2de8b3dc00cc2753e916cd63a2c6 Mon Sep 17 00:00:00 2001 From: Laurel S Date: Thu, 28 Oct 2021 14:20:34 -0700 Subject: [PATCH 24/25] Refactor make_response returns for consistency. Update tests accordingly. --- app/routes.py | 7 ++----- tests/test_routes.py | 10 ++++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/routes.py b/app/routes.py index 0f057786b..c661b80d1 100644 --- a/app/routes.py +++ b/app/routes.py @@ -18,7 +18,6 @@ def get_planet_from_id(planet_id): # Routes @planets_bp.route("", methods=["POST"], strict_slashes=False) -# def handle_planets(): def add_planet(): request_body = request.get_json() new_planet = Planet( @@ -28,7 +27,6 @@ def add_planet(): ) db.session.add(new_planet) db.session.commit() - # return make_response(f"Planet {new_planet.name} successfully created", 201) return make_response({"planet": new_planet.to_dict()}, 201) @planets_bp.route("", methods=["GET"], strict_slashes=False) @@ -39,12 +37,11 @@ def get_all_planets(): response_list.append(planet.to_dict()) return make_response(jsonify(response_list), 200) -# @planets_bp.route("/", methods=["GET", "PATCH", "DELETE"],) - +# Routes for single planet @planets_bp.route("/", methods=["GET"]) def get_planet(planet_id): selected_planet = get_planet_from_id(planet_id) - return selected_planet.to_dict() + return make_response({"planet": selected_planet.to_dict()}, 200) @planets_bp.route("/", methods=["PATCH"]) def update_planet(planet_id): diff --git a/tests/test_routes.py b/tests/test_routes.py index 9ae5f3fec..e4722a019 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -15,11 +15,13 @@ def test_get_single_planet_returns_one_saved_planet(client, one_planet): # assert assert response.status_code == 200 assert response.get_json() == { - "id": 1, - "name": "Jupiter", - "description": "A planet in the Milky Way", - "size_rank": 1 + "planet": { + "id": 1, + "name": "Jupiter", + "description": "A planet in the Milky Way", + "size_rank": 1 } + } def test_get_single_planet_with_empty_db_returns_404(client): # act From d4ce56f9409172db2080db31eca46d66c34b05dd Mon Sep 17 00:00:00 2001 From: Maria Obregon Garcia Date: Wed, 3 Nov 2021 14:25:11 -0400 Subject: [PATCH 25/25] Adds Procfile --- Procfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Procfile 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