I creating a database for tests with asynchronous connection.
...
DB_URL = (
f"postgresql+asyncpg://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
)
test_engine = create_async_engine(url=DB_URL, echo=True)
Next, I’m trying to write a fix for creating a table and then deleting it
@pytest.fixture(autouse=True, scope="module")
async def create_db():
async with test_engine.begin() as eng:
await eng.run_sync(Base.metadata.create_all)
yield
await eng.run_sync(Base.metadata.drop_all)
I try to run the tests
@pytest.mark.asyncio
async def test_create_city(aclient: AsyncClient):
response = await aclient.post("/create", json={"title": "test city"})
assert response.status_code == 200
but i get an error that is, at the time of passing the tests, the tables are not yet created, although in the echo logs I see sql commands for creating tables and deleting them after the tests.
FAILED tests/test_some.py::test_create_city - sqlalchemy.exc.ProgrammingError: (sqlalchemy.dialects.postgresql.asyncpg.ProgrammingError) <class 'asyncpg.exceptions.UndefinedTableError'>: relation "cities" does not exist