Skip to content

Continue adding tests #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]