librelist archives

« back to archive

use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-19 @ 16:53
Hi,

I tried to implement the example at https://gist.github.com/824472 and 
https://gist.github.com/4023180 (they are identical as far as I can 
tell) but I get the error below... I've been hunting for a solution but 
can't seem to come up with one. I've isolated the area of the error to 
be around 
'db=SQLAlchemyFixture(env=models,style=NamedDataStyle(),engine=engine)'

### The Error ###
ERROR: test_users (__main__.TestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "flasky_test.py", line 19, in setUp
     fixtures.install(self.app, *fixtures.all_data)
   File "/Users/sina/PycharmProjects/flasky/fixtures.py", line 14, in 
install
     data.setup()
   File "/Library/Python/2.7/site-packages/fixture/base.py", line 71, in 
setup
     self.loader.load(self.data)
   File 
"/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
193, in load
     self.wrap_in_transaction(loader, unloading=False)
   File 
"/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
310, in wrap_in_transaction
     routine()
   File 
"/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
192, in loader
     self.load_dataset(ds)
   File 
"/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
235, in load_dataset
     obj = ds.meta.storage_medium.save(row, column_vals())
   File 
"/Library/Python/2.7/site-packages/fixture/loadable/sqlalchemy_loadable.py", 
line 233, in save
     obj = self.medium()
LoadError: TypeError: __init__() takes exactly 4 arguments (1 given) 
(with 'user01' of '<fixture.dataset.dataset.user01 object at 
0x110784810>' in <UserData at 0x11074efd0 with keys ['user01']>)


### fixtures.py ###

from fixture import DataSet, SQLAlchemyFixture
from fixture.style import NamedDataStyle
from flask.ext.bcrypt import Bcrypt

import mymodel

bcrypt = Bcrypt()

def install(app, *args):
     engine = mymodel.create_tables(app)
     db = SQLAlchemyFixture(env=mymodel, style=NamedDataStyle(), 
engine=engine)
     data = db.data(*args)
     data.setup()
     db.dispose()


class UserData(DataSet):

     class user01:
         username = 'sina'
         email = 'sina@email.com'
         pwhash = bcrypt.generate_password_hash('password')

# A simple trick for installing all fixtures from an external module.
all_data = (UserData, )

### mymodel.py ###

from flask.ext.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['DB_URI'])
     db.metadata.create_all(engine)
     return engine

# models
class User(db.Model):
     __tablename__ = 'flasky_users'
     id = db.Column(db.Integer, primary_key=True, unique=True, 
autoincrement=True)
     username = db.Column(db.String(80), unique=True, nullable=False)
     email = db.Column(db.String(120), unique=True, nullable=False)
     pwhash = db.Column(db.String(80), nullable=False)

     def __init__(self, username, email, pwhash):
         self.username = username
         self.email = email
         self.pwhash = pwhash

     def __repr__(self):
         return '<User %r>' % self.username

### flasky_test.py ###

import unittest

from flask import Flask
from flask.ext.testing import TestCase

import fixtures
import mymodel

class TestCase(TestCase):

     def create_app(self):
         app = Flask(__name__)
         app.config['DB_URI'] = 
'mysql://flasky:czxlzq22@localhost/flasky_test'
         return app

     def setUp(self):
         mymodel.create_tables(self.app)
         fixtures.install(self.app, *fixtures.all_data)
         self.db = mymodel.init_app(self.app)

     def tearDown(self):
         self.db.session.remove()
         self.db.drop_all()

     def assert404(self, response):
         """
         Checks if a HTTP 404 returned
         e.g.
         resp = self.client.get("/")
         self.assert404(resp)
         """
         self.assertTrue(response.status_code == 404)

     def test_users(self):
         user = mymodel.User.query.first()
         self.assertEquals(user.username, 'sina')

     def test_404_resp(self):
         resp = self.client.get("/badabing")
         self.assert404(resp)

#    def test_egg(self):
#        egg = mymodel.Egg.query.first()
#        self.assertTrue(egg.description.startswith('green'))


if __name__ == '__main__':
     unittest.main()

### manage.py ###

from flask.ext.script import Manager

from flasky import app
import fixtures as _fixtures
import mymodel


manager = Manager(app)


@manager.command
def tables():
     "Create database tables."
     mymodel.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()

any help or direction would be greatly appreciated...

Sina

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sean Lynch
Date:
2012-11-19 @ 19:24
I don't use Flask-Testing but use Flask-SQLAlchemy with fixture.  Comparing
your code to mine, one difference I see is I use Flask-SQLAlchmey's
"db.engine"

dbfixture = SQLAlchemyFixture(env=models, style=NamedDataStyle(),
engine=db.engine)


Here is also my Flask-Script commands I use (I setup a SubManager that is
attached to "python manage.py database"
https://gist.github.com/4113061


On Mon, Nov 19, 2012 at 11:53 AM, Sina K <sina@sagitar.com> wrote:

>  Hi,
>
> I tried to implement the example at https://gist.github.com/824472 and
> https://gist.github.com/4023180 (they are identical as far as I can tell)
> but I get the error below... I've been hunting for a solution but can't
> seem to come up with one. I've isolated the area of the error to be around '
> db = SQLAlchemyFixture(env=models, style=NamedDataStyle(), engine=engine)'
>
> ### The Error ###
> ERROR: test_users (__main__.TestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "flasky_test.py", line 19, in setUp
>     fixtures.install(self.app, *fixtures.all_data)
>   File "/Users/sina/PycharmProjects/flasky/fixtures.py", line 14, in
> install
>     data.setup()
>   File "/Library/Python/2.7/site-packages/fixture/base.py", line 71, in
> setup
>     self.loader.load(self.data)
>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
> line 193, in load
>     self.wrap_in_transaction(loader, unloading=False)
>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
> line 310, in wrap_in_transaction
>     routine()
>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
> line 192, in loader
>     self.load_dataset(ds)
>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
> line 235, in load_dataset
>     obj = ds.meta.storage_medium.save(row, column_vals())
>   File
> "/Library/Python/2.7/site-packages/fixture/loadable/sqlalchemy_loadable.py",
> line 233, in save
>     obj = self.medium()
> LoadError: TypeError: __init__() takes exactly 4 arguments (1 given) (with
> 'user01' of '<fixture.dataset.dataset.user01 object at 0x110784810>' in
> <UserData at 0x11074efd0 with keys ['user01']>)
>
>
> ### fixtures.py ###
>
> from fixture import DataSet, SQLAlchemyFixture
> from fixture.style import NamedDataStyle
> from flask.ext.bcrypt import Bcrypt
>
> import mymodel
>
> bcrypt = Bcrypt()
>
> def install(app, *args):
>     engine = mymodel.create_tables(app)
>     db = SQLAlchemyFixture(env=mymodel, style=NamedDataStyle(),
> engine=engine)
>     data = db.data(*args)
>     data.setup()
>     db.dispose()
>
>
> class UserData(DataSet):
>
>     class user01:
>         username = 'sina'
>         email = 'sina@email.com'
>         pwhash = bcrypt.generate_password_hash('password')
>
> # A simple trick for installing all fixtures from an external module.
> all_data = (UserData, )
>
> ### mymodel.py ###
>
> from flask.ext.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['DB_URI'])
>     db.metadata.create_all(engine)
>     return engine
>
> # models
> class User(db.Model):
>     __tablename__ = 'flasky_users'
>     id = db.Column(db.Integer, primary_key=True, unique=True,
> autoincrement=True)
>     username = db.Column(db.String(80), unique=True, nullable=False)
>     email = db.Column(db.String(120), unique=True, nullable=False)
>     pwhash = db.Column(db.String(80), nullable=False)
>
>     def __init__(self, username, email, pwhash):
>         self.username = username
>         self.email = email
>         self.pwhash = pwhash
>
>     def __repr__(self):
>         return '<User %r>' % self.username
>
> ### flasky_test.py ###
>
> import unittest
>
> from flask import Flask
> from flask.ext.testing import TestCase
>
> import fixtures
> import mymodel
>
> class TestCase(TestCase):
>
>     def create_app(self):
>         app = Flask(__name__)
>         app.config['DB_URI'] = 'mysql://flasky:czxlzq22@localhost
> /flasky_test'
>         return app
>
>     def setUp(self):
>         mymodel.create_tables(self.app)
>         fixtures.install(self.app, *fixtures.all_data)
>         self.db = mymodel.init_app(self.app)
>
>     def tearDown(self):
>         self.db.session.remove()
>         self.db.drop_all()
>
>     def assert404(self, response):
>         """
>         Checks if a HTTP 404 returned
>         e.g.
>         resp = self.client.get("/")
>         self.assert404(resp)
>         """
>         self.assertTrue(response.status_code == 404)
>
>     def test_users(self):
>         user = mymodel.User.query.first()
>         self.assertEquals(user.username, 'sina')
>
>     def test_404_resp(self):
>         resp = self.client.get("/badabing")
>         self.assert404(resp)
>
> #    def test_egg(self):
> #        egg = mymodel.Egg.query.first()
> #        self.assertTrue(egg.description.startswith('green'))
>
>
> if __name__ == '__main__':
>     unittest.main()
>
> ### manage.py ###
>
> from flask.ext.script import Manager
>
> from flasky import app
> import fixtures as _fixtures
> import mymodel
>
>
> manager = Manager(app)
>
>
> @manager.command
> def tables():
>     "Create database tables."
>     mymodel.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()
>
> any help or direction would be greatly appreciated...
>
> Sina
>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
maplye
Date:
2012-11-22 @ 01:25
Thanks Sean, according to your code, I had implemented the fixture on my
flask application.

On Tue, Nov 20, 2012 at 3:24 AM, Sean Lynch <techniq35@gmail.com> wrote:

> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture.
>  Comparing your code to mine, one difference I see is I use
> Flask-SQLAlchmey's "db.engine"
>
> dbfixture = SQLAlchemyFixture(env=models, style=NamedDataStyle(),
> engine=db.engine)
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that is
> attached to "python manage.py database"
> https://gist.github.com/4113061
>
>
> On Mon, Nov 19, 2012 at 11:53 AM, Sina K <sina@sagitar.com> wrote:
>
>>  Hi,
>>
>> I tried to implement the example at https://gist.github.com/824472 and
>> https://gist.github.com/4023180 (they are identical as far as I can
>> tell) but I get the error below... I've been hunting for a solution but
>> can't seem to come up with one. I've isolated the area of the error to be
>> around 'db = SQLAlchemyFixture(env=models, style=NamedDataStyle(), engine
>> =engine)'
>>
>> ### The Error ###
>> ERROR: test_users (__main__.TestCase)
>> ----------------------------------------------------------------------
>> Traceback (most recent call last):
>>   File "flasky_test.py", line 19, in setUp
>>     fixtures.install(self.app, *fixtures.all_data)
>>   File "/Users/sina/PycharmProjects/flasky/fixtures.py", line 14, in
>> install
>>     data.setup()
>>   File "/Library/Python/2.7/site-packages/fixture/base.py", line 71, in
>> setup
>>     self.loader.load(self.data)
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 193, in load
>>     self.wrap_in_transaction(loader, unloading=False)
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 310, in wrap_in_transaction
>>     routine()
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 192, in loader
>>     self.load_dataset(ds)
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 235, in load_dataset
>>     obj = ds.meta.storage_medium.save(row, column_vals())
>>   File
>> "/Library/Python/2.7/site-packages/fixture/loadable/sqlalchemy_loadable.py",
>> line 233, in save
>>     obj = self.medium()
>> LoadError: TypeError: __init__() takes exactly 4 arguments (1 given)
>> (with 'user01' of '<fixture.dataset.dataset.user01 object at 0x110784810>'
>> in <UserData at 0x11074efd0 with keys ['user01']>)
>>
>>
>> ### fixtures.py ###
>>
>> from fixture import DataSet, SQLAlchemyFixture
>> from fixture.style import NamedDataStyle
>> from flask.ext.bcrypt import Bcrypt
>>
>> import mymodel
>>
>> bcrypt = Bcrypt()
>>
>> def install(app, *args):
>>     engine = mymodel.create_tables(app)
>>     db = SQLAlchemyFixture(env=mymodel, style=NamedDataStyle(),
>> engine=engine)
>>     data = db.data(*args)
>>     data.setup()
>>     db.dispose()
>>
>>
>> class UserData(DataSet):
>>
>>     class user01:
>>         username = 'sina'
>>         email = 'sina@email.com'
>>         pwhash = bcrypt.generate_password_hash('password')
>>
>> # A simple trick for installing all fixtures from an external module.
>> all_data = (UserData, )
>>
>> ### mymodel.py ###
>>
>> from flask.ext.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['DB_URI'])
>>     db.metadata.create_all(engine)
>>     return engine
>>
>> # models
>> class User(db.Model):
>>     __tablename__ = 'flasky_users'
>>     id = db.Column(db.Integer, primary_key=True, unique=True,
>> autoincrement=True)
>>     username = db.Column(db.String(80), unique=True, nullable=False)
>>     email = db.Column(db.String(120), unique=True, nullable=False)
>>     pwhash = db.Column(db.String(80), nullable=False)
>>
>>     def __init__(self, username, email, pwhash):
>>         self.username = username
>>         self.email = email
>>         self.pwhash = pwhash
>>
>>     def __repr__(self):
>>         return '<User %r>' % self.username
>>
>> ### flasky_test.py ###
>>
>> import unittest
>>
>> from flask import Flask
>> from flask.ext.testing import TestCase
>>
>> import fixtures
>> import mymodel
>>
>> class TestCase(TestCase):
>>
>>     def create_app(self):
>>         app = Flask(__name__)
>>         app.config['DB_URI'] = 'mysql://flasky:czxlzq22@localhost
>> /flasky_test'
>>         return app
>>
>>     def setUp(self):
>>         mymodel.create_tables(self.app)
>>         fixtures.install(self.app, *fixtures.all_data)
>>         self.db = mymodel.init_app(self.app)
>>
>>     def tearDown(self):
>>         self.db.session.remove()
>>         self.db.drop_all()
>>
>>     def assert404(self, response):
>>         """
>>         Checks if a HTTP 404 returned
>>         e.g.
>>         resp = self.client.get("/")
>>         self.assert404(resp)
>>         """
>>         self.assertTrue(response.status_code == 404)
>>
>>     def test_users(self):
>>         user = mymodel.User.query.first()
>>         self.assertEquals(user.username, 'sina')
>>
>>     def test_404_resp(self):
>>         resp = self.client.get("/badabing")
>>         self.assert404(resp)
>>
>> #    def test_egg(self):
>> #        egg = mymodel.Egg.query.first()
>> #        self.assertTrue(egg.description.startswith('green'))
>>
>>
>> if __name__ == '__main__':
>>     unittest.main()
>>
>> ### manage.py ###
>>
>> from flask.ext.script import Manager
>>
>> from flasky import app
>> import fixtures as _fixtures
>> import mymodel
>>
>>
>> manager = Manager(app)
>>
>>
>> @manager.command
>> def tables():
>>     "Create database tables."
>>     mymodel.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()
>>
>> any help or direction would be greatly appreciated...
>>
>> Sina
>>
>>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sean Lynch
Date:
2012-11-22 @ 01:46
Glad to hear.
On Nov 21, 2012 8:27 PM, "maplye" <maplye@gmail.com> wrote:

