|
7 | 7 | - test_commit: Make a transaction and commit. |
8 | 8 | - test_rollback: Make a transaction and rollback. |
9 | 9 | - test_invalid_connection_string: Check if initializing with an invalid connection string raises an exception. |
10 | | -Note: The cursor function is not yet implemented, so related tests are commented out. |
| 10 | +- test_connection_pooling_speed: Test connection pooling speed. |
| 11 | +- test_connection_pooling_basic: Test basic connection pooling functionality. |
| 12 | +- test_autocommit_default: Check if autocommit is False by default. |
| 13 | +- test_autocommit_setter: Test setting autocommit mode and its effect on transactions. |
| 14 | +- test_set_autocommit: Test the setautocommit method. |
| 15 | +- test_construct_connection_string: Check if the connection string is constructed correctly with kwargs. |
| 16 | +- test_connection_string_with_attrs_before: Check if the connection string is constructed correctly with attrs_before. |
| 17 | +- test_connection_string_with_odbc_param: Check if the connection string is constructed correctly with ODBC parameters. |
| 18 | +- test_rollback_on_close: Test that rollback occurs on connection close if autocommit is False. |
11 | 19 | """ |
12 | 20 |
|
13 | 21 | from mssql_python.exceptions import InterfaceError |
@@ -73,7 +81,7 @@ def test_connection_string_with_odbc_param(db_connection): |
73 | 81 | assert "Driver={ODBC Driver 18 for SQL Server};;APP=MSSQL-Python;Server=localhost;Uid=me;Pwd=mypwd;Database=mydb;Encrypt=yes;TrustServerCertificate=yes;" == conn_str, "Connection string is incorrect" |
74 | 82 |
|
75 | 83 | def test_autocommit_default(db_connection): |
76 | | - assert db_connection.autocommit is True, "Autocommit should be True by default" |
| 84 | + assert db_connection.autocommit is False, "Autocommit should be False by default" |
77 | 85 |
|
78 | 86 | def test_autocommit_setter(db_connection): |
79 | 87 | db_connection.autocommit = True |
@@ -140,6 +148,41 @@ def test_commit(db_connection): |
140 | 148 | cursor.execute("DROP TABLE #pytest_test_commit;") |
141 | 149 | db_connection.commit() |
142 | 150 |
|
| 151 | +def test_rollback_on_close(conn_str, db_connection): |
| 152 | + # Test that rollback occurs on connection close if autocommit is False |
| 153 | + # Using a permanent table to ensure rollback is tested correctly |
| 154 | + cursor = db_connection.cursor() |
| 155 | + drop_table_if_exists(cursor, "pytest_test_rollback_on_close") |
| 156 | + try: |
| 157 | + # Create a permanent table for testing |
| 158 | + cursor.execute("CREATE TABLE pytest_test_rollback_on_close (id INT PRIMARY KEY, value VARCHAR(50));") |
| 159 | + db_connection.commit() |
| 160 | + |
| 161 | + # This simulates a scenario where the connection is closed without committing |
| 162 | + # and checks if the rollback occurs |
| 163 | + temp_conn = connect(conn_str) |
| 164 | + temp_cursor = temp_conn.cursor() |
| 165 | + temp_cursor.execute("INSERT INTO pytest_test_rollback_on_close (id, value) VALUES (1, 'test');") |
| 166 | + |
| 167 | + # Verify data is visible within the same transaction |
| 168 | + temp_cursor.execute("SELECT * FROM pytest_test_rollback_on_close WHERE id = 1;") |
| 169 | + result = temp_cursor.fetchone() |
| 170 | + assert result is not None, "Rollback on close failed: No data found before close" |
| 171 | + assert result[1] == 'test', "Rollback on close failed: Incorrect data before close" |
| 172 | + |
| 173 | + # Close the temporary connection without committing |
| 174 | + temp_conn.close() |
| 175 | + |
| 176 | + # Now check if the data is rolled back |
| 177 | + cursor.execute("SELECT * FROM pytest_test_rollback_on_close WHERE id = 1;") |
| 178 | + result = cursor.fetchone() |
| 179 | + assert result is None, "Rollback on close failed: Data found after rollback" |
| 180 | + except Exception as e: |
| 181 | + pytest.fail(f"Rollback on close failed: {e}") |
| 182 | + finally: |
| 183 | + drop_table_if_exists(cursor, "pytest_test_rollback_on_close") |
| 184 | + db_connection.commit() |
| 185 | + |
143 | 186 | def test_rollback(db_connection): |
144 | 187 | # Make a transaction and rollback |
145 | 188 | cursor = db_connection.cursor() |
|
0 commit comments