How to setup testing for a fastapi with a single database session using pytest, SQLAlchemy, dependency injection

I am trying to setup testing on a FastAPI project using Pytest and also a database. I want it such that the database is created on every test and destroyed after every test.

My setup worked, but then, it fell apart when I introduced factories using the factory-boy package. I wanted to be able to use the factory-boy package to generate objects that will be persisted to the test database that I can use in my testcase.

The problem I have is defining the session that the factory class would use as factory-boy requires a session for the SQLAlchemy model.

NB: I use the dependency-injector library in my code to pass in dependencies to classes across my project.

In my db.py class, I have the database setup like this, a Database class for the actual application and a TestDatabase class that would be used in tests. The intention is that I’d override the database in the test environment.

class Database:
    def __init__(self, db_url: str) -> None:
        print("Initializing DB...")
        import pdb

        pdb.set_trace()
        engine_kwargs = {"echo": True if settings.DEBUG else False}
        self._engine = create_engine(db_url, **engine_kwargs)
        self._session_factory = scoped_session(
            sessionmaker(
                class_=Session,
                autocommit=False,
                autoflush=False,
                bind=self._engine,
            )
        )

    def session_factory(self):  # noqa
        return self._session_factory()

    @contextmanager
    def session(self) -> Callable[..., AbstractContextManager[Session]]:
        session: Session = self._session_factory()
        try:
            yield session
        except Exception as e:
            print(e)
            session.rollback()
            raise
        finally:
            session.close()


class TestDatabase(Database):
    def __init__(self, db_url: str) -> None:  # noqa
        import pdb

        pdb.set_trace()
        # super().__init__(db_url)
        print("Initializing DB...")
        engine_kwargs = {
            "echo": True,
            "connect_args": {
                "check_same_thread": False,
            },
            "poolclass": StaticPool,
        }
        self._engine = create_engine(db_url, **engine_kwargs)

        # Create database tables for for test environment.
        SQLModel.metadata.create_all(bind=self._engine)

        self._session_factory = scoped_session(
            sessionmaker(
                class_=Session,
                autocommit=False,
                autoflush=False,
                bind=self._engine,
            )
        )


I also have a container for the dependency injection setup:

class AppContainer(containers.DeclarativeContainer):
    """
    DI container for the project.
    """

    # Auto-wiring
    wiring_config = containers.WiringConfiguration(packages=["app.api"])

    # Singletons
    db = providers.Singleton(Database, db_url=settings.DATABASE_URI)

    # Repositories
    book_repository = providers.Factory(
        BookRepository,
        session_factory=db.provided.session_factory,
    )

    # Services
    book_service = providers.Factory(
        BookService,
        book_repository=book_repository,
    )

In my conftest.py, I have these fixtures setup, one to override the db before returning the container, one to return a session, and another for the test client:

@pytest.fixture()
def app_container():
    container = AppContainer()

    container.db.override(
        providers.Singleton(
            TestDatabase,
            db_url="sqlite:///:memory:",
        )
    )

    return container


@pytest.fixture()
def db_session(app_container):
    return app_container.db().session_factory()


@pytest.fixture()
def test_client():
    with TestClient(application) as test_client:
        yield test_client

In my factories.py, I have a factory class setup:

app_container = AppContainer()
app_container.db.override(
    providers.Singleton(
        TestDatabase,
        db_url="sqlite:///:memory:",
    )
)
session = app_container.db().session_factory()


class BookFactory(factory.alchemy.SQLAlchemyModelFactory):
    class Meta:
        model = Book
        sqlalchemy_session = session

    name = "Test Book"
    author = factory.Faker("name")


Then in a sample test function, I write a test like this:

def test_store_book(test_client, app_container, db_session):
    BookFactory(name="Sample Book Name 2")

    payload = {
        "name": "Sample Book Name",
        "author": "John Doe",
    }

    response = test_client.post("/books", json=payload)

    books = db_session.query(Book).all()

    assert response.status_code == 200
    assert len(books) == 2

My expectation from this test is that the book factory would create a book in the database and the endpoint will create another book in the database so that when I fetch the number of books from the database, the length of the books list should be 2. But that’s not the case here, it returns 1. My guess is that the database is being destroyed and recreated. So, the database where the factory stores the initial book created from the factory is not the same as the one the endpoint will store the book.

What I want is to have a single database through out the lifecycle of a single test case so that I can use factories to store items in the database and also store item via the endpoint and I’d see both of them.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật