Skip to content

Commit 8951d30

Browse files
authored
Merge pull request #3 from qaspen-python/feature/add_tests
Continue adding tests
2 parents fe2135a + 35843cb commit 8951d30

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

python/tests/conftest.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from psqlpy import PSQLPool
7+
from psqlpy import Cursor, PSQLPool
88

99

1010
@pytest.fixture()
@@ -96,3 +96,18 @@ async def create_deafult_data_for_tests(
9696
await psql_pool.execute(
9797
f"DROP TABLE {table_name}",
9898
)
99+
100+
101+
@pytest.fixture()
102+
async def test_cursor(
103+
psql_pool: PSQLPool,
104+
table_name: str,
105+
) -> AsyncGenerator[Cursor, None]:
106+
connection = await psql_pool.connection()
107+
transaction = connection.transaction()
108+
await transaction.begin()
109+
cursor = await transaction.cursor(
110+
querystring=f"SELECT * FROM {table_name}",
111+
)
112+
yield cursor
113+
await transaction.commit()

python/tests/test_cursor.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
11
import pytest
22

3-
from psqlpy import PSQLPool
3+
from psqlpy import Cursor
44

55

66
@pytest.mark.anyio
77
async def test_cursor_fetch(
8-
psql_pool: PSQLPool,
9-
table_name: str,
108
number_database_records: int,
9+
test_cursor: Cursor,
1110
) -> None:
12-
connection = await psql_pool.connection()
13-
transaction = connection.transaction()
14-
await transaction.begin()
15-
await transaction.cursor(
16-
querystring=f"SELECT * FROM {table_name}",
11+
"""Test cursor fetch with custom number of fetch."""
12+
result = await test_cursor.fetch(fetch_number=number_database_records // 2)
13+
assert len(result.result()) == number_database_records // 2
14+
15+
16+
@pytest.mark.anyio
17+
async def test_cursor_fetch_next(
18+
test_cursor: Cursor,
19+
) -> None:
20+
"""Test cursor fetch next."""
21+
result = await test_cursor.fetch_next()
22+
assert len(result.result()) == 1
23+
24+
25+
@pytest.mark.anyio
26+
async def test_cursor_fetch_prior(
27+
test_cursor: Cursor,
28+
) -> None:
29+
"""Test cursor fetch prior."""
30+
result = await test_cursor.fetch_prior()
31+
assert len(result.result()) == 0
32+
33+
await test_cursor.fetch(fetch_number=2)
34+
result = await test_cursor.fetch_prior()
35+
assert len(result.result()) == 1
36+
37+
38+
@pytest.mark.anyio
39+
async def test_cursor_fetch_first(
40+
test_cursor: Cursor,
41+
) -> None:
42+
"""Test cursor fetch first."""
43+
fetch_first = await test_cursor.fetch(fetch_number=1)
44+
45+
await test_cursor.fetch(fetch_number=3)
46+
47+
first = await test_cursor.fetch_first()
48+
49+
assert fetch_first.result() == first.result()
50+
51+
52+
@pytest.mark.anyio
53+
async def test_cursor_fetch_last(
54+
test_cursor: Cursor,
55+
number_database_records: int,
56+
) -> None:
57+
"""Test cursor fetch last."""
58+
all_res = await test_cursor.fetch(
59+
fetch_number=number_database_records,
1760
)
1861

19-
await transaction.commit()
62+
last_res = await test_cursor.fetch_last()
63+
64+
assert all_res.result()[-1] == last_res.result()[0]

0 commit comments

Comments
 (0)