Skip to content

Commit 22a5694

Browse files
committed
Added Python 3.13 support
Signed-off-by: chandr-andr (Kiselev Aleksandr) <[email protected]>
1 parent 3ad8e9b commit 22a5694

File tree

4 files changed

+127
-112
lines changed

4 files changed

+127
-112
lines changed

python/tests/test_binary_copy.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import os
2+
import typing
3+
from io import BytesIO
4+
5+
from pgpq import ArrowToPostgresBinaryEncoder
6+
from pyarrow import parquet
7+
8+
from psqlpy import ConnectionPool
9+
10+
11+
async def test_binary_copy_to_table_in_connection(
12+
psql_pool: ConnectionPool,
13+
) -> None:
14+
"""Test binary copy in connection."""
15+
table_name: typing.Final = "cars"
16+
await psql_pool.execute(f"DROP TABLE IF EXISTS {table_name}")
17+
await psql_pool.execute(
18+
"""
19+
CREATE TABLE IF NOT EXISTS cars (
20+
model VARCHAR,
21+
mpg FLOAT8,
22+
cyl INTEGER,
23+
disp FLOAT8,
24+
hp INTEGER,
25+
drat FLOAT8,
26+
wt FLOAT8,
27+
qsec FLOAT8,
28+
vs INTEGER,
29+
am INTEGER,
30+
gear INTEGER,
31+
carb INTEGER
32+
);
33+
""",
34+
)
35+
36+
arrow_table = parquet.read_table(
37+
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/MTcars.parquet", # noqa: PTH120, PTH100
38+
)
39+
encoder = ArrowToPostgresBinaryEncoder(arrow_table.schema)
40+
buf = BytesIO()
41+
buf.write(encoder.write_header())
42+
for batch in arrow_table.to_batches():
43+
buf.write(encoder.write_batch(batch))
44+
buf.write(encoder.finish())
45+
buf.seek(0)
46+
47+
async with psql_pool.acquire() as connection:
48+
inserted_rows = await connection.binary_copy_to_table(
49+
source=buf,
50+
table_name=table_name,
51+
)
52+
53+
expected_inserted_row: typing.Final = 32
54+
55+
assert inserted_rows == expected_inserted_row
56+
57+
real_table_rows: typing.Final = await psql_pool.execute(
58+
f"SELECT COUNT(*) AS rows_count FROM {table_name}",
59+
)
60+
assert real_table_rows.result()[0]["rows_count"] == expected_inserted_row
61+
62+
63+
async def test_binary_copy_to_table_in_transaction(
64+
psql_pool: ConnectionPool,
65+
) -> None:
66+
"""Test binary copy in transaction."""
67+
table_name: typing.Final = "cars"
68+
await psql_pool.execute(f"DROP TABLE IF EXISTS {table_name}")
69+
await psql_pool.execute(
70+
"""
71+
CREATE TABLE IF NOT EXISTS cars (
72+
model VARCHAR,
73+
mpg FLOAT8,
74+
cyl INTEGER,
75+
disp FLOAT8,
76+
hp INTEGER,
77+
drat FLOAT8,
78+
wt FLOAT8,
79+
qsec FLOAT8,
80+
vs INTEGER,
81+
am INTEGER,
82+
gear INTEGER,
83+
carb INTEGER
84+
);
85+
""",
86+
)
87+
88+
arrow_table = parquet.read_table(
89+
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/MTcars.parquet", # noqa: PTH120, PTH100
90+
)
91+
encoder = ArrowToPostgresBinaryEncoder(arrow_table.schema)
92+
buf = BytesIO()
93+
buf.write(encoder.write_header())
94+
for batch in arrow_table.to_batches():
95+
buf.write(encoder.write_batch(batch))
96+
buf.write(encoder.finish())
97+
buf.seek(0)
98+
99+
async with psql_pool.acquire() as connection:
100+
inserted_rows = await connection.binary_copy_to_table(
101+
source=buf,
102+
table_name=table_name,
103+
)
104+
105+
expected_inserted_row: typing.Final = 32
106+
107+
assert inserted_rows == expected_inserted_row
108+
109+
real_table_rows: typing.Final = await psql_pool.execute(
110+
f"SELECT COUNT(*) AS rows_count FROM {table_name}",
111+
)
112+
assert real_table_rows.result()[0]["rows_count"] == expected_inserted_row

python/tests/test_connection.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from __future__ import annotations
22

3-
import os
43
import typing
5-
from io import BytesIO
64

75
import pytest
8-
from pgpq import ArrowToPostgresBinaryEncoder
9-
from pyarrow import parquet
106
from tests.helpers import count_rows_in_test_table
117

