Skip to content

Commit cb20be1

Browse files
author
Daniil Babin
committed
Refactoring python tests: use env variables for db connection params
1 parent 2549109 commit cb20be1

File tree

3 files changed

+49
-45
lines changed

3 files changed

+49
-45
lines changed

.env_test.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
POSTGRES_HOST=localhost
2+
POSTGRES_USER=postgres
3+
POSTGRES_PASSWORD=postgres
4+
POSTGRES_PORT=5432
5+
POSTGRES_DBNAME=psqlpy_test
6+
POSTGRES_CERT_FILE=/home/runner/work/_temp/pgdata/server.crt

python/tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import random
33
from typing import AsyncGenerator
4+
from urllib import parse
45

56
import pytest
67
from psqlpy import ConnectionPool, Cursor
@@ -78,6 +79,17 @@ def map_parameters_table_name() -> str:
7879
return random_string()
7980

8081

82+
@pytest.fixture
83+
def dsn(
84+
postgres_host: str,
85+
postgres_user: str,
86+
postgres_password: str,
87+
postgres_port: int,
88+
postgres_dbname: str,
89+
) -> str:
90+
return f"postgres://{postgres_user}:{parse.quote(postgres_password)}@{postgres_host}:{postgres_port}/{postgres_dbname}"
91+
92+
8193
@pytest.fixture
8294
def number_database_records() -> int:
8395
return random.randint(10, 35)

python/tests/test_connection_pool.py

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,23 @@
1515
pytestmark = pytest.mark.anyio
1616

1717

18-
async def test_connect_func() -> None:
18+
async def test_connect_func(dsn: str) -> None:
1919
"""Test that connect function makes new connection pool."""
20-
pg_pool = connect_pool(
21-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
22-
)
20+
pg_pool = connect_pool(dsn=dsn)
2321

2422
conn = await pg_pool.connection()
2523
await conn.execute("SELECT 1")
2624

2725

28-
async def test_pool_dsn_startup() -> None:
26+
async def test_pool_dsn_startup(dsn: str) -> None:
2927
"""Test that connection pool can startup with dsn."""
30-
pg_pool = ConnectionPool(
31-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
32-
)
28+
pg_pool = ConnectionPool(dsn=dsn)
3329

3430
conn = await pg_pool.connection()
3531
await conn.execute("SELECT 1")
3632

3733

38-
async def test_pool_connection(
39-
psql_pool: ConnectionPool,
40-
) -> None:
34+
async def test_pool_connection(psql_pool: ConnectionPool) -> None:
4135
"""Test that ConnectionPool can return single connection from the pool."""
4236
connection = await psql_pool.connection()
4337
assert isinstance(connection, Connection)
@@ -53,37 +47,29 @@ async def test_pool_connection(
5347
)
5448
async def test_pool_conn_recycling_method(
5549
conn_recycling_method: ConnRecyclingMethod,
50+
dsn: str,
5651
) -> None:
5752
pg_pool = ConnectionPool(
58-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
53+
dsn=dsn,
5954
conn_recycling_method=conn_recycling_method,
6055
)
6156

6257
conn = await pg_pool.connection()
6358
await conn.execute("SELECT 1")
6459

6560

66-
async def test_build_pool_failure() -> None:
61+
async def test_build_pool_failure(dsn: str) -> None:
6762
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
68-
ConnectionPool(
69-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
70-
connect_timeout_nanosec=12,
71-
)
63+
ConnectionPool(dsn=dsn, connect_timeout_nanosec=12)
64+
7265
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
73-
ConnectionPool(
74-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
75-
connect_timeout_nanosec=12,
76-
)
66+
ConnectionPool(dsn=dsn, connect_timeout_nanosec=12)
67+
7768
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
78-
ConnectionPool(
79-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
80-
keepalives_idle_nanosec=12,
81-
)
69+
ConnectionPool(dsn=dsn, keepalives_idle_nanosec=12)
70+
8271
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
83-
ConnectionPool(
84-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
85-
keepalives_interval_nanosec=12,
86-
)
72+
ConnectionPool(dsn=dsn, keepalives_interval_nanosec=12)
8773

8874

8975
@pytest.mark.parametrize(
@@ -96,12 +82,18 @@ async def test_build_pool_failure() -> None:
9682
)
9783
async def test_pool_target_session_attrs(
9884
target_session_attrs: TargetSessionAttrs,
85+
postgres_host: str,
86+
postgres_user: str,
87+
postgres_password: str,
88+
postgres_port: int,
89+
postgres_dbname: str,
9990
) -> None:
10091
pg_pool = ConnectionPool(
101-
db_name="psqlpy_test",
102-
host="localhost",
103-
username="postgres",
104-
password="postgres", # noqa: S106
92+
db_name=postgres_dbname,
93+
host=postgres_host,
94+
port=postgres_port,
95+
username=postgres_user,
96+
password=postgres_password,
10597
target_session_attrs=target_session_attrs,
10698
)
10799

@@ -122,21 +114,17 @@ async def test_pool_target_session_attrs(
122114
)
123115
async def test_pool_load_balance_hosts(
124116
load_balance_hosts: LoadBalanceHosts,
117+
dsn: str,
125118
) -> None:
126-
pg_pool = ConnectionPool(
127-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
128-
load_balance_hosts=load_balance_hosts,
129-
)
119+
pg_pool = ConnectionPool(dsn=dsn, load_balance_hosts=load_balance_hosts)
130120

131121
conn = await pg_pool.connection()
132122
await conn.execute("SELECT 1")
133123

134124

135-
async def test_close_connection_pool() -> None:
125+
async def test_close_connection_pool(dsn: str) -> None:
136126
"""Test that `close` method closes connection pool."""
137-
pg_pool = ConnectionPool(
138-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
139-
)
127+
pg_pool = ConnectionPool(dsn=dsn)
140128

141129
conn = await pg_pool.connection()
142130
await conn.execute("SELECT 1")
@@ -147,11 +135,9 @@ async def test_close_connection_pool() -> None:
147135
await pg_pool.connection()
148136

149137

150-
async def test_connection_pool_as_context_manager() -> None:
138+
async def test_connection_pool_as_context_manager(dsn: str) -> None:
151139
"""Test connection pool as context manager."""
152-
with ConnectionPool(
153-
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
154-
) as pg_pool:
140+
with ConnectionPool(dsn=dsn) as pg_pool:
155141
conn = await pg_pool.connection()
156142
res = await conn.execute("SELECT 1")
157143
assert res.result()

0 commit comments

Comments
 (0)