|
| 1 | +import sys |
| 2 | + |
| 3 | +import polars as pl |
| 4 | +import pytest |
| 5 | +import sqlalchemy as sa |
| 6 | +from polars.testing import assert_frame_equal |
| 7 | + |
| 8 | +REFERENCE_FRAME = pl.from_records([{"mountain": "Mont Blanc", "height": 4808}]) |
| 9 | +SQL_SELECT_STATEMENT = "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 1;" |
| 10 | + |
| 11 | + |
| 12 | +if sys.version_info < (3, 8): |
| 13 | + pytest.skip("Does not work on Python 3.7", allow_module_level=True) |
| 14 | + |
| 15 | + |
| 16 | +def test_crate_read_sql(cratedb_http_host, cratedb_http_port): |
| 17 | + engine = sa.create_engine( |
| 18 | + url=f"crate://{cratedb_http_host}:{cratedb_http_port}", |
| 19 | + echo=True, |
| 20 | + ) |
| 21 | + conn = engine.connect() |
| 22 | + df = pl.read_database( |
| 23 | + query=SQL_SELECT_STATEMENT, |
| 24 | + connection=conn, |
| 25 | + schema_overrides={"value": pl.Utf8}, |
| 26 | + ) |
| 27 | + assert_frame_equal(df, REFERENCE_FRAME) |
| 28 | + |
| 29 | + |
| 30 | +def test_psycopg_read_sql(cratedb_psql_host, cratedb_psql_port): |
| 31 | + engine = sa.create_engine( |
| 32 | + url=f"postgresql+psycopg_relaxed://crate@{cratedb_psql_host}:{cratedb_psql_port}", |
| 33 | + isolation_level="AUTOCOMMIT", |
| 34 | + use_native_hstore=False, |
| 35 | + echo=True, |
| 36 | + ) |
| 37 | + conn = engine.connect() |
| 38 | + df = pl.read_database( |
| 39 | + query=SQL_SELECT_STATEMENT, |
| 40 | + connection=conn, |
| 41 | + schema_overrides={"value": pl.Utf8}, |
| 42 | + ) |
| 43 | + assert_frame_equal(df, REFERENCE_FRAME) |
0 commit comments