|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | +import sqlalchemy as sa |
| 4 | +from pandas._testing import assert_frame_equal |
| 5 | +from sqlalchemy.ext.asyncio import create_async_engine |
| 6 | + |
| 7 | +INPUT_FRAME = pd.DataFrame.from_records([{"id": 1, "name": "foo", "value": 42.42}]) |
| 8 | +SQL_SELECT_STATEMENT = "SELECT * FROM doc.foo;" |
| 9 | +SQL_REFRESH_STATEMENT = "REFRESH TABLE doc.foo;" |
| 10 | + |
| 11 | + |
| 12 | +pytest.skip("Does not work on Python 3.7", allow_module_level=True) |
| 13 | + |
| 14 | + |
| 15 | +def test_crate_to_sql(cratedb_http_host, cratedb_http_port): |
| 16 | + # Connect to database. |
| 17 | + engine = sa.create_engine( |
| 18 | + url=f"crate://{cratedb_http_host}:{cratedb_http_port}", |
| 19 | + echo=True, |
| 20 | + ) |
| 21 | + con = engine.connect() |
| 22 | + |
| 23 | + # Insert data using pandas. |
| 24 | + df = INPUT_FRAME |
| 25 | + retval = df.to_sql(name="foo", con=con, if_exists="replace", index=False) |
| 26 | + assert retval == -1 |
| 27 | + |
| 28 | + # Synchronize table content. |
| 29 | + con.execute(sa.text(SQL_REFRESH_STATEMENT)) |
| 30 | + |
| 31 | + # Read back and verify data using pandas. |
| 32 | + df = pd.read_sql(sql=sa.text(SQL_SELECT_STATEMENT), con=con) |
| 33 | + assert_frame_equal(df, INPUT_FRAME) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.skip(reason="Needs COLLATE and pg_table_is_visible") |
| 37 | +def test_psycopg_to_sql(cratedb_psql_host, cratedb_psql_port): |
| 38 | + # Connect to database. |
| 39 | + engine = sa.create_engine( |
| 40 | + url=f"postgresql+psycopg_relaxed://crate@{cratedb_psql_host}:{cratedb_psql_port}", |
| 41 | + isolation_level="AUTOCOMMIT", |
| 42 | + use_native_hstore=False, |
| 43 | + echo=True, |
| 44 | + ) |
| 45 | + conn = engine.connect() |
| 46 | + |
| 47 | + # Insert data using pandas. |
| 48 | + df = INPUT_FRAME |
| 49 | + retval = df.to_sql(name="foo", con=conn, if_exists="replace", index=False) |
| 50 | + assert retval == -1 |
| 51 | + |
| 52 | + # Synchronize table content. |
| 53 | + conn.execute(sa.text(SQL_REFRESH_STATEMENT)) |
| 54 | + |
| 55 | + # Read back and verify data using pandas. |
| 56 | + df = pd.read_sql(sql=sa.text(SQL_SELECT_STATEMENT), con=conn) |
| 57 | + assert_frame_equal(df, INPUT_FRAME) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.skip(reason="Needs COLLATE and pg_table_is_visible") |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_psycopg_async_to_sql(cratedb_psql_host, cratedb_psql_port): |
| 63 | + # Connect to database. |
| 64 | + engine = create_async_engine( |
| 65 | + url=f"postgresql+psycopg_relaxed://crate@{cratedb_psql_host}:{cratedb_psql_port}", |
| 66 | + isolation_level="AUTOCOMMIT", |
| 67 | + use_native_hstore=False, |
| 68 | + echo=True, |
| 69 | + ) |
| 70 | + |
| 71 | + # Insert data using pandas. |
| 72 | + async with engine.begin() as conn: |
| 73 | + df = INPUT_FRAME |
| 74 | + retval = await conn.run_sync(to_sql_sync, df=df, name="foo", if_exists="replace", index=False) |
| 75 | + assert retval == -1 |
| 76 | + |
| 77 | + # TODO: Read back dataframe and compare with original. |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.skip(reason="Needs COLLATE and pg_table_is_visible") |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_asyncpg_to_sql(cratedb_psql_host, cratedb_psql_port): |
| 83 | + # Connect to database. |
| 84 | + engine = create_async_engine( |
| 85 | + url=f"postgresql+asyncpg_relaxed://crate@{cratedb_psql_host}:{cratedb_psql_port}", |
| 86 | + isolation_level="AUTOCOMMIT", |
| 87 | + echo=True, |
| 88 | + ) |
| 89 | + |
| 90 | + # Insert data using pandas. |
| 91 | + async with engine.begin() as conn: |
| 92 | + df = INPUT_FRAME |
| 93 | + retval = await conn.run_sync(to_sql_sync, df=df, name="foo", if_exists="replace", index=False) |
| 94 | + assert retval == -1 |
| 95 | + |
| 96 | + # TODO: Read back dataframe and compare with original. |
| 97 | + |
| 98 | + |
| 99 | +def to_sql_sync(conn, df, name, **kwargs): |
| 100 | + """ |
| 101 | + Making df.to_sql connection the first argument to make it compatible |
| 102 | + with conn.run_sync(), see https://stackoverflow.com/a/70861276. |
| 103 | + """ |
| 104 | + return df.to_sql(name=name, con=conn, **kwargs) |
0 commit comments