|
| 1 | +"""Tests for AsyncCursor class.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from dqlitedbapi.aio.connection import AsyncConnection |
| 6 | +from dqlitedbapi.aio.cursor import AsyncCursor |
| 7 | +from dqlitedbapi.exceptions import InterfaceError |
| 8 | + |
| 9 | + |
| 10 | +class TestAsyncCursor: |
| 11 | + def test_description_initially_none(self) -> None: |
| 12 | + conn = AsyncConnection("localhost:9001") |
| 13 | + cursor = AsyncCursor(conn) |
| 14 | + assert cursor.description is None |
| 15 | + |
| 16 | + def test_rowcount_initially_minus_one(self) -> None: |
| 17 | + conn = AsyncConnection("localhost:9001") |
| 18 | + cursor = AsyncCursor(conn) |
| 19 | + assert cursor.rowcount == -1 |
| 20 | + |
| 21 | + def test_arraysize_default(self) -> None: |
| 22 | + conn = AsyncConnection("localhost:9001") |
| 23 | + cursor = AsyncCursor(conn) |
| 24 | + assert cursor.arraysize == 1 |
| 25 | + |
| 26 | + def test_arraysize_setter(self) -> None: |
| 27 | + conn = AsyncConnection("localhost:9001") |
| 28 | + cursor = AsyncCursor(conn) |
| 29 | + cursor.arraysize = 10 |
| 30 | + assert cursor.arraysize == 10 |
| 31 | + |
| 32 | + def test_lastrowid_initially_none(self) -> None: |
| 33 | + conn = AsyncConnection("localhost:9001") |
| 34 | + cursor = AsyncCursor(conn) |
| 35 | + assert cursor.lastrowid is None |
| 36 | + |
| 37 | + @pytest.mark.asyncio |
| 38 | + async def test_close_marks_cursor_closed(self) -> None: |
| 39 | + conn = AsyncConnection("localhost:9001") |
| 40 | + cursor = AsyncCursor(conn) |
| 41 | + await cursor.close() |
| 42 | + assert cursor._closed |
| 43 | + |
| 44 | + @pytest.mark.asyncio |
| 45 | + async def test_fetchone_on_closed_cursor_raises(self) -> None: |
| 46 | + conn = AsyncConnection("localhost:9001") |
| 47 | + cursor = AsyncCursor(conn) |
| 48 | + await cursor.close() |
| 49 | + |
| 50 | + with pytest.raises(InterfaceError, match="Cursor is closed"): |
| 51 | + await cursor.fetchone() |
| 52 | + |
| 53 | + @pytest.mark.asyncio |
| 54 | + async def test_fetchone_no_rows_returns_none(self) -> None: |
| 55 | + conn = AsyncConnection("localhost:9001") |
| 56 | + cursor = AsyncCursor(conn) |
| 57 | + # Simulate a query that returned zero rows |
| 58 | + cursor._description = [("id", None, None, None, None, None, None)] |
| 59 | + cursor._rows = [] |
| 60 | + result = await cursor.fetchone() |
| 61 | + assert result is None |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_fetchall_no_rows_returns_empty(self) -> None: |
| 65 | + conn = AsyncConnection("localhost:9001") |
| 66 | + cursor = AsyncCursor(conn) |
| 67 | + cursor._description = [("id", None, None, None, None, None, None)] |
| 68 | + cursor._rows = [] |
| 69 | + result = await cursor.fetchall() |
| 70 | + assert result == [] |
| 71 | + |
| 72 | + @pytest.mark.asyncio |
| 73 | + async def test_context_manager(self) -> None: |
| 74 | + conn = AsyncConnection("localhost:9001") |
| 75 | + async with AsyncCursor(conn) as cursor: |
| 76 | + assert not cursor._closed |
| 77 | + assert cursor._closed |
| 78 | + |
| 79 | + @pytest.mark.asyncio |
| 80 | + async def test_async_iterator(self) -> None: |
| 81 | + conn = AsyncConnection("localhost:9001") |
| 82 | + cursor = AsyncCursor(conn) |
| 83 | + cursor._rows = [(1, "a"), (2, "b"), (3, "c")] |
| 84 | + cursor._description = [("id", None, None, None, None, None, None)] |
| 85 | + |
| 86 | + results = [row async for row in cursor] |
| 87 | + assert results == [(1, "a"), (2, "b"), (3, "c")] |
| 88 | + |
| 89 | + def test_setinputsizes_noop(self) -> None: |
| 90 | + conn = AsyncConnection("localhost:9001") |
| 91 | + cursor = AsyncCursor(conn) |
| 92 | + cursor.setinputsizes([None, None]) |
| 93 | + |
| 94 | + def test_setoutputsize_noop(self) -> None: |
| 95 | + conn = AsyncConnection("localhost:9001") |
| 96 | + cursor = AsyncCursor(conn) |
| 97 | + cursor.setoutputsize(100, 0) |
0 commit comments