-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_connection_pool.py
More file actions
160 lines (132 loc) · 4.51 KB
/
test_connection_pool.py
File metadata and controls
160 lines (132 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pytest
from psqlpy import (
Connection,
ConnectionPool,
ConnRecyclingMethod,
LoadBalanceHosts,
TargetSessionAttrs,
connect_pool,
)
from psqlpy.exceptions import (
ConnectionPoolConfigurationError,
InterfaceError,
)
pytestmark = pytest.mark.anyio
async def test_connect_func() -> None:
"""Test that connect function makes new connection pool."""
pg_pool = connect_pool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
)
conn = await pg_pool.connection()
await conn.execute("SELECT 1")
async def test_pool_dsn_startup() -> None:
"""Test that connection pool can startup with dsn."""
pg_pool = ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
)
conn = await pg_pool.connection()
await conn.execute("SELECT 1")
async def test_pool_connection(
psql_pool: ConnectionPool,
) -> None:
"""Test that ConnectionPool can return single connection from the pool."""
connection = await psql_pool.connection()
assert isinstance(connection, Connection)
@pytest.mark.parametrize(
"conn_recycling_method",
[
ConnRecyclingMethod.Fast,
ConnRecyclingMethod.Verified,
ConnRecyclingMethod.Clean,
],
)
async def test_pool_conn_recycling_method(
conn_recycling_method: ConnRecyclingMethod,
) -> None:
pg_pool = ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
conn_recycling_method=conn_recycling_method,
)
conn = await pg_pool.connection()
await conn.execute("SELECT 1")
async def test_build_pool_failure() -> None:
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
connect_timeout_nanosec=12,
)
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
connect_timeout_nanosec=12,
)
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
keepalives_idle_nanosec=12,
)
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
keepalives_interval_nanosec=12,
)
@pytest.mark.parametrize(
"target_session_attrs",
[
TargetSessionAttrs.Any,
TargetSessionAttrs.ReadWrite,
TargetSessionAttrs.ReadOnly,
],
)
async def test_pool_target_session_attrs(
target_session_attrs: TargetSessionAttrs,
) -> None:
pg_pool = ConnectionPool(
db_name="psqlpy_test",
host="localhost",
username="postgres",
password="postgres", # noqa: S106
target_session_attrs=target_session_attrs,
)
if target_session_attrs == TargetSessionAttrs.ReadOnly:
with pytest.raises(expected_exception=InterfaceError):
await pg_pool.connection()
else:
conn = await pg_pool.connection()
await conn.execute("SELECT 1")
@pytest.mark.parametrize(
"load_balance_hosts",
[
LoadBalanceHosts.Disable,
LoadBalanceHosts.Random,
],
)
async def test_pool_load_balance_hosts(
load_balance_hosts: LoadBalanceHosts,
) -> None:
pg_pool = ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
load_balance_hosts=load_balance_hosts,
)
conn = await pg_pool.connection()
await conn.execute("SELECT 1")
async def test_close_connection_pool() -> None:
"""Test that `close` method closes connection pool."""
pg_pool = ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
)
conn = await pg_pool.connection()
await conn.execute("SELECT 1")
pg_pool.close()
with pytest.raises(expected_exception=InterfaceError):
await pg_pool.connection()
async def test_connection_pool_as_context_manager() -> None:
"""Test connection pool as context manager."""
with ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
) as pg_pool:
conn = await pg_pool.connection()
res = await conn.execute("SELECT 1")
assert res.result()
with pytest.raises(expected_exception=InterfaceError):
await pg_pool.connection()