Skip to content

Commit

Permalink
Continue adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chandr-andr committed Feb 29, 2024
1 parent 79049e5 commit 35843cb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
17 changes: 16 additions & 1 deletion python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from psqlpy import PSQLPool
from psqlpy import Cursor, PSQLPool


@pytest.fixture()
Expand Down Expand Up @@ -96,3 +96,18 @@ async def create_deafult_data_for_tests(
await psql_pool.execute(
f"DROP TABLE {table_name}",
)


@pytest.fixture()
async def test_cursor(
psql_pool: PSQLPool,
table_name: str,
) -> AsyncGenerator[Cursor, None]:
connection = await psql_pool.connection()
transaction = connection.transaction()
await transaction.begin()
cursor = await transaction.cursor(
querystring=f"SELECT * FROM {table_name}",
)
yield cursor
await transaction.commit()
63 changes: 54 additions & 9 deletions python/tests/test_cursor.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
import pytest

from psqlpy import PSQLPool
from psqlpy import Cursor


@pytest.mark.anyio
async def test_cursor_fetch(
psql_pool: PSQLPool,
table_name: str,
number_database_records: int,
test_cursor: Cursor,
) -> None:
connection = await psql_pool.connection()
transaction = connection.transaction()
await transaction.begin()
await transaction.cursor(
querystring=f"SELECT * FROM {table_name}",
"""Test cursor fetch with custom number of fetch."""
result = await test_cursor.fetch(fetch_number=number_database_records // 2)
assert len(result.result()) == number_database_records // 2


@pytest.mark.anyio
async def test_cursor_fetch_next(
test_cursor: Cursor,
) -> None:
"""Test cursor fetch next."""
result = await test_cursor.fetch_next()
assert len(result.result()) == 1


@pytest.mark.anyio
async def test_cursor_fetch_prior(
test_cursor: Cursor,
) -> None:
"""Test cursor fetch prior."""
result = await test_cursor.fetch_prior()
assert len(result.result()) == 0

await test_cursor.fetch(fetch_number=2)
result = await test_cursor.fetch_prior()
assert len(result.result()) == 1


@pytest.mark.anyio
async def test_cursor_fetch_first(
test_cursor: Cursor,
) -> None:
"""Test cursor fetch first."""
fetch_first = await test_cursor.fetch(fetch_number=1)

await test_cursor.fetch(fetch_number=3)

first = await test_cursor.fetch_first()

assert fetch_first.result() == first.result()


@pytest.mark.anyio
async def test_cursor_fetch_last(
test_cursor: Cursor,
number_database_records: int,
) -> None:
"""Test cursor fetch last."""
all_res = await test_cursor.fetch(
fetch_number=number_database_records,
)

await transaction.commit()
last_res = await test_cursor.fetch_last()

assert all_res.result()[-1] == last_res.result()[0]

0 comments on commit 35843cb

Please sign in to comment.