Skip to content

Commit

Permalink
Add software tests using Polars
Browse files Browse the repository at this point in the history
Polars does not support writing to databases yet, nor does it support
SQLAlchemy's AsyncEngine. So, this exercises `pl.read_database()` only,
together with the `crate` and `postgresql+psycopg_relaxed` dialects,
using urllib3 resp. Psycopg3.
  • Loading branch information
amotl committed Jan 29, 2024
1 parent 46313e5 commit 973cd13
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ release = [
test = [
"crate[sqlalchemy]",
"pandas<2.1",
"polars[pyarrow]<0.21",
"pytest<8",
"pytest-asyncio<1",
"pytest-cov<5",
Expand Down
43 changes: 43 additions & 0 deletions tests/test_cratedb_polars_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys

import polars as pl
import pytest
import sqlalchemy as sa
from polars.testing import assert_frame_equal

REFERENCE_FRAME = pl.from_records([{"mountain": "Mont Blanc", "height": 4808}])
SQL_SELECT_STATEMENT = "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 1;"


if sys.version_info < (3, 8):
pytest.skip("Does not work on Python 3.7", allow_module_level=True)


def test_crate_read_sql(cratedb_http_host, cratedb_http_port):
engine = sa.create_engine(
url=f"crate://{cratedb_http_host}:{cratedb_http_port}",
echo=True,
)
conn = engine.connect()
df = pl.read_database(
query=SQL_SELECT_STATEMENT,
connection=conn,
schema_overrides={"value": pl.Utf8},
)
assert_frame_equal(df, REFERENCE_FRAME)


def test_psycopg_read_sql(cratedb_psql_host, cratedb_psql_port):
engine = sa.create_engine(
url=f"postgresql+psycopg_relaxed://crate@{cratedb_psql_host}:{cratedb_psql_port}",
isolation_level="AUTOCOMMIT",
use_native_hstore=False,
echo=True,
)
conn = engine.connect()
df = pl.read_database(
query=SQL_SELECT_STATEMENT,
connection=conn,
schema_overrides={"value": pl.Utf8},
)
assert_frame_equal(df, REFERENCE_FRAME)

0 comments on commit 973cd13

Please sign in to comment.