I'm trying to use factory boy with async SQLAlchemy, concrete SQLModel ORM and having issue while calling factory boy create method it creates only instance od object but never stored in DB. My factory boy is looking like this
session = scoped_session(
sessionmaker(class_=AsyncSession, bind=engine, autoflush=False)
)
class BaseFactory(SQLAlchemyModelFactory):
class Meta:
model = InitialModel
abstract = True
sqlalchemy_session = session
and inside coftest I have session that is needed for FastAPI
@pytest.fixture(scope="function")
async def session_fixture() -> AsyncSession:
"""Create a new session for a test and rollback changes after test"""
async_session = sessionmaker(engine, class_=AsyncSession, autoflush=False)
async with async_session() as session:
yield session
for table in reversed(SQLModel.metadata.sorted_tables):
await session.execute(table.delete())
I'm not sure if there is problem with sessions or because of asynchronous calls. When I was working with synchronous calls on same type everything was stored propertly
@pytest.mark.asyncio
? – Mimosa