Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for enum reflection #450

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ def get_multi_indexes(

def initialize(self, connection: "Connection") -> None:
DefaultDialect.initialize(self, connection)
self.supports_native_enum = True

def create_connect_args(self, url: URL) -> Tuple[tuple, dict]:
opts = url.translate_connect_args(database="database")
Expand Down
55 changes: 54 additions & 1 deletion duckdb_engine/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,63 @@ def test_table_reflect(session: Session, engine: Engine) -> None:
user_table = Table("test", meta)
insp = inspect(engine)

reflect_table(insp, user_table, None)


def reflect_table(insp: Inspector, *args: Any, **kwargs: Any) -> None:
reflect_table = (
insp.reflecttable if hasattr(insp, "reflecttable") else insp.reflect_table
)
reflect_table(user_table, None)
return reflect_table(*args, **kwargs)


@mark.xfail(raises=AssertionError, reason="not yet supported by duckdb")
def test_enum_type_format(engine: Engine) -> None:
importorskip("duckdb", "0.6.1")

sql = [
"CREATE TYPE enum_t AS ENUM('a', 'b');",
"CREATE TABLE tmp (enum_col enum_t);",
]
with engine.connect() as conn:
for s in sql:
conn.execute(text(s))

(column,) = conn.execute(
text("select * from duckdb_columns() where column_name = 'enum_col'")
)
(type_actual,) = conn.execute(
text("select * from duckdb_types() where type_name = 'enum_t'"),
)

type_oid = type_actual["type_oid"]
data_type_id = column["data_type_id"]

(result,) = conn.execute(
text("SELECT pg_catalog.format_type(:type_oid, 0)"), {"type_oid": type_oid}
)

assert result[0] == "enum"

assert type_oid == data_type_id


def test_enum_reflection(session: Session, engine: Engine) -> None:
importorskip("duckdb", "0.6.1")

sql = [
"CREATE TYPE enum_t AS ENUM('a', 'b');",
"CREATE TABLE tmp (enum_col enum_t);",
"INSERT INTO tmp VALUES ('b');",
"INSERT INTO tmp VALUES ('a');",
]

with engine.connect() as conn:
for s in sql:
conn.execute(text(s))

tmp = Table("tmp", MetaData())
reflect_table(inspect(engine), tmp, include_columns=[])


def test_fetch_df_chunks() -> None:
Expand Down
Loading