Skip to content

Commit

Permalink
try TILED_TEST_POSTGRESQL_URI usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Seher Karakuzu authored and Seher Karakuzu committed Aug 30, 2024
1 parent ab08191 commit bdd88e0
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions tiled/_tests/adapters/test_sql.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import tempfile

import adbc_driver_postgresql
import adbc_driver_sqlite
import pyarrow as pa
import pytest

from tiled._tests.conftest import TILED_TEST_POSTGRESQL_URI
from tiled.adapters.sql import SQLAdapter
from tiled.structures.table import TableStructure

Expand Down Expand Up @@ -46,55 +48,72 @@ def test_invalid_structure() -> None:


@pytest.fixture
def adapter() -> SQLAdapter:
def adapter_sql() -> SQLAdapter:
data_uri = "sqlite://file://localhost" + tempfile.gettempdir() + "/test.db"
table = pa.Table.from_arrays(data0, names)
structure = TableStructure.from_arrow_table(table, npartitions=1)
asset = SQLAdapter.init_storage(data_uri, structure=structure)
return SQLAdapter(asset.data_uri, structure=structure)


def test_attributes(adapter: SQLAdapter) -> None:
assert adapter.structure().columns == names
assert adapter.structure().npartitions == 1
assert isinstance(adapter.conn, adbc_driver_sqlite.dbapi.AdbcSqliteConnection)
def test_attributes(adapter_sql: SQLAdapter) -> None:
assert adapter_sql.structure().columns == names
assert adapter_sql.structure().npartitions == 1
assert isinstance(adapter_sql.conn, adbc_driver_sqlite.dbapi.AdbcSqliteConnection)


def test_write_read(adapter: SQLAdapter) -> None:
# test writing to a partition and reading it
adapter.write(batch0)
result = adapter.read()
def test_write_read(adapter_sql: SQLAdapter) -> None:
# test writing and reading it
adapter_sql.write(batch0)
result = adapter_sql.read()
# the pandas dataframe gives the last column of the data as 0 and 1 since SQL does not save boolean
# so we explicitely convert the last column to boolean for testing purposes
result["f2"] = result["f2"].astype("boolean")

assert pa.Table.from_arrays(data0, names) == pa.Table.from_pandas(result)

adapter.write([batch0, batch1])
result = adapter.read()
adapter_sql.write([batch0, batch1])
result = adapter_sql.read()
# the pandas dataframe gives the last column of the data as 0 and 1 since SQL does not save boolean
# so we explicitely convert the last column to boolean for testing purposes
result["f2"] = result["f2"].astype("boolean")
assert pa.Table.from_batches([batch0, batch1]) == pa.Table.from_pandas(result)

adapter.write([batch0, batch1, batch2])
result = adapter.read()
adapter_sql.write([batch0, batch1, batch2])
result = adapter_sql.read()
# the pandas dataframe gives the last column of the data as 0 and 1 since SQL does not save boolean
# so we explicitely convert the last column to boolean for testing purposes
result["f2"] = result["f2"].astype("boolean")
assert pa.Table.from_batches([batch0, batch1, batch2]) == pa.Table.from_pandas(
result
)

# test write to all partitions and read all
adapter.write([batch0, batch1, batch2])
adapter.append([batch2, batch0, batch1])
adapter.append([batch1, batch2, batch0])
result = adapter.read()
# test write , append and read all
adapter_sql.write([batch0, batch1, batch2])
adapter_sql.append([batch2, batch0, batch1])
adapter_sql.append([batch1, batch2, batch0])
result = adapter_sql.read()
# the pandas dataframe gives the last column of the data as 0 and 1 since SQL does not save boolean
# so we explicitely convert the last column to boolean for testing purposes
result["f2"] = result["f2"].astype("boolean")

assert pa.Table.from_batches(
[batch0, batch1, batch2, batch2, batch0, batch1, batch1, batch2, batch0]
) == pa.Table.from_pandas(result)


@pytest.fixture
def adapter_psql() -> SQLAdapter:
data_uri = TILED_TEST_POSTGRESQL_URI
table = pa.Table.from_arrays(data0, names)
structure = TableStructure.from_arrow_table(table, npartitions=1)
asset = SQLAdapter.init_storage(data_uri, structure=structure)
return SQLAdapter(asset.data_uri, structure=structure)


def test_psql(adapter_psql: SQLAdapter) -> None:
assert adapter_psql.structure().columns == names
assert adapter_psql.structure().npartitions == 1
assert isinstance(
adapter_psql.conn, adbc_driver_postgresql.dbapi.AdbcSqliteConnection
)

0 comments on commit bdd88e0

Please sign in to comment.