-
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
4
commits 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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f647b8c
FIX: clarify decimal parsing in bulk fetch and add regression test
Chetan-Kansal 1aef4ec
Fix CI: Refactor test_decimal_separator.py for linting, coverage, and…
Chetan-Kansal d3b4702
Merge branch 'main' into fix/decimal-separator-fetching
bewithgaurav cfa638f
Merge branch 'main' into fix/decimal-separator-fetching
bewithgaurav 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,59 @@ | ||
| import os | ||
| from decimal import Decimal | ||
|
|
||
| import pytest | ||
| import mssql_python | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not os.getenv("DB_CONNECTION_STRING"), reason="Requires DB_CONNECTION_STRING") | ||
| def test_decimal_separator_bug(db_connection): | ||
| """ | ||
| Test that fetchall() dealing with DECIMALS works correctly even when | ||
| setDecimalSeparator is set to something other than '.' | ||
| """ | ||
| conn = db_connection | ||
| cursor = None | ||
|
|
||
| try: | ||
| cursor = conn.cursor() | ||
| # 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(".") | ||
| if cursor: | ||
| try: | ||
| cursor.execute("DROP TABLE #TestDecimal") | ||
| conn.commit() | ||
| except Exception: | ||
| pass | ||
| cursor.close() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job on adding this focused regression test!
Suggestion: Instead of creating a new file
test_decimal_separator.py, could we add these tests totest_004_cursor.py?There are already decimal separator tests there (around lines 8370-8730), so it would keep all the related tests together. Plus, these are really testing cursor fetch behavior with the decimal separator setting, which fits perfectly in that file.