> Thanks Sean, according to your code, I had implemented the fixture on my
> flask application.
>
> On Tue, Nov 20, 2012 at 3:24 AM, Sean Lynch <techniq35@gmail.com> wrote:
>
>> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture.
>>  Comparing your code to mine, one difference I see is I use
>> Flask-SQLAlchmey's "db.engine"
>>
>> dbfixture = SQLAlchemyFixture(env=models, style=NamedDataStyle(),
>> engine=db.engine)
>>
>>
>> Here is also my Flask-Script commands I use (I setup a SubManager that is
>> attached to "python manage.py database"
>> https://gist.github.com/4113061
>>
>>
>> On Mon, Nov 19, 2012 at 11:53 AM, Sina K <sina@sagitar.com> wrote:
>>
>>>  Hi,
>>>
>>> I tried to implement the example at https://gist.github.com/824472 and
>>> https://gist.github.com/4023180 (they are identical as far as I can
>>> tell) but I get the error below... I've been hunting for a solution but
>>> can't seem to come up with one. I've isolated the area of the error to be
>>> around 'db = SQLAlchemyFixture(env=models, style=NamedDataStyle(),
>>> engine=engine)'
>>>
>>> ### The Error ###
>>> ERROR: test_users (__main__.TestCase)
>>> ----------------------------------------------------------------------
>>> Traceback (most recent call last):
>>>   File "flasky_test.py", line 19, in setUp
>>>     fixtures.install(self.app, *fixtures.all_data)
>>>   File "/Users/sina/PycharmProjects/flasky/fixtures.py", line 14, in
>>> install
>>>     data.setup()
>>>   File "/Library/Python/2.7/site-packages/fixture/base.py", line 71, in
>>> setup
>>>     self.loader.load(self.data)
>>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>>> line 193, in load
>>>     self.wrap_in_transaction(loader, unloading=False)
>>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>>> line 310, in wrap_in_transaction
>>>     routine()
>>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>>> line 192, in loader
>>>     self.load_dataset(ds)
>>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>>> line 235, in load_dataset
>>>     obj = ds.meta.storage_medium.save(row, column_vals())
>>>   File
>>> "/Library/Python/2.7/site-packages/fixture/loadable/sqlalchemy_loadable.py",
>>> line 233, in save
>>>     obj = self.medium()
>>> LoadError: TypeError: __init__() takes exactly 4 arguments (1 given)
>>> (with 'user01' of '<fixture.dataset.dataset.user01 object at 0x110784810>'
>>> in <UserData at 0x11074efd0 with keys ['user01']>)
>>>
>>>
>>> ### fixtures.py ###
>>>
>>> from fixture import DataSet, SQLAlchemyFixture
>>> from fixture.style import NamedDataStyle
>>> from flask.ext.bcrypt import Bcrypt
>>>
>>> import mymodel
>>>
>>> bcrypt = Bcrypt()
>>>
>>> def install(app, *args):
>>>     engine = mymodel.create_tables(app)
>>>     db = SQLAlchemyFixture(env=mymodel, style=NamedDataStyle(),
>>> engine=engine)
>>>     data = db.data(*args)
>>>     data.setup()
>>>     db.dispose()
>>>
>>>
>>> class UserData(DataSet):
>>>
>>>     class user01:
>>>         username = 'sina'
>>>         email = 'sina@email.com'
>>>         pwhash = bcrypt.generate_password_hash('password')
>>>
>>> # A simple trick for installing all fixtures from an external module.
>>> all_data = (UserData, )
>>>
>>> ### mymodel.py ###
>>>
>>> from flask.ext.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['DB_URI'])
>>>     db.metadata.create_all(engine)
>>>     return engine
>>>
>>> # models
>>> class User(db.Model):
>>>     __tablename__ = 'flasky_users'
>>>     id = db.Column(db.Integer, primary_key=True, unique=True,
>>> autoincrement=True)
>>>     username = db.Column(db.String(80), unique=True, nullable=False)
>>>     email = db.Column(db.String(120), unique=True, nullable=False)
>>>     pwhash = db.Column(db.String(80), nullable=False)
>>>
>>>     def __init__(self, username, email, pwhash):
>>>         self.username = username
>>>         self.email = email
>>>         self.pwhash = pwhash
>>>
>>>     def __repr__(self):
>>>         return '<User %r>' % self.username
>>>
>>> ### flasky_test.py ###
>>>
>>> import unittest
>>>
>>> from flask import Flask
>>> from flask.ext.testing import TestCase
>>>
>>> import fixtures
>>> import mymodel
>>>
>>> class TestCase(TestCase):
>>>
>>>     def create_app(self):
>>>         app = Flask(__name__)
>>>         app.config['DB_URI'] = 'mysql://flasky:czxlzq22@localhost
>>> /flasky_test'
>>>         return app
>>>
>>>     def setUp(self):
>>>         mymodel.create_tables(self.app)
>>>         fixtures.install(self.app, *fixtures.all_data)
>>>         self.db = mymodel.init_app(self.app)
>>>
>>>     def tearDown(self):
>>>         self.db.session.remove()
>>>         self.db.drop_all()
>>>
>>>     def assert404(self, response):
>>>         """
>>>         Checks if a HTTP 404 returned
>>>         e.g.
>>>         resp = self.client.get("/")
>>>         self.assert404(resp)
>>>         """
>>>         self.assertTrue(response.status_code == 404)
>>>
>>>     def test_users(self):
>>>         user = mymodel.User.query.first()
>>>         self.assertEquals(user.username, 'sina')
>>>
>>>     def test_404_resp(self):
>>>         resp = self.client.get("/badabing")
>>>         self.assert404(resp)
>>>
>>> #    def test_egg(self):
>>> #        egg = mymodel.Egg.query.first()
>>> #        self.assertTrue(egg.description.startswith('green'))
>>>
>>>
>>> if __name__ == '__main__':
>>>     unittest.main()
>>>
>>> ### manage.py ###
>>>
>>> from flask.ext.script import Manager
>>>
>>> from flasky import app
>>> import fixtures as _fixtures
>>> import mymodel
>>>
>>>
>>> manager = Manager(app)
>>>
>>>
>>> @manager.command
>>> def tables():
>>>     "Create database tables."
>>>     mymodel.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()
>>>
>>> any help or direction would be greatly appreciated...
>>>
>>> Sina
>>>
>>>
>>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
maplye
Date:
2012-11-20 @ 00:01
according your page, I got the errors:

    orm.object_session(model)._model_changes[pk] = (model, operation)
