Skip to content

Commit 6895570

Browse files
committed
connection tests
1 parent 1bbe90e commit 6895570

4 files changed

Lines changed: 98 additions & 17 deletions

File tree

docs/guides/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ These pages describe the connection configuration options for each execution eng
598598
* [BigQuery](../integrations/engines/bigquery.md)
599599
* [Databricks](../integrations/engines/databricks.md)
600600
* [DuckDB](../integrations/engines/duckdb.md)
601+
* [Fabric](../integrations/engines/fabric.md)
601602
* [MotherDuck](../integrations/engines/motherduck.md)
602603
* [MySQL](../integrations/engines/mysql.md)
603604
* [MSSQL](../integrations/engines/mssql.md)

sqlmesh/core/config/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
ConnectionConfig as ConnectionConfig,
1111
DatabricksConnectionConfig as DatabricksConnectionConfig,
1212
DuckDBConnectionConfig as DuckDBConnectionConfig,
13+
FabricConnectionConfig as FabricConnectionConfig,
1314
GCPPostgresConnectionConfig as GCPPostgresConnectionConfig,
1415
MotherDuckConnectionConfig as MotherDuckConnectionConfig,
1516
MSSQLConnectionConfig as MSSQLConnectionConfig,

sqlmesh/core/engine_adapter/fabric.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from sqlglot import exp
55
from sqlmesh.core.engine_adapter.mssql import MSSQLEngineAdapter
66
from sqlmesh.core.engine_adapter.shared import InsertOverwriteStrategy, SourceQuery
7+
from sqlmesh.core.engine_adapter.base import EngineAdapter
78

89
if t.TYPE_CHECKING:
910
from sqlmesh.core._typing import TableName
@@ -110,22 +111,17 @@ def _insert_overwrite_by_condition(
110111
**kwargs: t.Any,
111112
) -> None:
112113
"""
113-
Implements the insert overwrite strategy for Fabric.
114+
Implements the insert overwrite strategy for Fabric using DELETE and INSERT.
114115
115-
Overridden to enforce a `DELETE`/`INSERT` strategy, as Fabric's
116-
`MERGE` statement has limitations.
116+
This method is overridden to avoid the MERGE statement from the parent
117+
MSSQLEngineAdapter, which is not fully supported in Fabric.
117118
"""
118-
119-
columns_to_types = columns_to_types or self.columns(table_name)
120-
121-
self.delete_from(table_name, where=where or exp.true())
122-
123-
for source_query in source_queries:
124-
with source_query as query:
125-
query = self._order_projections_and_filter(query, columns_to_types, where=where)
126-
self._insert_append_query(
127-
table_name,
128-
query,
129-
columns_to_types=columns_to_types,
130-
order_projections=False,
131-
)
119+
return EngineAdapter._insert_overwrite_by_condition(
120+
self,
121+
table_name=table_name,
122+
source_queries=source_queries,
123+
columns_to_types=columns_to_types,
124+
where=where,
125+
insert_overwrite_strategy_override=InsertOverwriteStrategy.DELETE_INSERT,
126+
**kwargs,
127+
)

tests/core/test_connection_config.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ConnectionConfig,
1313
DatabricksConnectionConfig,
1414
DuckDBAttachOptions,
15+
FabricConnectionConfig,
1516
DuckDBConnectionConfig,
1617
GCPPostgresConnectionConfig,
1718
MotherDuckConnectionConfig,
@@ -1392,3 +1393,85 @@ def test_mssql_pymssql_connection_factory():
13921393
# Clean up the mock module
13931394
if "pymssql" in sys.modules:
13941395
del sys.modules["pymssql"]
1396+
1397+
1398+
def test_fabric_connection_config_defaults(make_config):
1399+
"""Test Fabric connection config defaults to pyodbc and autocommit=True."""
1400+
config = make_config(type="fabric", host="localhost", check_import=False)
1401+
assert isinstance(config, FabricConnectionConfig)
1402+
assert config.driver == "pyodbc"
1403+
assert config.autocommit is True
1404+
1405+
# Ensure it creates the FabricAdapter
1406+
from sqlmesh.core.engine_adapter.fabric import FabricAdapter
1407+
1408+
assert isinstance(config.create_engine_adapter(), FabricAdapter)
1409+
1410+
1411+
def test_fabric_connection_config_parameter_validation(make_config):
1412+
"""Test Fabric connection config parameter validation."""
1413+
# Test that FabricConnectionConfig correctly handles pyodbc-specific parameters.
1414+
config = make_config(
1415+
type="fabric",
1416+
host="localhost",
1417+
driver_name="ODBC Driver 18 for SQL Server",
1418+
trust_server_certificate=True,
1419+
encrypt=False,
1420+
odbc_properties={"Authentication": "ActiveDirectoryServicePrincipal"},
1421+
check_import=False,
1422+
)
1423+
assert isinstance(config, FabricConnectionConfig)
1424+
assert config.driver == "pyodbc" # Driver is fixed to pyodbc
1425+
assert config.driver_name == "ODBC Driver 18 for SQL Server"
1426+
assert config.trust_server_certificate is True
1427+
assert config.encrypt is False
1428+
assert config.odbc_properties == {"Authentication": "ActiveDirectoryServicePrincipal"}
1429+
1430+
# Test that specifying a different driver for Fabric raises an error
1431+
with pytest.raises(ConfigError, match=r"Input should be 'pyodbc'"):
1432+
make_config(type="fabric", host="localhost", driver="pymssql", check_import=False)
1433+
1434+
1435+
def test_fabric_pyodbc_connection_string_generation():
1436+
"""Test that the Fabric pyodbc connection gets invoked with the correct ODBC connection string."""
1437+
with patch("pyodbc.connect") as mock_pyodbc_connect:
1438+
# Create a Fabric config
1439+
config = FabricConnectionConfig(
1440+
host="testserver.datawarehouse.fabric.microsoft.com",
1441+
port=1433,
1442+
database="testdb",
1443+
user="testuser",
1444+
password="testpass",
1445+
driver_name="ODBC Driver 18 for SQL Server",
1446+
trust_server_certificate=True,
1447+
encrypt=True,
1448+
login_timeout=30,
1449+
check_import=False,
1450+
)
1451+
1452+
# Get the connection factory with kwargs and call it
1453+
factory_with_kwargs = config._connection_factory_with_kwargs
1454+
connection = factory_with_kwargs()
1455+
1456+
# Verify pyodbc.connect was called with the correct connection string
1457+
mock_pyodbc_connect.assert_called_once()
1458+
call_args = mock_pyodbc_connect.call_args
1459+
1460+
# Check the connection string (first argument)
1461+
conn_str = call_args[0][0]
1462+
expected_parts = [
1463+
"DRIVER={ODBC Driver 18 for SQL Server}",
1464+
"SERVER=testserver.datawarehouse.fabric.microsoft.com,1433",
1465+
"DATABASE=testdb",
1466+
"Encrypt=YES",
1467+
"TrustServerCertificate=YES",
1468+
"Connection Timeout=30",
1469+
"UID=testuser",
1470+
"PWD=testpass",
1471+
]
1472+
1473+
for part in expected_parts:
1474+
assert part in conn_str
1475+
1476+
# Check autocommit parameter, should default to True for Fabric
1477+
assert call_args[1]["autocommit"] is True

0 commit comments

Comments
 (0)