async def app_factory(): app = web.Application() aiohttp_sqlalchemy.setup(app, [ aiohttp_sqlalchemy.bind(DB_URL), aiohttp_sqlalchemy.bind(DB_URL, SECOND_KEY), aiohttp_sqlalchemy.bind(DB_URL, THIRD_KEY, middleware=False), aiohttp_sqlalchemy.bind(DB_URL, FOURTH_KEY, middleware=False), ]) for key in KEY_LIST: await aiohttp_sqlalchemy.init_db(app, metadata, key) app.add_routes([web.get('/handler_a', function_handler)]) app.add_routes([web.get('/handler_b', ClassOrganizedHandler().get)]) app.add_routes([web.view('/handler_c', ClassBasedView)]) return app
async def app_factory(): app = web.Application() aiohttp_sqlalchemy.setup(app, [ aiohttp_sqlalchemy.bind('sqlite+aiosqlite:///'), ]) await aiohttp_sqlalchemy.init_db(app, metadata) app.add_routes([web.get('/', main)]) return app
async def test_duplicate_app_key_error( session_factory: TSessionFactory, ) -> None: with pytest.raises(DuplicateAppKeyError): aiohttp_sqlalchemy.setup(web.Application(), [ aiohttp_sqlalchemy.bind(session_factory), aiohttp_sqlalchemy.bind(session_factory), ]) with pytest.raises(DuplicateAppKeyError): aiohttp_sqlalchemy.setup(web.Application(), [ aiohttp_sqlalchemy.bind(session_factory, 'sa_secondary'), aiohttp_sqlalchemy.bind(session_factory, 'sa_secondary'), ]) aiohttp_sqlalchemy.setup(web.Application(), [ aiohttp_sqlalchemy.bind(session_factory), aiohttp_sqlalchemy.bind(session_factory, 'sa_secondary'), ])
def test_bind_to_url() -> None: binding = aiohttp_sqlalchemy.bind('sqlite+aiosqlite:///') session_factory = binding[0] session = session_factory() assert isinstance(session, AsyncSession)
def test_bind_to_none() -> None: with pytest.raises(TypeError): aiohttp_sqlalchemy.bind(None)
def test_bind_to_async_session_maker(orm_async_engine: AsyncEngine) -> None: Session = sessionmaker(orm_async_engine, AsyncSession) binding = aiohttp_sqlalchemy.bind(Session) Session = binding[0] session = Session() assert isinstance(session, AsyncSession)
def test_bind_with_sync_session() -> None: engine = create_engine('sqlite+aiosqlite:///') Session = sessionmaker(engine) session = Session() with pytest.raises(TypeError): aiohttp_sqlalchemy.bind(session)
def test_bind_with_ready_session(orm_async_engine: AsyncEngine) -> None: session = AsyncSession(orm_async_engine) with pytest.raises(TypeError): aiohttp_sqlalchemy.bind(session)
def test_bind_to_sync_engine() -> None: engine = create_engine('sqlite+aiosqlite:///') with pytest.raises(TypeError): aiohttp_sqlalchemy.bind(engine)
def test_bind_to_async_engine(orm_async_engine: AsyncEngine) -> None: binding = aiohttp_sqlalchemy.bind(orm_async_engine) session_factory = binding[0] session = session_factory() assert isinstance(session, AsyncSession)