| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from harlequin.catalog import Catalog, CatalogItem, InteractiveCatalogItem |
| 9 |
from harlequin.exception import HarlequinConnectionError |
| 10 |
from harlequin_duckdb.adapter import DuckDbAdapter |
| 11 |
|
| 12 |
|
| 13 |
def test_connect(tiny_duck: Path, small_duck: Path, tmp_path: Path) -> None: |
| 14 |
tiny = str(tiny_duck) |
| 15 |
small = str(small_duck) |
| 16 |
assert DuckDbAdapter([], no_init=True).connect() |
| 17 |
assert DuckDbAdapter([":memory:"], no_init=True).connect() |
| 18 |
assert DuckDbAdapter([tiny], read_only=False, no_init=True).connect() |
| 19 |
assert DuckDbAdapter([tiny], read_only=True, no_init=True).connect() |
| 20 |
assert DuckDbAdapter( |
| 21 |
[tiny, str(Path(":memory:")), small], read_only=False, no_init=True |
| 22 |
).connect() |
| 23 |
assert DuckDbAdapter([tiny, small], read_only=True, no_init=True).connect() |
| 24 |
assert DuckDbAdapter([str(tmp_path / "new.db")], no_init=True).connect() |
| 25 |
assert DuckDbAdapter([], allow_unsigned_extensions=True, no_init=True).connect() |
| 26 |
assert DuckDbAdapter([tiny], allow_unsigned_extensions=True, no_init=True).connect() |
| 27 |
assert DuckDbAdapter([tiny, small], read_only=True, no_init=True).connect() |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.online |
| 31 |
def test_connect_extensions() -> None: |
| 32 |
assert DuckDbAdapter([], extension=None, no_init=True).connect() |
| 33 |
assert DuckDbAdapter([], extension=[], no_init=True).connect() |
| 34 |
assert DuckDbAdapter([], extension=["spatial"], no_init=True).connect() |
| 35 |
assert DuckDbAdapter( |
| 36 |
[], allow_unsigned_extensions=True, extension=["spatial"], no_init=True |
| 37 |
).connect() |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.skipif( |
| 41 |
sys.platform == "win32", |
| 42 |
reason="PRQL not supported on windows with this duckdb version.", |
| 43 |
) |
| 44 |
@pytest.mark.online |
| 45 |
def test_connect_prql() -> None: |
| 46 |
# Note: this may fail in the future if the extension doesn't support the latest |
| 47 |
# duckdb version. |
| 48 |
assert DuckDbAdapter( |
| 49 |
[], |
| 50 |
allow_unsigned_extensions=True, |
| 51 |
extension=["prql"], |
| 52 |
custom_extension_repo="http://welsch.lu/duckdb/prql/latest", |
| 53 |
force_install_extensions=True, |
| 54 |
).connect() |
| 55 |
|
| 56 |
|
| 57 |
@pytest.mark.skipif( |
| 58 |
sys.version_info[0:2] != (3, 10), reason="Matrix is hitting MD too many times." |
| 59 |
) |
| 60 |
@pytest.mark.online |
| 61 |
def test_connect_motherduck(tiny_duck: Path) -> None: |
| 62 |
# note: set environment variable motherduck_token |
| 63 |
assert DuckDbAdapter(["md:"], no_init=True) |
| 64 |
assert DuckDbAdapter(["md:cloudf1"], md_saas=True, no_init=True) |
| 65 |
assert DuckDbAdapter(["md:", str(tiny_duck)], no_init=True) |
| 66 |
|
| 67 |
|
| 68 |
def test_cannot_connect(tiny_duck: Path) -> None: |
| 69 |
with pytest.raises(HarlequinConnectionError): |
| 70 |
DuckDbAdapter([":memory:"], read_only=True, no_init=True).connect() |
| 71 |
with pytest.raises(HarlequinConnectionError): |
| 72 |
DuckDbAdapter( |
| 73 |
[str(tiny_duck), ":memory:"], read_only=True, no_init=True |
| 74 |
).connect() |
| 75 |
|
| 76 |
|
| 77 |
def test_get_databases(tiny_duck: Path, small_duck: Path) -> None: |
| 78 |
conn = DuckDbAdapter([str(tiny_duck), str(small_duck)], no_init=True).connect() |
| 79 |
assert conn._get_databases() == [("small",), ("tiny",)] |
| 80 |
|
| 81 |
|
| 82 |
def test_get_schemas(small_duck: Path) -> None: |
| 83 |
conn = DuckDbAdapter([str(small_duck)], read_only=True, no_init=True).connect() |
| 84 |
assert conn._get_schemas("small") == [("empty",), ("main",)] |
| 85 |
|
| 86 |
|
| 87 |
def test_get_tables(small_duck: Path) -> None: |
| 88 |
conn = DuckDbAdapter([str(small_duck)], read_only=True, no_init=True).connect() |
| 89 |
assert conn._get_tables("small", "empty") == [] |
| 90 |
assert conn._get_tables("small", "main") == [("drivers", "BASE TABLE")] |
| 91 |
|
| 92 |
|
| 93 |
def test_get_columns(small_duck: Path) -> None: |
| 94 |
conn = DuckDbAdapter([str(small_duck)], read_only=True, no_init=True).connect() |
| 95 |
assert conn._get_columns("small", "main", "drivers") == [ |
| 96 |
("code", "VARCHAR"), |
| 97 |
("dob", "DATE"), |
| 98 |
("driverId", "BIGINT"), |
| 99 |
("driverRef", "VARCHAR"), |
| 100 |
("forename", "VARCHAR"), |
| 101 |
("nationality", "VARCHAR"), |
| 102 |
("number", "VARCHAR"), |
| 103 |
("surname", "VARCHAR"), |
| 104 |
("url", "VARCHAR"), |
| 105 |
] |
| 106 |
|
| 107 |
|
| 108 |
def test_get_catalog(tiny_duck: Path, small_duck: Path) -> None: |
| 109 |
conn = DuckDbAdapter( |
| 110 |
[str(tiny_duck), str(small_duck)], read_only=True, no_init=True |
| 111 |
).connect() |
| 112 |
expected = Catalog( |
| 113 |
items=[ |
| 114 |
CatalogItem( |
| 115 |
qualified_identifier='"small"', |
| 116 |
query_name='"small"', |
| 117 |
label="small", |
| 118 |
type_label="db", |
| 119 |
children=[ |
| 120 |
CatalogItem( |
| 121 |
qualified_identifier='"small"."empty"', |
| 122 |
query_name='"small"."empty"', |
| 123 |
label="empty", |
| 124 |
type_label="sch", |
| 125 |
children=[], |
| 126 |
), |
| 127 |
CatalogItem( |
| 128 |
qualified_identifier='"small"."main"', |
| 129 |
query_name='"small"."main"', |
| 130 |
label="main", |
| 131 |
type_label="sch", |
| 132 |
children=[ |
| 133 |
CatalogItem( |
| 134 |
qualified_identifier='"small"."main"."drivers"', |
| 135 |
query_name='"small"."main"."drivers"', |
| 136 |
label="drivers", |
| 137 |
type_label="t", |
| 138 |
children=[ |
| 139 |
CatalogItem( |
| 140 |
qualified_identifier='"small"."main"."drivers"."code"', |
| 141 |
query_name='"code"', |
| 142 |
label="code", |
| 143 |
type_label="s", |
| 144 |
), |
| 145 |
CatalogItem( |
| 146 |
qualified_identifier='"small"."main"."drivers"."dob"', |
| 147 |
query_name='"dob"', |
| 148 |
label="dob", |
| 149 |
type_label="d", |
| 150 |
), |
| 151 |
CatalogItem( |
| 152 |
qualified_identifier='"small"."main"."drivers"."driverId"', |
| 153 |
query_name='"driverId"', |
| 154 |
label="driverId", |
| 155 |
type_label="##", |
| 156 |
), |
| 157 |
CatalogItem( |
| 158 |
qualified_identifier='"small"."main"."drivers"."driverRef"', |
| 159 |
query_name='"driverRef"', |
| 160 |
label="driverRef", |
| 161 |
type_label="s", |
| 162 |
), |
| 163 |
CatalogItem( |
| 164 |
qualified_identifier='"small"."main"."drivers"."forename"', |
| 165 |
query_name='"forename"', |
| 166 |
label="forename", |
| 167 |
type_label="s", |
| 168 |
), |
| 169 |
CatalogItem( |
| 170 |
qualified_identifier='"small"."main"."drivers"."nationality"', |
| 171 |
query_name='"nationality"', |
| 172 |
label="nationality", |
| 173 |
type_label="s", |
| 174 |
), |
| 175 |
CatalogItem( |
| 176 |
qualified_identifier='"small"."main"."drivers"."number"', |
| 177 |
query_name='"number"', |
| 178 |
label="number", |
| 179 |
type_label="s", |
| 180 |
), |
| 181 |
CatalogItem( |
| 182 |
qualified_identifier='"small"."main"."drivers"."surname"', |
| 183 |
query_name='"surname"', |
| 184 |
label="surname", |
| 185 |
type_label="s", |
| 186 |
), |
| 187 |
CatalogItem( |
| 188 |
qualified_identifier='"small"."main"."drivers"."url"', |
| 189 |
query_name='"url"', |
| 190 |
label="url", |
| 191 |
type_label="s", |
| 192 |
), |
| 193 |
], |
| 194 |
) |
| 195 |
], |
| 196 |
), |
| 197 |
], |
| 198 |
), |
| 199 |
CatalogItem( |
| 200 |
qualified_identifier='"tiny"', |
| 201 |
query_name='"tiny"', |
| 202 |
label="tiny", |
| 203 |
type_label="db", |
| 204 |
children=[ |
| 205 |
CatalogItem( |
| 206 |
qualified_identifier='"tiny"."main"', |
| 207 |
query_name='"tiny"."main"', |
| 208 |
label="main", |
| 209 |
type_label="sch", |
| 210 |
children=[ |
| 211 |
CatalogItem( |
| 212 |
qualified_identifier='"tiny"."main"."foo"', |
| 213 |
query_name='"tiny"."main"."foo"', |
| 214 |
label="foo", |
| 215 |
type_label="t", |
| 216 |
children=[ |
| 217 |
CatalogItem( |
| 218 |
qualified_identifier='"tiny"."main"."foo"."foo_col"', |
| 219 |
query_name='"foo_col"', |
| 220 |
label="foo_col", |
| 221 |
type_label="#", |
| 222 |
) |
| 223 |
], |
| 224 |
) |
| 225 |
], |
| 226 |
) |
| 227 |
], |
| 228 |
), |
| 229 |
] |
| 230 |
) |
| 231 |
catalog = conn.get_catalog() |
| 232 |
assert [item.label for item in catalog.items] == [ |
| 233 |
item.label for item in expected.items |
| 234 |
] |
| 235 |
for i, database_item in enumerate(catalog.items): |
| 236 |
assert isinstance(database_item, InteractiveCatalogItem) |
| 237 |
assert database_item.children == [] |
| 238 |
schema_items = database_item.fetch_children() |
| 239 |
assert [item.label for item in schema_items] == [ |
| 240 |
item.label for item in expected.items[i].children |
| 241 |
] |
| 242 |
for j, schema_item in enumerate(schema_items): |
| 243 |
assert isinstance(schema_item, InteractiveCatalogItem) |
| 244 |
assert schema_item.children == [] |
| 245 |
relation_items = schema_item.fetch_children() |
| 246 |
assert [(item.label, item.type_label) for item in relation_items] == [ |
| 247 |
(item.label, item.type_label) |
| 248 |
for item in expected.items[i].children[j].children |
| 249 |
] |
| 250 |
for k, relation_item in enumerate(relation_items): |
| 251 |
assert isinstance(relation_item, InteractiveCatalogItem) |
| 252 |
assert relation_item.children == [] |
| 253 |
column_items = relation_item.fetch_children() |
| 254 |
assert [(item.label, item.type_label) for item in column_items] == [ |
| 255 |
(item.label, item.type_label) |
| 256 |
for item in expected.items[i].children[j].children[k].children |
| 257 |
] |
| 258 |
|
| 259 |
|
| 260 |
def test_catalog_items_carry_duckdbs_own_type_names(tmp_path: Path) -> None: |
| 261 |
"""`type_name` is the full type duckdb spells, which a 1-3 character |
| 262 |
`type_label` cannot carry.""" |
| 263 |
conn = DuckDbAdapter([str(tmp_path / "types.db")], no_init=True).connect() |
| 264 |
conn.execute("create table t (id bigint, total decimal(18,2))") |
| 265 |
conn.execute("create view v as select 1 as n") |
| 266 |
|
| 267 |
(database_item,) = conn.get_catalog().items |
| 268 |
assert isinstance(database_item, InteractiveCatalogItem) |
| 269 |
assert database_item.type_name == "database" |
| 270 |
(schema_item,) = database_item.fetch_children() |
| 271 |
assert isinstance(schema_item, InteractiveCatalogItem) |
| 272 |
assert schema_item.type_name == "schema" |
| 273 |
|
| 274 |
relation_items = schema_item.fetch_children() |
| 275 |
assert [(item.label, item.type_name) for item in relation_items] == [ |
| 276 |
("t", "BASE TABLE"), |
| 277 |
("v", "VIEW"), |
| 278 |
] |
| 279 |
|
| 280 |
table_item = relation_items[0] |
| 281 |
assert isinstance(table_item, InteractiveCatalogItem) |
| 282 |
assert [ |
| 283 |
(item.label, item.type_label, item.type_name) |
| 284 |
for item in table_item.fetch_children() |
| 285 |
] == [("id", "##", "BIGINT"), ("total", "#.#", "DECIMAL(18,2)")] |
| 286 |
|
| 287 |
|
| 288 |
def test_search_catalog_finds_relations_and_columns(tmp_path: Path) -> None: |
| 289 |
"""One query answers what a walk would have paid a round trip a level for, |
| 290 |
and every match carries the path that reaches it.""" |
| 291 |
conn = DuckDbAdapter([str(tmp_path / "search.db")], no_init=True).connect() |
| 292 |
conn.execute("create schema analytics") |
| 293 |
conn.execute("create table analytics.orders (id bigint, customer_id bigint)") |
| 294 |
conn.execute("create view analytics.order_summary as select 1 as n") |
| 295 |
conn.execute("create table main.customers (customer_id bigint)") |
| 296 |
|
| 297 |
assert [ |
| 298 |
(result.parents, result.item.label, result.item.type_name) |
| 299 |
for result in conn.search_catalog("ORDER") |
| 300 |
] == [ |
| 301 |
(("search", "analytics"), "order_summary", "VIEW"), |
| 302 |
(("search", "analytics"), "orders", "BASE TABLE"), |
| 303 |
] |
| 304 |
assert [ |
| 305 |
(result.parents, result.item.label, result.item.type_name) |
| 306 |
for result in conn.search_catalog("customer_id") |
| 307 |
] == [ |
| 308 |
(("search", "analytics", "orders"), "customer_id", "BIGINT"), |
| 309 |
(("search", "main", "customers"), "customer_id", "BIGINT"), |
| 310 |
] |
| 311 |
|
| 312 |
|
| 313 |
def test_search_catalog_matches_every_level(tmp_path: Path) -> None: |
| 314 |
"""A caller searching a catalog does not know its shape, so a database or a |
| 315 |
schema named like the term is an answer too -- and each arrives before the |
| 316 |
items under it.""" |
| 317 |
conn = DuckDbAdapter([str(tmp_path / "sales.db")], no_init=True).connect() |
| 318 |
conn.execute("create schema sales_eu") |
| 319 |
conn.execute("create table sales_eu.sales (sales bigint)") |
| 320 |
|
| 321 |
assert [ |
| 322 |
(result.parents, result.item.label, result.item.type_label) |
| 323 |
for result in conn.search_catalog("sales") |
| 324 |
] == [ |
| 325 |
((), "sales", "db"), |
| 326 |
(("sales",), "sales_eu", "sch"), |
| 327 |
(("sales", "sales_eu"), "sales", "t"), |
| 328 |
(("sales", "sales_eu", "sales"), "sales", "##"), |
| 329 |
] |
| 330 |
|
| 331 |
|
| 332 |
def test_search_catalog_takes_one_kind_at_a_time(tmp_path: Path) -> None: |
| 333 |
conn = DuckDbAdapter([str(tmp_path / "kinds.db")], no_init=True).connect() |
| 334 |
conn.execute("create table orders (orders bigint)") |
| 335 |
|
| 336 |
assert [ |
| 337 |
result.item.type_label for result in conn.search_catalog("orders", "relations") |
| 338 |
] == ["t"] |
| 339 |
assert [ |
| 340 |
result.item.type_label for result in conn.search_catalog("orders", "columns") |
| 341 |
] == ["##"] |
| 342 |
assert len(conn.search_catalog("orders", "all")) == 2 |
| 343 |
|
| 344 |
|
| 345 |
def test_search_catalog_reads_a_wildcard_as_a_character(tmp_path: Path) -> None: |
| 346 |
"""The term is a substring, so a LIKE metacharacter in it matches itself.""" |
| 347 |
conn = DuckDbAdapter([str(tmp_path / "wild.db")], no_init=True).connect() |
| 348 |
conn.execute('create table "a%b" (n bigint)') |
| 349 |
conn.execute("create table ab (n bigint)") |
| 350 |
|
| 351 |
assert [result.item.label for result in conn.search_catalog("a%b")] == ["a%b"] |
| 352 |
assert [result.item.label for result in conn.search_catalog("a_b")] == [] |
| 353 |
|
| 354 |
|
| 355 |
def test_search_catalog_reports_only_paths_the_catalog_walks(tmp_path: Path) -> None: |
| 356 |
"""duckdb's temp catalog holds objects the catalog tree never shows, and a |
| 357 |
path a listing cannot reach is not a path worth printing.""" |
| 358 |
conn = DuckDbAdapter([str(tmp_path / "temp.db")], no_init=True).connect() |
| 359 |
conn.execute("create temp table tmp_orders (n bigint)") |
| 360 |
|
| 361 |
assert conn.search_catalog("tmp_orders") == [] |
| 362 |
|
| 363 |
|
| 364 |
def test_init_script(tiny_duck: Path, tmp_path: Path) -> None: |
| 365 |
script = ( |
| 366 |
f".bail on\nselect \n1;\n.bail off\n.open {tiny_duck}\n" |
| 367 |
"create table test_init as select 2;" |
| 368 |
) |
| 369 |
commands = DuckDbAdapter._split_script(script) |
| 370 |
assert len(commands) == 5 |
| 371 |
rewritten = [DuckDbAdapter._rewrite_init_command(cmd) for cmd in commands] |
| 372 |
assert rewritten[0] == "" |
| 373 |
assert rewritten[1] == commands[1] |
| 374 |
assert rewritten[2] == "" |
| 375 |
assert rewritten[3].startswith(f"attach '{tiny_duck}'") |
| 376 |
assert rewritten[4] == commands[4] |
| 377 |
|
| 378 |
with open(tmp_path / "myscript", "w") as f: |
| 379 |
f.write(script) |
| 380 |
|
| 381 |
conn = DuckDbAdapter([":memory:"], init_path=tmp_path / "myscript").connect() |
| 382 |
cur = conn.execute("select * from test_init") |
| 383 |
assert cur |
| 384 |
assert cur.relation.fetchall() == [(2,)] |
| 385 |
|
| 386 |
|
| 387 |
def test_initialize_adapter_ignores_extra_kwargs() -> None: |
| 388 |
adapter = DuckDbAdapter((":memory:",), foo="bar") |
| 389 |
assert adapter |
| 390 |
|
| 391 |
|
| 392 |
def test_transaction_mode() -> None: |
| 393 |
adapter = DuckDbAdapter((":memory:",)) |
| 394 |
conn = adapter.connect() |
| 395 |
assert conn.transaction_mode is None |
| 396 |
assert conn.toggle_transaction_mode() is None |
| 397 |
assert conn.transaction_mode is None |