diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-10 15:42:35 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-11 22:11:07 -0400 |
commit | a45e2284dad17fbbba3bea9d5e5304aab21c8c94 (patch) | |
tree | ac31614f2d53059570e2edffe731baf384baea23 /test/ext/asyncio/test_session_py3k.py | |
parent | aa9cd878e8249a4a758c7f968e929e92fede42a5 (diff) | |
download | sqlalchemy-a45e2284dad17fbbba3bea9d5e5304aab21c8c94.tar.gz |
pep-484: asyncio
in this patch the asyncio/events.py module, which
existed only to raise errors when trying to attach event
listeners, is removed, as we were already coding an asyncio-specific
workaround in upstream Pool / Session to raise this error,
just moved the error out to the target and did the same thing
for Engine.
We also add an async_sessionmaker class. The initial rationale
here is because sessionmaker() is hardcoded to Session subclasses,
and there's not a way to get the use case of
sessionmaker(class_=AsyncSession) to type correctly without changing
the sessionmaker() symbol itself to be a function and not a class,
which gets too complicated for what this is. Additionally,
_SessionClassMethods has only three methods on it, one of which
is not usable with asyncio (close_all()), the others
not generally used from the session class.
Change-Id: I064a5fa5d91cc8d5bbe9597437536e37b4e801fe
Diffstat (limited to 'test/ext/asyncio/test_session_py3k.py')
-rw-r--r-- | test/ext/asyncio/test_session_py3k.py | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/test/ext/asyncio/test_session_py3k.py b/test/ext/asyncio/test_session_py3k.py index f04b87f37..ce38de511 100644 --- a/test/ext/asyncio/test_session_py3k.py +++ b/test/ext/asyncio/test_session_py3k.py @@ -10,6 +10,7 @@ from sqlalchemy import Table from sqlalchemy import testing from sqlalchemy import update from sqlalchemy.ext.asyncio import async_object_session +from sqlalchemy.ext.asyncio import async_sessionmaker from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import exc as async_exc from sqlalchemy.ext.asyncio.base import ReversibleProxy @@ -202,7 +203,7 @@ class AsyncSessionTransactionTest(AsyncFixture): await fn(async_session, trans_on_subject=True, execute_on_subject=True) @async_test - async def test_sessionmaker_block_one(self, async_engine): + async def test_orm_sessionmaker_block_one(self, async_engine): User = self.classes.User maker = sessionmaker(async_engine, class_=AsyncSession) @@ -226,7 +227,7 @@ class AsyncSessionTransactionTest(AsyncFixture): eq_(u1.name, "u1") @async_test - async def test_sessionmaker_block_two(self, async_engine): + async def test_orm_sessionmaker_block_two(self, async_engine): User = self.classes.User maker = sessionmaker(async_engine, class_=AsyncSession) @@ -248,6 +249,52 @@ class AsyncSessionTransactionTest(AsyncFixture): eq_(u1.name, "u1") @async_test + async def test_async_sessionmaker_block_one(self, async_engine): + + User = self.classes.User + maker = async_sessionmaker(async_engine) + + session = maker() + + async with session.begin(): + u1 = User(name="u1") + assert session.in_transaction() + session.add(u1) + + assert not session.in_transaction() + + async with maker() as session: + result = await session.execute( + select(User).where(User.name == "u1") + ) + + u1 = result.scalar_one() + + eq_(u1.name, "u1") + + @async_test + async def test_async_sessionmaker_block_two(self, async_engine): + + User = self.classes.User + maker = async_sessionmaker(async_engine) + + async with maker.begin() as session: + u1 = User(name="u1") + assert session.in_transaction() + session.add(u1) + + assert not session.in_transaction() + + async with maker() as session: + result = await session.execute( + select(User).where(User.name == "u1") + ) + + u1 = result.scalar_one() + + eq_(u1.name, "u1") + + @async_test async def test_trans(self, async_session, async_engine): async with async_engine.connect() as outer_conn: @@ -882,7 +929,7 @@ class OverrideSyncSession(AsyncFixture): is_true(isinstance(ass.sync_session, _MySession)) is_(ass.sync_session_class, _MySession) - def test_init_sessionmaker(self, async_engine): + def test_init_orm_sessionmaker(self, async_engine): sm = sessionmaker( async_engine, class_=AsyncSession, sync_session_class=_MySession ) @@ -891,6 +938,13 @@ class OverrideSyncSession(AsyncFixture): is_true(isinstance(ass.sync_session, _MySession)) is_(ass.sync_session_class, _MySession) + def test_init_asyncio_sessionmaker(self, async_engine): + sm = async_sessionmaker(async_engine, sync_session_class=_MySession) + ass = sm() + + is_true(isinstance(ass.sync_session, _MySession)) + is_(ass.sync_session_class, _MySession) + def test_subclass(self, async_engine): ass = _MyAS(async_engine) |