AttributeError: 'SessionMaker' object has no attribute '_model_changes'


On Tue, Nov 20, 2012 at 3:24 AM, Sean Lynch <techniq35@gmail.com> wrote:

> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture.
>  Comparing your code to mine, one difference I see is I use
> Flask-SQLAlchmey's "db.engine"
>
> dbfixture = SQLAlchemyFixture(env=models, style=NamedDataStyle(),
> engine=db.engine)
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that is
> attached to "python manage.py database"
> https://gist.github.com/4113061
>
>
> On Mon, Nov 19, 2012 at 11:53 AM, Sina K <sina@sagitar.com> wrote:
>
>>  Hi,
>>
>> I tried to implement the example at https://gist.github.com/824472 and
>> https://gist.github.com/4023180 (they are identical as far as I can
>> tell) but I get the error below... I've been hunting for a solution but
>> can't seem to come up with one. I've isolated the area of the error to be
>> around 'db = SQLAlchemyFixture(env=models, style=NamedDataStyle(), engine
>> =engine)'
>>
>> ### The Error ###
>> ERROR: test_users (__main__.TestCase)
>> ----------------------------------------------------------------------
>> Traceback (most recent call last):
>>   File "flasky_test.py", line 19, in setUp
>>     fixtures.install(self.app, *fixtures.all_data)
>>   File "/Users/sina/PycharmProjects/flasky/fixtures.py", line 14, in
>> install
>>     data.setup()
>>   File "/Library/Python/2.7/site-packages/fixture/base.py", line 71, in
>> setup
>>     self.loader.load(self.data)
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 193, in load
>>     self.wrap_in_transaction(loader, unloading=False)
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 310, in wrap_in_transaction
>>     routine()
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 192, in loader
>>     self.load_dataset(ds)
>>   File "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py",
>> line 235, in load_dataset
>>     obj = ds.meta.storage_medium.save(row, column_vals())
>>   File
>> "/Library/Python/2.7/site-packages/fixture/loadable/sqlalchemy_loadable.py",
>> line 233, in save
>>     obj = self.medium()
>> LoadError: TypeError: __init__() takes exactly 4 arguments (1 given)
>> (with 'user01' of '<fixture.dataset.dataset.user01 object at 0x110784810>'
>> in <UserData at 0x11074efd0 with keys ['user01']>)
>>
>>
>> ### fixtures.py ###
>>
>> from fixture import DataSet, SQLAlchemyFixture
>> from fixture.style import NamedDataStyle
>> from flask.ext.bcrypt import Bcrypt
>>
>> import mymodel
>>
>> bcrypt = Bcrypt()
>>
>> def install(app, *args):
>>     engine = mymodel.create_tables(app)
>>     db = SQLAlchemyFixture(env=mymodel, style=NamedDataStyle(),
>> engine=engine)
>>     data = db.data(*args)
>>     data.setup()
>>     db.dispose()
>>
>>
>> class UserData(DataSet):
>>
>>     class user01:
>>         username = 'sina'
>>         email = 'sina@email.com'
>>         pwhash = bcrypt.generate_password_hash('password')
>>
>> # A simple trick for installing all fixtures from an external module.
>> all_data = (UserData, )
>>
>> ### mymodel.py ###
>>
>> from flask.ext.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['DB_URI'])
>>     db.metadata.create_all(engine)
>>     return engine
>>
>> # models
>> class User(db.Model):
>>     __tablename__ = 'flasky_users'
>>     id = db.Column(db.Integer, primary_key=True, unique=True,
>> autoincrement=True)
>>     username = db.Column(db.String(80), unique=True, nullable=False)
>>     email = db.Column(db.String(120), unique=True, nullable=False)
>>     pwhash = db.Column(db.String(80), nullable=False)
>>
>>     def __init__(self, username, email, pwhash):
>>         self.username = username
>>         self.email = email
>>         self.pwhash = pwhash
>>
>>     def __repr__(self):
>>         return '<User %r>' % self.username
>>
>> ### flasky_test.py ###
>>
>> import unittest
>>
>> from flask import Flask
>> from flask.ext.testing import TestCase
>>
>> import fixtures
>> import mymodel
>>
>> class TestCase(TestCase):
>>
>>     def create_app(self):
>>         app = Flask(__name__)
>>         app.config['DB_URI'] = 'mysql://flasky:czxlzq22@localhost
>> /flasky_test'
>>         return app
>>
>>     def setUp(self):
>>         mymodel.create_tables(self.app)
>>         fixtures.install(self.app, *fixtures.all_data)
>>         self.db = mymodel.init_app(self.app)
>>
>>     def tearDown(self):
>>         self.db.session.remove()
>>         self.db.drop_all()
>>
>>     def assert404(self, response):
>>         """
>>         Checks if a HTTP 404 returned
>>         e.g.
>>         resp = self.client.get("/")
>>         self.assert404(resp)
>>         """
>>         self.assertTrue(response.status_code == 404)
>>
>>     def test_users(self):
>>         user = mymodel.User.query.first()
>>         self.assertEquals(user.username, 'sina')
>>
>>     def test_404_resp(self):
>>         resp = self.client.get("/badabing")
>>         self.assert404(resp)
>>
>> #    def test_egg(self):
>> #        egg = mymodel.Egg.query.first()
>> #        self.assertTrue(egg.description.startswith('green'))
>>
>>
>> if __name__ == '__main__':
>>     unittest.main()
>>
>> ### manage.py ###
>>
>> from flask.ext.script import Manager
>>
>> from flasky import app
>> import fixtures as _fixtures
>> import mymodel
>>
>>
>> manager = Manager(app)
>>
>>
>> @manager.command
>> def tables():
>>     "Create database tables."
>>     mymodel.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()
>>
>> any help or direction would be greatly appreciated...
>>
>> Sina
>>
>>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-20 @ 00:39
so did:


