Integration sample with SqlAlchemy ?
See original GitHub issueHi how would an integration with sqlalchemy works here ??, especially with the scoped_session
I currently have this conftest.py
setup on my flask project:
@pytest.fixture(scope='session')
def app(request):
"""Session-wide test `Flask` application."""
app = create_app('testing')
# Establish an application context before running the tests.
ctx = app.app_context()
ctx.push()
def teardown():
ctx.pop()
request.addfinalizer(teardown)
return app
@pytest.fixture(scope='session')
def db(app, request):
"""Session-wide test database."""
def teardown():
_db.drop_all()
_db.app = app
_db.create_all()
request.addfinalizer(teardown)
return _db
@pytest.fixture(scope='function')
def session(db, request):
"""Creates a new database session for a test."""
# connect to the database
connection = db.engine.connect()
# begin a non-ORM transaction
transaction = connection.begin()
# bind an individual session to the connection
options = dict(bind=connection, binds={})
session = db.create_scoped_session(options=options)
# overload the default session with the session above
db.session = session
def teardown():
session.close()
# rollback - everything that happened with the
# session above (including calls to commit())
# is rolled back.
transaction.rollback()
# return connection to the Engine
connection.close()
request.addfinalizer(teardown)
return session
# basically instead of creating and dropping the database for each test
# i just rollback the transaction.
# this setup works perfectly fine without using factory boy
I’ve been searching around the internet on how to make Factory Boy use the same session fixture that pytest use. And then stumble upon this library, but upon looking into the examples, I cant figure how I or this library would do the same.
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (6 by maintainers)
Top Results From Across the Web
Object Relational Tutorial (1.x API)
The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes ( ...
Read more >Flask Database Integration with SQLAlchemy - Section.io
In this article we will understand how to work with SQLAlchemy in a Flask web application. Storing data is an integral component of...
Read more >SQLAlchemy Integration — spyne 2.11.0 documentation
In this tutorial, we talk about using Spyne tools that make it easy to deal with database-related operations using SQLAlchemy. SQLAlchemy is a...
Read more >SQLAlchemy ORM Tutorial for Python Developers - Auth0
SQLAlchemy is a library that facilitates the communication between Python programs and databases. Most of the times, this library is used as an ......
Read more >How to Use Flask-SQLAlchemy to Interact with Databases in a ...
For example, if you modify your model by adding a new column, and run the db.create_all() function, the change you make to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is what ended up working for me with pytest-flask-sqlalchemy:
I suggest adding a new subclass of SQLAlchemyModelFactory that contains this setting and plugs automatically into pytest-flask-sqlalchemy without any extra configuration.
hello @olegpidsadnyi , sorry took me a while to respond, i was hooked up in my day job. But for what it’s worth.
The primary reason behind the whole
session
fixture is for me to be able to rollback my transaction (even forsession.commit()
) for every test and not needing to re-create and drop the whole database every test. For that i followed alex’s blog post, but as mentioned in his blog, it has some caveats, for Flask-SQLAlchemy v2.0 has some issues with it’sSignallingSession
, see this stackoverflow post and pull request 168. To resolve this problem I did alex’s suggestion and sub-classed theSignallingSession
, see code below:and then I reference this in my app’s
__init__.py
Although this whole
SignallingSession
problem has been fixed already with flask-sqlalchemy/pull/249 , it wont probably be released yet untill the whole 2.1 milestone has been completed.I tried your first recommendation, but using
pytest.set_trace()
, I can see that thesession
fixture uses a different sqlachemy session instance as with the one factory boy uses (i checked usingmodel_factory._meta.sqlalchemy_session
).I also tried your second recommendation, but I always encounter an error with using outside the application/request context. Probably a problem on where I placing the
get_session
function.But nevertheless, somehow I was able to make factory boy use the same session instance with my
session
fixture by encapsulating each factory class inside a pytest fixture which injects mysession
fixture. see example below:Don’t know though if this is a good solution.
EDIT: I retried your first recommendation, I think problem lies on how or on when I import factories, If I import the factories inside the test function itself, it will have the same
session
instance.of course to avoid having to do imports inside the test function, I made a fixture which imports the whole factories modules itself and return it.
EDIT: Looks like the above import strategy will only be true for the first time or test. I did some re-reading on a few blogs and stack posts, and I ended up with the second recommendation on this stack post, I probably didn’t pay attention to it that much before because I was using in-memory or sqlite database for the tests.
then i just declare and import my factories as just with the documentation.