Skip to content

Commit 1ea8bf7

Browse files
committed
Continue implementing new realization. Clippy. pre-commit enabled.
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 6f6462d commit 1ea8bf7

File tree

15 files changed

+132
-233
lines changed

15 files changed

+132
-233
lines changed

python/psqlpy/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
from typing import Optional
2-
from psqlpy._internal import Connection, ConnectionPool, QueryResult, Transaction, Cursor
3-
4-
5-
# class Cursor(Cursor):
6-
# async def __anext__(self) -> Optional[QueryResult]:
7-
# results = await self.fetch(10)
8-
# if not results.result():
9-
# return StopAsyncIteration
10-
# return results
11-
1+
from psqlpy._internal import (
2+
Connection,
3+
ConnectionPool,
4+
ConnRecyclingMethod,
5+
Cursor,
6+
IsolationLevel,
7+
QueryResult,
8+
ReadVariant,
9+
SingleQueryResult,
10+
Transaction,
11+
)
1212

1313
__all__ = [
1414
"ConnectionPool",
1515
"Transaction",
1616
"Connection",
1717
"Cursor",
18+
"QueryResult",
19+
"SingleQueryResult",
20+
"ConnRecyclingMethod",
21+
"IsolationLevel",
22+
"ReadVariant",
1823
]

