From b2dd53236779d3bc8a30e30064ca1d38150b3b41 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 14:27:20 -0800 Subject: [PATCH 01/62] create __init__.py in package model --- app/model/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/model/__init__.py diff --git a/app/model/__init__.py b/app/model/__init__.py new file mode 100644 index 000000000..e69de29bb From 95f28bc7aca1d0ce52c663d25e0daa3d9e9e83c6 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 14:31:07 -0800 Subject: [PATCH 02/62] create model moon with 4 attributes method to_dict and from_dict --- app/model/planet.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/model/planet.py diff --git a/app/model/planet.py b/app/model/planet.py new file mode 100644 index 000000000..41a346f5e --- /dev/null +++ b/app/model/planet.py @@ -0,0 +1,22 @@ + +class Planet(): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String) + description = db.Column(db.String) + radius = db.Column(db.Float) + + def to_dict(self): + planet_dict = {} + planet_dict["id"] = self.id + planet_dict["name"] = self.name + planet_dict["description"] = self.name + planet_dict["radius"] = self.name + return planet_dict + + @classmethod + def from_dict(cls, planet_data): + new_planet = Planet(name=planet_data["name"], + description=planet_data["description"], + radius=planet_data["radius"] + + return new_planet From d637c497036deee7e610fdfe05fdb2a31e1db3c0 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 14:38:10 -0800 Subject: [PATCH 03/62] add planet_bp --- app/routes.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8e9dfe684..ce8a1d965 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,2 +1,7 @@ +from app import db +from app.model.planet import Planet from flask import Blueprint +planets_bp = Blueprint("planets_bp",__name__, url_prefix="/planets") + + From ba508c352d4c45cdd771fb449f6fcf00a12cd249 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 14:38:30 -0800 Subject: [PATCH 04/62] import bd --- app/model/planet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/model/planet.py b/app/model/planet.py index 41a346f5e..a012cd097 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -1,4 +1,4 @@ - +from app import db class Planet(): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) From d15a881d55625c29b60ea610869c999754f997b0 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 14:47:15 -0800 Subject: [PATCH 05/62] fix parentheses --- app/model/planet.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/model/planet.py b/app/model/planet.py index a012cd097..ddcc9c5e4 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -17,6 +17,5 @@ def to_dict(self): def from_dict(cls, planet_data): new_planet = Planet(name=planet_data["name"], description=planet_data["description"], - radius=planet_data["radius"] - + radius=planet_data["radius"]) return new_planet From 8b26e62210411b8ba13dac5c548111ba828aaaf3 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:02:01 -0800 Subject: [PATCH 06/62] create migration --- migrations/README | 1 + migrations/alembic.ini | 45 ++++++++++++++++++ migrations/env.py | 96 +++++++++++++++++++++++++++++++++++++++ migrations/script.py.mako | 24 ++++++++++ 4 files changed, 166 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 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"} From a8d6b59e3c468890dbd6b62c6a1070e3ccb78497 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:05:23 -0800 Subject: [PATCH 07/62] connect to db --- app/model/planet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/model/planet.py b/app/model/planet.py index ddcc9c5e4..f4570d899 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -1,5 +1,5 @@ from app import db -class Planet(): +class Planet(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) description = db.Column(db.String) From c34f02b6366020b07147ad3e58bcb1c3c0683c25 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:07:26 -0800 Subject: [PATCH 08/62] migration new version 55fe3c99246d --- .../versions/55fe3c99246d_add_planet_model.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 migrations/versions/55fe3c99246d_add_planet_model.py diff --git a/migrations/versions/55fe3c99246d_add_planet_model.py b/migrations/versions/55fe3c99246d_add_planet_model.py new file mode 100644 index 000000000..27e46247b --- /dev/null +++ b/migrations/versions/55fe3c99246d_add_planet_model.py @@ -0,0 +1,34 @@ +"""add Planet model + +Revision ID: 55fe3c99246d +Revises: +Create Date: 2023-01-07 15:05:44.614849 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '55fe3c99246d' +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('radius', sa.Float(), 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 962666cef66cf1e65469fbefcd1c5d1696b7e87a Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:13:20 -0800 Subject: [PATCH 09/62] register planets_bp --- app/routes.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index ce8a1d965..479efdf40 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,7 +1,15 @@ from app import db +from flask import Blueprint, jsonify, abort, make_response, request from app.model.planet import Planet -from flask import Blueprint planets_bp = Blueprint("planets_bp",__name__, url_prefix="/planets") +@planets_bp.route("", methods=["POST"]) +def create_planet(): + request_body = request.get_json() + new_planet = Planet.from_dict(request_body) + db.session.add(new_planet) + db.session.commit() + + return make_response(f"Planet {new_planet.name} successfully created", 201) From d7ce15535478f86c7976514bb2c35ab94801c401 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:15:07 -0800 Subject: [PATCH 10/62] register planet_bp --- app/__init__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 70b4cabfe..857ceebba 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,7 +1,29 @@ 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__) + if test_config: + app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI") + app.config["Testing"] = True + else: + app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_DATABASE_URI") + + from app.model.planet import Planet + + db.init_app(app) + migrate.init_app(app, db) + + from .routes import planets_bp + app.register_blueprint(planets_bp) + return app From f273da9c8f3e0d7ac69b71a7737370f746087923 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:48:09 -0800 Subject: [PATCH 11/62] add SQLALCHEMY_TRACK_MODIFICATIONS = False --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 857ceebba..8f4d80577 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -11,7 +11,7 @@ def create_app(test_config=None): app = Flask(__name__) - + SQLALCHEMY_TRACK_MODIFICATIONS = False if test_config: app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI") app.config["Testing"] = True From 27a59e61ea7a1dea67d17546d9b6c85cc2ba168f Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:49:05 -0800 Subject: [PATCH 12/62] set ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False --- app/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/__init__.py b/app/__init__.py index 8f4d80577..4a76dde64 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -11,7 +11,7 @@ def create_app(test_config=None): app = Flask(__name__) - SQLALCHEMY_TRACK_MODIFICATIONS = False + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False if test_config: app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_TEST_DATABASE_URI") app.config["Testing"] = True From 22dc401ee55a555a203a6befef0f4c2d1f137118 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:55:53 -0800 Subject: [PATCH 13/62] change the right value --- app/model/planet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/model/planet.py b/app/model/planet.py index f4570d899..4014a8495 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -9,8 +9,8 @@ def to_dict(self): planet_dict = {} planet_dict["id"] = self.id planet_dict["name"] = self.name - planet_dict["description"] = self.name - planet_dict["radius"] = self.name + planet_dict["description"] = self.description + planet_dict["radius"] = self.radius return planet_dict @classmethod From 1684fdc187a8b274134ec06c75aae71aae1a0b3b Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:56:56 -0800 Subject: [PATCH 14/62] finish the get planet --- app/routes.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/routes.py b/app/routes.py index 479efdf40..250b60d39 100644 --- a/app/routes.py +++ b/app/routes.py @@ -13,3 +13,23 @@ def create_planet(): db.session.commit() return make_response(f"Planet {new_planet.name} successfully created", 201) + +@planets_bp.route("", methods=["GET"]) +def read_all_planets(): + planet_query = Planet.query + name_query = request.args.get("name") + if name_query: + planet_query = planet_query.filter(Planet.name.ilike(f"%{name_query}%")) + + sort_query = request.args.get("sort") + if sort_query: + if sort_query == "desc": + planet_query = planet_query.order_by(Planet.name.desc()) + else: + planet_query = planet_query.order_by(Planet.name.asc()) + + planets = planet_query.all() + planets_response = [] + for planet in planets: + planets_response.append(planet.to_dict()) + return jsonify(planets_response) From 61c1c8bb5d08e94a505d4c4949ce5c0c78411dbb Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 15:59:42 -0800 Subject: [PATCH 15/62] add validate id --- app/routes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/routes.py b/app/routes.py index 250b60d39..6d05eba3e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,6 +4,19 @@ planets_bp = Blueprint("planets_bp",__name__, url_prefix="/planets") +def validate_model(cls, model_id): + try: + model_id = int(model_id) + except: + abort(make_response({"message":f"{cls.__name__} {model_id} invalid"}, 400)) + + model = cls.query.get(model_id) + + if not model: + abort(make_response({"message":f"{cls.__name__} {model_id} not found"}, 404)) + + return model + @planets_bp.route("", methods=["POST"]) def create_planet(): request_body = request.get_json() From 32a73f3b9d1e5d5437d2c263358870d41cd99a35 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:03:28 -0800 Subject: [PATCH 16/62] endpoint get planet by id and put planet by id --- app/routes.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/routes.py b/app/routes.py index 6d05eba3e..8de1fc060 100644 --- a/app/routes.py +++ b/app/routes.py @@ -46,3 +46,22 @@ def read_all_planets(): for planet in planets: planets_response.append(planet.to_dict()) return jsonify(planets_response) + + +@planets_bp.route("/", methods=["GET"]) +def read_one_planet(planet_id): + planet = validate_model(Planet, planet_id) + return planet.to_dict() + +@planets_bp.route("/", methods=["PUT"]) +def update_planet(planet_id): + planet = validate_model(Planet, planet_id) + + request_body = request.get_json() + + planet.name = request_body["name"] + planet.description = request_body["description"] + planet.radius = request_body["radius"] + + db.session.commit() + return make_response(f"Planet #{planet.id} successfully updated") From 15d86c1a337dbd380d0de381af237cada6f668c8 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:06:28 -0800 Subject: [PATCH 17/62] add delete planet by id wave 4 done --- app/routes.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/routes.py b/app/routes.py index 8de1fc060..f675df96b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -65,3 +65,12 @@ def update_planet(planet_id): db.session.commit() return make_response(f"Planet #{planet.id} successfully updated") + +@planets_bp.route("/", methods=["DELETE"]) +def delete_one_planet(planet_id): + planet = validate_model(Planet, planet_id) + + db.session.delete(planet) + db.session.commit() + + return make_response(f"Planet #{planet.id} successfully deleted") \ No newline at end of file From 17c3d730f3c6a79a97f80ef553cc7d2522ff6e74 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:22:44 -0800 Subject: [PATCH 18/62] set conftest --- tests/confest.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/confest.py diff --git a/tests/confest.py b/tests/confest.py new file mode 100644 index 000000000..f6dcf4721 --- /dev/null +++ b/tests/confest.py @@ -0,0 +1,40 @@ +import pytest +from app import create_app +from app import db +from flask.signals import request_finished +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_saved_planets(app): + # Arrange + mars = Planet(name="Mars", + description="War planet", + radius=2106.1) + venus = Planet(name="Venus", + description="Planet Of Love", + radius=3760.4) + + db.session.add_all([mars, venus]) + db.session.commit() \ No newline at end of file From e23fec8bace20d4e7ef57cb1bd26352d50b9148a Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:23:18 -0800 Subject: [PATCH 19/62] rename conftest --- tests/confest.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 tests/confest.py diff --git a/tests/confest.py b/tests/confest.py deleted file mode 100644 index f6dcf4721..000000000 --- a/tests/confest.py +++ /dev/null @@ -1,40 +0,0 @@ -import pytest -from app import create_app -from app import db -from flask.signals import request_finished -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_saved_planets(app): - # Arrange - mars = Planet(name="Mars", - description="War planet", - radius=2106.1) - venus = Planet(name="Venus", - description="Planet Of Love", - radius=3760.4) - - db.session.add_all([mars, venus]) - db.session.commit() \ No newline at end of file From 35bd05293929cbbb5ec361f3682a33a88d0015d2 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:24:17 -0800 Subject: [PATCH 20/62] correct conftest file name --- tests/conftest.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..f6dcf4721 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,40 @@ +import pytest +from app import create_app +from app import db +from flask.signals import request_finished +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_saved_planets(app): + # Arrange + mars = Planet(name="Mars", + description="War planet", + radius=2106.1) + venus = Planet(name="Venus", + description="Planet Of Love", + radius=3760.4) + + db.session.add_all([mars, venus]) + db.session.commit() \ No newline at end of file From bd6c03a29cdf3ddfef975cc3c33ec93339bb8cc5 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:25:00 -0800 Subject: [PATCH 21/62] add tests folder --- tests/__init__.py | 0 tests/test_routes.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_routes.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 000000000..e69de29bb From 41c4d66727b755f78b7b2e31b59aa686cc257cc5 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:27:31 -0800 Subject: [PATCH 22/62] add get planets return empty list --- tests/test_routes.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index e69de29bb..e278404f8 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -0,0 +1,11 @@ +import pytest +from app.model.planet import Planet +from app.routes import validate_model + +def test_get_planets_empty_db_returns_empty_list(client): + # Act + response = client.get("/planets") + + # Assert + assert response.status_code == 200 + assert response.get_json() == [] From 992d8dca50de876524c691121de868fbd0cbd16f Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:29:03 -0800 Subject: [PATCH 23/62] add get planet by not exist id return 404 --- tests/test_routes.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index e278404f8..80c9c9e13 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -2,10 +2,19 @@ from app.model.planet import Planet from app.routes import validate_model -def test_get_planets_empty_db_returns_empty_list(client): +def test_get_planets_returns_empty_list(client): # Act response = client.get("/planets") # Assert assert response.status_code == 200 assert response.get_json() == [] + +def test_get_planet_with_id_returns_404(client): + # Act + response = client.get("/planets/100") + + # Assert + assert response.status_code == 404 + assert response.get_json() == {"message":f"Planet 100 not found"} + From e0a14ebf10f8c49e22cfce8f4ccd2670c7b0fdef Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:41:41 -0800 Subject: [PATCH 24/62] modify import path --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index f6dcf4721..cd51794c6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,7 @@ from app import create_app from app import db from flask.signals import request_finished -from app.models.planet import Planet +from app.model.planet import Planet @pytest.fixture From 6dd1227e16287db66e25ddad4a44c39e111427d6 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:42:58 -0800 Subject: [PATCH 25/62] add test get planets with desc_sort --- tests/test_routes.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 80c9c9e13..3bc9ea17a 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -2,7 +2,7 @@ from app.model.planet import Planet from app.routes import validate_model -def test_get_planets_returns_empty_list(client): +def test_get_planets_no_fixture_returns_empty_list(client): # Act response = client.get("/planets") @@ -10,7 +10,7 @@ def test_get_planets_returns_empty_list(client): assert response.status_code == 200 assert response.get_json() == [] -def test_get_planet_with_id_returns_404(client): +def test_get_planet_with_id_no_fixture_returns_404(client): # Act response = client.get("/planets/100") @@ -18,3 +18,34 @@ def test_get_planet_with_id_returns_404(client): assert response.status_code == 404 assert response.get_json() == {"message":f"Planet 100 not found"} +def test_get_planets_returns_seeded_planets(client, two_saved_planets): + response = client.get("/planets") + + assert response.status_code == 200 + planet_list = response.get_json() + assert len(planet_list) == 2 + assert planet_list[0]["id"] == 1 + assert planet_list[0]["name"] == "Mars" + assert planet_list[0]["description"] == "War planet" + assert planet_list[0]["radius"] == 2106.1 + + assert planet_list[1]["id"] == 2 + assert planet_list[1]["name"] == "Venus" + assert planet_list[1]["description"] == "Planet Of Love" + assert planet_list[1]["radius"] == 3760.4 + +def test_get_planets_with_desc_sort(client, two_saved_planets): + response = client.get("/planets?sort=desc") + + assert response.status_code == 200 + planet_list = response.get_json() + assert len(planet_list) == 2 + assert planet_list[0]["id"] == 2 + assert planet_list[0]["name"] == "Venus" + assert planet_list[0]["description"] == "Planet Of Love" + assert planet_list[0]["radius"] == 3760.4 + + assert planet_list[1]["id"] == 1 + assert planet_list[1]["name"] == "Mars" + assert planet_list[1]["description"] == "War planet" + assert planet_list[1]["radius"] == 2106.1 From 207823ea45700dbcd7e546079450191f616d62f7 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:43:55 -0800 Subject: [PATCH 26/62] get planet by id --- tests/test_routes.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index 3bc9ea17a..0607bfb80 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -49,3 +49,13 @@ def test_get_planets_with_desc_sort(client, two_saved_planets): assert planet_list[1]["name"] == "Mars" assert planet_list[1]["description"] == "War planet" assert planet_list[1]["radius"] == 2106.1 + +def test_get_one_planet_by_id(client, two_saved_planets): + response = client.get("/planets/2") + + assert response.status_code == 200 + planet_list = response.get_json() + assert planet_list["id"] == 2 + assert planet_list["name"] == "Venus" + assert planet_list["description"] == "Planet Of Love" + assert planet_list["radius"] == 3760.4 From aa228e30ebcd0f8dd44842dcce4f14720b8bfff2 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:57:37 -0800 Subject: [PATCH 27/62] add test create a planet --- tests/test_routes.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index 0607bfb80..6730c4dcc 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -59,3 +59,16 @@ def test_get_one_planet_by_id(client, two_saved_planets): assert planet_list["name"] == "Venus" assert planet_list["description"] == "Planet Of Love" assert planet_list["radius"] == 3760.4 + +def test_create_one_planet(client): + # Act + response = client.post("/planets", json={ + "name": "Pluto", + "description": "like the dog", + "radius": 10000 + }) + response_body = response.get_data(as_text=True) + + # Assert + assert response.status_code == 201 + assert response_body == "Planet Pluto successfully created" \ No newline at end of file From 0584e7b00da13b251d609a2def6499447497e913 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 16:59:35 -0800 Subject: [PATCH 28/62] add delete planet by id --- tests/test_routes.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 6730c4dcc..5a9a3519a 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -71,4 +71,30 @@ def test_create_one_planet(client): # Assert assert response.status_code == 201 - assert response_body == "Planet Pluto successfully created" \ No newline at end of file + assert response_body == "Planet Pluto successfully created" + +def test_delete_one_planet(client, two_saved_planets): + # Act + response = client.delete("/planets/1") + response_body = response.get_data(as_text=True) + + # Assert + assert response.status_code == 200 + assert response_body == "Planet #1 successfully deleted" + + +def test_does_not_delete_planet_with_invalid_id(client, two_saved_planets): + # Act + response = client.delete("/planets/helloworld") + + # Assert + assert response.status_code == 400 + assert response.get_json() == {"message": f"Planet helloworld invalid"} + +def test_does_not_delete_planet_with_nonexistent_id(client, two_saved_planets): + # Act + response = client.delete("/planets/100") + + # Assert + assert response.status_code == 404 + assert response.get_json() == {"message":f"Planet 100 not found"} \ No newline at end of file From 60b6933624c760212f9b774470d83ccb5df2e57f Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:01:18 -0800 Subject: [PATCH 29/62] add test put planet with invalid id --- tests/test_routes.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 5a9a3519a..b38e8c534 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -97,4 +97,16 @@ def test_does_not_delete_planet_with_nonexistent_id(client, two_saved_planets): # Assert assert response.status_code == 404 - assert response.get_json() == {"message":f"Planet 100 not found"} \ No newline at end of file + assert response.get_json() == {"message":f"Planet 100 not found"} + +def test_does_not_update_one_planet_with_invalid_id(client, two_saved_planets): + # Act + response = client.put("/planets/helloworld", json={ + "name": "Jupiter", + "description": "Planet Of Luck", + "radius": 43441 + }) + + # Assert + assert response.status_code == 400 + assert response.get_json() == {"message":f"Planet helloworld invalid"} \ No newline at end of file From f7444fd7f3a8af4166183d420c935524c34f1d60 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:02:38 -0800 Subject: [PATCH 30/62] add test put planet with not exist id --- tests/test_routes.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index b38e8c534..1aa45433d 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -109,4 +109,16 @@ def test_does_not_update_one_planet_with_invalid_id(client, two_saved_planets): # Assert assert response.status_code == 400 - assert response.get_json() == {"message":f"Planet helloworld invalid"} \ No newline at end of file + assert response.get_json() == {"message":f"Planet helloworld invalid"} + +def test_does_not_update_one_planet_with_nonexist_id(client, two_saved_planets): + # Act + response = client.put("/planets/300", json={ + "name": "Jupiter", + "description": "Planet Of Luck", + "radius": 43441 + }) + + # Assert + assert response.status_code == 404 + assert response.get_json() == {"message":f"Planet 300 not found"} From fd59b3b2e3feec1ad582b271d6665e16f64b52a8 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:27:52 -0800 Subject: [PATCH 31/62] make change in conftest --- tests/conftest.py | 14 ++++----- tests/test_routes.py | 71 ++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index cd51794c6..118ea076f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,14 +27,14 @@ def client(app): @pytest.fixture -def two_saved_planets(app): +def two_planets(app): # Arrange + earth = Planet(name="Earth", + description="Our planet", + radius=3.95) mars = Planet(name="Mars", - description="War planet", - radius=2106.1) - venus = Planet(name="Venus", - description="Planet Of Love", - radius=3760.4) + description="A dust,cold,desert world", + radius=2.10) - db.session.add_all([mars, venus]) + db.session.add_all([earth, mars]) db.session.commit() \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index 1aa45433d..abc2e5c83 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -18,62 +18,67 @@ def test_get_planet_with_id_no_fixture_returns_404(client): assert response.status_code == 404 assert response.get_json() == {"message":f"Planet 100 not found"} -def test_get_planets_returns_seeded_planets(client, two_saved_planets): +def test_get_planets_returns_seeded_planets(client, two_planets): response = client.get("/planets") assert response.status_code == 200 planet_list = response.get_json() assert len(planet_list) == 2 assert planet_list[0]["id"] == 1 - assert planet_list[0]["name"] == "Mars" - assert planet_list[0]["description"] == "War planet" - assert planet_list[0]["radius"] == 2106.1 + assert planet_list[0]["name"] == "Earth" + assert planet_list[0]["description"] == "Our planet" + assert planet_list[0]["radius"] == 3.95 assert planet_list[1]["id"] == 2 - assert planet_list[1]["name"] == "Venus" - assert planet_list[1]["description"] == "Planet Of Love" - assert planet_list[1]["radius"] == 3760.4 + assert planet_list[1]["name"] == "Mars" + assert planet_list[1]["description"] == "A dust,cold,desert world" + assert planet_list[1]["radius"] == 2.10 -def test_get_planets_with_desc_sort(client, two_saved_planets): +def test_get_planets_with_desc_sort(client, two_planets): response = client.get("/planets?sort=desc") + planet_list = response.get_json() assert response.status_code == 200 - planet_list = response.get_json() assert len(planet_list) == 2 assert planet_list[0]["id"] == 2 - assert planet_list[0]["name"] == "Venus" - assert planet_list[0]["description"] == "Planet Of Love" - assert planet_list[0]["radius"] == 3760.4 + assert planet_list[0]["name"] == "Mars" + assert planet_list[0]["description"] == "A dust,cold,desert world" + assert planet_list[0]["radius"] == 2.10 assert planet_list[1]["id"] == 1 - assert planet_list[1]["name"] == "Mars" - assert planet_list[1]["description"] == "War planet" - assert planet_list[1]["radius"] == 2106.1 + assert planet_list[1]["name"] == "Earth" + assert planet_list[1]["description"] == "Our planet" + assert planet_list[1]["radius"] == 3.95 -def test_get_one_planet_by_id(client, two_saved_planets): - response = client.get("/planets/2") +def test_get_planets_with_desc_sort(client, two_planets): + response = client.get("/planets?sort=asc") + planet_list = response.get_json() assert response.status_code == 200 - planet_list = response.get_json() - assert planet_list["id"] == 2 - assert planet_list["name"] == "Venus" - assert planet_list["description"] == "Planet Of Love" - assert planet_list["radius"] == 3760.4 + assert len(planet_list) == 2 + assert planet_list[0]["id"] == 1 + assert planet_list[0]["name"] == "Earth" + assert planet_list[0]["description"] == "Our planet" + assert planet_list[0]["radius"] == 3.95 + assert planet_list[1]["id"] == 2 + assert planet_list[1]["name"] == "Mars" + assert planet_list[1]["description"] == "A dust,cold,desert world" + assert planet_list[1]["radius"] == 2.10 def test_create_one_planet(client): # Act response = client.post("/planets", json={ - "name": "Pluto", - "description": "like the dog", - "radius": 10000 + "name": "Test1", + "description": "1", + "radius": 1 }) response_body = response.get_data(as_text=True) # Assert assert response.status_code == 201 - assert response_body == "Planet Pluto successfully created" + assert response_body == "Planet Test1 successfully created" -def test_delete_one_planet(client, two_saved_planets): +def test_delete_one_planet(client, two_planets): # Act response = client.delete("/planets/1") response_body = response.get_data(as_text=True) @@ -83,15 +88,15 @@ def test_delete_one_planet(client, two_saved_planets): assert response_body == "Planet #1 successfully deleted" -def test_does_not_delete_planet_with_invalid_id(client, two_saved_planets): +def test_does_not_delete_planet_with_invalid_id(client, two_planets): # Act - response = client.delete("/planets/helloworld") + response = client.delete("/planets/hhhh") # Assert assert response.status_code == 400 - assert response.get_json() == {"message": f"Planet helloworld invalid"} + assert response.get_json() == {"message": f"Planet hhhh invalid"} -def test_does_not_delete_planet_with_nonexistent_id(client, two_saved_planets): +def test_does_not_delete_planet_with_nonexistent_id(client, two_planets): # Act response = client.delete("/planets/100") @@ -99,7 +104,7 @@ def test_does_not_delete_planet_with_nonexistent_id(client, two_saved_planets): assert response.status_code == 404 assert response.get_json() == {"message":f"Planet 100 not found"} -def test_does_not_update_one_planet_with_invalid_id(client, two_saved_planets): +def test_does_not_update_one_planet_with_invalid_id(client, two_planets): # Act response = client.put("/planets/helloworld", json={ "name": "Jupiter", @@ -111,7 +116,7 @@ def test_does_not_update_one_planet_with_invalid_id(client, two_saved_planets): assert response.status_code == 400 assert response.get_json() == {"message":f"Planet helloworld invalid"} -def test_does_not_update_one_planet_with_nonexist_id(client, two_saved_planets): +def test_does_not_update_one_planet_with_nonexist_id(client, two_planets): # Act response = client.put("/planets/300", json={ "name": "Jupiter", From e724423ff344fc5d47d32bfd8480c0134804b916 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:36:56 -0800 Subject: [PATCH 32/62] wave 6 done --- tests/test_routes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_routes.py b/tests/test_routes.py index abc2e5c83..71ee334f1 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -127,3 +127,17 @@ def test_does_not_update_one_planet_with_nonexist_id(client, two_planets): # Assert assert response.status_code == 404 assert response.get_json() == {"message":f"Planet 300 not found"} + + +def test_does_not_update_one_planet_with_return_200(client, two_planets): + # Act + response = client.put("/planets/1", json={ + "name": "Jupiter", + "description": "Planet Of Luck", + "radius": 43441 + }) + response_body = response.get_data(as_text=True) + + # Assert + assert response.status_code == 200 + assert response_body == "Planet #1 successfully updated" From 80fe3071ebcb68603ac4c72537c437dfa3b71d42 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:50:44 -0800 Subject: [PATCH 33/62] add relation with Moon --- app/model/planet.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/model/planet.py b/app/model/planet.py index 4014a8495..647d9c78a 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -4,7 +4,8 @@ class Planet(db.Model): name = db.Column(db.String) description = db.Column(db.String) radius = db.Column(db.Float) - + moons = db.relationship("Moon", back_populates="planet") + def to_dict(self): planet_dict = {} planet_dict["id"] = self.id From 7ef92fab20340d61874cdd66404f0f7a0529eda9 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:51:01 -0800 Subject: [PATCH 34/62] create Moon model --- app/model/moon.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/model/moon.py diff --git a/app/model/moon.py b/app/model/moon.py new file mode 100644 index 000000000..b82e9696e --- /dev/null +++ b/app/model/moon.py @@ -0,0 +1,22 @@ +from app import db +class Moon(db.Model): + id = db.Column(db.Integer, primary_key=True, autoincrement=True) + name = db.Column(db.String) + size = db.Column(db.Float) + description = db.Column(db.String) + planet_id = db.Column(db.Integer, db.ForeignKey("planet.id")) + planet = db.relationship("Planet", back_populates="moons") + + + def to_dict(self): + moon_dict = {} + moon_dict["id"] = self.id + moon_dict["name"] = self.name + + return moon_dict + + @classmethod + def from_dict(cls, moon_data): + new_moon = Moon(name=moon_data["name"]) + + return new_moon From 9343616d55b7053aff0772d5de0966e4c9cd581a Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 17:53:27 -0800 Subject: [PATCH 35/62] add from_dict --- app/model/moon.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/model/moon.py b/app/model/moon.py index b82e9696e..282912432 100644 --- a/app/model/moon.py +++ b/app/model/moon.py @@ -12,11 +12,15 @@ def to_dict(self): moon_dict = {} moon_dict["id"] = self.id moon_dict["name"] = self.name + moon_dict["size"] = self.size + moon_dict["description"] = self.description return moon_dict @classmethod def from_dict(cls, moon_data): - new_moon = Moon(name=moon_data["name"]) + new_moon = Moon(name=moon_data["name"], + size=moon_data["size"], + description=moon_data["description"]) return new_moon From ebaa7157f252c8f805f6a88ee3fe9fa096cb56c5 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 18:11:15 -0800 Subject: [PATCH 36/62] delete Migration folder --- .../versions/55fe3c99246d_add_planet_model.py | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 migrations/versions/55fe3c99246d_add_planet_model.py diff --git a/migrations/versions/55fe3c99246d_add_planet_model.py b/migrations/versions/55fe3c99246d_add_planet_model.py deleted file mode 100644 index 27e46247b..000000000 --- a/migrations/versions/55fe3c99246d_add_planet_model.py +++ /dev/null @@ -1,34 +0,0 @@ -"""add Planet model - -Revision ID: 55fe3c99246d -Revises: -Create Date: 2023-01-07 15:05:44.614849 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = '55fe3c99246d' -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('radius', sa.Float(), 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 54fcec13cb656961aa9a42a0340b0694b10b8202 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 18:11:34 -0800 Subject: [PATCH 37/62] import Moon db --- app/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/__init__.py b/app/__init__.py index 4a76dde64..214fbbf20 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -19,6 +19,7 @@ def create_app(test_config=None): app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("SQLALCHEMY_DATABASE_URI") from app.model.planet import Planet + from app.model.moon import Moon db.init_app(app) migrate.init_app(app, db) From 4a33dc97f91d796cd28c5cc791bbc24c47c23bab Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 18:11:54 -0800 Subject: [PATCH 38/62] add planet and moon --- migrations/versions/63ad8f8ce601_adds_moon.py | 36 +++++++++++++++++++ .../ca4a40f798eb_adds_planet_and_moon.py | 34 ++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 migrations/versions/63ad8f8ce601_adds_moon.py create mode 100644 migrations/versions/ca4a40f798eb_adds_planet_and_moon.py diff --git a/migrations/versions/63ad8f8ce601_adds_moon.py b/migrations/versions/63ad8f8ce601_adds_moon.py new file mode 100644 index 000000000..5295e366e --- /dev/null +++ b/migrations/versions/63ad8f8ce601_adds_moon.py @@ -0,0 +1,36 @@ +"""adds moon + +Revision ID: 63ad8f8ce601 +Revises: ca4a40f798eb +Create Date: 2023-01-07 18:09:29.078259 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '63ad8f8ce601' +down_revision = 'ca4a40f798eb' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('moon', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('size', sa.Float(), nullable=True), + sa.Column('description', sa.String(), nullable=True), + sa.Column('planet_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['planet_id'], ['planet.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('moon') + # ### end Alembic commands ### diff --git a/migrations/versions/ca4a40f798eb_adds_planet_and_moon.py b/migrations/versions/ca4a40f798eb_adds_planet_and_moon.py new file mode 100644 index 000000000..b46326e92 --- /dev/null +++ b/migrations/versions/ca4a40f798eb_adds_planet_and_moon.py @@ -0,0 +1,34 @@ +"""adds planet and moon + +Revision ID: ca4a40f798eb +Revises: +Create Date: 2023-01-07 18:06:12.711798 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ca4a40f798eb' +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('radius', sa.Float(), 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 1ce70e476160a0c113e0d070b949426de80f3c9a Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 18:17:19 -0800 Subject: [PATCH 39/62] import model Moon --- app/routes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/routes.py b/app/routes.py index f675df96b..610988efc 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,7 @@ from app import db from flask import Blueprint, jsonify, abort, make_response, request from app.model.planet import Planet +from app.model.moon import Moon planets_bp = Blueprint("planets_bp",__name__, url_prefix="/planets") From c75f6058e71fe5faff58fff0d3258e5140784a3d Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 19:54:06 -0800 Subject: [PATCH 40/62] add test moon model to dict --- tests/test_model.py | 151 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 tests/test_model.py diff --git a/tests/test_model.py b/tests/test_model.py new file mode 100644 index 000000000..20d8ac6b2 --- /dev/null +++ b/tests/test_model.py @@ -0,0 +1,151 @@ +from app.model.planet import Planet +from app.model.moon import Moon +import pytest +#==============================test Planet model============================= +#============================================================================ +def test_planet_model_to_dict_no_missing_data(): + # Arrange + + test_data = Planet(id = 1, + name="Earth", + description="Our planet", + radius=3.95 + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] == "Earth" + assert result["description"] == "Our planet" + assert result["radius"] == 3.95 + +def test_planet_model_to_dict_missing_name(): + # Arrange + + test_data = Planet(id = 1, + name="", + description="Our planet", + radius=3.95 + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] =="" + assert result["description"] == "Our planet" + assert result["radius"] == 3.95 + +def test_planet_model_to_dict_missing_description(): + # Arrange + + test_data = Planet(id = 1, + name="Earth", + description="", + radius=3.95 + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["description"] == "" + assert result["name"] == "Earth" + assert result["radius"] == 3.95 + + + +def test_planet_model_to_dict_missing_radius(): + # Arrange + + test_data = Planet(id = 1, + name="Earth", + description="Our planet", + radius="" + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["description"] == "Our planet" + assert result["name"] == "Earth" + assert result["radius"] == "" + +#==============================test Moon model============================= +#============================================================================ + +def test_moon_model_to_dict_no_missing_data(): + # Arrange + + test_data = Moon(id = 1, + name="Moon 1", + description="Moon 1 is small", + size = "small" + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] == "Moon 1" + assert result["description"] == "Moon 1 is small" + assert result["size"] == "small" + +def test_moon_model_to_dict_missing_name(): + # Arrange + + test_data = Moon(id = 1, + name="", + description="Moon 1 is small", + size = "small" + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] == "" + assert result["description"] == "Moon 1 is small" + assert result["size"] == "small" + +def test_moon_model_to_dict_missing_size(): + # Arrange + + test_data = Moon(id = 1, + name="", + description="Moon 1 is small", + size = "" + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] == "" + assert result["description"] == "Moon 1 is small" + assert result["size"] == "" + # Arrange + + test_data = Moon(id = 1, + name="", + description="Moon 1 is small", + size = "" + ) + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 4 + assert result["id"] == 1 + assert result["name"] == "" + assert result["description"] == "Moon 1 is small" + assert result["size"] == "" From 39d9902a558920042cc8b8b38a582f3d41cda744 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 20:21:40 -0800 Subject: [PATCH 41/62] add test planet model from dict --- tests/test_model.py | 58 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/tests/test_model.py b/tests/test_model.py index 20d8ac6b2..8ebecc5c9 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -77,6 +77,49 @@ def test_planet_model_to_dict_missing_radius(): assert result["name"] == "Earth" assert result["radius"] == "" +def test_planet_from_dict_returns_planet(): + # Arrange + planet_data = { + "name": "Earth", + "description": "Our planet", + "radius": 3.95,} + + # Act + result = Planet.from_dict(planet_data) + + # Assert + assert result.name == "Earth" + assert result.description == "Our planet" + assert result.radius == 3.95 + +def test_planet_from_dict_no_name(): + # Arrange + planet_data = {"description": "Our planet", + "radius": 3.95,} + + # Act & Assert + with pytest.raises(KeyError, match = 'name'): + result = Planet.from_dict(planet_data) + +def test_planet_from_dict_no_description(): + # Arrange + planet_data = {"name":"Earth", + "radius": 3.95,} + + # Act & Assert + with pytest.raises(KeyError, match = 'description'): + result = Planet.from_dict(planet_data) + +def test_planet_from_dict_no_radius(): + # Arrange + planet_data = {"name":"Earth", + "description": "Our planet"} + + # Act & Assert + with pytest.raises(KeyError, match = 'radius'): + result = Planet.from_dict(planet_data) + + #==============================test Moon model============================= #============================================================================ @@ -133,12 +176,15 @@ def test_moon_model_to_dict_missing_size(): assert result["name"] == "" assert result["description"] == "Moon 1 is small" assert result["size"] == "" + + +def test_moon_model_to_dict_missing_description(): # Arrange test_data = Moon(id = 1, - name="", - description="Moon 1 is small", - size = "" + name="Moon1", + description="", + size = "small" ) # Act result = test_data.to_dict() @@ -146,6 +192,6 @@ def test_moon_model_to_dict_missing_size(): # Assert assert len(result) == 4 assert result["id"] == 1 - assert result["name"] == "" - assert result["description"] == "Moon 1 is small" - assert result["size"] == "" + assert result["name"] == "Moon1" + assert result["description"] == "" + assert result["size"] == "small" \ No newline at end of file From cf471b1f08b09bbb053eae6464960eeee2a62552 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 20:32:34 -0800 Subject: [PATCH 42/62] add test moon from dict --- tests/test_model.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/test_model.py b/tests/test_model.py index 8ebecc5c9..d51ab2e33 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -194,4 +194,47 @@ def test_moon_model_to_dict_missing_description(): assert result["id"] == 1 assert result["name"] == "Moon1" assert result["description"] == "" - assert result["size"] == "small" \ No newline at end of file + assert result["size"] == "small" + +def test_moon_from_dict_returns_moon(): + # Arrange + moon_data = { + "name": "Moon1", + "description": "Moon 1 is small", + "size": "small",} + + # Act + result = Moon.from_dict(moon_data) + + # Assert + assert result.name == "Moon1" + assert result.description == "Moon 1 is small" + assert result.size == "small" + +def test_moon_from_dict_no_name(): + # Arrange + test_data = {"description":"Moon 1 is small", + "size" :"small" + } + + # Act & Assert + with pytest.raises(KeyError, match = 'name'): + result = Moon.from_dict(test_data) + +def test_moon_from_dict_no_size(): + # Arrange + test_data = {"name":"Moon1", + "description":"Moon 1 is small"} + + # Act & Assert + with pytest.raises(KeyError, match = 'size'): + result = Moon.from_dict(test_data) + +def test_moon_from_dict_no_description(): + # Arrange + test_data = {"name":"Moon1", + "size":"small"} + + # Act & Assert + with pytest.raises(KeyError, match = 'description'): + result = Moon.from_dict(test_data) \ No newline at end of file From 06a961826c403726f59ee0ffbe0858b167da2b8e Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 20:35:50 -0800 Subject: [PATCH 43/62] add moons_bp --- app/routes.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 610988efc..942922454 100644 --- a/app/routes.py +++ b/app/routes.py @@ -4,6 +4,7 @@ from app.model.moon import Moon planets_bp = Blueprint("planets_bp",__name__, url_prefix="/planets") +moons_bp = Blueprint("moons_bp",__name__, url_prefix="/moons") def validate_model(cls, model_id): try: @@ -17,7 +18,8 @@ def validate_model(cls, model_id): abort(make_response({"message":f"{cls.__name__} {model_id} not found"}, 404)) return model - +#============================== planets_bp.route ============================= +#============================================================================ @planets_bp.route("", methods=["POST"]) def create_planet(): request_body = request.get_json() @@ -74,4 +76,7 @@ def delete_one_planet(planet_id): db.session.delete(planet) db.session.commit() - return make_response(f"Planet #{planet.id} successfully deleted") \ No newline at end of file + return make_response(f"Planet #{planet.id} successfully deleted") + +#============================== moons_bp.route ============================= +#============================================================================ \ No newline at end of file From ae241fcae23a708a6dfe1e561f39191040d932b3 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 20:36:25 -0800 Subject: [PATCH 44/62] register planet_bp --- app/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 214fbbf20..9713297a6 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -27,4 +27,6 @@ def create_app(test_config=None): from .routes import planets_bp app.register_blueprint(planets_bp) + from .routes import planets_bp + app.register_blueprint(planets_bp) return app From 0a96a8dfe9978d02a58788a5f2f7aa0171525ff5 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 21:16:24 -0800 Subject: [PATCH 45/62] endpoint post moon --- app/routes.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index 942922454..9ab4fe303 100644 --- a/app/routes.py +++ b/app/routes.py @@ -78,5 +78,41 @@ def delete_one_planet(planet_id): return make_response(f"Planet #{planet.id} successfully deleted") + +@planets_bp.route("//moons", methods=["POST"]) +def add_new_moon_to_planet(planet_id): + planet = validate_model(Planet, planet_id) + + request_body = request.get_json() + new_moon = Moon.from_dict(request_body) + new_moon.planet = planet + + db.session.add(new_moon) + db.session.commit() + + message = f"Moon {new_moon.name} created with Planet{planet.name}" + return make_response(jsonify(message), 201) + +@planets_bp.route("//moons", methods=["GET"]) +def get_all_moons_for_planet(planet_id): + planet = validate_model(Planet, planet_id) + + moons_response = [] + for moon in planet.moons: + moons_response.append(moon.to_dict()) + + return jsonify(moons_response) + #============================== moons_bp.route ============================= -#============================================================================ \ No newline at end of file +#============================================================================ + + +@moons_bp.route("", methods=["POST"]) +def create_moon(): + moon_data = request.get_json() + new_moon = Moon.from_dict(moon_data) + + db.session.add(new_moon) + db.session.commit() + + return make_response(f"Moon {new_moon.name} created", 201) \ No newline at end of file From 51772cee26573e1504eaceb7b78f3db703f30a41 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 21:24:36 -0800 Subject: [PATCH 46/62] post planet/1/moons --- app/routes.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 9ab4fe303..c92db8253 100644 --- a/app/routes.py +++ b/app/routes.py @@ -90,7 +90,7 @@ def add_new_moon_to_planet(planet_id): db.session.add(new_moon) db.session.commit() - message = f"Moon {new_moon.name} created with Planet{planet.name}" + message = f"Moon {new_moon.name} created with Planet {planet.name}" return make_response(jsonify(message), 201) @planets_bp.route("//moons", methods=["GET"]) @@ -115,4 +115,15 @@ def create_moon(): db.session.add(new_moon) db.session.commit() - return make_response(f"Moon {new_moon.name} created", 201) \ No newline at end of file + return make_response(f"Moon {new_moon.name} created", 201) + +@moons_bp.route("", methods=["GET"]) +def read_all_moons(): + moons = Moon.query.all() + + + + moons_response = [] + for moon in moons: + moons_response.append(moon.to_dict()) + return jsonify(moons_response) \ No newline at end of file From ba2540d781df050b1cec8c637acf62c85a5c9ba1 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 21:25:57 -0800 Subject: [PATCH 47/62] modify model --- app/model/moon.py | 2 +- app/model/planet.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/model/moon.py b/app/model/moon.py index 282912432..4388b27c1 100644 --- a/app/model/moon.py +++ b/app/model/moon.py @@ -2,7 +2,7 @@ class Moon(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) - size = db.Column(db.Float) + size = db.Column(db.String) description = db.Column(db.String) planet_id = db.Column(db.Integer, db.ForeignKey("planet.id")) planet = db.relationship("Planet", back_populates="moons") diff --git a/app/model/planet.py b/app/model/planet.py index 647d9c78a..c81cf5e24 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -12,7 +12,13 @@ def to_dict(self): planet_dict["name"] = self.name planet_dict["description"] = self.description planet_dict["radius"] = self.radius + + moon_names = [] + for moon in self.moons: + moon_names.append(moon.name) + planet_dict["moon"] = moon_names return planet_dict + @classmethod def from_dict(cls, planet_data): From 85d8625eec79078fd2d699328449d64446013d8c Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 21:26:18 -0800 Subject: [PATCH 48/62] register moon_bp --- app/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 9713297a6..67941fa9c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -27,6 +27,6 @@ def create_app(test_config=None): from .routes import planets_bp app.register_blueprint(planets_bp) - from .routes import planets_bp - app.register_blueprint(planets_bp) + from .routes import moons_bp + app.register_blueprint(moons_bp) return app From 9baa667a4ad5e2d81df0d0dac8366a1b22499bd5 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 21:41:58 -0800 Subject: [PATCH 49/62] add get moon by id --- app/routes.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/routes.py b/app/routes.py index c92db8253..a8b5ea969 100644 --- a/app/routes.py +++ b/app/routes.py @@ -120,10 +120,13 @@ def create_moon(): @moons_bp.route("", methods=["GET"]) def read_all_moons(): moons = Moon.query.all() - - - moons_response = [] for moon in moons: moons_response.append(moon.to_dict()) - return jsonify(moons_response) \ No newline at end of file + return jsonify(moons_response) + +@moons_bp.route("", methods=["GET"]) +def read_one_planet(moon_id): + moon = validate_model(Moon, moon_id) + return moon.to_dict() + From f445c67a44337d358d8bd1cbd7ea85c6c5c6a9ff Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 21:42:51 -0800 Subject: [PATCH 50/62] add --- app/routes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index a8b5ea969..b81d4b4a6 100644 --- a/app/routes.py +++ b/app/routes.py @@ -77,7 +77,8 @@ def delete_one_planet(planet_id): db.session.commit() return make_response(f"Planet #{planet.id} successfully deleted") - +#============================== "//moons" ============================= +#============================================================================ @planets_bp.route("//moons", methods=["POST"]) def add_new_moon_to_planet(planet_id): @@ -126,7 +127,7 @@ def read_all_moons(): return jsonify(moons_response) @moons_bp.route("", methods=["GET"]) -def read_one_planet(moon_id): +def read_one_moon(moon_id): moon = validate_model(Moon, moon_id) return moon.to_dict() From 91dd7ca14acb7aa87f7fa43139f383aab783ef7e Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 22:11:52 -0800 Subject: [PATCH 51/62] modify test model --- app/model/planet.py | 2 +- tests/test_model.py | 30 ++++++++++++++++++------------ tests/test_routes.py | 12 ++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/model/planet.py b/app/model/planet.py index c81cf5e24..dad6b7fa4 100644 --- a/app/model/planet.py +++ b/app/model/planet.py @@ -16,7 +16,7 @@ def to_dict(self): moon_names = [] for moon in self.moons: moon_names.append(moon.name) - planet_dict["moon"] = moon_names + planet_dict["moons"] = moon_names return planet_dict diff --git a/tests/test_model.py b/tests/test_model.py index d51ab2e33..53304aff0 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -2,24 +2,30 @@ from app.model.moon import Moon import pytest #==============================test Planet model============================= -#============================================================================ +#=========== ================================================================= def test_planet_model_to_dict_no_missing_data(): - # Arrange - - test_data = Planet(id = 1, - name="Earth", - description="Our planet", - radius=3.95 - ) + + moon1 = Moon( + description = "Earth's Moon", + size = "small", + name = "Moon" + ) + test_data = Planet(id = 1, + name="Earth", + description="Our planet", + radius=3.95, + moons=[moon1]) + # Act result = test_data.to_dict() # Assert - assert len(result) == 4 + assert len(result) == 5 assert result["id"] == 1 assert result["name"] == "Earth" assert result["description"] == "Our planet" assert result["radius"] == 3.95 + assert result["moons"] == ["Moon"] def test_planet_model_to_dict_missing_name(): # Arrange @@ -33,7 +39,7 @@ def test_planet_model_to_dict_missing_name(): result = test_data.to_dict() # Assert - assert len(result) == 4 + assert len(result) == 5 assert result["id"] == 1 assert result["name"] =="" assert result["description"] == "Our planet" @@ -51,7 +57,7 @@ def test_planet_model_to_dict_missing_description(): result = test_data.to_dict() # Assert - assert len(result) == 4 + assert len(result) == 5 assert result["id"] == 1 assert result["description"] == "" assert result["name"] == "Earth" @@ -71,7 +77,7 @@ def test_planet_model_to_dict_missing_radius(): result = test_data.to_dict() # Assert - assert len(result) == 4 + assert len(result) == 5 assert result["id"] == 1 assert result["description"] == "Our planet" assert result["name"] == "Earth" diff --git a/tests/test_routes.py b/tests/test_routes.py index 71ee334f1..05669c87e 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -1,6 +1,8 @@ import pytest from app.model.planet import Planet from app.routes import validate_model +#============================== test planets_bp.route ============================= +#============================================================================ def test_get_planets_no_fixture_returns_empty_list(client): # Act @@ -141,3 +143,13 @@ def test_does_not_update_one_planet_with_return_200(client, two_planets): # Assert assert response.status_code == 200 assert response_body == "Planet #1 successfully updated" + +#============================== test moons_bp.route ============================= +#============================================================================ +def test_get_moons_no_fixture_returns_empty_list(client): + # Act + response = client.get("/moons") + + # Assert + assert response.status_code == 200 + assert response.get_json() == [] \ No newline at end of file From 671a0c061fb596fbd00d405e987c3fdf775b8403 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 22:17:27 -0800 Subject: [PATCH 52/62] modify planet to dict test --- tests/test_model.py | 74 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/tests/test_model.py b/tests/test_model.py index 53304aff0..73c130908 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -29,12 +29,17 @@ def test_planet_model_to_dict_no_missing_data(): def test_planet_model_to_dict_missing_name(): # Arrange - - test_data = Planet(id = 1, - name="", - description="Our planet", - radius=3.95 - ) + moon1 = Moon( + description = "Earth's Moon", + size = "small", + name = "Moon" + ) + test_data = Planet(id = 1, + name="", + description="Our planet", + radius=3.95, + moons=[moon1]) + # Act result = test_data.to_dict() @@ -44,15 +49,21 @@ def test_planet_model_to_dict_missing_name(): assert result["name"] =="" assert result["description"] == "Our planet" assert result["radius"] == 3.95 + assert result["moons"] == ["Moon"] def test_planet_model_to_dict_missing_description(): # Arrange - - test_data = Planet(id = 1, - name="Earth", - description="", - radius=3.95 - ) + moon1 = Moon( + description = "Earth's Moon", + size = "small", + name = "Moon" + ) + test_data = Planet(id = 1, + name="Earth", + description="", + radius=3.95, + moons=[moon1]) + # Act result = test_data.to_dict() @@ -62,17 +73,43 @@ def test_planet_model_to_dict_missing_description(): assert result["description"] == "" assert result["name"] == "Earth" assert result["radius"] == 3.95 + assert result["moons"] == ["Moon"] def test_planet_model_to_dict_missing_radius(): # Arrange - - test_data = Planet(id = 1, - name="Earth", - description="Our planet", - radius="" - ) + moon1 = Moon( + description = "Earth's Moon", + size = "small", + name = "Moon" + ) + test_data = Planet(id = 1, + name="Earth", + description="Our planet", + radius="", + moons=[moon1]) + + # Act + result = test_data.to_dict() + + # Assert + assert len(result) == 5 + assert result["id"] == 1 + assert result["description"] == "Our planet" + assert result["name"] == "Earth" + assert result["radius"] == "" + assert result["moons"] == ["Moon"] + +def test_planet_model_to_dict_missing_radius(): + # Arrange + + test_data = Planet(id = 1, + name="Earth", + description="Our planet", + radius="", + moons=[]) + # Act result = test_data.to_dict() @@ -82,6 +119,7 @@ def test_planet_model_to_dict_missing_radius(): assert result["description"] == "Our planet" assert result["name"] == "Earth" assert result["radius"] == "" + assert result["moons"] == [] def test_planet_from_dict_returns_planet(): # Arrange From ee1f802a5045d22df2206709042af09ad9ec6b2f Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 22:41:41 -0800 Subject: [PATCH 53/62] add test get moon by id --- tests/conftest.py | 15 +++++++++++++++ tests/test_routes.py | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 118ea076f..68fc352e3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,7 @@ from app import db from flask.signals import request_finished from app.model.planet import Planet +from app.model.moon import Moon @pytest.fixture @@ -37,4 +38,18 @@ def two_planets(app): radius=2.10) db.session.add_all([earth, mars]) + db.session.commit() + + +@pytest.fixture +def two_moons(app): + # Arrange + moon1 = Moon(name="moon1", + description="moon1 is small", + size="small") + moon2 = Moon(name="moon2", + description="moon2 is large", + size="large") + + db.session.add_all([moon1, moon2]) db.session.commit() \ No newline at end of file diff --git a/tests/test_routes.py b/tests/test_routes.py index 05669c87e..602d47545 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -67,6 +67,17 @@ def test_get_planets_with_desc_sort(client, two_planets): assert planet_list[1]["description"] == "A dust,cold,desert world" assert planet_list[1]["radius"] == 2.10 +def test_get_one_planet_by_id(client, two_planets): + response = client.get("/planets/2") + + assert response.status_code == 200 + planet_list = response.get_json() + assert planet_list["id"] == 2 + assert planet_list["name"] == "Earth" + assert planet_list["description"] == "Our planet" + assert planet_list["radius"] == 3.95 + + def test_create_one_planet(client): # Act response = client.post("/planets", json={ @@ -152,4 +163,32 @@ def test_get_moons_no_fixture_returns_empty_list(client): # Assert assert response.status_code == 200 - assert response.get_json() == [] \ No newline at end of file + assert response.get_json() == [] + +def test_get_moons_returns_seeded_planets(client, two_moons): + response = client.get("/moons") + + assert response.status_code == 200 + planet_list = response.get_json() + assert len(planet_list) == 2 + assert planet_list[0]["id"] == 1 + assert planet_list[0]["name"] == "moon1" + assert planet_list[0]["description"] == "moon1 is small" + assert planet_list[0]["size"] == "small" + + assert planet_list[1]["id"] == 2 + assert planet_list[1]["name"] == "moon2" + assert planet_list[1]["description"] == "moon2 is large" + assert planet_list[1]["size"] == "large" + +def test_get_moons_by_id(client, two_moons): + response = client.get("/moons/1") + + assert response.status_code == 200 + planet_list = response.get_json() + assert len(planet_list) == 4 + assert planet_list["id"] == 1 + assert planet_list["name"] == "moon1" + assert planet_list["description"] == "moon1 is small" + assert planet_list["size"] == "small" + From 23031db34f4a83f95b64e176bed7a870ee361d41 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 22:46:46 -0800 Subject: [PATCH 54/62] test not pass --- tests/test_routes.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 602d47545..a9d5bb4c4 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -68,11 +68,11 @@ def test_get_planets_with_desc_sort(client, two_planets): assert planet_list[1]["radius"] == 2.10 def test_get_one_planet_by_id(client, two_planets): - response = client.get("/planets/2") + response = client.get("/planets/1") assert response.status_code == 200 planet_list = response.get_json() - assert planet_list["id"] == 2 + assert planet_list["id"] == 1 assert planet_list["name"] == "Earth" assert planet_list["description"] == "Our planet" assert planet_list["radius"] == 3.95 @@ -192,3 +192,12 @@ def test_get_moons_by_id(client, two_moons): assert planet_list["description"] == "moon1 is small" assert planet_list["size"] == "small" +#============================== test planets_bp.route ============================= +#============================================================================ +def test_get_moons_by_planet_id_no_fixture_returns_empty_list(client,): + # Act + response = client.get("/planet/1/moons") + + # Assert + assert response.status_code == 200 + assert response.get_json() == [] From b75ecc1b765058b854f5e4ce63340f5c12d3f650 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 22:50:13 -0800 Subject: [PATCH 55/62] wave 8 done --- tests/test_routes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index a9d5bb4c4..3dd1ac5fb 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -194,10 +194,10 @@ def test_get_moons_by_id(client, two_moons): #============================== test planets_bp.route ============================= #============================================================================ -def test_get_moons_by_planet_id_no_fixture_returns_empty_list(client,): - # Act - response = client.get("/planet/1/moons") +# def test_get_moons_by_planet_id_no_fixture_returns_empty_list(client): +# # Act +# response = client.get("/planets/1/moons") - # Assert - assert response.status_code == 200 - assert response.get_json() == [] +# # Assert +# assert response.status_code == 200 +# assert response.get_json() == [] From a9ae32a50299a8eb26ad22150af9109f38daebc9 Mon Sep 17 00:00:00 2001 From: annadu Date: Sat, 7 Jan 2023 22:53:20 -0800 Subject: [PATCH 56/62] create procfile --- Procfile | 1 + 1 file changed, 1 insertion(+) create mode 100644 Procfile diff --git a/Procfile b/Procfile new file mode 100644 index 000000000..62e430aca --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: gunicorn 'app:create_app()' \ No newline at end of file From 9eda7fd187e93b9d4ce93753a007e918684efb77 Mon Sep 17 00:00:00 2001 From: annadu Date: Mon, 9 Jan 2023 11:47:45 -0800 Subject: [PATCH 57/62] add --- requirements.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/requirements.txt b/requirements.txt index fba2b3e38..be4299910 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,19 +1,38 @@ alembic==1.5.4 +astroid==2.12.13 +attrs==21.2.0 autopep8==1.5.5 blinker==1.4 certifi==2020.12.5 chardet==4.0.0 +charset-normalizer==2.1.1 click==7.1.2 +coverage==7.0.4 +dill==0.3.6 +exceptiongroup==1.0.0 Flask==1.1.2 Flask-Migrate==2.6.0 Flask-SQLAlchemy==2.4.4 +greenlet==2.0.1 +gunicorn==20.1.0 idna==2.10 +iniconfig==1.1.1 +isort==5.11.4 itsdangerous==1.1.0 Jinja2==2.11.3 +lazy-object-proxy==1.8.0 Mako==1.1.4 MarkupSafe==1.1.1 +mccabe==0.7.0 +packaging==21.2 +platformdirs==2.6.2 +pluggy==1.0.0 psycopg2-binary==2.9.4 +py==1.10.0 pycodestyle==2.6.0 +pylint==2.15.9 +pylint-flask-sqlalchemy==0.2.0 +pyparsing==2.4.7 pytest==7.1.1 pytest-cov==2.12.1 python-dateutil==2.8.1 @@ -23,5 +42,8 @@ requests==2.25.1 six==1.15.0 SQLAlchemy==1.3.23 toml==0.10.2 +tomli==2.0.1 +tomlkit==0.11.6 urllib3==1.26.4 Werkzeug==1.0.1 +wrapt==1.14.1 From 5ff6b40651460cedf9f826755db33054f689e06c Mon Sep 17 00:00:00 2001 From: annadu Date: Mon, 9 Jan 2023 13:36:35 -0800 Subject: [PATCH 58/62] add delete moon by id --- app/routes.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/routes.py b/app/routes.py index b81d4b4a6..c47ae2738 100644 --- a/app/routes.py +++ b/app/routes.py @@ -131,3 +131,11 @@ def read_one_moon(moon_id): moon = validate_model(Moon, moon_id) return moon.to_dict() +@moons_bp.route("", methods=["DELETE"]) +def delete_one_moon(moon_id): + moon = validate_model(Moon, moon_id) + + db.session.delete(moon) + db.session.commit() + + return make_response(f"Moon #{moon.id} successfully deleted") \ No newline at end of file From e81cae5b5165bcfb382deac232012d9f4a447a47 Mon Sep 17 00:00:00 2001 From: annadu Date: Mon, 9 Jan 2023 13:40:06 -0800 Subject: [PATCH 59/62] add put moon by id --- app/routes.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/routes.py b/app/routes.py index c47ae2738..4e3613e1b 100644 --- a/app/routes.py +++ b/app/routes.py @@ -138,4 +138,17 @@ def delete_one_moon(moon_id): db.session.delete(moon) db.session.commit() - return make_response(f"Moon #{moon.id} successfully deleted") \ No newline at end of file + return make_response(f"Moon #{moon.id} successfully deleted") + +@moons_bp.route("", methods=["PUT"]) +def update_moon(moon_id): + moon = validate_model(Moon, moon_id) + + request_body = request.get_json() + + moon.name = request_body["name"] + moon.description = request_body["description"] + moon.size = request_body["size"] + + db.session.commit() + return make_response(f"Moon #{moon.id} successfully updated") \ No newline at end of file From 2a9f97e48bf97824a734d43d326343e43a7915a8 Mon Sep 17 00:00:00 2001 From: annadu Date: Mon, 9 Jan 2023 14:50:06 -0800 Subject: [PATCH 60/62] model change --- app/__init__.py | 3 ++- app/model/moon.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 67941fa9c..d77d84914 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,7 +5,8 @@ import os db = SQLAlchemy() -migrate = Migrate() +# migrate = Migrate() +migrate = Migrate(compare_type=True) load_dotenv() diff --git a/app/model/moon.py b/app/model/moon.py index 4388b27c1..282912432 100644 --- a/app/model/moon.py +++ b/app/model/moon.py @@ -2,7 +2,7 @@ class Moon(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String) - size = db.Column(db.String) + size = db.Column(db.Float) description = db.Column(db.String) planet_id = db.Column(db.Integer, db.ForeignKey("planet.id")) planet = db.relationship("Planet", back_populates="moons") From 4da4ae2a414837d7dd739ed9e313692852ce8e6b Mon Sep 17 00:00:00 2001 From: annadu Date: Mon, 9 Jan 2023 16:31:04 -0800 Subject: [PATCH 61/62] pass wave 9 --- tests/test_model.py | 6 +++--- tests/test_routes.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_model.py b/tests/test_model.py index 73c130908..dbd98580b 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -31,7 +31,7 @@ def test_planet_model_to_dict_missing_name(): # Arrange moon1 = Moon( description = "Earth's Moon", - size = "small", + size = 3.1, name = "Moon" ) test_data = Planet(id = 1, @@ -55,7 +55,7 @@ def test_planet_model_to_dict_missing_description(): # Arrange moon1 = Moon( description = "Earth's Moon", - size = "small", + size = 3.1, name = "Moon" ) test_data = Planet(id = 1, @@ -81,7 +81,7 @@ def test_planet_model_to_dict_missing_radius(): # Arrange moon1 = Moon( description = "Earth's Moon", - size = "small", + size = 3.1, name = "Moon" ) test_data = Planet(id = 1, diff --git a/tests/test_routes.py b/tests/test_routes.py index 3dd1ac5fb..d35a225d2 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -174,12 +174,12 @@ def test_get_moons_returns_seeded_planets(client, two_moons): assert planet_list[0]["id"] == 1 assert planet_list[0]["name"] == "moon1" assert planet_list[0]["description"] == "moon1 is small" - assert planet_list[0]["size"] == "small" + assert planet_list[0]["size"] == 2.4 assert planet_list[1]["id"] == 2 assert planet_list[1]["name"] == "moon2" assert planet_list[1]["description"] == "moon2 is large" - assert planet_list[1]["size"] == "large" + assert planet_list[1]["size"] == 3.1 def test_get_moons_by_id(client, two_moons): response = client.get("/moons/1") @@ -190,7 +190,7 @@ def test_get_moons_by_id(client, two_moons): assert planet_list["id"] == 1 assert planet_list["name"] == "moon1" assert planet_list["description"] == "moon1 is small" - assert planet_list["size"] == "small" + assert planet_list["size"] == 2.4 #============================== test planets_bp.route ============================= #============================================================================ From 13dd6820856be18a3eb48f70d062949b3a06a25a Mon Sep 17 00:00:00 2001 From: annadu Date: Mon, 9 Jan 2023 16:31:11 -0800 Subject: [PATCH 62/62] wave 9 --- tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 68fc352e3..86df16c19 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -46,10 +46,10 @@ def two_moons(app): # Arrange moon1 = Moon(name="moon1", description="moon1 is small", - size="small") + size=2.4) moon2 = Moon(name="moon2", description="moon2 is large", - size="large") + size=3.1) db.session.add_all([moon1, moon2]) db.session.commit() \ No newline at end of file