-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_connection.py
More file actions
193 lines (159 loc) · 5.49 KB
/
test_connection.py
File metadata and controls
193 lines (159 loc) · 5.49 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from __future__ import annotations
import typing
import pytest
from psqlpy import ConnectionPool, Cursor, QueryResult, Transaction
from psqlpy.exceptions import (
ConnectionClosedError,
ConnectionExecuteError,
TransactionExecuteError,
)
from tests.helpers import count_rows_in_test_table
pytestmark = pytest.mark.anyio
async def test_connection_execute(
psql_pool: ConnectionPool,
table_name: str,
number_database_records: int,
) -> None:
"""Test that single connection can execute queries."""
connection = await psql_pool.connection()
conn_result = await connection.execute(
querystring=f"SELECT * FROM {table_name}",
)
assert isinstance(conn_result, QueryResult)
assert len(conn_result.result()) == number_database_records
async def test_connection_fetch(
psql_pool: ConnectionPool,
table_name: str,
number_database_records: int,
) -> None:
"""Test that single connection can fetch queries."""
connection = await psql_pool.connection()
conn_result = await connection.fetch(
querystring=f"SELECT * FROM {table_name}",
)
assert isinstance(conn_result, QueryResult)
assert len(conn_result.result()) == number_database_records
async def test_connection_connection(
psql_pool: ConnectionPool,
) -> None:
"""Test that connection can create transactions."""
connection = await psql_pool.connection()
transaction = connection.transaction()
assert isinstance(transaction, Transaction)
@pytest.mark.parametrize(
("insert_values"),
[
[[1, "name1"], [2, "name2"]],
[[10, "name1"], [20, "name2"], [30, "name3"]],
[[1, "name1"]],
[],
],
)
async def test_connection_execute_many(
psql_pool: ConnectionPool,
table_name: str,
number_database_records: int,
insert_values: list[list[typing.Any]],
) -> None:
connection = await psql_pool.connection()
try:
await connection.execute_many(
f"INSERT INTO {table_name} VALUES ($1, $2)",
insert_values,
)
except TransactionExecuteError:
assert not insert_values
else:
assert await count_rows_in_test_table(
table_name,
connection,
) - number_database_records == len(insert_values)
async def test_connection_fetch_row(
psql_pool: ConnectionPool,
table_name: str,
) -> None:
connection = await psql_pool.connection()
database_single_query_result: typing.Final = await connection.fetch_row(
f"SELECT * FROM {table_name} LIMIT 1",
[],
)
result = database_single_query_result.result()
assert isinstance(result, dict)
async def test_connection_fetch_row_more_than_one_row(
psql_pool: ConnectionPool,
table_name: str,
) -> None:
connection = await psql_pool.connection()
with pytest.raises(ConnectionExecuteError):
await connection.fetch_row(
f"SELECT * FROM {table_name}",
[],
)
async def test_connection_fetch_val(
psql_pool: ConnectionPool,
table_name: str,
) -> None:
connection = await psql_pool.connection()
value: typing.Final = await connection.fetch_val(
f"SELECT COUNT(*) FROM {table_name}",
[],
)
assert isinstance(value, int)
async def test_connection_fetch_val_more_than_one_row(
psql_pool: ConnectionPool,
table_name: str,
) -> None:
connection = await psql_pool.connection()
with pytest.raises(ConnectionExecuteError):
await connection.fetch_row(
f"SELECT * FROM {table_name}",
[],
)
async def test_connection_cursor(
psql_pool: ConnectionPool,
table_name: str,
) -> None:
"""Test cursor from Connection."""
connection = await psql_pool.connection()
cursor: Cursor
transaction = connection.transaction()
await transaction.begin()
cursor = connection.cursor(querystring=f"SELECT * FROM {table_name}")
await cursor.start()
cursor.close()
await transaction.commit()
async def test_connection_async_context_manager(
psql_pool: ConnectionPool,
table_name: str,
number_database_records: int,
) -> None:
"""Test connection as a async context manager."""
async with psql_pool.acquire() as connection:
conn_result = await connection.execute(
querystring=f"SELECT * FROM {table_name}",
)
assert not psql_pool.status().available
assert psql_pool.status().available == 1
assert isinstance(conn_result, QueryResult)
assert len(conn_result.result()) == number_database_records
async def test_closed_connection_error(
psql_pool: ConnectionPool,
) -> None:
"""Test exception when connection is closed."""
connection = await psql_pool.connection()
connection.close()
with pytest.raises(expected_exception=ConnectionClosedError):
await connection.execute("SELECT 1")
async def test_execute_batch_method(psql_pool: ConnectionPool) -> None:
"""Test `execute_batch` method."""
connection = await psql_pool.connection()
await connection.execute(querystring="DROP TABLE IF EXISTS execute_batch")
await connection.execute(querystring="DROP TABLE IF EXISTS execute_batch2")
query = (
"CREATE TABLE execute_batch (name VARCHAR);"
"CREATE TABLE execute_batch2 (name VARCHAR);"
)
async with psql_pool.acquire() as conn:
await conn.execute_batch(querystring=query)
await conn.execute(querystring="SELECT * FROM execute_batch")
await conn.execute(querystring="SELECT * FROM execute_batch2")