Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrate use of fixture with Flask-SQLAlchemy and Flask-Testing.

Demonstrate use of fixture with Flask-SQLAlchemy and Flask-Testing. February 13, 2011 Ron DuPlain <ron.duplain@gmail.com>

Post any feedback to: flask@librelist.org

Get this gist:

git clone git://gist.github.com/824472.git Flask-SQLAlchemy-Fixture
cd Flask-SQLAlchemy-Fixture

To run, activate a virtualenv and:

python setup.py develop
python test_models.py

If you don't like DeprecationWarning, you can:

python -W ignore::DeprecationWarning test_models.py

You can also:

python manage.py fixtures
sqlite3 app.db

In sqlite3:

.schema
select * from spams;
select * from eggs;

If you need a license for this code, use BSD.

from flask import Flask
import config
import models
app = Flask(__name__)
app.config.from_object(config)
db = models.init_app(app)
SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db'
from fixture import DataSet, SQLAlchemyFixture
from fixture.style import NamedDataStyle
import models
def install(app, *args):
engine = models.create_tables(app)
db = SQLAlchemyFixture(env=models, style=NamedDataStyle(), engine=engine)
data = db.data(*args)
data.setup()
db.dispose()
class SpamData(DataSet):
class spam01:
name = 'spam spam spam'
class EggData(DataSet):
class egg01:
description = 'green, for eating with mechanically separated meat'
# A simple trick for installing all fixtures from an external module.
all_data = (SpamData, EggData,)
from flaskext.script import Manager
from app import app
import fixtures as _fixtures
import models
manager = Manager(app)
@manager.command
def tables():
"Create database tables."
models.create_tables(app)
@manager.command
def fixtures():
"Install test data fixtures into the configured database."
_fixtures.install(app, *_fixtures.all_data)
if __name__ == "__main__":
manager.run()
from flaskext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
db = SQLAlchemy()
def init_app(app):
"""Initializes Flask app."""
db.app = app
db.init_app(app)
return db
def create_tables(app):
"Create tables, and return engine in case of further processing."
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
db.metadata.create_all(engine)
return engine
class Spam(db.Model):
__tablename__ = 'spams'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
class Egg(db.Model):
__tablename__ = 'eggs'
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.Text)
from setuptools import setup
# Tested on Python 2.6 using the packages below.
setup(
name='FlaskFixtures',
install_requires=[
'fixture',
'Flask',
'Flask-Script',
'Flask-SQLAlchemy<0.10',
'Flask-Testing',
'SQLAlchemy',
],
)
import unittest
from flask import Flask
from flaskext.testing import TestCase
import fixtures
import models
class ModelsTestCase(TestCase):
db_uri = "sqlite:///tests.db"
def create_app(self):
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri
return app
def setUp(self):
models.create_tables(self.app)
fixtures.install(self.app, *fixtures.all_data)
self.db = models.init_app(self.app)
def tearDown(self):
self.db.session.remove()
self.db.drop_all()
def test_spam(self):
spam = models.Spam.query.first()
self.assertEquals(spam.name, 'spam spam spam')
def test_egg(self):
egg = models.Egg.query.first()
self.assertTrue(egg.description.startswith('green'))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.