|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import Any, Optional |
| 3 | + |
| 4 | +import pytest |
| 5 | +from dbt.adapters.capability import Capability, Support |
| 6 | +from dbt_common.exceptions import DbtValidationError |
| 7 | + |
| 8 | +from dbt.adapters.databricks.catalogs import UnityCatalogIntegration |
| 9 | +from dbt.adapters.databricks.impl import DatabricksAdapter |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class _Config: |
| 14 | + """Minimal CatalogIntegrationConfig stub for testing __init__ validation.""" |
| 15 | + |
| 16 | + name: str = "test_cat" |
| 17 | + catalog_type: str = "unity" |
| 18 | + catalog_name: Optional[str] = None |
| 19 | + table_format: Optional[str] = "iceberg" |
| 20 | + external_volume: Optional[str] = None |
| 21 | + file_format: Optional[str] = None |
| 22 | + adapter_properties: dict[str, Any] = field(default_factory=dict) |
| 23 | + |
| 24 | + |
| 25 | +# ===== Adapter-level ===== |
| 26 | + |
| 27 | + |
| 28 | +def test_catalogs_v2_capability_declared(): |
| 29 | + catalogs_v2 = getattr(Capability, "CatalogsV2", None) |
| 30 | + if catalogs_v2 is None: |
| 31 | + pytest.skip("CatalogsV2 not available in this dbt-adapters version") |
| 32 | + cap = DatabricksAdapter._capabilities[catalogs_v2] |
| 33 | + assert cap.support == Support.Full |
| 34 | + |
| 35 | + |
| 36 | +def test_v2_to_v1_type_unity(): |
| 37 | + adapter = object.__new__(DatabricksAdapter) |
| 38 | + assert adapter._v2_to_v1_type("unity") == "unity" |
| 39 | + |
| 40 | + |
| 41 | +def test_v2_to_v1_type_hive_metastore(): |
| 42 | + adapter = object.__new__(DatabricksAdapter) |
| 43 | + assert adapter._v2_to_v1_type("hive_metastore") == "hive_metastore" |
| 44 | + |
| 45 | + |
| 46 | +def test_v2_to_v1_type_unknown_passthrough(): |
| 47 | + adapter = object.__new__(DatabricksAdapter) |
| 48 | + assert adapter._v2_to_v1_type("custom_type") == "custom_type" |
| 49 | + |
| 50 | + |
| 51 | +# ===== UnityCatalogIntegration ===== |
| 52 | + |
| 53 | + |
| 54 | +def test_unity_parquet_without_uniform(): |
| 55 | + cfg = _Config(file_format="parquet") |
| 56 | + integration = UnityCatalogIntegration(cfg) |
| 57 | + assert integration.file_format == "parquet" |
| 58 | + |
| 59 | + |
| 60 | +def test_unity_with_location_root(): |
| 61 | + cfg = _Config(file_format="parquet", adapter_properties={"location_root": "/mnt/data"}) |
| 62 | + integration = UnityCatalogIntegration(cfg) |
| 63 | + assert integration.external_volume == "/mnt/data" |
| 64 | + |
| 65 | + |
| 66 | +def test_unity_blank_location_root_raises(): |
| 67 | + cfg = _Config(file_format="parquet", adapter_properties={"location_root": " "}) |
| 68 | + with pytest.raises(DbtValidationError, match="location_root cannot be blank"): |
| 69 | + UnityCatalogIntegration(cfg) |
| 70 | + |
| 71 | + |
| 72 | +def test_unity_empty_location_root_raises(): |
| 73 | + cfg = _Config(file_format="parquet", adapter_properties={"location_root": ""}) |
| 74 | + with pytest.raises(DbtValidationError, match="location_root cannot be blank"): |
| 75 | + UnityCatalogIntegration(cfg) |
0 commit comments