admin管理员组文章数量:1306179
So i'm building a FastAPI application whose tests are written in Pytest. Because i have some large models, it is unpracticable for me to define different data manually each time i have to query or do something that needs data saved in the database. To tackle that need, i 'm using Factory Boy.
Ok, my database is PostgreSQL and i'm using SQLModel (SQLAlchemy wrapper with typing). On Factory Boy documentation they say that they have specific classes for some of the most used ORMs, and i used the SQLAlchemy one.
The problem: I noticed that the data were not saved to the database even inside one single test function. Because of that, the API was returning an empty list everytime. How do i enable the persistence, what is causing this problem?
So i'm building a FastAPI application whose tests are written in Pytest. Because i have some large models, it is unpracticable for me to define different data manually each time i have to query or do something that needs data saved in the database. To tackle that need, i 'm using Factory Boy.
Ok, my database is PostgreSQL and i'm using SQLModel (SQLAlchemy wrapper with typing). On Factory Boy documentation they say that they have specific classes for some of the most used ORMs, and i used the SQLAlchemy one.
The problem: I noticed that the data were not saved to the database even inside one single test function. Because of that, the API was returning an empty list everytime. How do i enable the persistence, what is causing this problem?
Share Improve this question asked Feb 3 at 14:23 João Pedro Martins de PaulaJoão Pedro Martins de Paula 12111 bronze badges1 Answer
Reset to default 1So by looking a bit more patiently in the documentation, i noticed this section:
sqlalchemy_session_persistence
Control the action taken by sqlalchemy_session at the end of a create call.
Valid values are:
None: do nothing
'flush': perform a session flush()
'commit': perform a session commit()
The default value is None.
Why the hell the default option is 'None'? I don't know. But just by setting it manually to 'commit' the data started being saved on database.
本文标签: Pytests using SQLAlchemyFactory Boy are not persisting data on databaseStack Overflow
版权声明:本文标题:Pytests using SQLAlchemy + Factory Boy are not persisting data on database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741815805a2399070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论