Skip to content

how to use db session in router level unit test #27

@zzmao

Description

@zzmao

Hi,

I have added fastapi-sqlalchemy to my app and it runs well if I start from main.py, where

app.add_middleware(DBSessionMiddleware, db_url=settings.DATABASE_RUL)

is added.

Now I am trying to build router-level unit test, which uses client = TestClient(router) to create router level client. So it won't run the code in main. the test failed and reports:

    @property
    def session(self) -> Session:
        """Return an instance of Session local to the current async context."""
        if _Session is None:
>           raise SessionNotInitialisedError
E           fastapi_sqlalchemy.exceptions.SessionNotInitialisedError: 
E                   Session not initialised! Ensure that DBSessionMiddleware has been initialised before
E                   attempting database access.

Any suggestion to use it in router level unit test?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions