Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Python 3.13 support #97

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
name: ${{matrix.job.os}}-${{matrix.py_version}}
strategy:
matrix:
py_version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
py_version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
job:
- os: ubuntu-latest
ssl_cmd: sudo apt-get update && sudo apt-get install libssl-dev openssl
Expand Down
78 changes: 36 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "psqlpy"
version = "0.8.1"
version = "0.8.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -14,10 +14,14 @@ pyo3 = { version = "*", features = [
"chrono",
"experimental-async",
"rust_decimal",
"py-clone",
"gil-refs",
"macros",
] }
pyo3-asyncio = { git = "https://github.com/chandr-andr/pyo3-asyncio.git", version = "0.20.0", features = [
pyo3-async-runtimes = { git = "https://github.com/chandr-andr/pyo3-async-runtimes.git", branch = "main", features = [
"tokio-runtime",
] }

tokio = { version = "1.35.1", features = ["full"] }
thiserror = "1.0.56"
bytes = "1.5.0"
Expand Down
115 changes: 115 additions & 0 deletions python/tests/test_binary_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import os
import typing
from io import BytesIO

import pytest
from pgpq import ArrowToPostgresBinaryEncoder
from pyarrow import parquet

from psqlpy import ConnectionPool

pytestmark = pytest.mark.anyio


async def test_binary_copy_to_table_in_connection(
psql_pool: ConnectionPool,
) -> None:
"""Test binary copy in connection."""
table_name: typing.Final = "cars"
await psql_pool.execute(f"DROP TABLE IF EXISTS {table_name}")
await psql_pool.execute(
"""
CREATE TABLE IF NOT EXISTS cars (
model VARCHAR,
mpg FLOAT8,
cyl INTEGER,
disp FLOAT8,
hp INTEGER,
drat FLOAT8,
wt FLOAT8,
qsec FLOAT8,
vs INTEGER,
am INTEGER,
gear INTEGER,
carb INTEGER
);
""",
)

arrow_table = parquet.read_table(
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/MTcars.parquet", # noqa: PTH120, PTH100
)
encoder = ArrowToPostgresBinaryEncoder(arrow_table.schema)
buf = BytesIO()
buf.write(encoder.write_header())
for batch in arrow_table.to_batches():
buf.write(encoder.write_batch(batch))
buf.write(encoder.finish())
buf.seek(0)

async with psql_pool.acquire() as connection:
inserted_rows = await connection.binary_copy_to_table(
source=buf,
table_name=table_name,
)

expected_inserted_row: typing.Final = 32

assert inserted_rows == expected_inserted_row

real_table_rows: typing.Final = await psql_pool.execute(
f"SELECT COUNT(*) AS rows_count FROM {table_name}",
)
assert real_table_rows.result()[0]["rows_count"] == expected_inserted_row


async def test_binary_copy_to_table_in_transaction(
psql_pool: ConnectionPool,
) -> None:
"""Test binary copy in transaction."""
table_name: typing.Final = "cars"
await psql_pool.execute(f"DROP TABLE IF EXISTS {table_name}")
await psql_pool.execute(
"""
CREATE TABLE IF NOT EXISTS cars (
model VARCHAR,
mpg FLOAT8,
cyl INTEGER,
disp FLOAT8,
hp INTEGER,
drat FLOAT8,
wt FLOAT8,
qsec FLOAT8,
vs INTEGER,
am INTEGER,
gear INTEGER,
carb INTEGER
);
""",
)

arrow_table = parquet.read_table(
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/MTcars.parquet", # noqa: PTH120, PTH100
)
encoder = ArrowToPostgresBinaryEncoder(arrow_table.schema)
buf = BytesIO()
buf.write(encoder.write_header())
for batch in arrow_table.to_batches():
buf.write(encoder.write_batch(batch))
buf.write(encoder.finish())
buf.seek(0)

async with psql_pool.acquire() as connection:
inserted_rows = await connection.binary_copy_to_table(
source=buf,
table_name=table_name,
)

expected_inserted_row: typing.Final = 32

assert inserted_rows == expected_inserted_row

real_table_rows: typing.Final = await psql_pool.execute(
f"SELECT COUNT(*) AS rows_count FROM {table_name}",
)
assert real_table_rows.result()[0]["rows_count"] == expected_inserted_row
Loading
Loading