Skip to content

Using sqlmodel with pytest and factory boy #615

Closed Answered by farridav
farridav asked this question in Questions
Discussion options

You must be logged in to vote

I have managed to resolve this using the following structure:

conftest.py

import os
from typing import Any, Generator

import pytest
from factory.alchemy import SQLAlchemyModelFactory
from sqlmodel import Session, SQLModel

# Ensure our engine is shared
from my_app.database import engine


@pytest.fixture(scope="module")
def db_session() -> Generator[Session, Any, None]:
    from . import factories  # NOQA

    SQLModel.metadata.create_all(engine)
    session = Session(engine)

    # Ensure that all factories use the same session
    for factory in SQLAlchemyModelFactory.__subclasses__():
        factory._meta.sqlalchemy_session = session

    yield session

    session.rollback()
    ses…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by farridav
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant