import asyncio

import pytest
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio.engine import AsyncConnection
from sqlmodel.ext.asyncio.session import AsyncSession

from basesqlmodel import Base

engine = create_async_engine("sqlite+aiosqlite:///:memory:")


@pytest.fixture()
async def connection() -> AsyncConnection:
    async with engine.begin() as conn:
        yield conn
        await conn.rollback()


@pytest.fixture()
async def session(connection: AsyncConnection):
    async with AsyncSession(connection, expire_on_commit=False) as _session:
        yield _session


@pytest.fixture(scope="session", autouse=True)
def event_loop():
    """Reference: https://github.com/pytest-dev/pytest-asyncio/issues/38#issuecomment-264418154"""
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()


@pytest.fixture(scope="session", autouse=True)
async def init_database():
    import tests.utils  # noqa

    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

pytest.fixture asyncio.get_event_loop_policy sqlmodel.ext.asyncio.session.AsyncSession sqlalchemy.ext.asyncio.create_async_engine
[((238, 289), 'sqlalchemy.ext.asyncio.create_async_engine', 'create_async_engine', (['"""sqlite+aiosqlite:///:memory:"""'], {}), "('sqlite+aiosqlite:///:memory:')\n", (257, 289), False, 'from sqlalchemy.ext.asyncio import create_async_engine\n'), ((293, 309), 'pytest.fixture', 'pytest.fixture', ([], {}), '()\n', (307, 309), False, 'import pytest\n'), ((444, 460), 'pytest.fixture', 'pytest.fixture', ([], {}), '()\n', (458, 460), False, 'import pytest\n'), ((612, 657), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""', 'autouse': '(True)'}), "(scope='session', autouse=True)\n", (626, 657), False, 'import pytest\n'), ((870, 915), 'pytest.fixture', 'pytest.fixture', ([], {'scope': '"""session"""', 'autouse': '(True)'}), "(scope='session', autouse=True)\n", (884, 915), False, 'import pytest\n'), ((524, 572), 'sqlmodel.ext.asyncio.session.AsyncSession', 'AsyncSession', (['connection'], {'expire_on_commit': '(False)'}), '(connection, expire_on_commit=False)\n', (536, 572), False, 'from sqlmodel.ext.asyncio.session import AsyncSession\n'), ((786, 817), 'asyncio.get_event_loop_policy', 'asyncio.get_event_loop_policy', ([], {}), '()\n', (815, 817), False, 'import asyncio\n')]