Skip to content

Instantly share code, notes, and snippets.

@rduplain
Created February 13, 2011 05:21
Show Gist options
  • Save rduplain/824472 to your computer and use it in GitHub Desktop.
Save rduplain/824472 to your computer and use it in GitHub Desktop.

Revisions

  1. Ron DuPlain revised this gist Feb 16, 2011. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions README.rst
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,11 @@ Ron DuPlain <[email protected]>

    Post any feedback to: [email protected]

    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
  2. rduplain revised this gist Feb 13, 2011. 8 changed files with 177 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion README.rst
    Original file line number Diff line number Diff line change
    @@ -1 +1,27 @@
    Hello, world!
    Demonstrate use of ``fixture`` with Flask-SQLAlchemy and Flask-Testing.
    February 13, 2011
    Ron DuPlain <[email protected]>

    Post any feedback to: [email protected]

    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.
    10 changes: 10 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    from flask import Flask

    import config
    import models


    app = Flask(__name__)
    app.config.from_object(config)

    db = models.init_app(app)
    1 change: 1 addition & 0 deletions config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db'
    28 changes: 28 additions & 0 deletions fixtures.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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,)
    24 changes: 24 additions & 0 deletions manage.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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()
    33 changes: 33 additions & 0 deletions models.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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)
    16 changes: 16 additions & 0 deletions setup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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',
    ],
    )
    38 changes: 38 additions & 0 deletions test_models.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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()
  3. rduplain created this gist Feb 13, 2011.
    1 change: 1 addition & 0 deletions README.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Hello, world!