-
-
Save bmjjr/3bdcf51f45ec90cc6354b926fe581a76 to your computer and use it in GitHub Desktop.
Revisions
-
Ron DuPlain revised this gist
Feb 16, 2011 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 -
rduplain revised this gist
Feb 13, 2011 . 8 changed files with 177 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1 +1,27 @@ 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. This file contains hidden or 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 charactersOriginal 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) This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db' This file contains hidden or 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 charactersOriginal 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,) This file contains hidden or 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 charactersOriginal 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() This file contains hidden or 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 charactersOriginal 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) This file contains hidden or 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 charactersOriginal 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', ], ) This file contains hidden or 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 charactersOriginal 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() -
rduplain created this gist
Feb 13, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Hello, world!