Skip to content

TST: Increase test coverage for pandas.io.formats.excel.py #61697

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
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ def _border_style(
# 'slantDashDot'
# 'thick'
# 'thin'
if width is None and style is None and color is None:
# Return None will remove "border" from style dictionary
return None

if width is None and style is None:
# Return "none" will keep "border" in style dictionary
return "none"
if color is None:
# Return None will remove "border" from style dictionary
return None
else:
# Return "none" will keep "border" in style dictionary
return "none"

if style in ("none", "hidden"):
return "none"
Expand Down Expand Up @@ -411,11 +411,6 @@ def _get_decoration(self, props: Mapping[str, str]) -> Sequence[str]:
else:
return ()

def _get_underline(self, decoration: Sequence[str]) -> str | None:
if "underline" in decoration:
return "single"
return None

def _get_shadow(self, props: Mapping[str, str]) -> bool | None:
if "text-shadow" in props:
return bool(re.search("^[^#(]*[1-9]", props["text-shadow"]))
Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/io/formats/test_to_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@
"border-top-style: solid; border-top-width: 4pt",
{"border": {"top": {"style": "thick"}}},
),
(
"border-top-style: solid; border-top-width: none",
{"border": {"top": {"style": "none"}}},
),
(
"border-top-style: solid; border-top-width: 0.000001pt",
{"border": {"top": {"style": "none"}}},
),
(
"border-top-style: dotted",
{"border": {"top": {"style": "mediumDashDotDot"}}},
Expand Down Expand Up @@ -194,6 +202,10 @@
"border-top-color: blue",
{"border": {"top": {"color": "0000FF", "style": "none"}}},
),
(
"border-top-style: slantDashDot; border-top-color: blue",
{"border": {"top": {"style": "slantDashDot", "color": "0000FF"}}},
),
# ALIGNMENT
# - horizontal
("text-align: center", {"alignment": {"horizontal": "center"}}),
Expand Down Expand Up @@ -249,6 +261,20 @@ def test_css_to_excel_multiple():
} == actual


@pytest.mark.parametrize(
"css",
[
"border-top-style: unhandled-border-style",
"border-style: another-unhandled-style",
],
)
def test_css_to_excel_unhandled_border_style_warns(css):
"""Test that unhandled border styles raise a CSSWarning."""
convert = CSSToExcelConverter()
with tm.assert_produces_warning(CSSWarning, match="Unhandled border style format"):
convert(css)


@pytest.mark.parametrize(
"css,inherited,expected",
[
Expand Down Expand Up @@ -330,6 +356,23 @@ def test_css_to_excel_bad_colors(input_color):
assert expected == convert(css)


@pytest.mark.parametrize("input_color", ["#", "#1234567"])
def test_css_to_excel_invalid_color_raises(input_color):
"""Test that invalid colors raise a ValueError."""
css = (
f"border-top-color: {input_color}; "
f"border-right-color: {input_color}; "
f"border-bottom-color: {input_color}; "
f"border-left-color: {input_color}; "
f"background-color: {input_color}; "
f"color: {input_color}"
)

convert = CSSToExcelConverter()
with pytest.raises(ValueError, match=f"Unexpected color {input_color}"):
convert(css)


def tests_css_named_colors_valid():
upper_hexs = set(map(str.upper, string.hexdigits))
for color in CSSToExcelConverter.NAMED_COLORS.values():
Expand Down
Loading