|
| 1 | +"""Unit tests for MSSQL autocommit behavior. |
| 2 | +
|
| 3 | +Regression test for GitHub issue #107: |
| 4 | +CREATE DATABASE was failing with "CREATE DATABASE statement not allowed within |
| 5 | +multi-statement transaction" because the MSSQL adapter didn't set autocommit=True. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from unittest.mock import MagicMock, patch |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | + |
| 15 | +class TestMSSQLAutocommitBehavior: |
| 16 | + """Test that MSSQL adapter sets autocommit correctly.""" |
| 17 | + |
| 18 | + @pytest.fixture |
| 19 | + def mock_mssql_python(self): |
| 20 | + """Create a mock mssql_python module.""" |
| 21 | + mock_module = MagicMock() |
| 22 | + mock_conn = MagicMock() |
| 23 | + mock_conn.autocommit = False # Default behavior |
| 24 | + mock_module.connect.return_value = mock_conn |
| 25 | + with patch.dict("sys.modules", {"mssql_python": mock_module}): |
| 26 | + yield mock_module, mock_conn |
| 27 | + |
| 28 | + def test_mssql_adapter_sets_autocommit(self, mock_mssql_python): |
| 29 | + """Regression test: MSSQL adapter must set autocommit=True. |
| 30 | +
|
| 31 | + This prevents 'CREATE DATABASE statement not allowed within |
| 32 | + multi-statement transaction' errors (GitHub issue #107). |
| 33 | + """ |
| 34 | + from sqlit.domains.connections.domain.config import ConnectionConfig, TcpEndpoint |
| 35 | + from sqlit.domains.connections.providers.mssql.adapter import SQLServerAdapter |
| 36 | + |
| 37 | + _, mock_conn = mock_mssql_python |
| 38 | + |
| 39 | + adapter = SQLServerAdapter() |
| 40 | + config = ConnectionConfig( |
| 41 | + name="test_mssql", |
| 42 | + db_type="mssql", |
| 43 | + endpoint=TcpEndpoint( |
| 44 | + host="localhost", |
| 45 | + port="1433", |
| 46 | + database="master", |
| 47 | + username="sa", |
| 48 | + password="password", |
| 49 | + ), |
| 50 | + options={"auth_type": "sql"}, |
| 51 | + ) |
| 52 | + |
| 53 | + adapter.connect(config) |
| 54 | + |
| 55 | + assert mock_conn.autocommit is True, ( |
| 56 | + "MSSQL adapter must set autocommit=True after connecting " |
| 57 | + "to allow DDL statements like CREATE DATABASE" |
| 58 | + ) |
0 commit comments