最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pytests using SQLAlchemy + Factory Boy are not persisting data on database - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

So 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.

发布评论

评论列表(0)

  1. 暂无评论