-
-
Save mpenning/34fa8d274fc4be240933 to your computer and use it in GitHub Desktop.
Broken Flask-SQLAlchemy & py.test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, Blueprint | |
from flask.ext.restful import Api | |
from models import db | |
api = Api(prefix='/api') | |
api_bp = Blueprint('api_bp', __name__) | |
api.init_app(api_bp) | |
def create_app(config_name): | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' | |
app.config['SQLALCHEMY_ECHO'] = False | |
app.config['TESTING'] = True | |
db.init_app(app) | |
app.register_blueprint(api_bp) | |
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
import sys | |
import os | |
from app import create_app | |
from app import db as _db | |
DB_PATH = 'test.db' | |
DB_URI = 'sqlite:///{0}'.format(DB_PATH) | |
@pytest.yield_fixture(scope='session') | |
def app(request): | |
"""Session-wide test `Flask` application.""" | |
app = create_app('testing') | |
# Establish an application context before running the tests. | |
ctx = app.app_context() | |
ctx.push() | |
yield app | |
# app teardown here | |
ctx.pop() | |
@pytest.yield_fixture(scope='session') | |
def db(app, request): | |
"""Session-wide test database.""" | |
if os.path.exists(DB_PATH): | |
os.remove(DB_PATH) | |
# NOTE: app, is required to keep the db in the right Flask app context | |
_db.app = app | |
_db.create_all() | |
yield _db | |
# Finalize test here | |
_db.drop_all() | |
if os.path.exists(DB_PATH): | |
os.remove(DB_PATH) | |
@pytest.yield_fixture(scope='function') | |
def session(app, db): | |
connection = db.engine.connect() | |
transaction = connection.begin() | |
#options = dict(bind=connection, binds={}) | |
options = dict(bind=connection) | |
session = db.create_scoped_session(options=options) | |
yield session | |
# Finalize test here | |
transaction.rollback() | |
connection.close() | |
session.remove() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_sqlalchemy import SQLAlchemy | |
db = SQLAlchemy() | |
class User(db.Model): | |
__tablename__ = 'users' | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String(64)) | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return u'<User {0}>'.format(self.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from models import User | |
def test_user_schema1(session): | |
person_name = 'Fran Clan' | |
uu = User(name=person_name) | |
session.add(uu) | |
session.commit() | |
assert uu.id==1 | |
assert uu.name==person_name | |
def test_user_schema2(session): | |
person_name = 'Stan Clan' | |
uu = User(name=person_name) | |
session.add(uu) | |
session.commit() | |
assert uu.id==1 | |
assert uu.name==person_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment