-
Notifications
You must be signed in to change notification settings - Fork 33
FIX: clarify decimal parsing in bulk fetch and add regression test #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Chetan-Kansal
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Chetan-Kansal:fix/decimal-separator-fetching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import pytest | ||
| import mssql_python | ||
| import os | ||
| from decimal import Decimal | ||
|
|
||
| # Helper to get connection | ||
| def get_connection(): | ||
| server = os.getenv("TEST_SERVER", "localhost") | ||
| database = os.getenv("TEST_DATABASE", "master") | ||
| username = os.getenv("TEST_USERNAME", "sa") | ||
| password = os.getenv("TEST_PASSWORD", "yourStrong(!)Password") | ||
| driver = os.getenv("TEST_DRIVER", "ODBC Driver 17 for SQL Server") | ||
|
|
||
| conn_str = f"DRIVER={{{driver}}};SERVER={server};DATABASE={database};UID={username};PWD={password};TrustServerCertificate=yes" | ||
| return mssql_python.connect(conn_str) | ||
|
|
||
| @pytest.mark.skipif(not os.getenv("TEST_SERVER"), reason="TEST_SERVER not set") | ||
| def test_decimal_separator_bug(): | ||
| """ | ||
| Test that fetchall() dealing with DECIMALS works correctly even when | ||
| setDecimalSeparator is set to something other than '.' | ||
| """ | ||
| conn = get_connection() | ||
| cursor = conn.cursor() | ||
|
|
||
| try: | ||
| # Create a temp table | ||
| cursor.execute("CREATE TABLE #TestDecimal (Val DECIMAL(10, 2))") | ||
| cursor.execute("INSERT INTO #TestDecimal VALUES (1234.56)") | ||
| cursor.execute("INSERT INTO #TestDecimal VALUES (78.90)") | ||
| conn.commit() | ||
|
|
||
| # Set custom separator | ||
| mssql_python.setDecimalSeparator(",") | ||
|
|
||
| # Test fetchall | ||
| cursor.execute("SELECT Val FROM #TestDecimal ORDER BY Val") | ||
| rows = cursor.fetchall() | ||
|
|
||
| # Verify fetchall results | ||
| assert len(rows) == 2, f"Expected 2 rows, got {len(rows)}" | ||
| assert isinstance(rows[0][0], Decimal), f"Expected Decimal, got {type(rows[0][0])}" | ||
| assert rows[0][0] == Decimal("78.90"), f"Expected 78.90, got {rows[0][0]}" | ||
| assert rows[1][0] == Decimal("1234.56"), f"Expected 1234.56, got {rows[1][0]}" | ||
|
|
||
| # Verify fetchmany | ||
| cursor.execute("SELECT Val FROM #TestDecimal ORDER BY Val") | ||
| batch = cursor.fetchmany(2) | ||
| assert len(batch) == 2 | ||
| assert batch[1][0] == Decimal("1234.56") | ||
|
|
||
| # Verify fetchone behavior is consistent | ||
| cursor.execute("SELECT CAST(99.99 AS DECIMAL(10,2))") | ||
| val = cursor.fetchone()[0] | ||
| assert isinstance(val, Decimal) | ||
| assert val == Decimal("99.99") | ||
|
|
||
| finally: | ||
| # Reset separator to default just in case | ||
| mssql_python.setDecimalSeparator(".") | ||
| cursor.execute("DROP TABLE #TestDecimal") | ||
| conn.close() | ||
|
|
||
| if __name__ == "__main__": | ||
| # Allow running as a script too | ||
| try: | ||
| test_decimal_separator_bug() | ||
| print("Test PASSED") | ||
| except Exception as e: | ||
| print(f"Test FAILED: {e}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / devskim
Accessing localhost could indicate debug code, or could hinder scaling. Note test