Skip to main content

Posts

Showing posts from December, 2020

Setting up Flask Migrate with existing SQL database / Resetting Flask Migrations

Flask Migrate allows to manage SQLAlchemy database migrations for Flask applications using Alembic. One of the common user-scenaio is that you have an existing database being used in the Flask Application. If you would like to integrate Flask-Migrate, this post will help you to start with the setup. Other user-scenario that I have seen is that once you integrate Flask-Migrate, some times people accidentally delete the migrations folder or would like to reset the migrations, the same steps can be followed. Let's get started. First step is to make sure that the flask-migrate is installed and included in your flask project. from flask_sqlalchemy import SQLAlchemy from flask import Flask, render_template, redirect, url_for from flask_migrate import Migrate import os basedir = os.path.abspath(os.path.dirname( __file__ )) app = Flask( __name__ ) app.config[ 'SQLALCHEMY_DATABASE_URI' ] = os.environ.get( 'DATABASE_URL' ) or \ 'sqlite:///' + os.path.join

Starting with Pytest framework

Pytest is test framework implemented in Python used for API, Database and UI testing. Running Pytest: When we run pytest  command without any argument, it will look for the "test_*.py" OR "*_test.py" files inside directories and subdirectories and run them. All test functions inside these files must start with " test " in their names.  Let's create our first file with unit test. We'll create very basic suite to test the addition and subtraction of two numbers to understand the concepts by creating two files as shown below. Basic strtucture of any test case is : - Function name has " test " as either prefix or suffix - Perform some operations - Verify the result using "assert" # test_addition.py d ef test_add_two_numbers_1 (): x, y = 5 , 20 s = x + y assert s == 25 def test_add_two_numbers_2 (): x, y = 5 , 20 s = x + y assert s == 20 # test_subtraction.py def test_subtract_two_numbers_1 (): x,