diff --git a/pandas/tests/io/formats/style/test_tooltip.py b/pandas/tests/io/formats/style/test_tooltip.py index 9539780287f15..71ce496cca030 100644 --- a/pandas/tests/io/formats/style/test_tooltip.py +++ b/pandas/tests/io/formats/style/test_tooltip.py @@ -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 ( - '0' - + "" - 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 ( - '3' - + "" - 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 - assert '' in s - assert '' 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 +def styler(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, styler): + # GH 21266 + result = styler.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' 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' 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(styler): + # GH 21266 + result = styler.render() # no set_tooltips() creates no + assert '' in result + assert '' not in result + + +def test_tooltip_css_class(styler): + # GH 21266 + result = styler.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 = styler.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