Skip to content

Commit 29041d0

Browse files
authored
Merge pull request #33 from qaspen-python/feature/added_method_to_connect
Added connect method to create ConnectionPool
2 parents 891e713 + 7c81ccb commit 29041d0

File tree

5 files changed

+109
-332
lines changed

5 files changed

+109
-332
lines changed

python/psqlpy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ReadVariant,
99
SingleQueryResult,
1010
Transaction,
11+
connect,
1112
)
1213

1314
__all__ = [
@@ -20,4 +21,5 @@
2021
"ConnRecyclingMethod",
2122
"IsolationLevel",
2223
"ReadVariant",
24+
"connect",
2325
]

python/psqlpy/_internal/__init__.pyi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,27 @@ class ConnectionPool:
936936
"""
937937
def close(self: Self) -> None:
938938
"""Close the connection pool."""
939+
940+
def connect(
941+
dsn: Optional[str] = None,
942+
username: Optional[str] = None,
943+
password: Optional[str] = None,
944+
host: Optional[str] = None,
945+
port: Optional[int] = None,
946+
db_name: Optional[str] = None,
947+
max_db_pool_size: int = 2,
948+
conn_recycling_method: Optional[ConnRecyclingMethod] = None,
949+
) -> ConnectionPool:
950+
"""Create new connection pool.
951+
952+
### Parameters:
953+
- `dsn`: full dsn connection string.
954+
`postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
955+
- `username`: username of the user in postgres
956+
- `password`: password of the user in postgres
957+
- `host`: host of postgres
958+
- `port`: port of postgres
959+
- `db_name`: name of the database in postgres
960+
- `max_db_pool_size`: maximum size of the connection pool
961+
- `conn_recycling_method`: how a connection is recycled.
962+
"""

python/tests/test_connection_pool.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import pytest
22

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

66
pytestmark = pytest.mark.anyio
77

88

9+
async def test_connect_func() -> None:
10+
"""Test that connect function makes new connection pool."""
11+
pg_pool = connect(
12+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
13+
)
14+
15+
await pg_pool.execute("SELECT 1")
16+
17+
918
async def test_pool_dsn_startup() -> None:
1019
"""Test that connection pool can startup with dsn."""
1120
pg_pool = ConnectionPool(

0 commit comments

Comments
 (0)