async def load_users(ids: list[int]) -> list[User]: try: engine = get_engine() async with get_session(engine) as session: users = await UsersRepository(session).get_batch_by_ids(ids) users_by_id = {user.id: user for user in users} return [users_by_id.get(id) for id in ids] finally: await engine.dispose()
async def run(): engine = get_engine(echo=False) metadata = mapper_registry.metadata if not database_exists(DATABASE_URL): create_database(DATABASE_URL) async with engine.begin() as connection: await connection.run_sync(mapper_registry.metadata.create_all) else: async with engine.begin() as connection: for table in metadata.sorted_tables: await connection.execute(table.delete())
async def run(): engine = get_engine(echo=False) metadata = mapper_registry.metadata db_name = make_url(DATABASE_URL).database sync_database_url = DATABASE_URL.replace("asyncpg", "psycopg2").replace("TEST_", "") if not database_exists(sync_database_url, db_name): create_database(sync_database_url, db_name) async with engine.begin() as connection: await connection.run_sync(metadata.create_all) else: async with engine.begin() as connection: await connection.run_sync(metadata.drop_all) await connection.run_sync(metadata.create_all)
async def startup(): app.state.engine = get_engine()
import logging from unittest.mock import patch from ward import fixture from users.db import get_engine, get_session from users.domain.entities import mapper_registry logger = logging.getLogger(__name__) engine = get_engine(echo=False) test_session = get_session(engine) @fixture async def db(): with patch("main.get_session", return_value=test_session): yield test_session metadata = mapper_registry.metadata async with engine.begin() as connection: for table in metadata.sorted_tables: await connection.execute(table.delete()) logger.debug("rolling back after unit-test done") @fixture async def second_session(): return get_session(engine)