Skip to content
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

Rebuilding connection, make available create single connection #36

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions python/psqlpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
SingleQueryResult,
Transaction,
connect,
create_pool,
)

__all__ = [
Expand All @@ -21,5 +22,6 @@
"ConnRecyclingMethod",
"IsolationLevel",
"ReadVariant",
"create_pool",
"connect",
]
22 changes: 21 additions & 1 deletion python/psqlpy/_internal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ class ConnectionPool:
def close(self: Self) -> None:
"""Close the connection pool."""

def connect(
def create_pool(
dsn: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
Expand All @@ -963,3 +963,23 @@ def connect(
- `max_db_pool_size`: maximum size of the connection pool
- `conn_recycling_method`: how a connection is recycled.
"""

async def connect(
dsn: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
host: Optional[str] = None,
port: Optional[int] = None,
db_name: Optional[str] = None,
) -> Connection:
"""Create new single connection.

### Parameters:
- `dsn`: full dsn connection string.
`postgres://postgres:postgres@localhost:5432/postgres?target_session_attrs=read-write`
- `username`: username of the user in the PostgreSQL
- `password`: password of the user in PostgreSQL
- `host`: host of the PostgreSQL
- `port`: port of the PostgreSQL
- `db_name`: name of the database in PostgreSQL
"""
11 changes: 10 additions & 1 deletion python/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from tests.helpers import count_rows_in_test_table

from psqlpy import ConnectionPool, QueryResult, Transaction
from psqlpy import ConnectionPool, QueryResult, Transaction, connect
from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError

pytestmark = pytest.mark.anyio
Expand Down Expand Up @@ -113,3 +113,12 @@ async def test_connection_fetch_val_more_than_one_row(
f"SELECT * FROM {table_name}",
[],
)


async def test_connect_method() -> None:
connection = await connect(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
)

res = await connection.execute("SELECT 1")
assert res.result()
10 changes: 8 additions & 2 deletions python/tests/test_connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import pytest

from psqlpy import Connection, ConnectionPool, ConnRecyclingMethod, QueryResult, connect
from psqlpy import (
Connection,
ConnectionPool,
ConnRecyclingMethod,
QueryResult,
create_pool,
)
from psqlpy.exceptions import RustPSQLDriverPyBaseError

pytestmark = pytest.mark.anyio


async def test_connect_func() -> None:
"""Test that connect function makes new connection pool."""
pg_pool = connect(
pg_pool = create_pool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
)

Expand Down
Loading
Loading