question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

db_session needed?

See original GitHub issue

A usage question / request for clarification in the docs, but does the test need to use the db_session fixture, or is there a way to shim all usages of the session throughout the code? That way I can make sure to rollback the transaction anywhere there is an exception.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jeancochranecommented, Aug 7, 2018

That’s one way to do it! Although then all of your tests will load the fixtures, whether or not they touch the database – since the transactional fixtures are function-scoped and require DB interaction, that might mean an unnecessary performance hit to your tests. So far I’ve always preferred listing the db_session dependency explicitly, although it can definitely feel overly verbose.

There had been some talk about introducing a simple mark like @pytest.mark.transactional that would load the shims in a test without explicitly requiring db_session. (In a test implementation I did a few months back, this basically meant that db_session would be automatically required as a dependency but not loaded into the body of the test – so functionally identical to listing it in the parameters.) We decided against it because it seemed to not add value and was just another thing to document. For your use-case, would that be a more appealing solution than using either usefixtures/autouse or requiring it explicitly?

0reactions
jeancochranecommented, Feb 14, 2019

Thanks for the feedback @mbalaa-spring! Just added two new sections to the docs in https://github.com/jeancochrane/pytest-flask-sqlalchemy/commit/7e1e9e496f04cda635a8166abfc58fd821de2661, one to help clarify that a transactional fixture (like db_session) is required in a test signature order to enable transactions in that test, and one to suggest a solution for automatically enabling the fixture for all tests using pytest’s autouse fixture argument. For now I’m not planning on adding a feature to automatically enable transactions for all tests, mostly because the autouse workaround documented in https://github.com/jeancochrane/pytest-flask-sqlalchemy/commit/7e1e9e496f04cda635a8166abfc58fd821de2661 feels just as straightforward as any additional feature I could build on top of it. But if you have suggestions for a simple way to incorporate it into the plugin I’d be happy to review a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Session - SQLAlchemy 1.4 Documentation
Using the Session¶. The declarative base and ORM mapping functions described at ORM Mapped Class Configuration are the primary configurational interface for ......
Read more >
What is the difference between Session and db.session in ...
The Session can be established as the request begins, or using a lazy initialization pattern which establishes one as soon as it is...
Read more >
Everything you need to know about SQLAlchemy - 4Geeks
You can just say: db.session.commit() and all the things you have done in your code will be translated into SQL language code.
Read more >
Flask Application Context - Flask-SQLAlchemy
An active Flask application context is required to make queries and to access db.engine and db.session . This is because the session is...
Read more >
Database and SQL — Websauna Documentation
No software installation needed ... Request.dbsession attribute: ... request.dbsession transaction is bound to HTTP request lifecycle.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found