From 35843cbc94085792149d9d5941d9c3f697a734ba Mon Sep 17 00:00:00 2001 From: "chandr-andr (Kiselev Aleksandr)" Date: Thu, 29 Feb 2024 20:11:24 +0400 Subject: [PATCH] Continue adding tests --- python/tests/conftest.py | 17 +++++++++- python/tests/test_cursor.py | 63 +++++++++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/python/tests/conftest.py b/python/tests/conftest.py index 8d81582d..16236d51 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -4,7 +4,7 @@ import pytest -from psqlpy import PSQLPool +from psqlpy import Cursor, PSQLPool @pytest.fixture() @@ -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() diff --git a/python/tests/test_cursor.py b/python/tests/test_cursor.py index 88addc76..949a0a65 100644 --- a/python/tests/test_cursor.py +++ b/python/tests/test_cursor.py @@ -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]