@@ -5218,7 +5218,7 @@ def test_getinfo_string_decoding_utf8_fallback(db_connection):
52185218 This test verifies the fallback path in the encoding loop where
52195219 UTF-16LE fails but UTF-8 succeeds.
52205220 """
5221- from unittest .mock import patch
5221+ from unittest .mock import MagicMock
52225222
52235223 # UTF-8 encoded "Hello" - this is valid UTF-8 but NOT valid UTF-16LE
52245224 # (odd number of bytes would fail UTF-16LE decode)
@@ -5229,11 +5229,20 @@ def test_getinfo_string_decoding_utf8_fallback(db_connection):
52295229 # Use a string-type info_type (SQL_DRIVER_NAME = 6 is in string_type_constants)
52305230 info_type = sql_const .SQL_DRIVER_NAME .value
52315231
5232- with patch .object (db_connection ._conn , "get_info" , return_value = mock_result ):
5232+ # Save the original _conn and replace with a mock
5233+ original_conn = db_connection ._conn
5234+ try :
5235+ mock_conn = MagicMock ()
5236+ mock_conn .get_info .return_value = mock_result
5237+ db_connection ._conn = mock_conn
5238+
52335239 result = db_connection .getinfo (info_type )
52345240
52355241 assert result == "Hello" , f"Expected 'Hello', got { repr (result )} "
52365242 assert isinstance (result , str ), f"Expected str, got { type (result ).__name__ } "
5243+ finally :
5244+ # Restore the original connection
5245+ db_connection ._conn = original_conn
52375246
52385247
52395248def test_getinfo_string_decoding_all_fail_returns_none (db_connection ):
@@ -5242,7 +5251,7 @@ def test_getinfo_string_decoding_all_fail_returns_none(db_connection):
52425251 This test verifies that when both UTF-16LE and UTF-8 decoding fail,
52435252 the method returns None to avoid silent data corruption.
52445253 """
5245- from unittest .mock import patch
5254+ from unittest .mock import MagicMock
52465255
52475256 # Invalid byte sequence that cannot be decoded as UTF-16LE or UTF-8
52485257 # 0xFF 0xFE is a BOM, but followed by invalid continuation bytes for UTF-8
@@ -5254,11 +5263,20 @@ def test_getinfo_string_decoding_all_fail_returns_none(db_connection):
52545263 # Use a string-type info_type (SQL_DRIVER_NAME = 6 is in string_type_constants)
52555264 info_type = sql_const .SQL_DRIVER_NAME .value
52565265
5257- with patch .object (db_connection ._conn , "get_info" , return_value = mock_result ):
5266+ # Save the original _conn and replace with a mock
5267+ original_conn = db_connection ._conn
5268+ try :
5269+ mock_conn = MagicMock ()
5270+ mock_conn .get_info .return_value = mock_result
5271+ db_connection ._conn = mock_conn
5272+
52585273 result = db_connection .getinfo (info_type )
52595274
52605275 # Should return None when all decoding fails
52615276 assert result is None , f"Expected None for invalid encoding, got { repr (result )} "
5277+ finally :
5278+ # Restore the original connection
5279+ db_connection ._conn = original_conn
52625280
52635281
52645282def test_getinfo_string_encoding_utf16_primary (db_connection ):
@@ -5267,7 +5285,7 @@ def test_getinfo_string_encoding_utf16_primary(db_connection):
52675285 This test verifies the primary (expected) encoding path where
52685286 UTF-16LE decoding succeeds on first try.
52695287 """
5270- from unittest .mock import patch
5288+ from unittest .mock import MagicMock
52715289
52725290 # Valid UTF-16LE encoded "Test" with null terminator
52735291 utf16_data = "Test" .encode ("utf-16-le" ) + b"\x00 \x00 "
@@ -5277,11 +5295,20 @@ def test_getinfo_string_encoding_utf16_primary(db_connection):
52775295 # Use a string-type info_type
52785296 info_type = sql_const .SQL_DRIVER_NAME .value
52795297
5280- with patch .object (db_connection ._conn , "get_info" , return_value = mock_result ):
5298+ # Save the original _conn and replace with a mock
5299+ original_conn = db_connection ._conn
5300+ try :
5301+ mock_conn = MagicMock ()
5302+ mock_conn .get_info .return_value = mock_result
5303+ db_connection ._conn = mock_conn
5304+
52815305 result = db_connection .getinfo (info_type )
52825306
52835307 assert result == "Test" , f"Expected 'Test', got { repr (result )} "
52845308 assert "\x00 " not in result , f"Result contains null bytes: { repr (result )} "
5309+ finally :
5310+ # Restore the original connection
5311+ db_connection ._conn = original_conn
52855312
52865313
52875314def test_getinfo_sql_support (db_connection ):
0 commit comments