Skip to content

Commit 2b23c45

Browse files
committed
Added more parameters to configure connection, added tests
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent a019bdc commit 2b23c45

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

python/tests/test_connection_pool.py

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import pytest
22

3-
from psqlpy import Connection, ConnectionPool, ConnRecyclingMethod, QueryResult, connect
4-
from psqlpy.exceptions import RustPSQLDriverPyBaseError
3+
from psqlpy import (
4+
Connection,
5+
ConnectionPool,
6+
ConnLoadBalanceHosts,
7+
ConnRecyclingMethod,
8+
ConnTargetSessionAttrs,
9+
QueryResult,
10+
connect,
11+
)
12+
from psqlpy.exceptions import DBPoolConfigurationError, RustPSQLDriverPyBaseError
513

614
pytestmark = pytest.mark.anyio
715

@@ -68,6 +76,73 @@ async def test_pool_conn_recycling_method(
6876
await pg_pool.execute("SELECT 1")
6977

7078

79+
async def test_build_pool_failure() -> None:
80+
with pytest.raises(expected_exception=DBPoolConfigurationError):
81+
ConnectionPool(
82+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
83+
connect_timeout_nanosec=12,
84+
)
85+
with pytest.raises(expected_exception=DBPoolConfigurationError):
86+
ConnectionPool(
87+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
88+
connect_timeout_nanosec=12,
89+
)
90+
with pytest.raises(expected_exception=DBPoolConfigurationError):
91+
ConnectionPool(
92+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
93+
keepalives_idle_nanosec=12,
94+
)
95+
with pytest.raises(expected_exception=DBPoolConfigurationError):
96+
ConnectionPool(
97+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
98+
keepalives_interval_nanosec=12,
99+
)
100+
101+
102+
@pytest.mark.parametrize(
103+
"target_session_attrs",
104+
[
105+
ConnTargetSessionAttrs.Any,
106+
ConnTargetSessionAttrs.ReadWrite,
107+
ConnTargetSessionAttrs.ReadOnly,
108+
],
109+
)
110+
async def test_pool_target_session_attrs(
111+
target_session_attrs: ConnTargetSessionAttrs,
112+
) -> None:
113+
pg_pool = ConnectionPool(
114+
db_name="psqlpy_test",
115+
host="localhost",
116+
username="postgres",
117+
password="postgres", # noqa: S106
118+
target_session_attrs=target_session_attrs,
119+
)
120+
121+
if target_session_attrs == ConnTargetSessionAttrs.ReadOnly:
122+
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
123+
await pg_pool.execute("SELECT 1")
124+
else:
125+
await pg_pool.execute("SELECT 1")
126+
127+
128+
@pytest.mark.parametrize(
129+
"load_balance_hosts",
130+
[
131+
ConnLoadBalanceHosts.Disable,
132+
ConnLoadBalanceHosts.Random,
133+
],
134+
)
135+
async def test_pool_load_balance_hosts(
136+
load_balance_hosts: ConnLoadBalanceHosts,
137+
) -> None:
138+
pg_pool = ConnectionPool(
139+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
140+
load_balance_hosts=load_balance_hosts,
141+
)
142+
143+
await pg_pool.execute("SELECT 1")
144+
145+
71146
async def test_close_connection_pool() -> None:
72147
"""Test that `close` method closes connection pool."""
73148
pg_pool = ConnectionPool(

src/driver/utils.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ pub fn build_connection_config(
6464
} else {
6565
pg_config = tokio_postgres::Config::new();
6666

67-
if let (Some(password), Some(username)) = (password, username) {
67+
if let Some(password) = password {
6868
pg_config.password(&password);
69+
}
70+
71+
if let Some(username) = username {
6972
pg_config.user(&username);
7073
}
7174

0 commit comments

Comments
 (0)