http://flask.pocoo.org/mailinglist/archive/2011/3/6/flask-sqlalchemy-and-fixtures/



does not look like he found a resolution... I guess I will have to find 
another way to do testing in re to model...

> maplye <mailto:maplye@gmail.com>
> November 20, 2012 1:01 AM
> according your page, I got the errors:
>
>     orm.object_session(model)._model_changes[pk] = (model, operation)
> AttributeError: 'SessionMaker' object has no attribute '_model_changes'
>
>
>
> Sean Lynch <mailto:techniq35@gmail.com>
> November 19, 2012 8:24 PM
> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture. 
>  Comparing your code to mine, one difference I see is I use 
> Flask-SQLAlchmey's "db.engine"
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that 
> is attached to "python manage.py database"
> https://gist.github.com/4113061
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sean Lynch
Date:
2012-11-20 @ 01:18
could not decode message

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Brice Leroy
Date:
2012-11-20 @ 01:21
could not decode message

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-20 @ 13:49
hello Brice,

Do you mind sharing your create_app(config='config_test')?

Thanks,
Sina

> Brice Leroy <mailto:bbrriiccee@gmail.com>
> November 20, 2012 2:21 AM
> Here is the way I handle Fixture within unittest. I use flask-unitest 
> but I subclass it to add the auto loading fixture to it. Also setUp 
> and tearDown are used there for data purpose, I thought that calling 
> set_up and tear_down more elegant (and PEP8 compliant) so use those 
> function in my unittest. I keep my fixture with my tests script. They 
> live in a tests directory in my blueprint directory 
> (/app/identity/tests/fixtures.py ... )
>
> https://gist.github.com/4115311
>
> I hope it helps.
>
>
>
>
>
> -- 
> blog: http://www.debrice.com
> Time tracking tool: http://alpha.kaaloo.com
> Django site generator: http://alpha.djangogenerator.com
> linkedin: http://www.linkedin.com/in/bricepleroy
>
> Sina K <mailto:sina@sagitar.com>
> November 20, 2012 1:39 AM
> so did:
>
> 
http://flask.pocoo.org/mailinglist/archive/2011/3/6/flask-sqlalchemy-and-fixtures/

>
>
> does not look like he found a resolution... I guess I will have to 
> find another way to do testing in re to model...
>
> Sina K <mailto:sina@sagitar.com>
> November 20, 2012 1:38 AM
> so did:
>
> 
http://flask.pocoo.org/mailinglist/archive/2011/3/6/flask-sqlalchemy-and-fixtures/

>
>
> does not look like he found a resolution... I guess I will have to 
> find another way to do testing in re to model...
>
> maplye <mailto:maplye@gmail.com>
> November 20, 2012 1:01 AM
> according your page, I got the errors:
>
>     orm.object_session(model)._model_changes[pk] = (model, operation)
> AttributeError: 'SessionMaker' object has no attribute '_model_changes'
>
>
>
> Sean Lynch <mailto:techniq35@gmail.com>
> November 19, 2012 8:24 PM
> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture. 
>  Comparing your code to mine, one difference I see is I use 
> Flask-SQLAlchmey's "db.engine"
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that 
> is attached to "python manage.py database"
> https://gist.github.com/4113061
>
>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Brice Leroy
Date:
2012-11-20 @ 15:20
could not decode message

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-21 @ 01:21
Hi Brice,

I have a working app and a somewhat working test. This last part has 
baffled me for the whole day:

Failure
Traceback (most recent call last):
   File "/Users/sina/PycharmProjects/flasky/flasky_test.py", line 28, in 