128
from psqlpy import ConnectionPool, Cursor, QueryResult, Transaction
@@ -186,58 +182,6 @@ async def test_closed_connection_error(
186182
await connection.execute("SELECT 1")
187183

188184

189-
async def test_binary_copy_to_table(
190-
psql_pool: ConnectionPool,
191-
) -> None:
192-
"""Test binary copy in connection."""
193-
table_name: typing.Final = "cars"
194-
await psql_pool.execute(f"DROP TABLE IF EXISTS {table_name}")
195-
await psql_pool.execute(
196-
"""
197-
CREATE TABLE IF NOT EXISTS cars (
198-
model VARCHAR,
199-
mpg FLOAT8,
200-
cyl INTEGER,
201-
disp FLOAT8,
202-
hp INTEGER,
203-
drat FLOAT8,
204-
wt FLOAT8,
205-
qsec FLOAT8,
206-
vs INTEGER,
207-
am INTEGER,
208-
gear INTEGER,
209-
carb INTEGER
210-
);
211-
""",
212-
)
213-
214-
arrow_table = parquet.read_table(
215-
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/MTcars.parquet", # noqa: PTH120, PTH100
216-
)
217-
encoder = ArrowToPostgresBinaryEncoder(arrow_table.schema)
218-
buf = BytesIO()
219-
buf.write(encoder.write_header())
220-
for batch in arrow_table.to_batches():
221-
buf.write(encoder.write_batch(batch))
222-
buf.write(encoder.finish())
223-
buf.seek(0)
224-
225-
async with psql_pool.acquire() as connection:
226-
inserted_rows = await connection.binary_copy_to_table(
227-
source=buf,
228-
table_name=table_name,
229-
)
230-
231-
expected_inserted_row: typing.Final = 32
232-
233-
assert inserted_rows == expected_inserted_row
234-
235-
real_table_rows: typing.Final = await psql_pool.execute(
236-
f"SELECT COUNT(*) AS rows_count FROM {table_name}",
237-
)
238-
assert real_table_rows.result()[0]["rows_count"] == expected_inserted_row
239-
240-
241185
async def test_execute_batch_method(psql_pool: ConnectionPool) -> None:
242186
"""Test `execute_batch` method."""
243187
await psql_pool.execute(querystring="DROP TABLE IF EXISTS execute_batch")

python/tests/test_transaction.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from __future__ import annotations
22

3-
import os
43
import typing
5-
from io import BytesIO
64

75
import pytest
8-
from pgpq import ArrowToPostgresBinaryEncoder
9-
from pyarrow import parquet
106
from tests.helpers import count_rows_in_test_table
117

128
from psqlpy import (
@@ -346,58 +342,6 @@ async def test_transaction_send_underlying_connection_to_pool_manually(
346342
assert psql_pool.status().available == 1
347343

348344

349-
async def test_binary_copy_to_table(
350-
psql_pool: ConnectionPool,
351-
) -> None:
352-
"""Test binary copy in transaction."""
353-
table_name: typing.Final = "cars"
354-
await psql_pool.execute(f"DROP TABLE IF EXISTS {table_name}")
355-
await psql_pool.execute(
356-
"""
357-
CREATE TABLE IF NOT EXISTS cars (
358-
model VARCHAR,
359-
mpg FLOAT8,
360-
cyl INTEGER,
361-
disp FLOAT8,
362-
hp INTEGER,
363-
drat FLOAT8,
364-
wt FLOAT8,
365-
qsec FLOAT8,
366-
vs INTEGER,
367-
am INTEGER,
368-
gear INTEGER,
369-
carb INTEGER
370-
);
371-
""",
372-
)
373-
374-
arrow_table = parquet.read_table(
375-
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/MTcars.parquet", # noqa: PTH120, PTH100
376-
)
377-
encoder = ArrowToPostgresBinaryEncoder(arrow_table.schema)
378-
buf = BytesIO()
379-
buf.write(encoder.write_header())
380-
for batch in arrow_table.to_batches():
381-
buf.write(encoder.write_batch(batch))
382-
buf.write(encoder.finish())
383-
buf.seek(0)
384-
385-
async with psql_pool.acquire() as connection:
386-
inserted_rows = await connection.binary_copy_to_table(
387-
source=buf,
388-
table_name=table_name,
389-
)
390-
391-
expected_inserted_row: typing.Final = 32
392-
393-
assert inserted_rows == expected_inserted_row
394-
395-
real_table_rows: typing.Final = await psql_pool.execute(
396-
f"SELECT COUNT(*) AS rows_count FROM {table_name}",
397-
)
398-
assert real_table_rows.result()[0]["rows_count"] == expected_inserted_row
399-
400-
401345
async def test_execute_batch_method(psql_pool: ConnectionPool) -> None:
402346
"""Test `execute_batch` method."""
403347
await psql_pool.execute(querystring="DROP TABLE IF EXISTS execute_batch")

tox.ini

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ commands_pre =
3131
maturin develop
3232
commands =
3333
pytest -vv
34+
35+
[testenv:py313]
36+
skip_install = true
37+
deps =
38+
pytest>=7,<8
39+
anyio>=3,<4
40+
maturin>=1,<2
41+
pydantic>=2
42+
pyarrow>=17
43+
pgpq>=0.9
44+
allowlist_externals = maturin
45+
commands_pre =
46+
maturin develop
47+
commands =
48+
pytest -vv --ignore="./python/tests/test_binary_copy.py"

0 commit comments

Comments
 (0)