croach / Flask-Fixtures

A simple library for adding database fixtures for unit tests using nothing but JSON or YAML.
MIT License
63 stars 30 forks source link

Application not registered on db instance and no application bound to current context #22

Closed ornj closed 8 years ago

ornj commented 8 years ago

setup fails when SQLAlchemy.init_app() is used to initialize the application for the database.

Example:

Works

app = Flask(__name__)
db = SQLAlchemy(app)
FixturesMixin.init_app(app, db)

Doesn't work

app = Flask(__name__)
db = SQLAlchemy()
db.init_app(app)
FixturesMixin.init_app(app, db)

This creates problems when creating the application through a factory function.

yaoelvon commented 8 years ago

I encountered the same problem. How do you deal with this problem?

ornj commented 8 years ago

Sorry I don't have a good answer for you. To get around it temporarily I had a janky, temporary work around but ultimately I didn't use this in my project.

croach commented 8 years ago

Hello, I've just added a fix for this issue. I'm now checking for a request context, and if one does not exist, I'm creating a new test request context with the given Flask app. This should allow tests to work regardless of whether the app was passed into the SQLAlchemy constructor or the init_app function was called instead. Give it a try, and let me know if this change doesn't fix your problem.

yaoelvon commented 8 years ago

Thanks croach, this change fix my problem.Can you release a new version?I want to use this in my project.

croach commented 8 years ago

Sorry about that, completely forgot to upload the latest release to PyPI. Anyway, I've made the push now and version 0.3.6 is live, so you should be able to update to the latest version to fix your problem(s).

yaoelvon commented 8 years ago

Thanks, I can pip install flask-fixtures==0.3.6 and test it ok. But maybe you forget add new tag 0.3.6 at github.

croach commented 8 years ago

Ok, so I pushed the tag for 0.3.6, and I also made a new update and pushed that to this repo and to PyPI. The new change makes the framework even less dependent on the application object. There is a slight change in how you use the framework---you now no longer call init_app, but instead you specify the app and db in class variables. This gives you the ability to test multiple applications by simply subclassing a test class and replacing the app object. If you'd prefer to use this version instead, just upgrade to v0.3.7.