Testing FastAPI database with async connection

306 views Asked by At

I have application on FastAPI. For sending queries to db I'm using SQLAlchemy. Connection in my app looks like `

import databases
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base

from settings import settings


SQLALCHEMY_DATABASE_URL = settings.DB_CONNECT

engine = create_engine(
    SQLALCHEMY_DATABASE_URL,
)

database = databases.Database(SQLALCHEMY_DATABASE_URL)


Base = declarative_base()

`

How can I test my app with this db connection? Now I'm tring `

@pytest_asyncio.fixture(autouse=True)
async def db():
    await database.connect()
    yield
    await database.disconnect()

` but this way I can only connect to current and working database. I need to create test database for testing. I will be glad for any help.

0

There are 0 answers