Skip to content

TST: test_tooltip.py convert to functional tests instead of class #40550

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

Merged
merged 5 commits into from
Mar 23, 2021
Merged
Changes from 3 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
170 changes: 75 additions & 95 deletions pandas/tests/io/formats/style/test_tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,99 +7,79 @@
from pandas.io.formats.style import Styler


class TestStylerTooltip:
@pytest.mark.parametrize(
"ttips",
[
DataFrame(
data=[["Min", "Max"], [np.nan, ""]],
columns=["A", "B"],
index=["a", "b"],
),
DataFrame(data=[["Max", "Min"]], columns=["B", "A"], index=["a"]),
DataFrame(
data=[["Min", "Max", None]], columns=["A", "B", "C"], index=["a"]
),
],
@pytest.fixture
def df():
return DataFrame(
data=[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
columns=["A", "B", "C"],
index=["x", "y", "z"],
)
def test_tooltip_render(self, ttips):
# GH 21266
df = DataFrame(data=[[0, 3], [1, 2]], columns=["A", "B"], index=["a", "b"])
s = Styler(df, uuid_len=0).set_tooltips(ttips).render()

# test tooltip table level class
assert "#T__ .pd-t {\n visibility: hidden;\n" in s

# test 'Min' tooltip added
assert (
"#T__ #T__row0_col0:hover .pd-t {\n visibility: visible;\n}\n"
+ '#T__ #T__row0_col0 .pd-t::after {\n content: "Min";\n}'
in s
)
assert (
'<td id="T__row0_col0" class="data row0 col0" >0<span class="pd-t">'
+ "</span></td>"
in s
)

# test 'Max' tooltip added
assert (
"#T__ #T__row0_col1:hover .pd-t {\n visibility: visible;\n}\n"
+ '#T__ #T__row0_col1 .pd-t::after {\n content: "Max";\n}'
in s
)
assert (
'<td id="T__row0_col1" class="data row0 col1" >3<span class="pd-t">'
+ "</span></td>"
in s
)

def test_tooltip_reindex(self):
# GH 39317
df = DataFrame(
data=[[0, 1, 2], [3, 4, 5], [6, 7, 8]], columns=[0, 1, 2], index=[0, 1, 2]
)
ttips = DataFrame(
data=[["Mi", "Ma"], ["Mu", "Mo"]],
columns=[0, 2],
index=[0, 2],
)
s = Styler(df, uuid_len=0).set_tooltips(DataFrame(ttips)).render()
assert '#T__ #T__row0_col0 .pd-t::after {\n content: "Mi";\n}' in s
assert '#T__ #T__row0_col2 .pd-t::after {\n content: "Ma";\n}' in s
assert '#T__ #T__row2_col0 .pd-t::after {\n content: "Mu";\n}' in s
assert '#T__ #T__row2_col2 .pd-t::after {\n content: "Mo";\n}' in s

def test_tooltip_ignored(self):
# GH 21266
df = DataFrame(data=[[0, 1], [2, 3]])
s = Styler(df).render() # no set_tooltips() creates no <span>
assert '<style type="text/css">\n</style>' in s
assert '<span class="pd-t"></span>' not in s

def test_tooltip_css_class(self):
# GH 21266
df = DataFrame(data=[[0, 1], [2, 3]])
s = (
Styler(df, uuid_len=0)
.set_tooltips(
DataFrame([["tooltip"]]),
css_class="other-class",
props=[("color", "green")],
)
.render()
)
assert "#T__ .other-class {\n color: green;\n" in s
assert '#T__ #T__row0_col0 .other-class::after {\n content: "tooltip";\n' in s

# GH 39563
s = (
Styler(df, uuid_len=0)
.set_tooltips(
DataFrame([["tooltip"]]),
css_class="other-class",
props="color:green;color:red;",
)
.render()
)
assert "#T__ .other-class {\n color: green;\n color: red;\n}" in s


@pytest.fixture
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, make this styler

put these in a conftest.py to share across all tests in this dir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name changed.

currently the dfand styler in different modules are different. Can do a followup to implement consistent and universal fixtures.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i c, ok then

def s(df):
return Styler(df, uuid_len=0)


@pytest.mark.parametrize(
"ttips",
[
DataFrame( # Test basic reindex and ignoring blank
data=[["Min", "Max"], [np.nan, ""]],
columns=["A", "C"],
index=["x", "y"],
),
DataFrame( # Test non-referenced columns, reversed col names, short index
data=[["Max", "Min", "Bad-Col"]], columns=["C", "A", "D"], index=["x"]
),
],
)
def test_tooltip_render(ttips, s):
# GH 21266
result = s.set_tooltips(ttips).render()

# test tooltip table level class
assert "#T__ .pd-t {\n visibility: hidden;\n" in result

# test 'Min' tooltip added
assert "#T__ #T__row0_col0:hover .pd-t {\n visibility: visible;\n}" in result
assert '#T__ #T__row0_col0 .pd-t::after {\n content: "Min";\n}' in result
assert 'class="data row0 col0" >0<span class="pd-t"></span></td>' in result

# test 'Max' tooltip added
assert "#T__ #T__row0_col2:hover .pd-t {\n visibility: visible;\n}" in result
assert '#T__ #T__row0_col2 .pd-t::after {\n content: "Max";\n}' in result
assert 'class="data row0 col2" >2<span class="pd-t"></span></td>' in result

# test Nan, empty string and bad column ignored
assert "#T__ #T__row1_col0:hover .pd-t {\n visibility: visible;\n}" not in result
assert "#T__ #T__row1_col1:hover .pd-t {\n visibility: visible;\n}" not in result
assert "#T__ #T__row0_col1:hover .pd-t {\n visibility: visible;\n}" not in result
assert "#T__ #T__row1_col2:hover .pd-t {\n visibility: visible;\n}" not in result
assert "Bad-Col" not in result


def test_tooltip_ignored(s):
# GH 21266
result = s.render() # no set_tooltips() creates no <span>
assert '<style type="text/css">\n</style>' in result
assert '<span class="pd-t"></span>' not in result


def test_tooltip_css_class(s):
# GH 21266
result = s.set_tooltips(
DataFrame([["tooltip"]], index=["x"], columns=["A"]),
css_class="other-class",
props=[("color", "green")],
).render()
assert "#T__ .other-class {\n color: green;\n" in result
assert '#T__ #T__row0_col0 .other-class::after {\n content: "tooltip";\n' in result

# GH 39563
result = s.set_tooltips( # set_tooltips overwrites previous
DataFrame([["tooltip"]], index=["x"], columns=["A"]),
css_class="another-class",
props="color:green;color:red;",
).render()
assert "#T__ .another-class {\n color: green;\n color: red;\n}" in result