litestar-org/polyfactory

Enhancement: Passing DB Session from pytest fixture to SyncPersistenceHandler::save

sharla05 opened this issue · 4 comments

Hi,
How can one pass DB Session from pytest fixture to be used in SyncPersistenceHandler::save to persist the object?
With factory_boy, one can set _meta.sqlalchemy_session to point to the fixture, as in below code, how to achieve something like this?

@pytest.fixture(scope='function', autouse=False)
def db_session():
    db_session = Session(pytest.db_engine)
    yield db_session
    db_session.rollback()
class TestUserFactory:
    def test_user_factory(self, db_session: Session) -> None:
        UserFactory._meta.sqlalchemy_session = db_session 
        UserFactory.create()
        assert db_session.query(User).one()

Its currently not possible. We can slate this for v2, which is in the works.

Great Thanks.

need this one open for tracking

So, the solution is to override the factory's create method