Skip to content

Commit

Permalink
Merge pull request #97 from psqlpy-python/release/0.8.2
Browse files Browse the repository at this point in the history
Added Python 3.13 support
  • Loading branch information
chandr-andr authored Oct 17, 2024
2 parents 89f92bf + 3ba42e6 commit fd9a25a
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 191 deletions.
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

0 comments on commit fd9a25a

Please sign in to comment.