|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from _pytest.fixtures import FixtureRequest |
| 7 | +from unittest.mock import patch |
7 | 8 |
|
8 | 9 | from sqlmesh.core.config.connection import ( |
9 | 10 | BigQueryConnectionConfig, |
|
19 | 20 | SnowflakeConnectionConfig, |
20 | 21 | TrinoAuthenticationMethod, |
21 | 22 | AthenaConnectionConfig, |
| 23 | + MSSQLConnectionConfig, |
22 | 24 | _connection_config_validator, |
23 | 25 | _get_engine_import_validator, |
24 | 26 | ) |
@@ -1127,3 +1129,54 @@ class TestConfigC(PydanticModel): |
1127 | 1129 | _engine_import_validator = _get_engine_import_validator("sqlmesh", "bigquery") |
1128 | 1130 |
|
1129 | 1131 | TestConfigC() |
| 1132 | + |
| 1133 | + |
| 1134 | +def test_mssql_engine_import_validator(): |
| 1135 | + """Test that MSSQL import validator respects driver configuration.""" |
| 1136 | + with pytest.raises( |
| 1137 | + ConfigError, |
| 1138 | + match=re.escape( |
| 1139 | + "Failed to import the 'pyodbc' library for MSSQL connections. This may be due to a missing " |
| 1140 | + "or incompatible installation. Please ensure the required dependency is installed by " |
| 1141 | + 'running: `pip install "sqlmesh[mssql-odbc]"`. For more details, check the logs ' |
| 1142 | + "in the 'logs/' folder, or rerun the command with the '--debug' flag." |
| 1143 | + ), |
| 1144 | + ): |
| 1145 | + # Test PyODBC driver suggests mssql-odbc extra |
| 1146 | + with patch("importlib.import_module") as mock_import: |
| 1147 | + mock_import.side_effect = ImportError("No module named 'pyodbc'") |
| 1148 | + MSSQLConnectionConfig(host="localhost", driver="pyodbc") |
| 1149 | + |
| 1150 | + with pytest.raises( |
| 1151 | + ConfigError, |
| 1152 | + match=re.escape( |
| 1153 | + "Failed to import the 'pymssql' library for MSSQL connections. This may be due to a missing " |
| 1154 | + "or incompatible installation. Please ensure the required dependency is installed by " |
| 1155 | + 'running: `pip install "sqlmesh[mssql]"`. For more details, check the logs ' |
| 1156 | + "in the 'logs/' folder, or rerun the command with the '--debug' flag." |
| 1157 | + ), |
| 1158 | + ): |
| 1159 | + # Test PyMSSQL driver suggests mssql extra |
| 1160 | + with patch("importlib.import_module") as mock_import: |
| 1161 | + mock_import.side_effect = ImportError("No module named 'pymssql'") |
| 1162 | + MSSQLConnectionConfig(host="localhost", driver="pymssql") |
| 1163 | + |
| 1164 | + with pytest.raises( |
| 1165 | + ConfigError, |
| 1166 | + match=re.escape( |
| 1167 | + "Failed to import the 'pymssql' library for MSSQL connections. This may be due to a missing " |
| 1168 | + "or incompatible installation. Please ensure the required dependency is installed by " |
| 1169 | + 'running: `pip install "sqlmesh[mssql]"`. For more details, check the logs ' |
| 1170 | + "in the 'logs/' folder, or rerun the command with the '--debug' flag." |
| 1171 | + ), |
| 1172 | + ): |
| 1173 | + # Test default driver (pymssql) suggests mssql extra |
| 1174 | + with patch("importlib.import_module") as mock_import: |
| 1175 | + mock_import.side_effect = ImportError("No module named 'pymssql'") |
| 1176 | + MSSQLConnectionConfig(host="localhost") # No driver specified |
| 1177 | + |
| 1178 | + # Test successful import doesn't raise exception |
| 1179 | + with patch("importlib.import_module") as mock_import: |
| 1180 | + mock_import.return_value = None |
| 1181 | + config = MSSQLConnectionConfig(host="localhost", driver="pyodbc") |
| 1182 | + assert config.driver == "pyodbc" |
0 commit comments