From b7ceb35f29703e3850f7f4d98210c09ae3b79b4c Mon Sep 17 00:00:00 2001 From: Liliana Date: Fri, 22 Apr 2022 14:02:49 -0400 Subject: [PATCH 01/10] Added Planets class and planets endpoint --- app/__init__.py | 3 ++- app/routes.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..776061489 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,5 +3,6 @@ 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..a6b5abbf9 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,31 @@ -from flask import Blueprint +from flask import Blueprint, jsonify +class Planet: + def __init__(self, id, name, description, life): + self.id = id + self.name = name + self.description = description + self.life = life + + +planets = [ + Planet(1, "Earth", "Best planet", True), + Planet(2, "Saturn", "Got a ring on it", False), + Planet(3, "Mars", "We want to go there", False) +] + +bp = Blueprint("planets_bp", __name__, url_prefix="/planets") + +@bp.route("", methods=["GET"]) +def get_planets(): + planets_list = [] + + for planet in planets: + planets_list.append({ + "id": planet.id, + "name": planet.name, + "description": planet.description, + "life": planet.life + }) + + return jsonify(planets_list) From bb3c58a398cba1a5e774ce42dfdaab94ea93902f Mon Sep 17 00:00:00 2001 From: Georgia Adam Date: Mon, 25 Apr 2022 15:04:17 -0400 Subject: [PATCH 02/10] creating get_one_planet endpoint, make a validate planet helper function, make a Planet instance method to_dict --- app/routes.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/app/routes.py b/app/routes.py index a6b5abbf9..a73cb98bf 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint, jsonify +from flask import Blueprint, jsonify, abort, make_response class Planet: def __init__(self, id, name, description, life): @@ -7,6 +7,14 @@ def __init__(self, id, name, description, life): self.description = description self.life = life + def to_dict(self): + return dict( + id=self.id, + name=self.name, + description=self.description, + life=self.life + ) + planets = [ Planet(1, "Earth", "Best planet", True), @@ -16,16 +24,30 @@ def __init__(self, id, name, description, life): bp = Blueprint("planets_bp", __name__, url_prefix="/planets") + +def validate_planet(id): + try: + id = int(id) + except: + abort(make_response({"message":f"planet {id} invalid"}, 400)) + + for planet in planets: + if planet.id == id: + return planet + + abort(make_response({"message":f"planet {id} not found"}, 404)) + + @bp.route("", methods=["GET"]) def get_planets(): planets_list = [] for planet in planets: - planets_list.append({ - "id": planet.id, - "name": planet.name, - "description": planet.description, - "life": planet.life - }) + planets_list.append(planet.to_dict()) return jsonify(planets_list) + +@bp.route("/", methods=["GET"]) +def get_one_planet(id): + planet = validate_planet(id) + return planet.to_dict() \ No newline at end of file From c0d57052f6ef5995f981f2e708a158097fb7fc09 Mon Sep 17 00:00:00 2001 From: Liliana Date: Fri, 29 Apr 2022 15:15:46 -0400 Subject: [PATCH 03/10] Refactored and made Planet model, created POST endpoint for Planets, refactored GET endpoint for Planets, and some other small refactoring --- app/__init__.py | 19 +++- app/models/__init__.py | 0 app/models/planet.py | 15 +++ app/routes.py | 53 ---------- app/routes/routes.py | 48 ++++++++++ migrations/README | 1 + migrations/alembic.ini | 45 +++++++++ migrations/env.py | 96 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../e29e7cf3fb9b_adds_planet_model.py | 34 +++++++ 10 files changed, 280 insertions(+), 55 deletions(-) create mode 100644 app/models/__init__.py create mode 100644 app/models/planet.py delete mode 100644 app/routes.py create mode 100644 app/routes/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/e29e7cf3fb9b_adds_planet_model.py diff --git a/app/__init__.py b/app/__init__.py index 776061489..6f72d5961 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,8 +1,23 @@ 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__) - from .routes import bp - app.register_blueprint(bp) + + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' + + from app.models.planet import Planet + + db.init_app(app) + migrate.init_app(app, db) + + from app.routes.routes import planets_bp + app.register_blueprint(planets_bp) + return app + 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..68fcc7eca --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1,15 @@ +from app import db + +class Planet(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String, nullable=False) + description = db.Column(db.String) + has_life = db.Column(db.Boolean, nullable=False) + + def to_dict(self): + return dict( + id=self.id, + name=self.name, + description=self.description, + has_life=self.has_life + ) diff --git a/app/routes.py b/app/routes.py deleted file mode 100644 index a73cb98bf..000000000 --- a/app/routes.py +++ /dev/null @@ -1,53 +0,0 @@ -from flask import Blueprint, jsonify, abort, make_response - -class Planet: - def __init__(self, id, name, description, life): - self.id = id - self.name = name - self.description = description - self.life = life - - def to_dict(self): - return dict( - id=self.id, - name=self.name, - description=self.description, - life=self.life - ) - - -planets = [ - Planet(1, "Earth", "Best planet", True), - Planet(2, "Saturn", "Got a ring on it", False), - Planet(3, "Mars", "We want to go there", False) -] - -bp = Blueprint("planets_bp", __name__, url_prefix="/planets") - - -def validate_planet(id): - try: - id = int(id) - except: - abort(make_response({"message":f"planet {id} invalid"}, 400)) - - for planet in planets: - if planet.id == id: - return planet - - abort(make_response({"message":f"planet {id} not found"}, 404)) - - -@bp.route("", methods=["GET"]) -def get_planets(): - planets_list = [] - - for planet in planets: - planets_list.append(planet.to_dict()) - - return jsonify(planets_list) - -@bp.route("/", methods=["GET"]) -def get_one_planet(id): - planet = validate_planet(id) - return planet.to_dict() \ No newline at end of file diff --git a/app/routes/routes.py b/app/routes/routes.py new file mode 100644 index 000000000..1066978e0 --- /dev/null +++ b/app/routes/routes.py @@ -0,0 +1,48 @@ +from app import db +from app.models.planet import Planet +from flask import Blueprint, jsonify, make_response, request + + +planets_bp = Blueprint("planets", __name__, url_prefix="/planets") + +@planets_bp.route("", methods=["POST"]) +def create_planet(): + request_body = request.get_json() + + planet = Planet( + name=request_body["name"], + description=request_body["description"], + has_life=request_body["has_life"]) + + db.session.add(planet) + db.session.commit() + + return jsonify(planet.to_dict()), 201 + + +@planets_bp.route("", methods=["GET"]) +def get_planets(): + planets = Planet.query.all() + + planets_response = [planet.to_dict() for planet in planets] + + return jsonify(planets_response) + +# ----old code not yet refactored---- +# def validate_planet(id): +# try: +# id = int(id) +# except: +# abort(make_response({"message":f"planet {id} invalid"}, 400)) + +# for planet in planets: +# if planet.id == id: +# return planet + +# abort(make_response({"message":f"planet {id} not found"}, 404)) + + +# @bp.route("/", methods=["GET"]) +# def get_one_planet(id): +# planet = validate_planet(id) +# return planet.to_dict() \ No newline at end of file diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..f8ed4801f --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 000000000..8b3fb3353 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from flask import current_app + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option( + 'sqlalchemy.url', + str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 000000000..2c0156303 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/e29e7cf3fb9b_adds_planet_model.py b/migrations/versions/e29e7cf3fb9b_adds_planet_model.py new file mode 100644 index 000000000..bd2846983 --- /dev/null +++ b/migrations/versions/e29e7cf3fb9b_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds Planet model + +Revision ID: e29e7cf3fb9b +Revises: +Create Date: 2022-04-29 14:14:00.240448 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'e29e7cf3fb9b' +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=False), + sa.Column('description', sa.String(), nullable=True), + sa.Column('has_life', sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('planet') + # ### end Alembic commands ### From 29710d3a3b3d4df9bd14c4f216f96dcaef09f98f Mon Sep 17 00:00:00 2001 From: Georgia Adam Date: Tue, 3 May 2022 14:06:23 -0400 Subject: [PATCH 04/10] added put and delete --- app/routes/routes.py | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/app/routes/routes.py b/app/routes/routes.py index 1066978e0..0a0992cdb 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -1,6 +1,7 @@ +from unicodedata import name 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 planets_bp = Blueprint("planets", __name__, url_prefix="/planets") @@ -28,18 +29,38 @@ def get_planets(): return jsonify(planets_response) +@planets_bp.route("/", methods=["PUT"]) +def replace_planet(planet_id): + planet = validate_planet(planet_id) + request_body = request.get_json() + + try: + planet.name = request_body["name"] + planet.description = request_body["description"] + planet.has_life = request_body["has_life"] + except KeyError: + return make_response("Key Error", 400) + + db.session.commit() + return make_response(f"Planet {planet_id} successfully updated!", 200) + + + + + # ----old code not yet refactored---- -# def validate_planet(id): -# try: -# id = int(id) -# except: -# abort(make_response({"message":f"planet {id} invalid"}, 400)) +def validate_planet(id): + try: + id = int(id) + except: + abort(make_response({"message":f"planet {id} invalid"}, 400)) -# for planet in planets: -# if planet.id == id: -# return planet + planet = Planet.query.get(id) -# abort(make_response({"message":f"planet {id} not found"}, 404)) + if not planet: + abort(make_response({"message":f"planet {id} not found"}, 404)) + else: + return planet # @bp.route("/", methods=["GET"]) From 691f864fd61b1b648c27d8b5b2050eda55209589 Mon Sep 17 00:00:00 2001 From: Georgia Adam Date: Tue, 3 May 2022 14:13:40 -0400 Subject: [PATCH 05/10] delete added --- app/routes/routes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/routes/routes.py b/app/routes/routes.py index 0a0992cdb..4cca7de64 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -44,7 +44,13 @@ def replace_planet(planet_id): db.session.commit() return make_response(f"Planet {planet_id} successfully updated!", 200) +@planets_bp.route("/", methods=["DELETE"]) +def delete_planet(planet_id): + planet = validate_planet(planet_id) + db.session.delete(planet) + db.session.commit() + return make_response(f"Planet {planet_id} successfully deleted.") From 982ece02ca58e4d656ddeab70039bf4b213b7149 Mon Sep 17 00:00:00 2001 From: Liliana Date: Tue, 3 May 2022 14:16:54 -0400 Subject: [PATCH 06/10] Moved validate_planet function --- app/routes/routes.py | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/app/routes/routes.py b/app/routes/routes.py index 4cca7de64..76fca05c1 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -6,6 +6,19 @@ planets_bp = Blueprint("planets", __name__, url_prefix="/planets") +def validate_planet(id): + try: + id = int(id) + except: + abort(make_response({"message":f"planet {id} invalid"}, 400)) + + planet = Planet.query.get(id) + + if not planet: + abort(make_response({"message":f"planet {id} not found"}, 404)) + else: + return planet + @planets_bp.route("", methods=["POST"]) def create_planet(): request_body = request.get_json() @@ -51,25 +64,3 @@ def delete_planet(planet_id): db.session.delete(planet) db.session.commit() return make_response(f"Planet {planet_id} successfully deleted.") - - - -# ----old code not yet refactored---- -def validate_planet(id): - try: - id = int(id) - except: - abort(make_response({"message":f"planet {id} invalid"}, 400)) - - planet = Planet.query.get(id) - - if not planet: - abort(make_response({"message":f"planet {id} not found"}, 404)) - else: - return planet - - -# @bp.route("/", methods=["GET"]) -# def get_one_planet(id): -# planet = validate_planet(id) -# return planet.to_dict() \ No newline at end of file From cef35b278f446ac8126ce245747183f71fa501b9 Mon Sep 17 00:00:00 2001 From: Liliana Date: Wed, 4 May 2022 14:22:27 -0400 Subject: [PATCH 07/10] Added query param 'name' to get_planets, added routes_helper file, created error_message function --- app/routes/routes.py | 28 +++++++++++----------------- app/routes/routes_helper.py | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 app/routes/routes_helper.py diff --git a/app/routes/routes.py b/app/routes/routes.py index 76fca05c1..2c912b3b9 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -1,24 +1,12 @@ from unicodedata import name 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 .routes_helper import validate_planet, error_message planets_bp = Blueprint("planets", __name__, url_prefix="/planets") -def validate_planet(id): - try: - id = int(id) - except: - abort(make_response({"message":f"planet {id} invalid"}, 400)) - - planet = Planet.query.get(id) - - if not planet: - abort(make_response({"message":f"planet {id} not found"}, 404)) - else: - return planet - @planets_bp.route("", methods=["POST"]) def create_planet(): request_body = request.get_json() @@ -36,7 +24,13 @@ def create_planet(): @planets_bp.route("", methods=["GET"]) def get_planets(): - 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() planets_response = [planet.to_dict() for planet in planets] @@ -51,8 +45,8 @@ def replace_planet(planet_id): planet.name = request_body["name"] planet.description = request_body["description"] planet.has_life = request_body["has_life"] - except KeyError: - return make_response("Key Error", 400) + except KeyError as err: + error_message(f"Missing key {err}", 400) db.session.commit() return make_response(f"Planet {planet_id} successfully updated!", 200) diff --git a/app/routes/routes_helper.py b/app/routes/routes_helper.py new file mode 100644 index 000000000..24e18b6f9 --- /dev/null +++ b/app/routes/routes_helper.py @@ -0,0 +1,19 @@ +from flask import jsonify, make_response, abort +from app.models.planet import Planet + +def error_message(message, status_code): + abort(make_response(message, status_code)) + +def validate_planet(id): + try: + id = int(id) + except: + error_message({"message":f"planet {id} invalid"}, 400) + + planet = Planet.query.get(id) + + if not planet: + error_message({"message":f"planet {id} not found"}, 404) + else: + return planet + From 189cfea7a008ef9741bb5db0f06943a9608fff0d Mon Sep 17 00:00:00 2001 From: Georgia Adam Date: Thu, 5 May 2022 14:33:17 -0400 Subject: [PATCH 08/10] wave 6 draft implementations --- app/__init__.py | 10 ++++++++-- app/routes/routes.py | 2 +- requirements.txt | 2 +- tests/__init__.py | 0 tests/conftest.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_routes.py | 19 +++++++++++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_routes.py diff --git a/app/__init__.py b/app/__init__.py index 6f72d5961..5f2ee41ca 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,17 +1,23 @@ 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' - from app.models.planet import Planet + 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) diff --git a/app/routes/routes.py b/app/routes/routes.py index 2c912b3b9..49a35da38 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -34,7 +34,7 @@ def get_planets(): planets_response = [planet.to_dict() for planet in planets] - return jsonify(planets_response) + return jsonify(planets_response), 200 @planets_bp.route("/", methods=["PUT"]) def replace_planet(planet_id): diff --git a/requirements.txt b/requirements.txt index a506b4d12..ad425a4b9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ Mako==1.1.4 MarkupSafe==1.1.1 psycopg2-binary==2.8.6 pycodestyle==2.6.0 -pytest==6.2.3 +pytest==6.2.5 pytest-cov==2.12.1 python-dateutil==2.8.1 python-dotenv==0.15.0 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..52ec6afef --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,34 @@ +from flask.signals import request_finished +from app import db +import pytest +from app.models.planet import Planet + +from app import create_app + +@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(name="earth", description="green and blue", has_life=True) + mercury = Planet(name="mercury", description="extremely hot", has_life=False) + + db.session.add(earth) + db.session.add(mercury) + db.session.commit() \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..33e473534 --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,19 @@ +def test_get_all_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_one_cat(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": "green and blue", + "has_life": True + } \ No newline at end of file From ce5c4efe425837ef2e5a3bdba0c57ebb93f72c0f Mon Sep 17 00:00:00 2001 From: Georgia Adam Date: Thu, 5 May 2022 15:01:58 -0400 Subject: [PATCH 09/10] tests working --- app/routes/routes.py | 8 ++++++++ tests/test_routes.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/routes/routes.py b/app/routes/routes.py index 49a35da38..9a140b90e 100644 --- a/app/routes/routes.py +++ b/app/routes/routes.py @@ -36,6 +36,14 @@ def get_planets(): return jsonify(planets_response), 200 + +@planets_bp.route("/", methods=["GET"]) +def get_planet_by_id(planet_id): + + planet = validate_planet(planet_id) + + return jsonify(planet.to_dict()), 200 + @planets_bp.route("/", methods=["PUT"]) def replace_planet(planet_id): planet = validate_planet(planet_id) diff --git a/tests/test_routes.py b/tests/test_routes.py index 33e473534..0f5a8ebff 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -6,7 +6,7 @@ def test_get_all_planets_with_empty_db_return_empty_list(client): assert response.status_code == 200 assert response_body == [] -def test_get_one_cat(client, two_planets): +def test_get_one_planet(client, two_planets): response = client.get("/planets/1") response_body = response.get_json() From 224dcac67dd0905ee93788bac2e409131498c706 Mon Sep 17 00:00:00 2001 From: Georgia Adam Date: Thu, 5 May 2022 17:59:25 -0400 Subject: [PATCH 10/10] all waves complete --- tests/test_routes.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 0f5a8ebff..e49088349 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -1,6 +1,5 @@ def test_get_all_planets_with_empty_db_return_empty_list(client): response = client.get('/planets') - response_body = response.get_json() assert response.status_code == 200 @@ -16,4 +15,44 @@ def test_get_one_planet(client, two_planets): "name": "earth", "description": "green and blue", "has_life": True + } + +def test_get_one_planet_empty_db_returns_404(client): + response = client.get("/planets/1") + + assert response.status_code == 404 + +def test_get_all_planets(client, two_planets): + response = client.get('/planets') + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == [ + { + "id": 1, + "name": "earth", + "description": "green and blue", + "has_life": True + }, + { + "id": 2, + "name": "mercury", + "description": "extremely hot", + "has_life": False + } ] + +def test_create_planet_returns_201(client): + response = client.post('/planets', json={ + "name": "venus", + "description": "women only", + "has_life": False + }) + response_body = response.get_json() + + assert response.status_code == 201 + assert response_body == { + "id": 1, + "name": "venus", + "description": "women only", + "has_life": False } \ No newline at end of file