|
12 | 12 | ConnectionConfig, |
13 | 13 | DatabricksConnectionConfig, |
14 | 14 | DuckDBAttachOptions, |
| 15 | + FabricConnectionConfig, |
15 | 16 | DuckDBConnectionConfig, |
16 | 17 | GCPPostgresConnectionConfig, |
17 | 18 | MotherDuckConnectionConfig, |
@@ -1392,3 +1393,85 @@ def test_mssql_pymssql_connection_factory(): |
1392 | 1393 | # Clean up the mock module |
1393 | 1394 | if "pymssql" in sys.modules: |
1394 | 1395 | 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