python/psqlpy/_internal/__init__.pyi

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -854,93 +854,6 @@ class Connection:
854854
- `deferrable`: configure deferrable of the transaction.
855855
"""
856856

857-
class PSQLPool:
858-
"""Connection pool for executing queries.
859-
860-
This is the main entrypoint in the library.
861-
"""
862-
863-
def __init__(
864-
self: Self,
865-
dsn: Optional[str] = None,
866-
username: Optional[str] = None,
867-
password: Optional[str] = None,
868-
host: Optional[str] = None,
869-
port: Optional[int] = None,
870-
db_name: Optional[str] = None,
871-
max_db_pool_size: int = 2,
872-
conn_recycling_method: Optional[ConnRecyclingMethod] = None,
873-
) -> None:
874-
"""Create new PostgreSQL connection pool.
875-
876-
It connects to the database and create pool.
877-
878-
You cannot set the minimum size for the connection
879-
pool, by default it is 1.
880-
881-
This connection pool can:
882-
- Startup itself with `startup` method
883-
- Execute queries and return `QueryResult` class as a result
884-
- Create new instance of `Transaction`
885-
886-
### Parameters:
887-
- `dsn`: full dsn connection string.
888-
`postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
889-
- `username`: username of the user in postgres
890-
- `password`: password of the user in postgres
891-
- `host`: host of postgres
892-
- `port`: port of postgres
893-
- `db_name`: name of the database in postgres
894-
- `max_db_pool_size`: maximum size of the connection pool
895-
- `conn_recycling_method`: how a connection is recycled.
896-
"""
897-
async def close(self: Self) -> None:
898-
"""Close the connection pool.
899-
900-
By default it will be closed automatically,
901-
but you can call it manually.
902-
"""
903-
async def execute(
904-
self: Self,
905-
querystring: str,
906-
parameters: list[Any] | None = None,
907-
prepared: bool = True,
908-
) -> QueryResult:
909-
"""Execute the query.
910-
911-
Querystring can contain `$<number>` parameters
912-
for converting them in the driver side.
913-
914-
### Parameters:
915-
- `querystring`: querystring to execute.
916-
- `parameters`: list of parameters to pass in the query.
917-
- `prepared`: should the querystring be prepared before the request.
918-
By default any querystring will be prepared.
919-
920-
### Example:
921-
```python
922-
import asyncio
923-
924-
from psqlpy import PSQLPool, QueryResult
925-
926-
927-
async def main() -> None:
928-
db_pool = PSQLPool()
929-
query_result: QueryResult = await psqlpy.execute(
930-
"SELECT username FROM users WHERE id = $1",
931-
[100],
932-
)
933-
dict_result: List[Dict[Any, Any]] = query_result.result()
934-
# you don't need to close the pool,
935-
# it will be dropped on Rust side.
936-
```
937-
"""
938-
async def connection(self: Self) -> Connection:
939-
"""Create new connection.
940-
941-
It acquires new connection from the database pool.
942-
"""
943-
944857
class ConnectionPool:
945858
"""Connection pool for executing queries.
946859
@@ -1021,3 +934,5 @@ class ConnectionPool:
1021934
1022935
It acquires new connection from the database pool.
1023936
"""
937+
def close(self: Self) -> None:
938+
"""Close the connection pool."""

python/tests/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from pydantic import BaseModel
77

8-
from psqlpy import Cursor, PSQLPool
8+
from psqlpy import ConnectionPool, Cursor
99

1010

1111
class DefaultPydanticModel(BaseModel):
@@ -80,8 +80,8 @@ async def psql_pool(
8080
postgres_password: str,
8181
postgres_port: int,
8282
postgres_dbname: str,
83-
) -> PSQLPool:
84-
return PSQLPool(
83+
) -> ConnectionPool:
84+
return ConnectionPool(
8585
username=postgres_user,
8686
password=postgres_password,
8787
host=postgres_host,
@@ -92,7 +92,7 @@ async def psql_pool(
9292

9393
@pytest.fixture(autouse=True)
9494
async def create_deafult_data_for_tests(
95-
psql_pool: PSQLPool,
95+
psql_pool: ConnectionPool,
9696
table_name: str,
9797
number_database_records: int,
9898
) -> AsyncGenerator[None, None]:
@@ -114,7 +114,7 @@ async def create_deafult_data_for_tests(
114114

115115
@pytest.fixture()
116116
async def test_cursor(
117-
psql_pool: PSQLPool,
117+
psql_pool: ConnectionPool,
118118
table_name: str,
119119
) -> AsyncGenerator[Cursor, None]:
120120
connection = await psql_pool.connection()

python/tests/test_connection.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import pytest
66
from tests.helpers import count_rows_in_test_table
77

8-
from psqlpy import PSQLPool, QueryResult, Transaction
8+
from psqlpy import ConnectionPool, QueryResult, Transaction
99
from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError
1010

1111
pytestmark = pytest.mark.anyio
1212

1313

1414
async def test_connection_execute(
15-
psql_pool: PSQLPool,
15+
psql_pool: ConnectionPool,
1616
table_name: str,
1717
number_database_records: int,
1818
) -> None:
@@ -27,7 +27,7 @@ async def test_connection_execute(
2727

2828

2929
async def test_connection_connection(
30-
psql_pool: PSQLPool,
30+
psql_pool: ConnectionPool,
3131
) -> None:
3232
"""Test that connection can create transactions."""
3333
connection = await psql_pool.connection()
@@ -46,7 +46,7 @@ async def test_connection_connection(
4646
],
4747
)
4848
async def test_connection_execute_many(
49-
psql_pool: PSQLPool,
49+
psql_pool: ConnectionPool,
5050
table_name: str,
5151
number_database_records: int,
5252
insert_values: list[list[typing.Any]],
@@ -67,7 +67,7 @@ async def test_connection_execute_many(
6767

6868

6969
async def test_connection_fetch_row(
70-
psql_pool: PSQLPool,
70+
psql_pool: ConnectionPool,
7171
table_name: str,
7272
) -> None:
7373
connection = await psql_pool.connection()
@@ -80,7 +80,7 @@ async def test_connection_fetch_row(
8080

8181

8282
async def test_connection_fetch_row_more_than_one_row(
83-
psql_pool: PSQLPool,
83+
psql_pool: ConnectionPool,
8484
table_name: str,
8585
) -> None:
8686
connection = await psql_pool.connection()
@@ -92,7 +92,7 @@ async def test_connection_fetch_row_more_than_one_row(
9292

9393

9494
async def test_connection_fetch_val(
95-
psql_pool: PSQLPool,
95+
psql_pool: ConnectionPool,
9696
table_name: str,
9797
) -> None:
9898
connection = await psql_pool.connection()
@@ -104,7 +104,7 @@ async def test_connection_fetch_val(
104104

105105

106106
async def test_connection_fetch_val_more_than_one_row(
107-
psql_pool: PSQLPool,
107+
psql_pool: ConnectionPool,
108108
table_name: str,
109109
) -> None:
110110
connection = await psql_pool.connection()

python/tests/test_connection_pool.py

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

3-
from psqlpy import Connection, ConnRecyclingMethod, PSQLPool, QueryResult
3+
from psqlpy import Connection, ConnectionPool, ConnRecyclingMethod, QueryResult
44
from psqlpy.exceptions import RustPSQLDriverPyBaseError
55

66
pytestmark = pytest.mark.anyio
77

88

99
async def test_pool_dsn_startup() -> None:
1010
"""Test that connection pool can startup with dsn."""
11-
pg_pool = PSQLPool(
11+
pg_pool = ConnectionPool(
1212
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
1313
)
1414

1515
await pg_pool.execute("SELECT 1")
1616

1717

1818
async def test_pool_execute(
19-
psql_pool: PSQLPool,
19+
psql_pool: ConnectionPool,
2020
table_name: str,
2121
number_database_records: int,
2222
) -> None:
23-
"""Test that PSQLPool can execute queries."""
23+
"""Test that ConnectionPool can execute queries."""
2424
select_result = await psql_pool.execute(
2525
f"SELECT * FROM {table_name}",
2626
)
@@ -33,9 +33,9 @@ async def test_pool_execute(
3333

3434

3535
async def test_pool_connection(
36-
psql_pool: PSQLPool,
36+
psql_pool: ConnectionPool,
3737
) -> None:
38-
"""Test that PSQLPool can return single connection from the pool."""
38+
"""Test that ConnectionPool can return single connection from the pool."""
3939
connection = await psql_pool.connection()
4040
assert isinstance(connection, Connection)
4141

@@ -51,7 +51,7 @@ async def test_pool_connection(
5151
async def test_pool_conn_recycling_method(
5252
conn_recycling_method: ConnRecyclingMethod,
5353
) -> None:
54-
pg_pool = PSQLPool(
54+
pg_pool = ConnectionPool(
5555
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
5656
conn_recycling_method=conn_recycling_method,
5757
)
@@ -61,13 +61,13 @@ async def test_pool_conn_recycling_method(
6161

6262
async def test_close_connection_pool() -> None:
6363
"""Test that `close` method closes connection pool."""
64-
pg_pool = PSQLPool(
64+
pg_pool = ConnectionPool(
6565
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
6666
)
6767

6868
await pg_pool.execute("SELECT 1")
6969

70-
await pg_pool.close()
70+
pg_pool.close()
7171

7272
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
7373
await pg_pool.execute("SELECT 1")

python/tests/test_cursor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from psqlpy import Cursor, PSQLPool, QueryResult, Transaction
7+
from psqlpy import ConnectionPool, Cursor, QueryResult, Transaction
88

99
pytestmark = pytest.mark.anyio
1010

@@ -151,7 +151,7 @@ async def test_cursor_fetch_backward_all(
151151

152152

153153
async def test_cursor_as_async_manager(
154-
psql_pool: PSQLPool,
154+
psql_pool: ConnectionPool,
155155
table_name: str,
156156
number_database_records: int,
157157
) -> None:

0 commit comments

Comments
 (0)