Skip to content

Commit 5a25c10

Browse files
Cover AsyncAdaptedConnection commit/rollback/cursor delegations
These three methods are thin sync-over-async shims via await_only; a refactor that awaited the wrong attribute would silently skip transaction boundaries or hand back a cursor of the wrong type. Add Mock-based unit tests that assert the inner connection's commit / rollback coroutines are awaited exactly once and that cursor() returns an AsyncAdaptedCursor wrapping the inner instance. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 238524f commit 5a25c10

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

tests/test_async_adapter.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,63 @@ def test_iter_drains_deque(self) -> None:
301301
cursor = self._cursor_with_rows([(1,), (2,), (3,)])
302302
assert list(cursor) == [(1,), (2,), (3,)]
303303
assert len(cursor._rows) == 0 # type: ignore[attr-defined]
304+
305+
306+
class TestAioAdapterConnectionDelegations:
307+
"""AsyncAdaptedConnection.commit / rollback / cursor are thin
308+
sync-over-async shims via ``await_only``. A refactor that awaits
309+
the wrong attribute would silently skip transaction boundaries or
310+
hand back a cursor of the wrong type. Unit tests are cheap.
311+
"""
312+
313+
def test_commit_delegates_through_await_only(self) -> None:
314+
from unittest.mock import AsyncMock, MagicMock, patch
315+
316+
from sqlalchemydqlite.aio import AsyncAdaptedConnection
317+
318+
inner = MagicMock()
319+
inner.commit = AsyncMock()
320+
adapter = AsyncAdaptedConnection.__new__(AsyncAdaptedConnection)
321+
adapter._connection = inner
322+
323+
def _run_sync(coro: object) -> object:
324+
import asyncio
325+
326+
return asyncio.new_event_loop().run_until_complete(coro) # type: ignore[arg-type]
327+
328+
with patch("sqlalchemydqlite.aio.await_only", side_effect=_run_sync):
329+
adapter.commit()
330+
331+
inner.commit.assert_awaited_once()
332+
333+
def test_rollback_delegates_through_await_only(self) -> None:
334+
from unittest.mock import AsyncMock, MagicMock, patch
335+
336+
from sqlalchemydqlite.aio import AsyncAdaptedConnection
337+
338+
inner = MagicMock()
339+
inner.rollback = AsyncMock()
340+
adapter = AsyncAdaptedConnection.__new__(AsyncAdaptedConnection)
341+
adapter._connection = inner
342+
343+
def _run_sync(coro: object) -> object:
344+
import asyncio
345+
346+
return asyncio.new_event_loop().run_until_complete(coro) # type: ignore[arg-type]
347+
348+
with patch("sqlalchemydqlite.aio.await_only", side_effect=_run_sync):
349+
adapter.rollback()
350+
351+
inner.rollback.assert_awaited_once()
352+
353+
def test_cursor_returns_async_adapted_cursor_wrapping_inner(self) -> None:
354+
from unittest.mock import MagicMock
355+
356+
from sqlalchemydqlite.aio import AsyncAdaptedConnection, AsyncAdaptedCursor
357+
358+
inner = MagicMock()
359+
adapter = AsyncAdaptedConnection.__new__(AsyncAdaptedConnection)
360+
adapter._connection = inner
361+
362+
cursor = adapter.cursor()
363+
assert isinstance(cursor, AsyncAdaptedCursor)

0 commit comments

Comments
 (0)