From ea3df1c52e6b0db69568cae1fb0fab051e49ed36 Mon Sep 17 00:00:00 2001 From: jae Date: Fri, 22 Apr 2022 13:11:06 -0500 Subject: [PATCH 01/22] planets info --- app/__init__.py | 4 ++++ app/routes.py | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..f94fb6fee 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,4 +4,8 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import bp + app.register_blueprint(bp) + + return app diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..812e8ec59 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,40 @@ -from flask import Blueprint +from flask import Blueprint, jsonify + +class Planet: + def __init__(self, id, name, description, no_life=True): + self.id = id + self.name = name + self.description = description + self.no_life = no_life + + +planets = [ + Planet(1, "Methuselah", "oldest exoplanet"), + Planet(2, "Epsilon Eridani b", "closest exoplanet, has 2 astroid belts!!"), + Planet(3, "Aquarii A", "the most suns!"), + Planet(4, "Gliese 876 b", "gas giant, big, icy moons, water might be found", False) + + +] + +bp = Blueprint("planets", __name__, url_prefix="/planets") + +@bp.route("", methods=["GET"],) + # ^ this , makes it a tuple! + # ^ can leave blank cuz we already filled the method +def index_planets(): +# we have our list of cat instances, but not a dictionary of their info! need to make a dictionary of cats + all_planets = [] + for planet in planets: + all_planets.append(dict( + id = planet.id, + name = planet.name, + description = planet.description, + no_life = planet.no_life, +)) + + return jsonify(all_planets) + + + From db32efa0cdec1b62dd1d2edb773d9abee52977d0 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Mon, 25 Apr 2022 14:04:42 -0500 Subject: [PATCH 02/22] wave 2 completed --- app/__init__.py | 2 +- app/planet_routes.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/planet_routes.py diff --git a/app/__init__.py b/app/__init__.py index f94fb6fee..3d0c7e3c3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,7 +4,7 @@ def create_app(test_config=None): app = Flask(__name__) - from .routes import bp + from .planet_routes import bp app.register_blueprint(bp) diff --git a/app/planet_routes.py b/app/planet_routes.py new file mode 100644 index 000000000..b25c84dd5 --- /dev/null +++ b/app/planet_routes.py @@ -0,0 +1,55 @@ +from flask import Blueprint, jsonify, abort, make_response + +class Planet: + def __init__(self, id, name, description, no_life=True): + self.id = id + self.name = name + self.description = description + self.no_life = no_life + + def to_dict(self): + return dict( + id=self.id, + name=self.name, + description=self.description, + no_life=self.no_life) + + +planets = [ + Planet(1, "Methuselah", "oldest exoplanet"), + Planet(2, "Epsilon Eridani b", "closest exoplanet, has 2 astroid belts!!"), + Planet(3, "Aquarii A", "the most suns!"), + Planet(4, "Gliese 876 b", "gas giant, big, icy moons, water might be found", False) +] + +bp = Blueprint("planets", __name__, url_prefix="/planets") + +@bp.route("", methods=["GET"]) + # ^ this , makes it a tuple! + # ^ can leave blank cuz we already filled the method +def index_planets(): +# we have our list of cat instances, but not a dictionary of their info! need to make a dictionary of cats + result_list = [planet.to_dict() for planet in planets] + + return jsonify(result_list) + +def validate_planet(id): + try: + id = int(id) + except ValueError: + abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) + + for planet in planets: + if planet.id == id: + return planet + + abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) + + +@bp.route("/", methods=["GET"]) +def get_planet(id): + planet = validate_planet(id) + return jsonify(planet.to_dict()) + + + From 7c5a72b880ca8d987380941f3e2ee1cc3d581824 Mon Sep 17 00:00:00 2001 From: jae Date: Tue, 26 Apr 2022 15:17:44 -0500 Subject: [PATCH 03/22] delete some unnecessary comments --- app/planet_routes.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/planet_routes.py b/app/planet_routes.py index b25c84dd5..d5a7dd229 100644 --- a/app/planet_routes.py +++ b/app/planet_routes.py @@ -25,10 +25,8 @@ def to_dict(self): bp = Blueprint("planets", __name__, url_prefix="/planets") @bp.route("", methods=["GET"]) - # ^ this , makes it a tuple! - # ^ can leave blank cuz we already filled the method + def index_planets(): -# we have our list of cat instances, but not a dictionary of their info! need to make a dictionary of cats result_list = [planet.to_dict() for planet in planets] return jsonify(result_list) From 367ffbf2e9ced9467362ea5d618c5be891c13d7b Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Tue, 26 Apr 2022 15:40:23 -0500 Subject: [PATCH 04/22] routes deleted --- app/routes.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 app/routes.py diff --git a/app/routes.py b/app/routes.py deleted file mode 100644 index 812e8ec59..000000000 --- a/app/routes.py +++ /dev/null @@ -1,40 +0,0 @@ -from flask import Blueprint, jsonify - -class Planet: - def __init__(self, id, name, description, no_life=True): - self.id = id - self.name = name - self.description = description - self.no_life = no_life - - -planets = [ - Planet(1, "Methuselah", "oldest exoplanet"), - Planet(2, "Epsilon Eridani b", "closest exoplanet, has 2 astroid belts!!"), - Planet(3, "Aquarii A", "the most suns!"), - Planet(4, "Gliese 876 b", "gas giant, big, icy moons, water might be found", False) - - -] - -bp = Blueprint("planets", __name__, url_prefix="/planets") - -@bp.route("", methods=["GET"],) - # ^ this , makes it a tuple! - # ^ can leave blank cuz we already filled the method -def index_planets(): -# we have our list of cat instances, but not a dictionary of their info! need to make a dictionary of cats - all_planets = [] - for planet in planets: - all_planets.append(dict( - id = planet.id, - name = planet.name, - description = planet.description, - no_life = planet.no_life, -)) - - return jsonify(all_planets) - - - - From 61b6335934721286e77370fd4ddfe91e2837bf30 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Tue, 26 Apr 2022 15:49:36 -0500 Subject: [PATCH 05/22] fixed? --- app/planet_routes.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/planet_routes.py b/app/planet_routes.py index b25c84dd5..bb089acd7 100644 --- a/app/planet_routes.py +++ b/app/planet_routes.py @@ -25,10 +25,7 @@ def to_dict(self): bp = Blueprint("planets", __name__, url_prefix="/planets") @bp.route("", methods=["GET"]) - # ^ this , makes it a tuple! - # ^ can leave blank cuz we already filled the method def index_planets(): -# we have our list of cat instances, but not a dictionary of their info! need to make a dictionary of cats result_list = [planet.to_dict() for planet in planets] return jsonify(result_list) From 1accee4f46bf60204d5f1278a1822e54421cef5d Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Fri, 29 Apr 2022 15:09:20 -0500 Subject: [PATCH 06/22] create planet route --- app/__init__.py | 17 +++- app/models/__init__.py | 0 app/models/planet.py | 17 ++++ app/planet_routes.py | 52 ----------- app/routes/__init__.py | 0 app/routes/planet_routes.py | 68 ++++++++++++++ migrations/README | 1 + migrations/alembic.ini | 50 ++++++++++ migrations/env.py | 91 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../be16659b15a9_adds_planet_model.py | 35 +++++++ 11 files changed, 300 insertions(+), 55 deletions(-) create mode 100644 app/models/__init__.py create mode 100644 app/models/planet.py delete mode 100644 app/planet_routes.py create mode 100644 app/routes/__init__.py create mode 100644 app/routes/planet_routes.py 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/be16659b15a9_adds_planet_model.py diff --git a/app/__init__.py b/app/__init__.py index 3d0c7e3c3..ef9218af0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,11 +1,22 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +db = SQLAlchemy() +migrate = Migrate() +# connection_string = 'postgresql+psycopg2://postgres:postgres@localhost:5432/planets_development' def create_app(test_config=None): app = Flask(__name__) - from .planet_routes import bp - app.register_blueprint(bp) + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/planets_development' + + db.init_app(app) + migrate.init_app(app, db) + from app.routes import planet_routes + app.register_blueprint(planet_routes.planets_bp) - return app + + return app \ No newline at end of file diff --git a/app/models/__init__.py b/app/models/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/models/planet.py b/app/models/planet.py new file mode 100644 index 000000000..04a0ce708 --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1,17 @@ +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) + life = db.Column(db.String) + moons = db.Column(db.String) + + def to_dict(self): + return dict( + id=self.id, + name=self.name, + description=self.description, + life=self.life, + moons=self.moons, + ) \ No newline at end of file diff --git a/app/planet_routes.py b/app/planet_routes.py deleted file mode 100644 index bb089acd7..000000000 --- a/app/planet_routes.py +++ /dev/null @@ -1,52 +0,0 @@ -from flask import Blueprint, jsonify, abort, make_response - -class Planet: - def __init__(self, id, name, description, no_life=True): - self.id = id - self.name = name - self.description = description - self.no_life = no_life - - def to_dict(self): - return dict( - id=self.id, - name=self.name, - description=self.description, - no_life=self.no_life) - - -planets = [ - Planet(1, "Methuselah", "oldest exoplanet"), - Planet(2, "Epsilon Eridani b", "closest exoplanet, has 2 astroid belts!!"), - Planet(3, "Aquarii A", "the most suns!"), - Planet(4, "Gliese 876 b", "gas giant, big, icy moons, water might be found", False) -] - -bp = Blueprint("planets", __name__, url_prefix="/planets") - -@bp.route("", methods=["GET"]) -def index_planets(): - result_list = [planet.to_dict() for planet in planets] - - return jsonify(result_list) - -def validate_planet(id): - try: - id = int(id) - except ValueError: - abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) - - for planet in planets: - if planet.id == id: - return planet - - abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) - - -@bp.route("/", methods=["GET"]) -def get_planet(id): - planet = validate_planet(id) - return jsonify(planet.to_dict()) - - - diff --git a/app/routes/__init__.py b/app/routes/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py new file mode 100644 index 000000000..98c6a5c3e --- /dev/null +++ b/app/routes/planet_routes.py @@ -0,0 +1,68 @@ +from app import db +from app.models.planet import Planet +from flask import Blueprint, jsonify, make_response, request + +# class Planet: +# def __init__(self, id, name, description, no_life=True): +# self.id = id +# self.name = name +# self.description = description +# self.no_life = no_life + +# def to_dict(self): +# return dict( +# id=self.id, +# name=self.name, +# description=self.description, +# no_life=self.no_life) + + +# planets = [ +# Planet(1, "Methuselah", "oldest exoplanet"), +# Planet(2, "Epsilon Eridani b", "closest exoplanet, has 2 astroid belts!!"), +# Planet(3, "Aquarii A", "the most suns!"), +# Planet(4, "Gliese 876 b", "gas giant, big, icy moons, water might be found", False) +# ] + +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + +@planets_bp.route("", methods=["POST"]) +def handle_planets(): + request_body = request.get_json() + new_planet = Planet( + name=request_body["name"], + description=request_body["description"], + life = request_body["life"], + moons = request_body["moons"]) + + db.session.add(new_planet) + db.session.commit() + + return jsonify(new_planet.to_dict()), 201 + +@planets_bp.route("", methods=["GET"]) +def index_planets(): + planets = Planet.query.all() + result_list = [planet.to_dict() for planet in planets] + return jsonify(result_list) + +# def validate_planet(id): +# try: +# id = int(id) +# except ValueError: +# abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) + +# for planet in planets: +# if planet.id == id: +# return planet + +# abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) + + +# @bp.route("/", methods=["GET"]) +# def get_planet(id): +# planet = validate_planet(id) +# return jsonify(planet.to_dict()) + + + diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..0e0484415 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Single-database configuration for Flask. diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..ec9d45c26 --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,50 @@ +# 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,flask_migrate + +[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 + +[logger_flask_migrate] +level = INFO +handlers = +qualname = flask_migrate + +[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..68feded2a --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,91 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +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.get_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 = current_app.extensions['migrate'].db.get_engine() + + 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/be16659b15a9_adds_planet_model.py b/migrations/versions/be16659b15a9_adds_planet_model.py new file mode 100644 index 000000000..7a9a0d584 --- /dev/null +++ b/migrations/versions/be16659b15a9_adds_planet_model.py @@ -0,0 +1,35 @@ +"""adds Planet model + +Revision ID: be16659b15a9 +Revises: +Create Date: 2022-04-29 14:25:08.410086 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'be16659b15a9' +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('life', sa.String(), nullable=True), + sa.Column('moons', sa.String(), 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 465bf2a25262f56ef95c7f82ac2df3b103b4a976 Mon Sep 17 00:00:00 2001 From: jae Date: Tue, 3 May 2022 13:12:49 -0500 Subject: [PATCH 07/22] add get planet by id --- app/routes/planet_routes.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index 98c6a5c3e..45d55cccb 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -1,6 +1,7 @@ +from xml.dom.minidom import Identified from app import db from app.models.planet import Planet -from flask import Blueprint, jsonify, make_response, request +from flask import Blueprint, jsonify, make_response, request, abort # class Planet: # def __init__(self, id, name, description, no_life=True): @@ -46,17 +47,25 @@ def index_planets(): result_list = [planet.to_dict() for planet in planets] return jsonify(result_list) -# def validate_planet(id): -# try: -# id = int(id) -# except ValueError: -# abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) - -# for planet in planets: -# if planet.id == id: -# return planet +@planets_bp.route("/", methods=["GET"]) +def get_planet(id): + planet = validate_planet(id) + return jsonify(planet.to_dict()) + + + +def validate_planet(id): + try: + id = int(id) + except ValueError: + abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) -# abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) + + planet = Planet.query.get(id) + + if not planet: + abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) + return planet # @bp.route("/", methods=["GET"]) From 2ceb8604ea837285f84f9bd0a09cd096735c4e1b Mon Sep 17 00:00:00 2001 From: jae Date: Tue, 3 May 2022 13:24:27 -0500 Subject: [PATCH 08/22] add delete & put --- app/routes/planet_routes.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index 45d55cccb..a39333f02 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -53,7 +53,6 @@ def get_planet(id): return jsonify(planet.to_dict()) - def validate_planet(id): try: id = int(id) @@ -67,6 +66,29 @@ def validate_planet(id): abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) return planet +@planets_bp.route("/", methods=["PUT"]) +def update_planet(id): + planet = validate_planet(id) + request_body = request.get_json() + planet.name = request_body['name'] + planet.description = request_body['description'] + planet.life = request_body['life'] + planet.moons = request_body['moons'] + db.session.commit() + return jsonify(planet.to_dict()) + +@planets_bp.route("/", methods=["DELETE"]) +def delete_planet(id): + planet = validate_planet(id) + db.session.delete(planet) + db.session.commit() + return make_response(f'*** You have successfully destroyed {planet.name} ! *** ') + + + + + + # @bp.route("/", methods=["GET"]) # def get_planet(id): From 4916a2407e73c2d14699c0c60d54108c836fa3db Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Wed, 4 May 2022 13:21:58 -0500 Subject: [PATCH 09/22] somewhat refactored --- app/models/planet.py | 18 ++++++++++++++- app/routes/planet_routes.py | 44 +++++++++++++++++++++++++++---------- app/routes/routes_helper.py | 4 ++++ 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 app/routes/routes_helper.py diff --git a/app/models/planet.py b/app/models/planet.py index 04a0ce708..065c0cddd 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -14,4 +14,20 @@ def to_dict(self): description=self.description, life=self.life, moons=self.moons, - ) \ No newline at end of file + ) + + @classmethod + def from_dict(cls, data_dict): + return cls( + name=data_dict["name"], + description=data_dict["description"], + life=data_dict["life"], + moons=data_dict["moons"] + ) + + def replace_details(self, data_dict): + self.name=data_dict["name"] + self.description=data_dict["description"] + self.life=data_dict["life"] + self.moons=data_dict["moons"] + diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index a39333f02..56d8abc8b 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -2,6 +2,7 @@ from app import db from app.models.planet import Planet from flask import Blueprint, jsonify, make_response, request, abort +from app.routes.routes_helper import error_message # class Planet: # def __init__(self, id, name, description, no_life=True): @@ -27,14 +28,27 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") +def make_planet_safely(data_dict): + try: + return Planet.from_dict(data_dict) + except KeyError as err: + error_message(f"Missing key: {err}", 400) + +def replace_planet_safely(planet, data_dict): + try: + planet.replace_details(data_dict) + except KeyError as err: + error_message(f"Missing key: {err}", 400) + @planets_bp.route("", methods=["POST"]) def handle_planets(): request_body = request.get_json() - new_planet = Planet( - name=request_body["name"], - description=request_body["description"], - life = request_body["life"], - moons = request_body["moons"]) + # new_planet = Planet( + # name=request_body["name"], + # description=request_body["description"], + # life = request_body["life"], + # moons = request_body["moons"]) + new_planet = make_planet_safely(request_body) db.session.add(new_planet) db.session.commit() @@ -43,7 +57,14 @@ def handle_planets(): @planets_bp.route("", methods=["GET"]) def index_planets(): - planets = Planet.query.all() + # planets = Planet.query.all() + name_param = request.args.get("name") + + if name_param: + planets = Planet.query.filter_by(name=name_param) + else: + planets = Planet.query.all() + result_list = [planet.to_dict() for planet in planets] return jsonify(result_list) @@ -57,14 +78,15 @@ def validate_planet(id): try: id = int(id) except ValueError: - abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) - + # abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) + error_message(f"Invalid id {id}", 400) planet = Planet.query.get(id) - if not planet: - abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) - return planet + if planet: + # abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) + return planet + error_message(f"No planet with id {id} found", 404) @planets_bp.route("/", methods=["PUT"]) def update_planet(id): diff --git a/app/routes/routes_helper.py b/app/routes/routes_helper.py new file mode 100644 index 000000000..4482f0442 --- /dev/null +++ b/app/routes/routes_helper.py @@ -0,0 +1,4 @@ +from flask import jsonify, abort, make_response + +def error_message(message, status_code): + abort(make_response(jsonify(dict(details=message)), status_code)) \ No newline at end of file From d5cfa9a9828192226fbf85e0ef8a91fe7c1d4ba3 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Thu, 5 May 2022 12:48:41 -0500 Subject: [PATCH 10/22] tests --- app/__init__.py | 8 +++++++- app/tests/__init__.py | 0 app/tests/conftests.py | 33 +++++++++++++++++++++++++++++++++ app/tests/test_routes.py | 20 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 app/tests/__init__.py create mode 100644 app/tests/conftests.py create mode 100644 app/tests/test_routes.py diff --git a/app/__init__.py b/app/__init__.py index ef9218af0..69161f693 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,6 +1,8 @@ +from dotenv import load_dotenv from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate +import os db = SQLAlchemy() migrate = Migrate() @@ -10,7 +12,11 @@ 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/planets_development' + if test_config is None: + 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_DATABASE_TEST_URI") db.init_app(app) migrate.init_app(app, db) diff --git a/app/tests/__init__.py b/app/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/tests/conftests.py b/app/tests/conftests.py new file mode 100644 index 000000000..ea059dc8f --- /dev/null +++ b/app/tests/conftests.py @@ -0,0 +1,33 @@ +import pytest +from flask.signals import request_finished +from app import db +from app import create_app +from app.models.planet import Planet + +@pytest.fixture +def app(): + app = create_app({"TESTING": True}) + + @request_finished.connect_via(app) + def expire_session(sender, response, **extra): + db.session.remove() + + with app.app_context(): + db.create_all() + yield app + + with app.app_context(): + db.drop_all() + +@pytest.fixture +def client(app): + return app.test_client() + +@pytest.fixture +def two_planets(app): + Earth = Planet(id=1, name="Earth", description="home", life=True, moons="luna") + Mars = Planet(id=2, name="Mars", description="1st Colony", life=True, moons=None) + + db.session.add(Earth) + db.session.add(Mars) + db.session.commit() \ No newline at end of file diff --git a/app/tests/test_routes.py b/app/tests/test_routes.py new file mode 100644 index 000000000..dfb3d7923 --- /dev/null +++ b/app/tests/test_routes.py @@ -0,0 +1,20 @@ +def test_index_planets_with_empty_db_return_empty_list(client): + response = client.get('/planets') + + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == [] + +def test_get_planet_by_id(client, two_planets): + response = client.get("planets/1") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == { + "id":1, + "name": "Earth", + "description": "home", + "moons": "Luna", + "life":True + } \ No newline at end of file From f334f7943c9181837ad509464cfa30f5dd991530 Mon Sep 17 00:00:00 2001 From: jae Date: Thu, 5 May 2022 13:30:38 -0500 Subject: [PATCH 11/22] update to init based on learn --- app/__init__.py | 35 +++++++-- app/models/galaxy.py | 33 +++++++++ app/routes/galaxy_routes.py | 143 ++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 app/models/galaxy.py create mode 100644 app/routes/galaxy_routes.py diff --git a/app/__init__.py b/app/__init__.py index 69161f693..df21d6cd0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -6,23 +6,46 @@ db = SQLAlchemy() migrate = Migrate() +load_dotenv() # connection_string = 'postgresql+psycopg2://postgres:postgres@localhost:5432/planets_development' def create_app(test_config=None): app = Flask(__name__) - app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - if test_config is None: - app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_DATABASE_URI") + if not test_config: + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + 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_DATABASE_TEST_URI") - + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get( + "SQLALCHEMY_TEST_DATABASE_URI") + db.init_app(app) migrate.init_app(app, db) + from app.models.planet import Planet + from app.routes import planet_routes app.register_blueprint(planet_routes.planets_bp) + + + return app + + # app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + # if test_config is None: + # 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_DATABASE_TEST_URI") + + # db.init_app(app) + # migrate.init_app(app, db) + + # from app.routes import planet_routes + # app.register_blueprint(planet_routes.planets_bp) + - return app \ No newline at end of file + # return app \ No newline at end of file diff --git a/app/models/galaxy.py b/app/models/galaxy.py new file mode 100644 index 000000000..46882ef8f --- /dev/null +++ b/app/models/galaxy.py @@ -0,0 +1,33 @@ +from app import db + +class Galaxy(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String) + description = db.Column(db.String) + location = db.Column(db.String) + members = db.Column(db.String) + + def to_dict(self): + return dict( + id=self.id, + name=self.name, + description=self.description, + location=self.life, + members=self.moons, + ) + + @classmethod + def from_dict(cls, data_dict): + return cls( + name=data_dict["name"], + description=data_dict["description"], + location=data_dict["location"], + members=data_dict["members"] + ) + + def replace_details(self, data_dict): + self.name=data_dict["name"] + self.description=data_dict["description"] + self.life=data_dict["location"] + self.moons=data_dict["members"] + \ No newline at end of file diff --git a/app/routes/galaxy_routes.py b/app/routes/galaxy_routes.py new file mode 100644 index 000000000..dc67d39e1 --- /dev/null +++ b/app/routes/galaxy_routes.py @@ -0,0 +1,143 @@ +from xml.dom.minidom import Identified +from app import db +from app.models.galaxy import Galaxy +from flask import Blueprint, jsonify, make_response, request, abort +from app.routes.routes_helper import error_message + + +galaxy_bp = Blueprint("galaxys", __name__, url_prefix="/galaxys") + +def make_galaxy_safely(data_dict): + try: + return Galaxy.from_dict(data_dict) + except KeyError as err: + error_message(f"Missing key: {err}", 400) + +def replace_galaxy_safely(galaxy, data_dict): + try: + galaxy.replace_details(data_dict) + except KeyError as err: + error_message(f"Missing key: {err}", 400) + +@galaxy_bp.route("", methods=["POST"]) +def handle_galaxys(): + request_body = request.get_json() + # new_galaxy = Galaxy( + # name=request_body["name"], + # description=request_body["description"], + # life = request_body["life"], + # moons = request_body["moons"]) + new_galaxy = make_galaxy_safely(request_body) + + db.session.add(new_galaxy) + db.session.commit() + + return jsonify(new_galaxy.to_dict()), 201 + +@galaxy_bp.route("", methods=["GET"]) +def index_galaxys(): + # galaxys = Galaxy.query.all() + name_param = request.args.get("name") + + if name_param: + galaxys = Galaxy.query.filter_by(name=name_param) + else: + galaxys = Galaxy.query.all() + + result_list = [galaxy.to_dict() for galaxy in galaxys] + return jsonify(result_list) + +@galaxy_bp.route("/", methods=["GET"]) +def get_galaxy(id): + galaxy = validate_galaxy(id) + return jsonify(galaxy.to_dict()) + + +def validate_galaxy(id): + try: + id = int(id) + except ValueError: + # abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) + error_message(f"Invalid id {id}", 400) + + galaxy = Galaxy.query.get(id) + + if galaxy: + # abort(make_response(jsonify(dict(details=f"galaxy id {id} not found")), 404)) + return galaxy + error_message(f"No galaxy with id {id} found", 404) + +@galaxy_bp.route("/", methods=["PUT"]) +def update_galaxy(id): + galaxy = validate_galaxy(id) + request_body = request.get_json() + galaxy.name = request_body['name'] + galaxy.location = request_body['location'] + galaxy.members = request_body['members'] + + db.session.commit() + return jsonify(galaxy.to_dict()) + +@galaxy_bp.route("/", methods=["DELETE"]) +def delete_galaxy(id): + galaxy = validate_galaxy(id) + db.session.delete(galaxy) + db.session.commit() + return make_response(f'*** You have successfully destroyed {galaxy.name} ! *** ') + + + + + + + +class Galaxy: + def __init__(self, id, name, location, members=None): + self.id = id + self.name = name + self.location = (f'{location} light-years') + self.members = members if members is not None else [] + + + def to_dict(self): + return dict( + id=self.id, + name=self.name, + location = self.location, + members = self.members) + + +galaxies = [ + + Galaxy(2, "Epsilon Eridani", 10.5, galaxys, ), + +] + + +galaxy_bp = Blueprint("galaxies", __name__, url_prefix="/galaxies") + +@galaxy_bp.route("", methods=["GET"]) + +def index_galaxies(): + result_list = [galaxy.to_dict() for galaxy in galaxies] + + return jsonify(result_list) + +def validate_galaxy(id): + try: + id = int(id) + except ValueError: + abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) + + for galaxy in galaxies: + if galaxy.id == id: + return galaxy + + abort(make_response(jsonify(dict(details=f"galaxy id {id} not found")), 404)) + + +@galaxy_bp.route("/", methods=["GET"]) +def get_galaxy(id): + galaxy = validate_galaxy(id) + return jsonify(galaxy.to_dict()) + From 198262302271a50c98f4ccb2cfb9c07b95b9c870 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Thu, 5 May 2022 14:59:56 -0500 Subject: [PATCH 12/22] moved tests folder --- app/models/galaxy.py | 33 ----- app/routes/galaxy_routes.py | 143 -------------------- app/tests/test_routes.py | 20 --- {app/tests => tests}/__init__.py | 0 app/tests/conftests.py => tests/conftest.py | 4 +- tests/test_routes.py | 29 ++++ 6 files changed, 31 insertions(+), 198 deletions(-) delete mode 100644 app/models/galaxy.py delete mode 100644 app/routes/galaxy_routes.py delete mode 100644 app/tests/test_routes.py rename {app/tests => tests}/__init__.py (100%) rename app/tests/conftests.py => tests/conftest.py (94%) create mode 100644 tests/test_routes.py diff --git a/app/models/galaxy.py b/app/models/galaxy.py deleted file mode 100644 index 46882ef8f..000000000 --- a/app/models/galaxy.py +++ /dev/null @@ -1,33 +0,0 @@ -from app import db - -class Galaxy(db.Model): - id = db.Column(db.Integer, primary_key=True, autoincrement=True) - name = db.Column(db.String) - description = db.Column(db.String) - location = db.Column(db.String) - members = db.Column(db.String) - - def to_dict(self): - return dict( - id=self.id, - name=self.name, - description=self.description, - location=self.life, - members=self.moons, - ) - - @classmethod - def from_dict(cls, data_dict): - return cls( - name=data_dict["name"], - description=data_dict["description"], - location=data_dict["location"], - members=data_dict["members"] - ) - - def replace_details(self, data_dict): - self.name=data_dict["name"] - self.description=data_dict["description"] - self.life=data_dict["location"] - self.moons=data_dict["members"] - \ No newline at end of file diff --git a/app/routes/galaxy_routes.py b/app/routes/galaxy_routes.py deleted file mode 100644 index dc67d39e1..000000000 --- a/app/routes/galaxy_routes.py +++ /dev/null @@ -1,143 +0,0 @@ -from xml.dom.minidom import Identified -from app import db -from app.models.galaxy import Galaxy -from flask import Blueprint, jsonify, make_response, request, abort -from app.routes.routes_helper import error_message - - -galaxy_bp = Blueprint("galaxys", __name__, url_prefix="/galaxys") - -def make_galaxy_safely(data_dict): - try: - return Galaxy.from_dict(data_dict) - except KeyError as err: - error_message(f"Missing key: {err}", 400) - -def replace_galaxy_safely(galaxy, data_dict): - try: - galaxy.replace_details(data_dict) - except KeyError as err: - error_message(f"Missing key: {err}", 400) - -@galaxy_bp.route("", methods=["POST"]) -def handle_galaxys(): - request_body = request.get_json() - # new_galaxy = Galaxy( - # name=request_body["name"], - # description=request_body["description"], - # life = request_body["life"], - # moons = request_body["moons"]) - new_galaxy = make_galaxy_safely(request_body) - - db.session.add(new_galaxy) - db.session.commit() - - return jsonify(new_galaxy.to_dict()), 201 - -@galaxy_bp.route("", methods=["GET"]) -def index_galaxys(): - # galaxys = Galaxy.query.all() - name_param = request.args.get("name") - - if name_param: - galaxys = Galaxy.query.filter_by(name=name_param) - else: - galaxys = Galaxy.query.all() - - result_list = [galaxy.to_dict() for galaxy in galaxys] - return jsonify(result_list) - -@galaxy_bp.route("/", methods=["GET"]) -def get_galaxy(id): - galaxy = validate_galaxy(id) - return jsonify(galaxy.to_dict()) - - -def validate_galaxy(id): - try: - id = int(id) - except ValueError: - # abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) - error_message(f"Invalid id {id}", 400) - - galaxy = Galaxy.query.get(id) - - if galaxy: - # abort(make_response(jsonify(dict(details=f"galaxy id {id} not found")), 404)) - return galaxy - error_message(f"No galaxy with id {id} found", 404) - -@galaxy_bp.route("/", methods=["PUT"]) -def update_galaxy(id): - galaxy = validate_galaxy(id) - request_body = request.get_json() - galaxy.name = request_body['name'] - galaxy.location = request_body['location'] - galaxy.members = request_body['members'] - - db.session.commit() - return jsonify(galaxy.to_dict()) - -@galaxy_bp.route("/", methods=["DELETE"]) -def delete_galaxy(id): - galaxy = validate_galaxy(id) - db.session.delete(galaxy) - db.session.commit() - return make_response(f'*** You have successfully destroyed {galaxy.name} ! *** ') - - - - - - - -class Galaxy: - def __init__(self, id, name, location, members=None): - self.id = id - self.name = name - self.location = (f'{location} light-years') - self.members = members if members is not None else [] - - - def to_dict(self): - return dict( - id=self.id, - name=self.name, - location = self.location, - members = self.members) - - -galaxies = [ - - Galaxy(2, "Epsilon Eridani", 10.5, galaxys, ), - -] - - -galaxy_bp = Blueprint("galaxies", __name__, url_prefix="/galaxies") - -@galaxy_bp.route("", methods=["GET"]) - -def index_galaxies(): - result_list = [galaxy.to_dict() for galaxy in galaxies] - - return jsonify(result_list) - -def validate_galaxy(id): - try: - id = int(id) - except ValueError: - abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) - - for galaxy in galaxies: - if galaxy.id == id: - return galaxy - - abort(make_response(jsonify(dict(details=f"galaxy id {id} not found")), 404)) - - -@galaxy_bp.route("/", methods=["GET"]) -def get_galaxy(id): - galaxy = validate_galaxy(id) - return jsonify(galaxy.to_dict()) - diff --git a/app/tests/test_routes.py b/app/tests/test_routes.py deleted file mode 100644 index dfb3d7923..000000000 --- a/app/tests/test_routes.py +++ /dev/null @@ -1,20 +0,0 @@ -def test_index_planets_with_empty_db_return_empty_list(client): - response = client.get('/planets') - - response_body = response.get_json() - - assert response.status_code == 200 - assert response_body == [] - -def test_get_planet_by_id(client, two_planets): - response = client.get("planets/1") - response_body = response.get_json() - - assert response.status_code == 200 - assert response_body == { - "id":1, - "name": "Earth", - "description": "home", - "moons": "Luna", - "life":True - } \ No newline at end of file diff --git a/app/tests/__init__.py b/tests/__init__.py similarity index 100% rename from app/tests/__init__.py rename to tests/__init__.py diff --git a/app/tests/conftests.py b/tests/conftest.py similarity index 94% rename from app/tests/conftests.py rename to tests/conftest.py index ea059dc8f..23fa8bf67 100644 --- a/app/tests/conftests.py +++ b/tests/conftest.py @@ -25,8 +25,8 @@ def client(app): @pytest.fixture def two_planets(app): - Earth = Planet(id=1, name="Earth", description="home", life=True, moons="luna") - Mars = Planet(id=2, name="Mars", description="1st Colony", life=True, moons=None) + Earth = Planet(id=1, name="Earth", description="home", life="True", moons="luna") + Mars = Planet(id=2, name="Mars", description="1st Colony", life="True", moons="None") db.session.add(Earth) db.session.add(Mars) diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..c7590e6cd --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,29 @@ +def test_get_all_planets_with_no_records(client): + # Act + response = client.get("/planets") + response_body = response.get_json() + + # Assert + assert response.status_code == 200 + assert response_body == [] + +def test_index_planets_with_empty_db_return_empty_list(client): + response = client.get('/planets') + + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == [] + +def test_get_planet_by_id(client, two_planets): + response = client.get("planets/1") + response_body = response.get_json() + +# assert response.status_code == 200 +# assert response_body == { +# "id":1, +# "name": "Earth", +# "description": "home", +# "moons": "Luna", +# "life":True +# } \ No newline at end of file From a1956e4c3840285360b32129031dfef8febc12a6 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Thu, 5 May 2022 15:16:13 -0500 Subject: [PATCH 13/22] passing 2 tests --- tests/test_routes.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index c7590e6cd..a697081c1 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -19,11 +19,11 @@ def test_get_planet_by_id(client, two_planets): response = client.get("planets/1") response_body = response.get_json() -# assert response.status_code == 200 -# assert response_body == { -# "id":1, -# "name": "Earth", -# "description": "home", -# "moons": "Luna", -# "life":True -# } \ No newline at end of file + assert response.status_code == 200 + assert response_body == { + "id":1, + "name": "Earth", + "description": "home", + "moons": "Luna", + "life":True + } \ No newline at end of file From b4a09694e48a6681c2a9ef850f974fd16eb2f817 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Thu, 5 May 2022 15:28:24 -0500 Subject: [PATCH 14/22] made more tests --- tests/conftest.py | 2 +- tests/test_routes.py | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 23fa8bf67..c4a9d16a0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,7 +25,7 @@ def client(app): @pytest.fixture def two_planets(app): - Earth = Planet(id=1, name="Earth", description="home", life="True", moons="luna") + Earth = Planet(id=1, name="Earth", description="home", life="True", moons="Luna") Mars = Planet(id=2, name="Mars", description="1st Colony", life="True", moons="None") db.session.add(Earth) diff --git a/tests/test_routes.py b/tests/test_routes.py index a697081c1..233e3754f 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -7,13 +7,22 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] -def test_index_planets_with_empty_db_return_empty_list(client): +def test_get_all_planeets(client, two_planets): response = client.get('/planets') response_body = response.get_json() assert response.status_code == 200 - assert response_body == [] + assert response_body == [{"id":1, + "name": "Earth", + "description": "home", + "moons": "Luna", + "life":"True"}, + {"id":2, + "name": "Mars", + "description": "1st Colony", + "moons": "None", + "life":"True"}] def test_get_planet_by_id(client, two_planets): response = client.get("planets/1") @@ -25,5 +34,24 @@ def test_get_planet_by_id(client, two_planets): "name": "Earth", "description": "home", "moons": "Luna", - "life":True + "life":"True" + } + +def test_create_one_planet(client): + + response = client.post("/planets", json={ + "name": "Pluto", + "description": "A real planet!", + "moons": "None", + "life":"False" + }) + response_body = response.get_json() + + assert response.status_code == 201 + assert response_body == { + "id":1, + "name": "Pluto", + "description": "A real planet!", + "moons": "None", + "life":"False" } \ No newline at end of file From fa1cf44e96c3419ec3fd97319ba385b39942b8e7 Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Thu, 5 May 2022 15:43:12 -0500 Subject: [PATCH 15/22] at least one test for everything except patch --- app/routes/planet_routes.py | 50 ++----------------------------------- tests/test_routes.py | 10 +++++++- 2 files changed, 11 insertions(+), 49 deletions(-) diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index 56d8abc8b..b5999c775 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -1,31 +1,8 @@ -from xml.dom.minidom import Identified from app import db from app.models.planet import Planet -from flask import Blueprint, jsonify, make_response, request, abort +from flask import Blueprint, jsonify, make_response, request from app.routes.routes_helper import error_message -# class Planet: -# def __init__(self, id, name, description, no_life=True): -# self.id = id -# self.name = name -# self.description = description -# self.no_life = no_life - -# def to_dict(self): -# return dict( -# id=self.id, -# name=self.name, -# description=self.description, -# no_life=self.no_life) - - -# planets = [ -# Planet(1, "Methuselah", "oldest exoplanet"), -# Planet(2, "Epsilon Eridani b", "closest exoplanet, has 2 astroid belts!!"), -# Planet(3, "Aquarii A", "the most suns!"), -# Planet(4, "Gliese 876 b", "gas giant, big, icy moons, water might be found", False) -# ] - planets_bp = Blueprint("planets", __name__, url_prefix="/planets") def make_planet_safely(data_dict): @@ -43,11 +20,6 @@ def replace_planet_safely(planet, data_dict): @planets_bp.route("", methods=["POST"]) def handle_planets(): request_body = request.get_json() - # new_planet = Planet( - # name=request_body["name"], - # description=request_body["description"], - # life = request_body["life"], - # moons = request_body["moons"]) new_planet = make_planet_safely(request_body) db.session.add(new_planet) @@ -57,7 +29,6 @@ def handle_planets(): @planets_bp.route("", methods=["GET"]) def index_planets(): - # planets = Planet.query.all() name_param = request.args.get("name") if name_param: @@ -73,18 +44,15 @@ def get_planet(id): planet = validate_planet(id) return jsonify(planet.to_dict()) - def validate_planet(id): try: id = int(id) except ValueError: - # abort(make_response(jsonify(dict(details=f"invalid id: {id}")), 400)) error_message(f"Invalid id {id}", 400) planet = Planet.query.get(id) if planet: - # abort(make_response(jsonify(dict(details=f"planet id {id} not found")), 404)) return planet error_message(f"No planet with id {id} found", 404) @@ -104,18 +72,4 @@ def delete_planet(id): planet = validate_planet(id) db.session.delete(planet) db.session.commit() - return make_response(f'*** You have successfully destroyed {planet.name} ! *** ') - - - - - - - -# @bp.route("/", methods=["GET"]) -# def get_planet(id): -# planet = validate_planet(id) -# return jsonify(planet.to_dict()) - - - + return make_response('*** You have successfully destroyed Earth ! ***') \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index 233e3754f..f45487119 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -54,4 +54,12 @@ def test_create_one_planet(client): "description": "A real planet!", "moons": "None", "life":"False" - } \ No newline at end of file + } + +def test_delete_planet(client, two_planets): + + response = client.delete("/planets/1") + response_body = response.get_data(as_text=True) + + assert response.status_code == 200 + assert response_body == '*** You have successfully destroyed Earth ! ***' \ No newline at end of file From a33ba3dcaf7cfe20c495c8f09fdd3afa33eab994 Mon Sep 17 00:00:00 2001 From: jae Date: Thu, 5 May 2022 17:19:44 -0500 Subject: [PATCH 16/22] added patch, and 2 tests --- app/routes/planet_routes.py | 23 ++++++++++++++++++++++- tests/test_routes.py | 13 ++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index b5999c775..bfa810118 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -54,7 +54,7 @@ def validate_planet(id): if planet: return planet - error_message(f"No planet with id {id} found", 404) + error_message(f'No planet with id {id} found', 404) @planets_bp.route("/", methods=["PUT"]) def update_planet(id): @@ -67,6 +67,27 @@ def update_planet(id): db.session.commit() return jsonify(planet.to_dict()) +@planets_bp.route("/", methods=["PATCH"]) +def upgrade_planet_with_id(id): + planet = validate_planet(id) + request_body = request.get_json() + planet_keys = request_body.keys() + + if "name" in planet_keys: + planet.name = request_body["name"] + if "description" in planet_keys: + planet.description = request_body["description"] + if "life" in planet_keys: + planet.life = request_body["life"] + + + + db.session.commit() + return jsonify(planet.to_dict()) + + + + @planets_bp.route("/", methods=["DELETE"]) def delete_planet(id): planet = validate_planet(id) diff --git a/tests/test_routes.py b/tests/test_routes.py index f45487119..3110598ac 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -62,4 +62,15 @@ def test_delete_planet(client, two_planets): response_body = response.get_data(as_text=True) assert response.status_code == 200 - assert response_body == '*** You have successfully destroyed Earth ! ***' \ No newline at end of file + assert response_body == '*** You have successfully destroyed Earth ! ***' + + +def test_get_planet_id_not_found(client): + + response = client.delete("/planets/1") + response_body = response.get_json() + + assert response.status_code == 404 + assert response_body == {'details': 'No planet with id 1 found'} + + \ No newline at end of file From 8d254456698d05eb1ed67a13d41d08229c43dfd6 Mon Sep 17 00:00:00 2001 From: jae Date: Fri, 6 May 2022 09:01:01 -0500 Subject: [PATCH 17/22] added moons to patch --- app/routes/planet_routes.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index bfa810118..a50ecf881 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -69,21 +69,21 @@ def update_planet(id): @planets_bp.route("/", methods=["PATCH"]) def upgrade_planet_with_id(id): - planet = validate_planet(id) - request_body = request.get_json() - planet_keys = request_body.keys() - - if "name" in planet_keys: - planet.name = request_body["name"] - if "description" in planet_keys: - planet.description = request_body["description"] - if "life" in planet_keys: - planet.life = request_body["life"] - - - - db.session.commit() - return jsonify(planet.to_dict()) + planet = validate_planet(id) + request_body = request.get_json() + planet_keys = request_body.keys() + + if "name" in planet_keys: + planet.name = request_body["name"] + if "description" in planet_keys: + planet.description = request_body["description"] + if "life" in planet_keys: + planet.life = request_body["life"] + if "moon" in planet_keys: + planet.life = request_body["moon"] + + db.session.commit() + return jsonify(planet.to_dict()) From 2089ea9d715bc2f0111835b0bbeebc0238962c2d Mon Sep 17 00:00:00 2001 From: jae Date: Fri, 6 May 2022 09:39:52 -0500 Subject: [PATCH 18/22] added patch test? --- tests/test_routes.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 3110598ac..6b841dc36 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -67,10 +67,31 @@ def test_delete_planet(client, two_planets): def test_get_planet_id_not_found(client): - response = client.delete("/planets/1") + response = client.get("/planets/1") response_body = response.get_json() assert response.status_code == 404 assert response_body == {'details': 'No planet with id 1 found'} - + + + +def test_update_planet_info(client, two_planets): + + response = client.patch("/planets/1", json={ + + "description": "A big blue planet!", + + }) + + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == { + "id" : 1, + "name": "Earth", + "description": "A big blue planet!", + "moons": "Luna", + "life":"True" + } + \ No newline at end of file From 79815182bd32c8e338e91fd3caf2bc49dece0060 Mon Sep 17 00:00:00 2001 From: jae Date: Fri, 6 May 2022 12:11:36 -0500 Subject: [PATCH 19/22] update tests --- tests/conftest.py | 1 + tests/test_routes.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index c4a9d16a0..a944cdae6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,7 @@ from app import create_app from app.models.planet import Planet + @pytest.fixture def app(): app = create_app({"TESTING": True}) diff --git a/tests/test_routes.py b/tests/test_routes.py index 6b841dc36..e5a96ce58 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -7,7 +7,7 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] -def test_get_all_planeets(client, two_planets): +def test_get_all_planets(client, two_planets): response = client.get('/planets') response_body = response.get_json() From 47e9242fbb807d09a95c128853f40d243cf76a2b Mon Sep 17 00:00:00 2001 From: Christina Webber Date: Fri, 6 May 2022 12:41:56 -0500 Subject: [PATCH 20/22] added tests for coverage --- app/models/planet.py | 2 +- app/routes/planet_routes.py | 9 ++---- tests/test_routes.py | 58 ++++++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index 065c0cddd..51cafa4c9 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -30,4 +30,4 @@ def replace_details(self, data_dict): self.description=data_dict["description"] self.life=data_dict["life"] self.moons=data_dict["moons"] - + return self.to_dict() diff --git a/app/routes/planet_routes.py b/app/routes/planet_routes.py index a50ecf881..c617a44b4 100644 --- a/app/routes/planet_routes.py +++ b/app/routes/planet_routes.py @@ -13,7 +13,7 @@ def make_planet_safely(data_dict): def replace_planet_safely(planet, data_dict): try: - planet.replace_details(data_dict) + return planet.replace_details(data_dict) except KeyError as err: error_message(f"Missing key: {err}", 400) @@ -60,12 +60,9 @@ def validate_planet(id): def update_planet(id): planet = validate_planet(id) request_body = request.get_json() - planet.name = request_body['name'] - planet.description = request_body['description'] - planet.life = request_body['life'] - planet.moons = request_body['moons'] + result = replace_planet_safely(planet, request_body) db.session.commit() - return jsonify(planet.to_dict()) + return jsonify(result) @planets_bp.route("/", methods=["PATCH"]) def upgrade_planet_with_id(id): diff --git a/tests/test_routes.py b/tests/test_routes.py index 6b841dc36..330ce854b 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -7,7 +7,7 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] -def test_get_all_planeets(client, two_planets): +def test_get_all_planets(client, two_planets): response = client.get('/planets') response_body = response.get_json() @@ -37,6 +37,13 @@ def test_get_planet_by_id(client, two_planets): "life":"True" } +def test_get_planet_by_invalid_id(client): + response = client.get("planets/a") + response_body = response.get_json() + + assert response.status_code == 400 + assert response_body == {"details":"Invalid id a"} + def test_create_one_planet(client): response = client.post("/planets", json={ @@ -56,6 +63,20 @@ def test_create_one_planet(client): "life":"False" } +def test_create_planet_with_a_missing_key(client): + + response = client.post("/planets", json={ + "description": "A big blue planet!", + "moons": "Luna", + "life":"True" + }) + + response_body = response.get_json() + + assert response.status_code == 400 + assert response_body == {"details":"Missing key: 'name'"} + + def test_delete_planet(client, two_planets): response = client.delete("/planets/1") @@ -78,9 +99,7 @@ def test_get_planet_id_not_found(client): def test_update_planet_info(client, two_planets): response = client.patch("/planets/1", json={ - "description": "A big blue planet!", - }) response_body = response.get_json() @@ -94,4 +113,35 @@ def test_update_planet_info(client, two_planets): "life":"True" } - \ No newline at end of file +def test_update_planet_with_a_put(client, two_planets): + + response = client.put("/planets/1", json={ + "name": "Earth", + "description": "A big blue planet!", + "moons": "Luna", + "life":"True" + }) + + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == { + "id" : 1, + "name": "Earth", + "description": "A big blue planet!", + "moons": "Luna", + "life":"True" + } + +def test_update_planet_with_a_put_missing_key(client, two_planets): + + response = client.put("/planets/1", json={ + "description": "A big blue planet!", + "moons": "Luna", + "life":"True" + }) + + response_body = response.get_json() + + assert response.status_code == 400 + assert response_body == {"details":"Missing key: 'name'"} \ No newline at end of file From e16bbb47a8b749e7a6849148eb7502eb903d0257 Mon Sep 17 00:00:00 2001 From: jae Date: Fri, 6 May 2022 14:38:55 -0500 Subject: [PATCH 21/22] update planet schema, add test for filter --- app/__init__.py | 18 +---- app/models/planet.py | 4 +- migrations/README | 2 +- migrations/alembic.ini | 7 +- migrations/env.py | 11 ++- .../be16659b15a9_adds_planet_model.py | 35 ---------- tests/conftest.py | 4 +- tests/test_routes.py | 69 +++++++++++-------- 8 files changed, 56 insertions(+), 94 deletions(-) delete mode 100644 migrations/versions/be16659b15a9_adds_planet_model.py diff --git a/app/__init__.py b/app/__init__.py index df21d6cd0..f65ec33b7 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -32,20 +32,4 @@ def create_app(test_config=None): - return app - - # app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - # if test_config is None: - # 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_DATABASE_TEST_URI") - - # db.init_app(app) - # migrate.init_app(app, db) - - # from app.routes import planet_routes - # app.register_blueprint(planet_routes.planets_bp) - - - # return app \ No newline at end of file + return app \ No newline at end of file diff --git a/app/models/planet.py b/app/models/planet.py index 51cafa4c9..a3dd5f347 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -4,8 +4,8 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) - life = db.Column(db.String) - moons = db.Column(db.String) + life = db.Column(db.Boolean) + moons = db.Column(db.Integer) def to_dict(self): return dict( diff --git a/migrations/README b/migrations/README index 0e0484415..98e4f9c44 100644 --- a/migrations/README +++ b/migrations/README @@ -1 +1 @@ -Single-database configuration for Flask. +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini index ec9d45c26..f8ed4801f 100644 --- a/migrations/alembic.ini +++ b/migrations/alembic.ini @@ -11,7 +11,7 @@ # Logging configuration [loggers] -keys = root,sqlalchemy,alembic,flask_migrate +keys = root,sqlalchemy,alembic [handlers] keys = console @@ -34,11 +34,6 @@ level = INFO handlers = qualname = alembic -[logger_flask_migrate] -level = INFO -handlers = -qualname = flask_migrate - [handler_console] class = StreamHandler args = (sys.stderr,) diff --git a/migrations/env.py b/migrations/env.py index 68feded2a..8b3fb3353 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -3,6 +3,8 @@ 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 @@ -22,8 +24,7 @@ # target_metadata = mymodel.Base.metadata config.set_main_option( 'sqlalchemy.url', - str(current_app.extensions['migrate'].db.get_engine().url).replace( - '%', '%%')) + 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, @@ -71,7 +72,11 @@ def process_revision_directives(context, revision, directives): directives[:] = [] logger.info('No changes in schema detected.') - connectable = current_app.extensions['migrate'].db.get_engine() + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) with connectable.connect() as connection: context.configure( diff --git a/migrations/versions/be16659b15a9_adds_planet_model.py b/migrations/versions/be16659b15a9_adds_planet_model.py deleted file mode 100644 index 7a9a0d584..000000000 --- a/migrations/versions/be16659b15a9_adds_planet_model.py +++ /dev/null @@ -1,35 +0,0 @@ -"""adds Planet model - -Revision ID: be16659b15a9 -Revises: -Create Date: 2022-04-29 14:25:08.410086 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'be16659b15a9' -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('life', sa.String(), nullable=True), - sa.Column('moons', sa.String(), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('planet') - # ### end Alembic commands ### diff --git a/tests/conftest.py b/tests/conftest.py index a944cdae6..712caa3fb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,8 +26,8 @@ def client(app): @pytest.fixture def two_planets(app): - Earth = Planet(id=1, name="Earth", description="home", life="True", moons="Luna") - Mars = Planet(id=2, name="Mars", description="1st Colony", life="True", moons="None") + Earth = Planet(id=1, name="Earth", description="home", life=True, moons=1) + Mars = Planet(id=2, name="Mars", description="1st Colony", life=True, moons=0) db.session.add(Earth) db.session.add(Mars) diff --git a/tests/test_routes.py b/tests/test_routes.py index 4d02fa8bb..c214329da 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -16,13 +16,13 @@ def test_get_all_planets(client, two_planets): assert response_body == [{"id":1, "name": "Earth", "description": "home", - "moons": "Luna", - "life":"True"}, + "moons": 1, + "life": True}, {"id":2, "name": "Mars", "description": "1st Colony", - "moons": "None", - "life":"True"}] + "moons": 0, + "life": True}] def test_get_planet_by_id(client, two_planets): response = client.get("planets/1") @@ -33,10 +33,23 @@ def test_get_planet_by_id(client, two_planets): "id":1, "name": "Earth", "description": "home", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True } +def test_get_planet_by_name(client, two_planets): + response = client.get("planets?name=Earth") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == [{ + "id":1, + "name": "Earth", + "description": "home", + "moons": 1, + "life": True + }] + def test_get_planet_by_invalid_id(client): response = client.get("planets/a") response_body = response.get_json() @@ -49,8 +62,8 @@ def test_create_one_planet(client): response = client.post("/planets", json={ "name": "Pluto", "description": "A real planet!", - "moons": "None", - "life":"False" + "moons": 0, + "life": False }) response_body = response.get_json() @@ -59,16 +72,16 @@ def test_create_one_planet(client): "id":1, "name": "Pluto", "description": "A real planet!", - "moons": "None", - "life":"False" + "moons": 0, + "life": False } def test_create_planet_with_a_missing_key(client): response = client.post("/planets", json={ "description": "A big blue planet!", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True }) response_body = response.get_json() @@ -109,8 +122,8 @@ def test_update_planet_description(client, two_planets): "id" : 1, "name": "Earth", "description": "A big blue planet!", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True } def test_update_planet_name(client, two_planets): @@ -126,14 +139,14 @@ def test_update_planet_name(client, two_planets): "id" : 1, "name": "Earth 2.0", "description": "home", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True } def test_update_planet_life(client, two_planets): response = client.patch("/planets/1", json={ - "life":"Not anymore", + "life": False, }) response_body = response.get_json() @@ -143,14 +156,14 @@ def test_update_planet_life(client, two_planets): "id" : 1, "name": "Earth", "description": "home", - "moons":"Luna", - "life":"Not anymore" + "moons": 1, + "life": False } def test_update_planet_moons(client, two_planets): response = client.patch("/planets/1", json={ - "moons":"Not anymore", + "moons":0, }) response_body = response.get_json() @@ -160,8 +173,8 @@ def test_update_planet_moons(client, two_planets): "id" : 1, "name": "Earth", "description": "home", - "moons":"Not anymore", - "life":"True" + "moons":0, + "life": True } def test_update_planet_with_a_put(client, two_planets): @@ -169,8 +182,8 @@ def test_update_planet_with_a_put(client, two_planets): response = client.put("/planets/1", json={ "name": "Earth", "description": "A big blue planet!", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True }) response_body = response.get_json() @@ -180,16 +193,16 @@ def test_update_planet_with_a_put(client, two_planets): "id" : 1, "name": "Earth", "description": "A big blue planet!", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True } def test_update_planet_with_a_put_missing_key(client, two_planets): response = client.put("/planets/1", json={ "description": "A big blue planet!", - "moons": "Luna", - "life":"True" + "moons": 1, + "life": True }) response_body = response.get_json() From ad8ab8a17d7ec612534575a9070d46dae0629510 Mon Sep 17 00:00:00 2001 From: jae Date: Fri, 6 May 2022 14:40:52 -0500 Subject: [PATCH 22/22] deleted a line --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index f65ec33b7..a2d99bcf3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -7,7 +7,7 @@ db = SQLAlchemy() migrate = Migrate() load_dotenv() -# connection_string = 'postgresql+psycopg2://postgres:postgres@localhost:5432/planets_development' + def create_app(test_config=None): app = Flask(__name__)