Skip to content

Commit cf55998

Browse files
committed
Adding tests for uncovered lines
1 parent 10acb24 commit cf55998

3 files changed

Lines changed: 727 additions & 310 deletions

File tree

mssql_python/cursor.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2296,15 +2296,28 @@ def executemany( # pylint: disable=too-many-locals,too-many-branches,too-many-s
22962296
check_error(ddbc_sql_const.SQL_HANDLE_STMT.value, self.hstmt, ret)
22972297
self.rowcount = ddbc_bindings.DDBCSQLRowCount(self.hstmt)
22982298
self.last_executed_stmt = operation
2299-
self._initialize_description()
2299+
2300+
# Fetch column metadata (e.g. for INSERT … OUTPUT)
2301+
column_metadata = []
2302+
try:
2303+
ddbc_bindings.DDBCSQLDescribeCol(self.hstmt, column_metadata)
2304+
self._initialize_description(column_metadata)
2305+
except Exception: # pylint: disable=broad-exception-caught
2306+
self.description = None
23002307

23012308
if self.description:
23022309
self.rowcount = -1
23032310
self._reset_rownumber()
2311+
self._cached_column_map = {
2312+
col_desc[0]: i for i, col_desc in enumerate(self.description)
2313+
}
2314+
self._cached_converter_map = self._build_converter_map()
23042315
self._uuid_str_indices = self._compute_uuid_str_indices()
23052316
else:
23062317
self.rowcount = ddbc_bindings.DDBCSQLRowCount(self.hstmt)
23072318
self._clear_rownumber()
2319+
self._cached_column_map = None
2320+
self._cached_converter_map = None
23082321
self._uuid_str_indices = None
23092322
finally:
23102323
# Reset input sizes after execution

tests/test_001_globals.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,3 +869,49 @@ def test_connection_native_uuid_attribute():
869869
params = sig.parameters
870870
assert "native_uuid" in params, "Connection.__init__ should accept native_uuid parameter"
871871
assert params["native_uuid"].default is None
872+
873+
874+
def test_compute_uuid_str_indices_no_description(db_connection):
875+
"""Test _compute_uuid_str_indices returns None when cursor has no description."""
876+
cursor = db_connection.cursor()
877+
try:
878+
# Execute a statement that produces no result set
879+
cursor.execute(
880+
"CREATE TABLE #no_desc_uuid_test (id INT); " "INSERT INTO #no_desc_uuid_test VALUES (1)"
881+
)
882+
# description should be None after a non-SELECT statement
883+
assert cursor.description is None
884+
885+
# Directly call the helper — should return None via the early guard
886+
result = cursor._compute_uuid_str_indices()
887+
assert (
888+
result is None
889+
), "_compute_uuid_str_indices should return None when description is None"
890+
finally:
891+
cursor.execute("DROP TABLE IF EXISTS #no_desc_uuid_test")
892+
cursor.close()
893+
894+
895+
def test_stringify_uuids_with_tuple_values():
896+
"""Test Row._stringify_uuids converts tuple values to list for in-place mutation."""
897+
import uuid as _uuid
898+
from mssql_python.row import Row
899+
900+
test_uuid = _uuid.UUID("12345678-1234-5678-1234-567812345678")
901+
902+
# Pass values as a tuple (not a list) to trigger the isinstance guard
903+
row = Row(
904+
(42, test_uuid, "hello"),
905+
{"id": 0, "guid": 1, "name": 2},
906+
cursor=None,
907+
converter_map=None,
908+
uuid_str_indices=(1,),
909+
)
910+
911+
# The UUID should have been stringified to uppercase
912+
assert row[1] == "12345678-1234-5678-1234-567812345678".upper()
913+
# Other values should be unaffected
914+
assert row[0] == 42
915+
assert row[2] == "hello"
916+
# Internal storage should now be a list (converted from tuple)
917+
assert isinstance(row._values, list)

0 commit comments

Comments
 (0)