Skip to content

Commit 6e11847

Browse files
authored
FIX: Adding row string (#136)
### ADO Work Item Reference <!-- Insert your ADO Work Item ID below (e.g. AB#37452) --> > [AB#37929](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/37929) ------------------------------------------------------------------- ### Summary This pull request modifies the `Row` class in `mssql_python/row.py` to improve its string representation methods. The changes include adding a `__str__` method for user-friendly output and updating the `__repr__` method to focus on debugging. Enhancements to string representation: * Added a `__str__` method to provide a user-friendly string representation of the `Row` object by returning the tuple of its values as a string. * Updated the `__repr__` method to provide a more detailed representation for debugging purposes by using the `repr` function on the tuple of values instead of a formatted string. --------- Co-authored-by: Jahnvi Thakkar <jathakkar@microsoft.com>
1 parent 4116996 commit 6e11847

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

mssql_python/row.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ def __iter__(self):
6060
"""Allow iteration through values"""
6161
return iter(self._values)
6262

63+
def __str__(self):
64+
"""Return string representation of the row"""
65+
return str(tuple(self._values))
66+
6367
def __repr__(self):
64-
"""Return a string representation of the row"""
65-
return f"Row{tuple(self._values)}"
68+
"""Return a detailed string representation for debugging"""
69+
return repr(tuple(self._values))

tests/test_004_cursor.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,79 @@ def test_row_comparison_with_list(cursor, db_connection):
12401240
cursor.execute("DROP TABLE #pytest_row_comparison_test")
12411241
db_connection.commit()
12421242

1243+
def test_row_string_representation(cursor, db_connection):
1244+
"""Test Row string and repr representations"""
1245+
try:
1246+
cursor.execute("""
1247+
CREATE TABLE #pytest_row_test (
1248+
id INT PRIMARY KEY,
1249+
text_col NVARCHAR(50),
1250+
null_col INT
1251+
)
1252+
""")
1253+
db_connection.commit()
1254+
1255+
cursor.execute("""
1256+
INSERT INTO #pytest_row_test (id, text_col, null_col)
1257+
VALUES (?, ?, ?)
1258+
""", [1, "test", None])
1259+
db_connection.commit()
1260+
1261+
cursor.execute("SELECT * FROM #pytest_row_test")
1262+
row = cursor.fetchone()
1263+
1264+
# Test str()
1265+
str_representation = str(row)
1266+
assert str_representation == "(1, 'test', None)", "Row str() representation incorrect"
1267+
1268+
# Test repr()
1269+
repr_representation = repr(row)
1270+
assert repr_representation == "(1, 'test', None)", "Row repr() representation incorrect"
1271+
1272+
except Exception as e:
1273+
pytest.fail(f"Row string representation test failed: {e}")
1274+
finally:
1275+
cursor.execute("DROP TABLE #pytest_row_test")
1276+
db_connection.commit()
1277+
1278+
def test_row_column_mapping(cursor, db_connection):
1279+
"""Test Row column name mapping"""
1280+
try:
1281+
cursor.execute("""
1282+
CREATE TABLE #pytest_row_test (
1283+
FirstColumn INT PRIMARY KEY,
1284+
Second_Column NVARCHAR(50),
1285+
[Complex Name!] INT
1286+
)
1287+
""")
1288+
db_connection.commit()
1289+
1290+
cursor.execute("""
1291+
INSERT INTO #pytest_row_test ([FirstColumn], [Second_Column], [Complex Name!])
1292+
VALUES (?, ?, ?)
1293+
""", [1, "test", 42])
1294+
db_connection.commit()
1295+
1296+
cursor.execute("SELECT * FROM #pytest_row_test")
1297+
row = cursor.fetchone()
1298+
1299+
# Test different column name styles
1300+
assert row.FirstColumn == 1, "CamelCase column access failed"
1301+
assert row.Second_Column == "test", "Snake_case column access failed"
1302+
assert getattr(row, "Complex Name!") == 42, "Complex column name access failed"
1303+
1304+
# Test column map completeness
1305+
assert len(row._column_map) == 3, "Column map size incorrect"
1306+
assert "FirstColumn" in row._column_map, "Column map missing CamelCase column"
1307+
assert "Second_Column" in row._column_map, "Column map missing snake_case column"
1308+
assert "Complex Name!" in row._column_map, "Column map missing complex name column"
1309+
1310+
except Exception as e:
1311+
pytest.fail(f"Row column mapping test failed: {e}")
1312+
finally:
1313+
cursor.execute("DROP TABLE #pytest_row_test")
1314+
db_connection.commit()
1315+
12431316
def test_close(db_connection):
12441317
"""Test closing the cursor"""
12451318
try:

0 commit comments

Comments
 (0)