test_emailRegister
     self.assertStatus(response, 301)
   File "/Library/Python/2.7/site-packages/flask_testing.py", line 181, 
in assertStatus
     self.assertEqual(response.status_code, status_code)
AssertionError: 404 != 301

for some reason, it is returning a 404 but I know it is working because 
the data is showing up in the db. the code in the app that is executed 
by the test is:

def signup():
     form = signUpForm()
     if form.validate_on_submit():
         user = get_user_by_email(form.email.data)
         if user is None:
             # username does not exist so register the user
             register_user(form)
             flash(u'You\'ve successfully signed up, please check your 
email to activate your account', category=u'success')
             # TODO: Send confirmation email
             return redirect(url_for('dashbard'), 301)
         else:
             flash(u'An account already exists with this email address. 
<a href=\''+url_for('forgot')+u'\'>Recover Password</a>', category=u'info')
     return render_template('signup.html', nav_bar=header_menu, form=form)

the test itself  is a near duplicate of your own:

     def test_emailRegister(self):
         response = self.client.post(url_for("signup"),
             data=self.reg_data)
         self.assertStatus(response, 301)
         assert User.query.filter_by(email=self.reg_data["email"]).first()

Thoughts?

Thanks in advance,
Sina
> Brice Leroy <mailto:bbrriiccee@gmail.com>
> November 20, 2012 4:20 PM
> Hi Sina,
>
> I updated the gist with my test config and the create app. If it can 
> help, here is my directory structure:
>
> /config.py # main config
> /config_test.py # config loaded when unittest on
> /manage.py # using flask script
> /app/__init__.py (contains create_app())
> /app/identities/(__init__.py|views.py|forms.py|models.py...)
> /app/identities/tests/fixtures.py
> /app/identities/tests/test_auth.py
>
>
> I use discover to run all my test at once. when I want to run just one 
> test:
>
>
> # run all the tests in TestAuth
> python -m unittest app.identities.tests.test_auth.TestAuth
>
> # run one test in TestAuth
> python -m unittest 
> app.identities.tests.test_auth.TestAuth.test_registerExistingEmail
>
>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Brice Leroy
Date:
2012-11-21 @ 01:55
could not decode message

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-21 @ 02:22
No worries. Just as a test, I made a simple test for a static page and 
got the same 404 error - must be something I have configured wrong... I 
guess it's what I get for trying to roll my own instead of using the 
"swiss army django" solution...

     def test_staticPages(self):
         response = self.client.get('/')
         self.assertStatus(response, 200)

"There's your problem, someone set this thing to Evil"
-- The Simpsons Krusty Doll Repair Man
> Brice Leroy <mailto:bbrriiccee@gmail.com>
> November 21, 2012 2:55 AM
> This is all guess since I can't see much.
>
> Check your dashbard (I guess dashboard) method, you might have 
> something like
>
> user = User.query.get_or_404(g.user_id) or something like that. That 
> could be the reason why you get a 404 since you seems to be 
> redirecting the user to a "login only" page and I don't see any code 
> really "logging-in" the user (like storing the user_id in an encrypted 
> session)
>
> If this is the case I'd recommend you to use a decorator (like 
> login_required) that will redirect the user to the login page. Check 
> out http://packages.python.org/Flask-Login/ ;-)
>
>
>
>
>
> -- 
> blog: http://www.debrice.com
> Time tracking tool: http://alpha.kaaloo.com
> Django site generator: http://alpha.djangogenerator.com
> linkedin: http://www.linkedin.com/in/bricepleroy
>
> Sina K <mailto:sina@sagitar.com>
> November 21, 2012 2:21 AM
> Hi Brice,
>
> I have a working app and a somewhat working test. This last part has 
> baffled me for the whole day:
>
> Failure
> Traceback (most recent call last):
>   File "/Users/sina/PycharmProjects/flasky/flasky_test.py", line 28, 
> in test_emailRegister
>     self.assertStatus(response, 301)
>   File "/Library/Python/2.7/site-packages/flask_testing.py", line 181, 
> in assertStatus
>     self.assertEqual(response.status_code, status_code)
> AssertionError: 404 != 301
>
> for some reason, it is returning a 404 but I know it is working 
> because the data is showing up in the db. the code in the app that is 
> executed by the test is:
>
> def signup():
>     form = signUpForm()
>     if form.validate_on_submit():
>         user = get_user_by_email(form.email.data)
>         if user is None:
>             # username does not exist so register the user
>             register_user(form)
>             flash(u'You\'ve successfully signed up, please check your 
> email to activate your account', category=u'success')
>             # TODO: Send confirmation email
>             return redirect(url_for('dashbard'), 301)
>         else:
>             flash(u'An account already exists with this email address. 
> <a href=\''+url_for('forgot')+u'\'>Recover Password</a>', 
> category=u'info')
>     return render_template('signup.html', nav_bar=header_menu, form=form)
>
> the test itself  is a near duplicate of your own:
>
>     def test_emailRegister(self):
>         response = self.client.post(url_for("signup"),
>             data=self.reg_data)
>         self.assertStatus(response, 301)
>         assert User.query.filter_by(email=self.reg_data["email"]).first()
>
> Thoughts?
>
> Thanks in advance,
> Sina
> Brice Leroy <mailto:bbrriiccee@gmail.com>
> November 20, 2012 4:20 PM
> Hi Sina,
>
> I updated the gist with my test config and the create app. If it can 
> help, here is my directory structure:
>
> /config.py # main config
> /config_test.py # config loaded when unittest on
> /manage.py # using flask script
> /app/__init__.py (contains create_app())
> /app/identities/(__init__.py|views.py|forms.py|models.py...)
> /app/identities/tests/fixtures.py
> /app/identities/tests/test_auth.py
>
>
> I use discover to run all my test at once. when I want to run just one 
> test:
>
>
> # run all the tests in TestAuth
> python -m unittest app.identities.tests.test_auth.TestAuth
>
> # run one test in TestAuth
> python -m unittest 
> app.identities.tests.test_auth.TestAuth.test_registerExistingEmail
>
>
>
>
>
> Sina K <mailto:sina@sagitar.com>
> November 20, 2012 2:49 PM
> hello Brice,
>
> Do you mind sharing your create_app(config='config_test')?
>
> Thanks,
> Sina
>
> Brice Leroy <mailto:bbrriiccee@gmail.com>
> November 20, 2012 2:21 AM
> Here is the way I handle Fixture within unittest. I use flask-unitest 
> but I subclass it to add the auto loading fixture to it. Also setUp 
> and tearDown are used there for data purpose, I thought that calling 
> set_up and tear_down more elegant (and PEP8 compliant) so use those 
> function in my unittest. I keep my fixture with my tests script. They 
> live in a tests directory in my blueprint directory 
> (/app/identity/tests/fixtures.py ... )
>
> https://gist.github.com/4115311
>
> I hope it helps.
>
>
>
>
>
> -- 
> blog: http://www.debrice.com
> Time tracking tool: http://alpha.kaaloo.com
> Django site generator: http://alpha.djangogenerator.com
> linkedin: http://www.linkedin.com/in/bricepleroy
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Brice Leroy
Date:
2012-11-21 @ 03:28
could not decode message

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-20 @ 11:39
Hi Brice,

Was very helpful. Thank you. I am going to try and reverse engineer what 
I think your create app does:

from what I gather it basically gets all the environment vars based on 
the config param passed to it - in this case "config_test" otherwise it 
likely loads "config"

going to give it a shot and see how it works out but so far so good.

Thanks again...

Sina

> Brice Leroy <mailto:bbrriiccee@gmail.com>
> November 20, 2012 2:21 AM
> Here is the way I handle Fixture within unittest. I use flask-unitest 
> but I subclass it to add the auto loading fixture to it. Also setUp 
> and tearDown are used there for data purpose, I thought that calling 
> set_up and tear_down more elegant (and PEP8 compliant) so use those 
> function in my unittest. I keep my fixture with my tests script. They 
> live in a tests directory in my blueprint directory 
> (/app/identity/tests/fixtures.py ... )
>
> https://gist.github.com/4115311
>
> I hope it helps.
>
>
>
>
>
> -- 
> blog: http://www.debrice.com
> Time tracking tool: http://alpha.kaaloo.com
> Django site generator: http://alpha.djangogenerator.com
> linkedin: http://www.linkedin.com/in/bricepleroy
>
> Sina K <mailto:sina@sagitar.com>
> November 20, 2012 1:39 AM
> so did:
>
> 
http://flask.pocoo.org/mailinglist/archive/2011/3/6/flask-sqlalchemy-and-fixtures/

>
>
> does not look like he found a resolution... I guess I will have to 
> find another way to do testing in re to model...
>
> Sina K <mailto:sina@sagitar.com>
> November 20, 2012 1:38 AM
> so did:
>
> 
http://flask.pocoo.org/mailinglist/archive/2011/3/6/flask-sqlalchemy-and-fixtures/

>
>
> does not look like he found a resolution... I guess I will have to 
> find another way to do testing in re to model...
>
> maplye <mailto:maplye@gmail.com>
> November 20, 2012 1:01 AM
> according your page, I got the errors:
>
>     orm.object_session(model)._model_changes[pk] = (model, operation)
> AttributeError: 'SessionMaker' object has no attribute '_model_changes'
>
>
>
> Sean Lynch <mailto:techniq35@gmail.com>
> November 19, 2012 8:24 PM
> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture. 
>  Comparing your code to mine, one difference I see is I use 
> Flask-SQLAlchmey's "db.engine"
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that 
> is attached to "python manage.py database"
> https://gist.github.com/4113061
>
>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-19 @ 20:25
Hi Sean,

Thanks for the example but I am not sure how it would fit into what I am 
doing. The main issue I seem to be having is with creating the 
connection. I looked through your code, but I did not see anything 
related to db.engine - is it in your fixtures file? this looks like a 
manager file to me. If you don't mind elaborating a bit more, I would 
appreciate it - I will admit to being somewhat new to python and flask 
(I am a php escapee - after over a decade)

Sina
> Sean Lynch <mailto:techniq35@gmail.com>
> November 19, 2012 8:24 PM
> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture. 
>  Comparing your code to mine, one difference I see is I use 
> Flask-SQLAlchmey's "db.engine"
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that 
> is attached to "python manage.py database"
> https://gist.github.com/4113061
>
>
>
>

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sean Lynch
Date:
2012-11-19 @ 20:49
could not decode message

Re: [flask] use of fixture with Flask-SQLAlchemy and Flask-Testing - need some help

From:
Sina K
Date:
2012-11-19 @ 21:08
Thanks Sean... I will try to adapt it to my side and see if I can work 
it out...

> Sean Lynch <mailto:techniq35@gmail.com>
> November 19, 2012 9:49 PM
> Sorry, it was a complete example.  All the relavant code is spread 
> across a few files with a directory hierarchy that I can't reproduce 
> using a Gist.  I updated the example a little to hopefully show more.
>
> Also, "db" is an instance of the Flask-SQLAlchemy extension, e.g. "db 
> = SQLAlchemy()", so db.engine is provided by the extension.  The full 
> code is in a private Github repo currently so I would provide a full 
> working example.
>
>
>
> Sina K <mailto:sina@sagitar.com>
> November 19, 2012 9:25 PM
> Hi Sean,
>
> Thanks for the example but I am not sure how it would fit into what I 
> am doing. The main issue I seem to be having is with creating the 
> connection. I looked through your code, but I did not see anything 
> related to db.engine - is it in your fixtures file? this looks like a 
> manager file to me. If you don't mind elaborating a bit more, I would 
> appreciate it - I will admit to being somewhat new to python and flask 
> (I am a php escapee - after over a decade)
>
> Sina
> Sina K <mailto:sina@sagitar.com>
> November 19, 2012 9:15 PM
> Hi Sean,
>
> Thanks for the example but I am not sure how it would fit into what I 
> am doing. The main issue I seem to be having is with creating the 
> connection. I looked through your code, but I did not see anything 
> related to db.engine - is it in your fixtures file? this looks like a 
> manager file to me. If you don't mind elaborating a bit more, I would 
> appreciate it - I will admit to being somewhat new to python and flask 
> (I am a php escapee - after over a decade)
>
> Sina
> Sean Lynch <mailto:techniq35@gmail.com>
> November 19, 2012 8:24 PM
> I don't use Flask-Testing but use Flask-SQLAlchemy with fixture. 
>  Comparing your code to mine, one difference I see is I use 
> Flask-SQLAlchmey's "db.engine"
>
>
> Here is also my Flask-Script commands I use (I setup a SubManager that 
> is attached to "python manage.py database"
> https://gist.github.com/4113061
>
>
>
> Sina K <mailto:sina@sagitar.com>
> November 19, 2012 5:53 PM
> Hi,
>
> I tried to implement the example at https://gist.github.com/824472 and 
> https://gist.github.com/4023180 (they are identical as far as I can 
> tell) but I get the error below... I've been hunting for a solution 
> but can't seem to come up with one. I've isolated the area of the 
> error to be around 
> 'db=SQLAlchemyFixture(env=models,style=NamedDataStyle(),engine=engine)'
>
> ### The Error ###
> ERROR: test_users (__main__.TestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
>   File "flasky_test.py", line 19, in setUp
>     fixtures.install(self.app, *fixtures.all_data)
>   File "/Users/sina/PycharmProjects/flasky/fixtures.py", line 14, in 
> install
>     data.setup()
>   File "/Library/Python/2.7/site-packages/fixture/base.py", line 71, 
> in setup
>     self.loader.load(self.data)
>   File 
> "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
> 193, in load
>     self.wrap_in_transaction(loader, unloading=False)
>   File 
> "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
> 310, in wrap_in_transaction
>     routine()
>   File 
> "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
> 192, in loader
>     self.load_dataset(ds)
>   File 
> "/Library/Python/2.7/site-packages/fixture/loadable/loadable.py", line 
> 235, in load_dataset
>     obj = ds.meta.storage_medium.save(row, column_vals())
>   File 
> "/Library/Python/2.7/site-packages/fixture/loadable/sqlalchemy_loadable.py", 
> line 233, in save
>     obj = self.medium()
> LoadError: TypeError: __init__() takes exactly 4 arguments (1 given) 
> (with 'user01' of '<fixture.dataset.dataset.user01 object at 
> 0x110784810>' in <UserData at 0x11074efd0 with keys ['user01']>)
>
>
> ### fixtures.py ###
>
> from fixture import DataSet, SQLAlchemyFixture
> from fixture.style import NamedDataStyle
> from flask.ext.bcrypt import Bcrypt
>
> import mymodel
>
> bcrypt = Bcrypt()
>
> def install(app, *args):
>     engine = mymodel.create_tables(app)
>     db = SQLAlchemyFixture(env=mymodel, style=NamedDataStyle(), 
> engine=engine)
>     data = db.data(*args)
>     data.setup()
>     db.dispose()
>
>
> class UserData(DataSet):
>
>     class user01:
>         username = 'sina'
>         email = 'sina@email.com'
>         pwhash = bcrypt.generate_password_hash('password')
>
> # A simple trick for installing all fixtures from an external module.
> all_data = (UserData, )
>
> ### mymodel.py ###
>
> from flask.ext.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['DB_URI'])
>     db.metadata.create_all(engine)
>     return engine
>
> # models
> class User(db.Model):
>     __tablename__ = 'flasky_users'
>     id = db.Column(db.Integer, primary_key=True, unique=True, 
> autoincrement=True)
>     username = db.Column(db.String(80), unique=True, nullable=False)
>     email = db.Column(db.String(120), unique=True, nullable=False)
>     pwhash = db.Column(db.String(80), nullable=False)
>
>     def __init__(self, username, email, pwhash):
>         self.username = username
>         self.email = email
>         self.pwhash = pwhash
>
>     def __repr__(self):
>         return '<User %r>' % self.username
>
> ### flasky_test.py ###
>
> import unittest
>
> from flask import Flask
> from flask.ext.testing import TestCase
>
> import fixtures
> import mymodel
>
> class TestCase(TestCase):
>
>     def create_app(self):
>         app = Flask(__name__)
>         app.config['DB_URI'] = 
> 'mysql://flasky:czxlzq22@localhost/flasky_test'
>         return app
>
>     def setUp(self):
>         mymodel.create_tables(self.app)
>         fixtures.install(self.app, *fixtures.all_data)
>         self.db = mymodel.init_app(self.app)
>
>     def tearDown(self):
>         self.db.session.remove()
>         self.db.drop_all()
>
>     def assert404(self, response):
>         """
>         Checks if a HTTP 404 returned
>         e.g.
>         resp = self.client.get("/")
>         self.assert404(resp)
>         """
>         self.assertTrue(response.status_code == 404)
>
>     def test_users(self):
>         user = mymodel.User.query.first()
>         self.assertEquals(user.username, 'sina')
>
>     def test_404_resp(self):
>         resp = self.client.get("/badabing")
>         self.assert404(resp)
>
> #    def test_egg(self):
> #        egg = mymodel.Egg.query.first()
> #        self.assertTrue(egg.description.startswith('green'))
>
>
> if __name__ == '__main__':
>     unittest.main()
>
> ### manage.py ###
>
> from flask.ext.script import Manager
>
> from flasky import app
> import fixtures as _fixtures
> import mymodel
>
>
> manager = Manager(app)
>
>
> @manager.command
> def tables():
>     "Create database tables."
>     mymodel.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()
>
> any help or direction would be greatly appreciated...
>
> Sina
>