Skip to content

Commit 444464f

Browse files
committed
Prepared psqlpy for OTLP
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 59c9c62 commit 444464f

File tree

4 files changed

+147
-122
lines changed

4 files changed

+147
-122
lines changed

python/tests/test_binary_copy.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ async def test_binary_copy_to_table_in_connection(
4747
buf.write(encoder.finish())
4848
buf.seek(0)
4949

50-
async with psql_pool.acquire() as connection:
51-
inserted_rows = await connection.binary_copy_to_table(
52-
source=buf,
53-
table_name=table_name,
54-
)
50+
inserted_rows = await connection.binary_copy_to_table(
51+
source=buf,
52+
table_name=table_name,
53+
)
5554

5655
expected_inserted_row: typing.Final = 32
5756

python/tests/test_listener.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ async def notify(
6262
if with_delay:
6363
await asyncio.sleep(0.5)
6464

65-
await (await psql_pool.connection()).execute(f"NOTIFY {channel}, '{TEST_PAYLOAD}'")
65+
connection = await psql_pool.connection()
66+
await connection.execute(f"NOTIFY {channel}, '{TEST_PAYLOAD}'")
67+
connection.back_to_pool()
6668

6769

6870
async def check_insert_callback(
@@ -71,8 +73,9 @@ async def check_insert_callback(
7173
is_insert_exist: bool = True,
7274
number_of_data: int = 1,
7375
) -> None:
76+
connection = await psql_pool.connection()
7477
test_data_seq = (
75-
await (await psql_pool.connection()).execute(
78+
await connection.execute(
7679
f"SELECT * FROM {listener_table_name}",
7780
)
7881
).result()
@@ -88,14 +91,18 @@ async def check_insert_callback(
8891
assert data_record["payload"] == TEST_PAYLOAD
8992
assert data_record["channel"] == TEST_CHANNEL
9093

94+
connection.back_to_pool()
95+
9196

9297
async def clear_test_table(
9398
psql_pool: ConnectionPool,
9499
listener_table_name: str,
95100
) -> None:
96-
await (await psql_pool.connection()).execute(
101+
connection = await psql_pool.connection()
102+
await connection.execute(
97103
f"DELETE FROM {listener_table_name}",
98104
)
105+
connection.back_to_pool()
99106

100107

101108
@pytest.mark.usefixtures("create_table_for_listener_tests")
@@ -244,7 +251,8 @@ async def test_listener_more_than_one_callback(
244251
number_of_data=2,
245252
)
246253

247-
query_result = await (await psql_pool.connection()).execute(
254+
connection = await psql_pool.connection()
255+
query_result = await connection.execute(
248256
querystring=(f"SELECT * FROM {listener_table_name} WHERE channel = $1"),
249257
parameters=(additional_channel,),
250258
)

python/tests/test_transaction.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ async def test_transaction_init_parameters(
3939
deferrable: bool | None,
4040
read_variant: ReadVariant | None,
4141
) -> None:
42-
connection = await psql_pool.connection()
43-
async with connection.transaction(
42+
async with psql_pool.acquire() as connection, connection.transaction(
4443
isolation_level=isolation_level,
4544
deferrable=deferrable,
4645
read_variant=read_variant,
@@ -79,6 +78,8 @@ async def test_transaction_begin(
7978

8079
assert len(result.result()) == number_database_records
8180

81+
await transaction.commit()
82+
8283

8384
async def test_transaction_commit(
8485
psql_pool: ConnectionPool,
@@ -97,15 +98,16 @@ async def test_transaction_commit(
9798

9899
# Make request from other connection, it mustn't know
99100
# about new INSERT data before commit.
100-
result = await (await psql_pool.connection()).execute(
101+
connection = await psql_pool.connection()
102+
result = await connection.execute(
101103
f"SELECT * FROM {table_name} WHERE name = $1",
102104
parameters=[test_name],
103105
)
104106
assert not result.result()
105107

106108
await transaction.commit()
107109

108-
result = await (await psql_pool.connection()).execute(
110+
result = await connection.execute(
109111
f"SELECT * FROM {table_name} WHERE name = $1",
110112
parameters=[test_name],
111113
)
@@ -136,7 +138,8 @@ async def test_transaction_savepoint(
136138
assert result.result()
137139

138140
await transaction.rollback_savepoint(savepoint_name=savepoint_name)
139-
result = await (await psql_pool.connection()).execute(
141+
connection = await psql_pool.connection()
142+
result = await connection.execute(
140143
f"SELECT * FROM {table_name} WHERE name = $1",
141144
parameters=[test_name],
142145
)
@@ -174,10 +177,12 @@ async def test_transaction_rollback(
174177
parameters=[test_name],
175178
)
176179

177-
result_from_conn = await (await psql_pool.connection()).execute(
180+
connection = await psql_pool.connection()
181+
result_from_conn = await connection.execute(
178182
f"INSERT INTO {table_name} VALUES ($1, $2)",
179183
parameters=[100, test_name],
180184
)
185+
connection.back_to_pool()
181186

182187
assert not (result_from_conn.result())
183188

@@ -344,14 +349,17 @@ async def test_transaction_send_underlying_connection_to_pool_manually(
344349

345350
async def test_execute_batch_method(psql_pool: ConnectionPool) -> None:
346351
"""Test `execute_batch` method."""
347-
await (await psql_pool.connection()).execute(querystring="DROP TABLE IF EXISTS execute_batch")
348-
await (await psql_pool.connection()).execute(querystring="DROP TABLE IF EXISTS execute_batch2")
352+
connection = await psql_pool.connection()
353+
await connection.execute(querystring="DROP TABLE IF EXISTS execute_batch")
354+
await connection.execute(querystring="DROP TABLE IF EXISTS execute_batch2")
349355
query = "CREATE TABLE execute_batch (name VARCHAR);CREATE TABLE execute_batch2 (name VARCHAR);"
350-
async with psql_pool.acquire() as conn, conn.transaction() as transaction:
356+
async with connection.transaction() as transaction:
351357
await transaction.execute_batch(querystring=query)
352358
await transaction.execute(querystring="SELECT * FROM execute_batch")
353359
await transaction.execute(querystring="SELECT * FROM execute_batch2")
354360

361+
connection.back_to_pool()
362+
355363

356364
@pytest.mark.parametrize(
357365
"synchronous_commit",

0 commit comments

Comments
 (0)