|
16 | 16 | # under the License. |
17 | 17 |
|
18 | 18 |
|
| 19 | +from decimal import Decimal |
19 | 20 | from typing import Any |
20 | 21 | from unittest import mock |
21 | 22 |
|
@@ -785,3 +786,99 @@ def test_get_chart_csv_data_closes_response( |
785 | 786 | == b"csv" |
786 | 787 | ) |
787 | 788 | response.close.assert_called_once() |
| 789 | + |
| 790 | + |
| 791 | +@pytest.mark.parametrize("decimal_separator", [".", ","]) |
| 792 | +@pytest.mark.parametrize("amount", ["12.3400", "-1.25", "0E-18"]) |
| 793 | +def test_csv_export_honors_decimal_separator_for_exact_values( |
| 794 | + decimal_separator: str, amount: str |
| 795 | +) -> None: |
| 796 | + """Typed decimals keep their scale and match the float column's separator.""" |
| 797 | + frame = pd.DataFrame( |
| 798 | + {"amount": [Decimal(amount)], "float": [1.25], "text": ["keep.this"]} |
| 799 | + ) |
| 800 | + expected_amount = format(Decimal(amount), "f").replace(".", decimal_separator) |
| 801 | + expected_float = "1.25".replace(".", decimal_separator) |
| 802 | + result = df_to_escaped_csv(frame, index=False, sep=";", decimal=decimal_separator) |
| 803 | + assert ( |
| 804 | + result == f"amount;float;text\n{expected_amount};{expected_float};keep.this\n" |
| 805 | + ) |
| 806 | + |
| 807 | + |
| 808 | +@pytest.mark.parametrize("decimal_separator", [".", ","]) |
| 809 | +def test_csv_export_honors_decimal_separator_for_mixed_numbers( |
| 810 | + decimal_separator: str, |
| 811 | +) -> None: |
| 812 | + """Mixed numeric cells match streaming exports without localizing strings.""" |
| 813 | + frame = pd.DataFrame( |
| 814 | + {"mixed": [Decimal("-12.3400"), 1.25, True, "1.25", "=formula", None]}, |
| 815 | + dtype=object, |
| 816 | + ) |
| 817 | + amount = "-12.3400".replace(".", decimal_separator) |
| 818 | + number = "1.25".replace(".", decimal_separator) |
| 819 | + assert df_to_escaped_csv( |
| 820 | + frame, index=False, decimal=decimal_separator, sep=";" |
| 821 | + ) == (f'mixed\n{amount}\n{number}\nTrue\n1.25\n\'=formula\n""\n') |
| 822 | + |
| 823 | + |
| 824 | +@pytest.mark.parametrize("decimal_separator", [".", ","]) |
| 825 | +@pytest.mark.parametrize( |
| 826 | + "duplicate_axis", ["unique", "index", "columns", "both", "multiindex"] |
| 827 | +) |
| 828 | +def test_csv_export_preserves_duplicate_labels( |
| 829 | + decimal_separator: str, duplicate_axis: str |
| 830 | +) -> None: |
| 831 | + """Formatting a cell must not overwrite another row or column with its label.""" |
| 832 | + frame = pd.DataFrame( |
| 833 | + [ |
| 834 | + [Decimal("1.2500"), Decimal("3.7500")], |
| 835 | + [Decimal("2.5000"), Decimal("4.2500")], |
| 836 | + ], |
| 837 | + columns=["amount", "amount"] |
| 838 | + if duplicate_axis in {"columns", "both"} |
| 839 | + else ["first", "second"], |
| 840 | + index=["group", "group"] |
| 841 | + if duplicate_axis in {"index", "both"} |
| 842 | + else ["first", "second"], |
| 843 | + ) |
| 844 | + if duplicate_axis == "multiindex": |
| 845 | + frame.index = pd.MultiIndex.from_tuples([("group", 1), ("group", 1)]) |
| 846 | + original = frame.copy(deep=True) |
| 847 | + expected_values = "1.2500;3.7500\n2.5000;4.2500\n".replace(".", decimal_separator) |
| 848 | + expected_header = ";".join(frame.columns) |
| 849 | + |
| 850 | + assert ( |
| 851 | + df_to_escaped_csv(frame, index=False, sep=";", decimal=decimal_separator) |
| 852 | + == f"{expected_header}\n{expected_values}" |
| 853 | + ) |
| 854 | + pd.testing.assert_frame_equal(frame, original) |
| 855 | + |
| 856 | + |
| 857 | +@pytest.mark.parametrize("decimal_separator", [".", ","]) |
| 858 | +@pytest.mark.parametrize("null_value", [None, np.nan], ids=["none", "nan"]) |
| 859 | +def test_csv_export_duplicate_labels_preserve_mixed_types( |
| 860 | + decimal_separator: str, |
| 861 | + null_value: Any, |
| 862 | +) -> None: |
| 863 | + """Duplicate labels preserve exact numbers, nulls, bools and formula escaping.""" |
| 864 | + frame = pd.DataFrame( |
| 865 | + [ |
| 866 | + [Decimal("-1.2500"), 1.5, True, 7, "=1+2"], |
| 867 | + [Decimal("2.5000"), null_value, False, 8, "keep.this"], |
| 868 | + ], |
| 869 | + columns=["value"] * 5, |
| 870 | + index=[0, 0], |
| 871 | + dtype=object, |
| 872 | + ) |
| 873 | + original = frame.copy(deep=True) |
| 874 | + amount = "-1.2500".replace(".", decimal_separator) |
| 875 | + number = "1.5".replace(".", decimal_separator) |
| 876 | + second_amount = "2.5000".replace(".", decimal_separator) |
| 877 | + |
| 878 | + assert df_to_escaped_csv( |
| 879 | + frame, index=False, sep=";", decimal=decimal_separator |
| 880 | + ) == ( |
| 881 | + f"value;value;value;value;value\n{amount};{number};True;7;'=1+2\n" |
| 882 | + f"{second_amount};;False;8;keep.this\n" |
| 883 | + ) |
| 884 | + pd.testing.assert_frame_equal(frame, original) |
0 commit comments