def app(): """ Creates a new Flask application for a test duration. Uses application factory from `budgetApp.app.create_app`. For a tests using SQLite DB, a `PRAGMA foreign_keys=ON` is issued (it helps with ON DELETE CASCADE). """ _app = create_app("testingsession", config_object=TestConfig) # _app.testing = False # _app.debug = True if _app.config["SQLALCHEMY_DATABASE_URI"].lower().startswith("sqlite"): @event.listens_for(Engine, "connect") def set_sqlite_pragma(dbapi_connection, connection_record): """ Make SQLite recognize ON DELETE CASCADE. """ cursor = dbapi_connection.cursor() cursor.execute("PRAGMA foreign_keys=ON") cursor.close() Base.metadata.create_all(bind=_app.engine) _app.connection = _app.engine.connect() # No idea why, but between this app() fixture and session() fixture there # is being created a new session object somewhere. And in my tests I found # out that in order to have transactions working properly, I need to have # all these scoped sessions configured to use current connection. budgetApp.app.DbSession.configure(bind=_app.connection) yield _app # the code after yield statement works as a teardown _app.connection.close() Base.metadata.drop_all(bind=_app.engine)
import os import sys import subprocess import requests from flask.ext.script import Manager, Shell, Server # from flask.ext.migrate import MigrateCommand from budgetApp.app import DbSession, create_app from budgetApp.models import Base, User, Budget from budgetApp.settings import DevConfig, ProdConfig # from budgetApp.extensions import db if os.environ.get("BUDGETAPP_ENV") == 'prod': app = create_app(__name__, ProdConfig) else: app = create_app(__name__, DevConfig) manager = Manager(app) def _make_context(): """ Return context dict for a shell session so you can access app, db, and the User model by default. """ from budgetApp.serializers import UserSerializer, BudgetSerializer return { "app": app, "db": DbSession(),