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

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?

Upvotes: 0

Views: 18

Answers (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.

Upvotes: 0

Related Questions