From ffd12c0e6eeb0c2bfbc7ca7c8cbf5605edd838a6 Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Fri, 15 Oct 2021 12:56:26 -0500 Subject: [PATCH 01/20] created Planet class and instances of Planet class --- app/routes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..c3b19881e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,15 @@ from flask import Blueprint +class Planet: + def __init__(self, id, name, description, distance): + self.id = id + self.name = name + self.description = description + self.distance = distance + +planets = [ + Planet(1, "Mars", "The red planet", "Inner part of solar system"), + Planet(2, "Jupiter", "The giant planet", "Outer part of the solar system"), + Planet(3, "Pluto", "The no-longer a planet, planet", "The outer, outer limits"), + Planet(4, "Venus", "The gassy one", "Inner planet, Earth's neighbor") +] \ No newline at end of file From e54025d84159d58bde35ff42ca5fd8bfb87b7366 Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Fri, 15 Oct 2021 12:59:09 -0500 Subject: [PATCH 02/20] created endpoint for planets_bp --- app/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..6596942bd 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,5 +3,7 @@ def create_app(test_config=None): app = Flask(__name__) + from .routes import planets_bp + app.register_blueprint(planets_bp) return app From be4bc0a875b8627a6ee1c235210d31a9d2316559 Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Fri, 15 Oct 2021 13:15:40 -0500 Subject: [PATCH 03/20] created endpoint to get one planet specifically --- app/routes.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index c3b19881e..6ca421144 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from flask import Blueprint +from flask import Blueprint, jsonify class Planet: def __init__(self, id, name, description, distance): @@ -12,4 +12,34 @@ def __init__(self, id, name, description, distance): Planet(2, "Jupiter", "The giant planet", "Outer part of the solar system"), Planet(3, "Pluto", "The no-longer a planet, planet", "The outer, outer limits"), Planet(4, "Venus", "The gassy one", "Inner planet, Earth's neighbor") -] \ No newline at end of file +] + +planets_bp = Blueprint("/planets", __name__, url_prefix="/planets") +@planets_bp.route("", methods=["GET"]) +def get_planets(): + planets_response = [] + for planet in planets: + response = { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "distance": planet.distance, + "status": 200 + } + planets_response.append(response) + return jsonify(planets_response) + + +@planets_bp.route("/", methods = ["GET"]) +def get_one_planet(planet_id): + planet_id = int(planet_id) + for planet in planets: + if planet.id == planet_id: + response = { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "distance": planet.distance, + "status": 200 + } + return jsonify(response) \ No newline at end of file From 688dabf71a2998f2c6d98c81fa3c13da1039f545 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Mon, 18 Oct 2021 12:34:22 -0500 Subject: [PATCH 04/20] Added more planet instances --- app/routes.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index 6ca421144..e1645c048 100644 --- a/app/routes.py +++ b/app/routes.py @@ -8,10 +8,15 @@ def __init__(self, id, name, description, distance): self.distance = distance planets = [ - Planet(1, "Mars", "The red planet", "Inner part of solar system"), - Planet(2, "Jupiter", "The giant planet", "Outer part of the solar system"), - Planet(3, "Pluto", "The no-longer a planet, planet", "The outer, outer limits"), - Planet(4, "Venus", "The gassy one", "Inner planet, Earth's neighbor") + Planet(1, "Mercury", "Hot, but not too hot for ice", "Innermost planet, closest to the Sun"), + Planet(2, "Venus", "The gassy one", "Inner planet, Earth's neighbor"), + Planet(3, "Earth", "Blue and green rock where we exist", "Inner solar system, Goldilocks"), + Planet(4, "Mars", "The red planet", "Inner part of solar system"), + Planet(5, "Jupiter", "The giant planet", "Outer part of the solar system"), + Planet(6, "Saturn", "The one with all of the (most visible) rings", "Outter part of the solar system"), + Planet(7, "Uranus", "The lazy one that rotates on it's side", "Outter part of the solar system"), + Planet(8, "Neptune", "Named after it's beautiful blue color, but not discovered by sight, instead by mathematical calculations", "Outter part of the solar syatem"), + Planet(9, "Pluto", "The no-longer a planet, planet", "The outer, outer limits") ] planets_bp = Blueprint("/planets", __name__, url_prefix="/planets") From 4a98a9c7d4dd6d4d68af3cf5d6a1e69bba5b97ff Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Mon, 18 Oct 2021 19:50:58 -0500 Subject: [PATCH 05/20] imported bodies_bp and registered bodies_bp blueprint --- app/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 6596942bd..8866eab6c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,5 +5,7 @@ def create_app(test_config=None): app = Flask(__name__) from .routes import planets_bp app.register_blueprint(planets_bp) + from .routes import bodies_bp + app.register_blueprint(bodies_bp) return app From c06f7f21712a16c3f768901d47790b91db1987b9 Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Mon, 18 Oct 2021 19:51:47 -0500 Subject: [PATCH 06/20] wrote blueprint to access bodies from le system solaire api. created functions to access data --- app/routes.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index e1645c048..05e0de731 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,5 @@ from flask import Blueprint, jsonify +import requests class Planet: def __init__(self, id, name, description, distance): @@ -47,4 +48,31 @@ def get_one_planet(planet_id): "distance": planet.distance, "status": 200 } - return jsonify(response) \ No newline at end of file + return jsonify(response) + +PATH = "https://api.le-systeme-solaire.net/rest/bodies?filter[]%3D=isPlanet,neq,false" + +bodies_bp = Blueprint("bodies", __name__, url_prefix="/bodies") +@bodies_bp.route("", methods = ["GET"]) +def get_bodies(): + i = 0 + bodies_dict = {} + response = requests.get(PATH) + response_bodies = response.json() + for body in response_bodies["bodies"]: + bodies_dict[i] = { + "id": body["id"], + "english_name": body["englishName"], + "is_planet": body["isPlanet"] + } + i +=1 + return bodies_dict + +@bodies_bp.route("/", methods = ["GET"]) +def get_body_id(id): + id = str(id.lower()) + response = requests.get(PATH) + response_bodies = response.json() + for body in response_bodies["bodies"]: + if id == body["id"] or id == body["englishName"]: + return jsonify(body) \ No newline at end of file From e662f464f7ca35f8ff97fa7b2add012f09ea7ba1 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Fri, 22 Oct 2021 12:40:00 -0500 Subject: [PATCH 07/20] Refactored app/__init__.py --- app/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 8866eab6c..612fae4ab 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,8 +1,22 @@ from flask import Flask +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +db = SQLAlchemy() +migrate = Migrate() def create_app(test_config=None): app = Flask(__name__) + + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/NEED_TO_ADD_DATABASE_NAME' + + # import models here + from app.models.planet import Planet + db.init_app(app) + migrate.init_app(app, db) + + # register blueprints here from .routes import planets_bp app.register_blueprint(planets_bp) from .routes import bodies_bp From 7c3ca0bc68a4bf0b9c4f7204dc2efffdff140adc Mon Sep 17 00:00:00 2001 From: sjolivas Date: Fri, 22 Oct 2021 12:41:44 -0500 Subject: [PATCH 08/20] Created model folder with init and planet files --- app/models/__init__.py | 0 app/models/planet.py | 8 ++++++++ 2 files changed, 8 insertions(+) create mode 100644 app/models/__init__.py create mode 100644 app/models/planet.py 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..4934f1125 --- /dev/null +++ b/app/models/planet.py @@ -0,0 +1,8 @@ +from app import db + +class Planet(bd.Model): + + id = bd.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String) + description = db.Column(db.String + distance = db.Column(db.String) \ No newline at end of file From 3fc45e458ff5b35f3d1d5aa96bee20f5ee2f0722 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Fri, 22 Oct 2021 12:43:05 -0500 Subject: [PATCH 09/20] Initialized changes to routes.py --- app/routes.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/app/routes.py b/app/routes.py index 05e0de731..b58e52296 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,24 +1,25 @@ -from flask import Blueprint, jsonify -import requests +from flask import Blueprint, jsonify, make_response, request +from app import db +from app.models.planet import Planet -class Planet: - def __init__(self, id, name, description, distance): - self.id = id - self.name = name - self.description = description - self.distance = distance +# class Planet: +# def __init__(self, id, name, description, distance): +# self.id = id +# self.name = name +# self.description = description +# self.distance = distance -planets = [ - Planet(1, "Mercury", "Hot, but not too hot for ice", "Innermost planet, closest to the Sun"), - Planet(2, "Venus", "The gassy one", "Inner planet, Earth's neighbor"), - Planet(3, "Earth", "Blue and green rock where we exist", "Inner solar system, Goldilocks"), - Planet(4, "Mars", "The red planet", "Inner part of solar system"), - Planet(5, "Jupiter", "The giant planet", "Outer part of the solar system"), - Planet(6, "Saturn", "The one with all of the (most visible) rings", "Outter part of the solar system"), - Planet(7, "Uranus", "The lazy one that rotates on it's side", "Outter part of the solar system"), - Planet(8, "Neptune", "Named after it's beautiful blue color, but not discovered by sight, instead by mathematical calculations", "Outter part of the solar syatem"), - Planet(9, "Pluto", "The no-longer a planet, planet", "The outer, outer limits") -] +# planets = [ +# Planet(1, "Mercury", "Hot, but not too hot for ice", "Innermost planet, closest to the Sun"), +# Planet(2, "Venus", "The gassy one", "Inner planet, Earth's neighbor"), +# Planet(3, "Earth", "Blue and green rock where we exist", "Inner solar system, Goldilocks"), +# Planet(4, "Mars", "The red planet", "Inner part of solar system"), +# Planet(5, "Jupiter", "The giant planet", "Outer part of the solar system"), +# Planet(6, "Saturn", "The one with all of the (most visible) rings", "Outter part of the solar system"), +# Planet(7, "Uranus", "The lazy one that rotates on it's side", "Outter part of the solar system"), +# Planet(8, "Neptune", "Named after it's beautiful blue color, but not discovered by sight, instead by mathematical calculations", "Outter part of the solar syatem"), +# Planet(9, "Pluto", "The no-longer a planet, planet", "The outer, outer limits") +# ] planets_bp = Blueprint("/planets", __name__, url_prefix="/planets") @planets_bp.route("", methods=["GET"]) From 08dc36cd6b98f0632de29cd97270804fe6b9e67a Mon Sep 17 00:00:00 2001 From: sjolivas Date: Fri, 22 Oct 2021 17:04:30 -0500 Subject: [PATCH 10/20] Refactored __init__.py, routes.py, and planet.py --- app/__init__.py | 4 +- app/models/planet.py | 29 ++++++++-- app/routes.py | 135 ++++++++++++++++++++++--------------------- 3 files changed, 95 insertions(+), 73 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 612fae4ab..9890dfb86 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -9,16 +9,18 @@ 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/NEED_TO_ADD_DATABASE_NAME' + app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' # import models here from app.models.planet import Planet + db.init_app(app) migrate.init_app(app, db) # register blueprints here from .routes import planets_bp app.register_blueprint(planets_bp) + from .routes import bodies_bp app.register_blueprint(bodies_bp) diff --git a/app/models/planet.py b/app/models/planet.py index 4934f1125..1ebfbcdbc 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -1,8 +1,27 @@ from app import db -class Planet(bd.Model): - - id = bd.Column(db.Integer, primary_key=True, autoincrement=True) +class Planet(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) - description = db.Column(db.String - distance = db.Column(db.String) \ No newline at end of file + description = db.Column(db.String) + distance = db.Column(db.String) + + +# class Planet: +# def __init__(self, id, name, description, distance): +# self.id = id +# self.name = name +# self.description = description +# self.distance = distance + +# planets = [ +# Planet(1, "Mercury", "Hot, but not too hot for ice", "Innermost planet, closest to the Sun"), +# Planet(2, "Venus", "The gassy one", "Inner planet, Earth's neighbor"), +# Planet(3, "Earth", "Blue and green rock where we exist", "Inner solar system, Goldilocks"), +# Planet(4, "Mars", "The red planet", "Inner part of solar system"), +# Planet(5, "Jupiter", "The giant planet", "Outer part of the solar system"), +# Planet(6, "Saturn", "The one with all of the (most visible) rings", "Outter part of the solar system"), +# Planet(7, "Uranus", "The lazy one that rotates on it's side", "Outter part of the solar system"), +# Planet(8, "Neptune", "Named after it's beautiful blue color, but not discovered by sight, instead by mathematical calculations", "Outter part of the solar syatem"), +# Planet(9, "Pluto", "The no-longer a planet, planet", "The outer, outer limits") +# ] diff --git a/app/routes.py b/app/routes.py index b58e52296..2b6cff9ed 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,79 +1,80 @@ -from flask import Blueprint, jsonify, make_response, request from app import db from app.models.planet import Planet +from flask import Blueprint, jsonify, make_response, request + +planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") +bodies_bp = Blueprint("bodies", __name__, url_prefix="/bodies") + +@planets_bp.route("", methods=["POST", "GET"]) +def handle_planets(): + if request.method == "POST": + request_body = request.get_json() + print(request_body) + if "name" not in request_body: + return make_response("Invalid Request", 400) -# class Planet: -# def __init__(self, id, name, description, distance): -# self.id = id -# self.name = name -# self.description = description -# self.distance = distance + new_planet = Planet( + name=request_body["name"], + description=request_body["description"], + distance=request_body["distance"] + ) + db.session.add(new_planet) + db.session.commit() -# planets = [ -# Planet(1, "Mercury", "Hot, but not too hot for ice", "Innermost planet, closest to the Sun"), -# Planet(2, "Venus", "The gassy one", "Inner planet, Earth's neighbor"), -# Planet(3, "Earth", "Blue and green rock where we exist", "Inner solar system, Goldilocks"), -# Planet(4, "Mars", "The red planet", "Inner part of solar system"), -# Planet(5, "Jupiter", "The giant planet", "Outer part of the solar system"), -# Planet(6, "Saturn", "The one with all of the (most visible) rings", "Outter part of the solar system"), -# Planet(7, "Uranus", "The lazy one that rotates on it's side", "Outter part of the solar system"), -# Planet(8, "Neptune", "Named after it's beautiful blue color, but not discovered by sight, instead by mathematical calculations", "Outter part of the solar syatem"), -# Planet(9, "Pluto", "The no-longer a planet, planet", "The outer, outer limits") -# ] + return make_response(f"Planet {new_planet.name} was successfully created", 201) -planets_bp = Blueprint("/planets", __name__, url_prefix="/planets") -@planets_bp.route("", methods=["GET"]) -def get_planets(): - planets_response = [] - for planet in planets: - response = { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "distance": planet.distance, - "status": 200 - } - planets_response.append(response) - return jsonify(planets_response) + elif request.method == "GET": + planets = Planet.query.all() + planets_response = [] + for planet in planets: + planets_response.append( + { + "name": planet.name, + "description": planet.description, + "distance": planet.distance + } + ) + return jsonify(planets_response) @planets_bp.route("/", methods = ["GET"]) def get_one_planet(planet_id): - planet_id = int(planet_id) - for planet in planets: - if planet.id == planet_id: - response = { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "distance": planet.distance, - "status": 200 - } - return jsonify(response) -PATH = "https://api.le-systeme-solaire.net/rest/bodies?filter[]%3D=isPlanet,neq,false" + planet = Planet.query.get(planet_id) -bodies_bp = Blueprint("bodies", __name__, url_prefix="/bodies") -@bodies_bp.route("", methods = ["GET"]) -def get_bodies(): - i = 0 - bodies_dict = {} - response = requests.get(PATH) - response_bodies = response.json() - for body in response_bodies["bodies"]: - bodies_dict[i] = { - "id": body["id"], - "english_name": body["englishName"], - "is_planet": body["isPlanet"] - } - i +=1 - return bodies_dict + if planet is None: + return make_response(f"Planet {planet_id} not found", 404) + + return { + "name": planet.name, + "description": planet.description, + "distance": planet.distance + } + + +# PATH = "https://api.le-systeme-solaire.net/rest/bodies?filter[]%3D=isPlanet,neq,false" + + +# @bodies_bp.route("", methods = ["GET"]) +# def get_bodies(): +# i = 0 +# bodies_dict = {} +# response = requests.get(PATH) +# response_bodies = response.json() +# for body in response_bodies["bodies"]: +# bodies_dict[i] = { +# "id": body["id"], +# "english_name": body["englishName"], +# "is_planet": body["isPlanet"] +# } +# i +=1 +# return bodies_dict -@bodies_bp.route("/", methods = ["GET"]) -def get_body_id(id): - id = str(id.lower()) - response = requests.get(PATH) - response_bodies = response.json() - for body in response_bodies["bodies"]: - if id == body["id"] or id == body["englishName"]: - return jsonify(body) \ No newline at end of file +# @bodies_bp.route("/", methods = ["GET"]) +# def get_body_id(id): +# id = str(id.lower()) +# response = requests.get(PATH) +# response_bodies = response.json() +# for body in response_bodies["bodies"]: +# if id == body["id"] or id == body["englishName"]: +# return jsonify(body) \ No newline at end of file From 60407a3ad2d39c85f75c25e4cce5e0fc71c0f829 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Fri, 22 Oct 2021 17:05:21 -0500 Subject: [PATCH 11/20] Added migrations folder and files --- migrations/README | 1 + migrations/alembic.ini | 45 +++++++++ migrations/env.py | 96 +++++++++++++++++++ migrations/script.py.mako | 24 +++++ .../78da57da46d7_adds_planet_model.py | 34 +++++++ 5 files changed, 200 insertions(+) create mode 100644 migrations/README create mode 100644 migrations/alembic.ini create mode 100644 migrations/env.py create mode 100644 migrations/script.py.mako create mode 100644 migrations/versions/78da57da46d7_adds_planet_model.py diff --git a/migrations/README b/migrations/README new file mode 100644 index 000000000..98e4f9c44 --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 000000000..f8ed4801f --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 000000000..8b3fb3353 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool +from flask import current_app + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option( + 'sqlalchemy.url', + str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 000000000..2c0156303 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/78da57da46d7_adds_planet_model.py b/migrations/versions/78da57da46d7_adds_planet_model.py new file mode 100644 index 000000000..eb058f9d3 --- /dev/null +++ b/migrations/versions/78da57da46d7_adds_planet_model.py @@ -0,0 +1,34 @@ +"""adds Planet model + +Revision ID: 78da57da46d7 +Revises: +Create Date: 2021-10-22 15:48:29.048143 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '78da57da46d7' +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('distance', 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 5465ac2823b5c5b42834064d210dc463c11280c4 Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Fri, 22 Oct 2021 19:53:43 -0500 Subject: [PATCH 12/20] refactored handle_planets function; deleted commented out code --- app/__init__.py | 3 --- app/models/planet.py | 22 +----------------- app/routes.py | 53 +++++++------------------------------------- 3 files changed, 9 insertions(+), 69 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 9890dfb86..fd22dad11 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -21,7 +21,4 @@ def create_app(test_config=None): from .routes import planets_bp app.register_blueprint(planets_bp) - from .routes import bodies_bp - app.register_blueprint(bodies_bp) - return app diff --git a/app/models/planet.py b/app/models/planet.py index 1ebfbcdbc..e4a419947 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -4,24 +4,4 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) - distance = db.Column(db.String) - - -# class Planet: -# def __init__(self, id, name, description, distance): -# self.id = id -# self.name = name -# self.description = description -# self.distance = distance - -# planets = [ -# Planet(1, "Mercury", "Hot, but not too hot for ice", "Innermost planet, closest to the Sun"), -# Planet(2, "Venus", "The gassy one", "Inner planet, Earth's neighbor"), -# Planet(3, "Earth", "Blue and green rock where we exist", "Inner solar system, Goldilocks"), -# Planet(4, "Mars", "The red planet", "Inner part of solar system"), -# Planet(5, "Jupiter", "The giant planet", "Outer part of the solar system"), -# Planet(6, "Saturn", "The one with all of the (most visible) rings", "Outter part of the solar system"), -# Planet(7, "Uranus", "The lazy one that rotates on it's side", "Outter part of the solar system"), -# Planet(8, "Neptune", "Named after it's beautiful blue color, but not discovered by sight, instead by mathematical calculations", "Outter part of the solar syatem"), -# Planet(9, "Pluto", "The no-longer a planet, planet", "The outer, outer limits") -# ] + distance = db.Column(db.String) \ No newline at end of file diff --git a/app/routes.py b/app/routes.py index 2b6cff9ed..00a311574 100644 --- a/app/routes.py +++ b/app/routes.py @@ -9,72 +9,35 @@ def handle_planets(): if request.method == "POST": request_body = request.get_json() - print(request_body) - if "name" not in request_body: + if ("name" or "description" or "distance") not in request_body: return make_response("Invalid Request", 400) new_planet = Planet( + id = request_body["id"], name=request_body["name"], description=request_body["description"], distance=request_body["distance"] ) db.session.add(new_planet) db.session.commit() - return make_response(f"Planet {new_planet.name} was successfully created", 201) - elif request.method == "GET": planets = Planet.query.all() - planets_response = [] - for planet in planets: - planets_response.append( - { - "name": planet.name, - "description": planet.description, - "distance": planet.distance - } - ) + planets_response = [ + {"id": planet.id, "name": planet.name, "description": planet.description, + "distance": planet.distance} for planet in planets + ] return jsonify(planets_response) @planets_bp.route("/", methods = ["GET"]) def get_one_planet(planet_id): - planet = Planet.query.get(planet_id) - if planet is None: return make_response(f"Planet {planet_id} not found", 404) - return { + "id": planet.id, "name": planet.name, "description": planet.description, "distance": planet.distance - } - - -# PATH = "https://api.le-systeme-solaire.net/rest/bodies?filter[]%3D=isPlanet,neq,false" - - -# @bodies_bp.route("", methods = ["GET"]) -# def get_bodies(): -# i = 0 -# bodies_dict = {} -# response = requests.get(PATH) -# response_bodies = response.json() -# for body in response_bodies["bodies"]: -# bodies_dict[i] = { -# "id": body["id"], -# "english_name": body["englishName"], -# "is_planet": body["isPlanet"] -# } -# i +=1 -# return bodies_dict - -# @bodies_bp.route("/", methods = ["GET"]) -# def get_body_id(id): -# id = str(id.lower()) -# response = requests.get(PATH) -# response_bodies = response.json() -# for body in response_bodies["bodies"]: -# if id == body["id"] or id == body["englishName"]: -# return jsonify(body) \ No newline at end of file + } \ No newline at end of file From eb84a301c665a87eac125de7b991213113321e52 Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Mon, 25 Oct 2021 16:08:43 -0500 Subject: [PATCH 13/20] updated endpoints for PUT and DELETE. added ECHO command from SQLAlchemy --- app/__init__.py | 2 +- app/routes.py | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index fd22dad11..c6ca3f2ee 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -10,7 +10,7 @@ def create_app(test_config=None): app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/solar_system_development' - + app.config['SQLALCHEMY_ECHO'] = True # import models here from app.models.planet import Planet diff --git a/app/routes.py b/app/routes.py index 00a311574..de00cf689 100644 --- a/app/routes.py +++ b/app/routes.py @@ -30,14 +30,28 @@ def handle_planets(): return jsonify(planets_response) -@planets_bp.route("/", methods = ["GET"]) +@planets_bp.route("/", methods = ["GET", "PUT", "DELETE"]) def get_one_planet(planet_id): planet = Planet.query.get(planet_id) if planet is None: return make_response(f"Planet {planet_id} not found", 404) - return { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "distance": planet.distance - } \ No newline at end of file + if request.method == "GET": + response_body = { + "id": planet.id, + "name": planet.name, + "description": planet.description, + "distance": planet.distance + } + return jsonify(response_body) + elif request.method == "PUT": + response_body = request.get_json() + planet.name = response_body["name"] + planet.description = response_body["description"] + planet.distance = response_body["distance"] + db.session.commit() + return jsonify(f"{planet.name} was successfully updated"), 200 + + elif request.method == "DELETE": + db.session.delete(planet) + db.session.commit() + return jsonify(f"{planet.name} was successfully deleted"), 200 \ No newline at end of file From 662a9248da0989f9c429361a84ea9137637ae1ff Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Mon, 25 Oct 2021 16:55:12 -0500 Subject: [PATCH 14/20] created PATCH request method/response --- app/routes.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index de00cf689..2ba3043bb 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,6 +5,7 @@ planets_bp = Blueprint("planets_bp", __name__, url_prefix="/planets") bodies_bp = Blueprint("bodies", __name__, url_prefix="/bodies") + @planets_bp.route("", methods=["POST", "GET"]) def handle_planets(): if request.method == "POST": @@ -13,7 +14,7 @@ def handle_planets(): return make_response("Invalid Request", 400) new_planet = Planet( - id = request_body["id"], + id=request_body["id"], name=request_body["name"], description=request_body["description"], distance=request_body["distance"] @@ -25,12 +26,12 @@ def handle_planets(): planets = Planet.query.all() planets_response = [ {"id": planet.id, "name": planet.name, "description": planet.description, - "distance": planet.distance} for planet in planets + "distance": planet.distance} for planet in planets ] return jsonify(planets_response) -@planets_bp.route("/", methods = ["GET", "PUT", "DELETE"]) +@planets_bp.route("/", methods=["GET", "PUT", "PATCH", "DELETE"]) def get_one_planet(planet_id): planet = Planet.query.get(planet_id) if planet is None: @@ -43,6 +44,7 @@ def get_one_planet(planet_id): "distance": planet.distance } return jsonify(response_body) + elif request.method == "PUT": response_body = request.get_json() planet.name = response_body["name"] @@ -51,7 +53,18 @@ def get_one_planet(planet_id): db.session.commit() return jsonify(f"{planet.name} was successfully updated"), 200 + elif request.method == "PATCH": + response_body = request.get_json() + if "name" in response_body: + planet.name = response_body["name"] + elif "description" in response_body: + planet.description = response_body["description"] + elif "distance" in response_body: + planet.distance = response_body["distance"] + db.session.commit() + return jsonify(f"{planet.name} was successfully updated"), 200 + elif request.method == "DELETE": db.session.delete(planet) db.session.commit() - return jsonify(f"{planet.name} was successfully deleted"), 200 \ No newline at end of file + return jsonify(f"{planet.name} was successfully deleted"), 200 From 906c1c997c9334ac331e5ab7981e42c34604bb09 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Tue, 26 Oct 2021 12:28:20 -0500 Subject: [PATCH 15/20] Added query params to "" endpoint in routes.py --- app/routes.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index 2ba3043bb..49c0575ab 100644 --- a/app/routes.py +++ b/app/routes.py @@ -11,7 +11,7 @@ def handle_planets(): if request.method == "POST": request_body = request.get_json() if ("name" or "description" or "distance") not in request_body: - return make_response("Invalid Request", 400) + return make_response("Invalid Request"), 400 new_planet = Planet( id=request_body["id"], @@ -23,10 +23,15 @@ def handle_planets(): db.session.commit() return make_response(f"Planet {new_planet.name} was successfully created", 201) elif request.method == "GET": - planets = Planet.query.all() + planet_name_query = request.args.get("name") + if planet_name_query: + planets = Planet.query.filter_by(name=planet_name_query) + else: + planets = Planet.query.all() + planets_response = [ {"id": planet.id, "name": planet.name, "description": planet.description, - "distance": planet.distance} for planet in planets + "distance": planet.distance} for planet in planets ] return jsonify(planets_response) @@ -35,7 +40,8 @@ def handle_planets(): def get_one_planet(planet_id): planet = Planet.query.get(planet_id) if planet is None: - return make_response(f"Planet {planet_id} not found", 404) + return jsonify(f"Planet {planet_id} not found"), 404 + if request.method == "GET": response_body = { "id": planet.id, From 8575a0580d15a615cb46d682d394c3494cd4efd5 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 27 Oct 2021 13:10:00 -0500 Subject: [PATCH 16/20] Set up configuration for API testing --- app/__init__.py | 15 +++++++++++++-- requirements.txt | 7 +++++++ tests/__init__.py | 0 tests/conftest.py | 21 +++++++++++++++++++++ tests/test_routes.py | 8 ++++++++ 5 files changed, 49 insertions(+), 2 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 c6ca3f2ee..3a57ce642 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,16 +1,27 @@ 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' - app.config['SQLALCHEMY_ECHO'] = True + + if not test_config: + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_DATABASE_URI") + app.config['SQLALCHEMY_ECHO'] = True + + else: + app.config["TESTING"] = True + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI") + app.config['SQLALCHEMY_ECHO'] = True + # import models here from app.models.planet import Planet diff --git a/requirements.txt b/requirements.txt index fd90fffa8..6ee946d37 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ alembic==1.5.4 +attrs==21.2.0 autopep8==1.5.5 certifi==2020.12.5 chardet==4.0.0 @@ -7,12 +8,18 @@ Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 idna==2.10 +iniconfig==1.1.1 itsdangerous==1.1.0 Jinja2==2.11.3 Mako==1.1.4 MarkupSafe==1.1.1 +packaging==21.0 +pluggy==1.0.0 psycopg2-binary==2.8.6 +py==1.10.0 pycodestyle==2.6.0 +pyparsing==3.0.2 +pytest==6.2.5 python-dateutil==2.8.1 python-dotenv==0.15.0 python-editor==1.0.4 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..3dcde1d36 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,21 @@ +import pytest +from app import create_app +from app import db +from app.models.planet import Planet + + +@pytest.fixture +def app(): + app = create_app({"TESTING": True}) + + with app.app_context(): + db.create_all() + yield app + + with app.app_context(): + db.drop_all() + + +@pytest.fixture +def client(app): + return app.test_client() \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..30c1eedfc --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,8 @@ +def test_get_all_planets_with_no_records(client): + response = client.get("/planets") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == [] + + From 05fbee2e9fbafc764c1fb4d03efe945c8d0823ae Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Wed, 27 Oct 2021 13:33:17 -0500 Subject: [PATCH 17/20] configured two tests --- app/models/planet.py | 10 +++++++++- tests/conftest.py | 9 ++++++++- tests/test_routes.py | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/models/planet.py b/app/models/planet.py index e4a419947..63375d3f1 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -4,4 +4,12 @@ class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) - distance = db.Column(db.String) \ No newline at end of file + distance = db.Column(db.String) + + def planet_dict(self): + return { + "id": self.id, + "name": self.name, + "description": self.description, + "distance": self.distance + } \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 3dcde1d36..d8772f2c2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,4 +18,11 @@ def app(): @pytest.fixture def client(app): - return app.test_client() \ No newline at end of file + return app.test_client() + +@pytest.fixture +def two_saved_planets(app): + planet1 = Planet(name= "Earth", description= "Blue green marble", distance= "Right here") + planet2 = Planet(name= "Mars", description= "red", distance= "next door") + db.session.add_all([planet1, planet2]) + db.session.commit() diff --git a/tests/test_routes.py b/tests/test_routes.py index 30c1eedfc..18a77db58 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -5,4 +5,22 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] +def test_get_two_planets_returns_two_planets(client, two_saved_planets): + response = client.get("/planets") + response_body = response.get_json() + + assert response.status_code == 200 + assert len(response_body) == 2 + +def get_planet_returns_expected_planet(client, two_saved_planets): + response = client.get("/planets/1") + response_body = response.get_json() + + assert response.status_code == 200 + assert response_body == { + "id": 1, + "name": "Earth", + "description":"Blue green marble", + "distance": "Right here" + } From 5584d9f5a8357cc7d355497277bc90156962f8e6 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 27 Oct 2021 13:53:40 -0500 Subject: [PATCH 18/20] added one test --- app/routes.py | 4 ++-- tests/conftest.py | 1 + tests/test_routes.py | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/routes.py b/app/routes.py index 49c0575ab..df25bcd41 100644 --- a/app/routes.py +++ b/app/routes.py @@ -11,7 +11,7 @@ def handle_planets(): if request.method == "POST": request_body = request.get_json() if ("name" or "description" or "distance") not in request_body: - return make_response("Invalid Request"), 400 + return jsonify("Invalid Request"), 400 new_planet = Planet( id=request_body["id"], @@ -21,7 +21,7 @@ def handle_planets(): ) db.session.add(new_planet) db.session.commit() - return make_response(f"Planet {new_planet.name} was successfully created", 201) + return jsonify(new_planet), 201 elif request.method == "GET": planet_name_query = request.args.get("name") if planet_name_query: diff --git a/tests/conftest.py b/tests/conftest.py index d8772f2c2..9e4d69d27 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,3 +26,4 @@ def two_saved_planets(app): planet2 = Planet(name= "Mars", description= "red", distance= "next door") db.session.add_all([planet1, planet2]) db.session.commit() + diff --git a/tests/test_routes.py b/tests/test_routes.py index 18a77db58..8c3ca5998 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -11,8 +11,14 @@ def test_get_two_planets_returns_two_planets(client, two_saved_planets): assert response.status_code == 200 assert len(response_body) == 2 + assert response_body[0] == { + "id": 1, + "name": "Earth", + "description":"Blue green marble", + "distance": "Right here" + } -def get_planet_returns_expected_planet(client, two_saved_planets): +def test_get_planet_returns_expected_planet(client, two_saved_planets): response = client.get("/planets/1") response_body = response.get_json() @@ -24,3 +30,14 @@ def get_planet_returns_expected_planet(client, two_saved_planets): "distance": "Right here" } + +def test_returns_new_planet(client, two_saved_planets): + response = client.post("/planets") + response_body = { + "name": "Earth", + "description": "Blue green marble", + "distance": "Right here" + } + + assert response.status_code == 201 + assert len(response_body) == 1 From 4d2a369fa6c570309aa8251c5d35319543cf1a11 Mon Sep 17 00:00:00 2001 From: sjolivas Date: Wed, 27 Oct 2021 14:46:39 -0500 Subject: [PATCH 19/20] Added test and refactored routes.py --- app/routes.py | 16 ++++------------ tests/test_routes.py | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/routes.py b/app/routes.py index df25bcd41..dbf94126e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -11,17 +11,16 @@ def handle_planets(): if request.method == "POST": request_body = request.get_json() if ("name" or "description" or "distance") not in request_body: - return jsonify("Invalid Request"), 400 + return jsonify("Invalid Request"), 404 new_planet = Planet( - id=request_body["id"], name=request_body["name"], description=request_body["description"], distance=request_body["distance"] ) db.session.add(new_planet) db.session.commit() - return jsonify(new_planet), 201 + return jsonify(new_planet.planet_dict()), 201 elif request.method == "GET": planet_name_query = request.args.get("name") if planet_name_query: @@ -30,8 +29,7 @@ def handle_planets(): planets = Planet.query.all() planets_response = [ - {"id": planet.id, "name": planet.name, "description": planet.description, - "distance": planet.distance} for planet in planets + planet.planet_dict() for planet in planets ] return jsonify(planets_response) @@ -43,13 +41,7 @@ def get_one_planet(planet_id): return jsonify(f"Planet {planet_id} not found"), 404 if request.method == "GET": - response_body = { - "id": planet.id, - "name": planet.name, - "description": planet.description, - "distance": planet.distance - } - return jsonify(response_body) + return jsonify(planet.planet_dict()) elif request.method == "PUT": response_body = request.get_json() diff --git a/tests/test_routes.py b/tests/test_routes.py index 8c3ca5998..a6e235034 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -1,3 +1,5 @@ +import json + def test_get_all_planets_with_no_records(client): response = client.get("/planets") response_body = response.get_json() @@ -32,12 +34,19 @@ def test_get_planet_returns_expected_planet(client, two_saved_planets): def test_returns_new_planet(client, two_saved_planets): - response = client.post("/planets") - response_body = { + data = { "name": "Earth", "description": "Blue green marble", "distance": "Right here" } + response = client.post("/planets", data=json.dumps(data), headers={"Content-Type": "application/json"}) assert response.status_code == 201 - assert len(response_body) == 1 + + +def test_get_planet_returns_404(client): + response = client.get("/planets/1") + + assert response.status_code == 404 + + From 699b57a7b6cd4dc3e122f6aaa52613b046dc89df Mon Sep 17 00:00:00 2001 From: Rebecca Z <80494343+rzuick@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:48:49 -0500 Subject: [PATCH 20/20] formatted all files with python formatting --- app/__init__.py | 11 +++++++---- app/models/planet.py | 3 ++- tests/conftest.py | 7 ++++--- tests/test_routes.py | 16 +++++++++------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 3a57ce642..49342ac1e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -8,18 +8,21 @@ migrate = Migrate() load_dotenv() + def create_app(test_config=None): app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - + if not test_config: - app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_DATABASE_URI") + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( + "SQLALCHEMY_DATABASE_URI") app.config['SQLALCHEMY_ECHO'] = True else: app.config["TESTING"] = True - app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI") + app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get( + "SQLALCHEMY_TEST_DATABASE_URI") app.config['SQLALCHEMY_ECHO'] = True # import models here @@ -28,7 +31,7 @@ def create_app(test_config=None): db.init_app(app) migrate.init_app(app, db) - # register blueprints here + # register blueprints here from .routes import planets_bp app.register_blueprint(planets_bp) diff --git a/app/models/planet.py b/app/models/planet.py index 63375d3f1..04dd96b22 100644 --- a/app/models/planet.py +++ b/app/models/planet.py @@ -1,5 +1,6 @@ from app import db + class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) @@ -12,4 +13,4 @@ def planet_dict(self): "name": self.name, "description": self.description, "distance": self.distance - } \ No newline at end of file + } diff --git a/tests/conftest.py b/tests/conftest.py index 9e4d69d27..27400e9db 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,10 +20,11 @@ def app(): def client(app): return app.test_client() + @pytest.fixture def two_saved_planets(app): - planet1 = Planet(name= "Earth", description= "Blue green marble", distance= "Right here") - planet2 = Planet(name= "Mars", description= "red", distance= "next door") + planet1 = Planet( + name="Earth", description="Blue green marble", distance="Right here") + planet2 = Planet(name="Mars", description="red", distance="next door") db.session.add_all([planet1, planet2]) db.session.commit() - diff --git a/tests/test_routes.py b/tests/test_routes.py index a6e235034..58aad1738 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -1,5 +1,6 @@ import json + def test_get_all_planets_with_no_records(client): response = client.get("/planets") response_body = response.get_json() @@ -7,6 +8,7 @@ def test_get_all_planets_with_no_records(client): assert response.status_code == 200 assert response_body == [] + def test_get_two_planets_returns_two_planets(client, two_saved_planets): response = client.get("/planets") response_body = response.get_json() @@ -16,10 +18,11 @@ def test_get_two_planets_returns_two_planets(client, two_saved_planets): assert response_body[0] == { "id": 1, "name": "Earth", - "description":"Blue green marble", + "description": "Blue green marble", "distance": "Right here" } - + + def test_get_planet_returns_expected_planet(client, two_saved_planets): response = client.get("/planets/1") response_body = response.get_json() @@ -28,7 +31,7 @@ def test_get_planet_returns_expected_planet(client, two_saved_planets): assert response_body == { "id": 1, "name": "Earth", - "description":"Blue green marble", + "description": "Blue green marble", "distance": "Right here" } @@ -39,7 +42,8 @@ def test_returns_new_planet(client, two_saved_planets): "description": "Blue green marble", "distance": "Right here" } - response = client.post("/planets", data=json.dumps(data), headers={"Content-Type": "application/json"}) + response = client.post("/planets", data=json.dumps(data), + headers={"Content-Type": "application/json"}) assert response.status_code == 201 @@ -47,6 +51,4 @@ def test_returns_new_planet(client, two_saved_planets): def test_get_planet_returns_404(client): response = client.get("/planets/1") - assert response.status_code == 404 - - + assert response.status_code == 404 \ No newline at end of file