From 5027d0f1a283366d0b43c0422e39725e4736f71a Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 21 Mar 2021 09:53:27 +0100 Subject: [PATCH 1/4] test_highlight convert to functional tests not class --- .../tests/io/formats/style/test_highlight.py | 111 ++++++++++-------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/pandas/tests/io/formats/style/test_highlight.py b/pandas/tests/io/formats/style/test_highlight.py index 7cf958be11392..228f266634d6e 100644 --- a/pandas/tests/io/formats/style/test_highlight.py +++ b/pandas/tests/io/formats/style/test_highlight.py @@ -3,63 +3,70 @@ from pandas import DataFrame +from pandas.io.formats.style import Styler + pytest.importorskip("jinja2") -class TestStylerHighlight: - def setup_method(self, method): - np.random.seed(24) - self.s = DataFrame({"A": np.random.permutation(range(6))}) - self.df = DataFrame({"A": [0, 1], "B": np.random.randn(2)}) +@pytest.fixture +def df(): + return DataFrame({"A": [0, np.nan, 10], "B": [1, None, 2]}) + - def test_highlight_null(self): - df = DataFrame({"A": [0, np.nan]}) - result = df.style.highlight_null()._compute().ctx - expected = {(1, 0): [("background-color", "red")]} - assert result == expected +@pytest.fixture +def s(df): + return Styler(df, uuid_len=0) - def test_highlight_null_subset(self): - # GH 31345 - df = DataFrame({"A": [0, np.nan], "B": [0, np.nan]}) - result = ( - df.style.highlight_null(null_color="red", subset=["A"]) - .highlight_null(null_color="green", subset=["B"]) - ._compute() - .ctx - ) - expected = { - (1, 0): [("background-color", "red")], - (1, 1): [("background-color", "green")], - } - assert result == expected - @pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) - def test_highlight_minmax_basic(self, f): - expected = { - (0, 0): [("background-color", "red")], - (1, 0): [("background-color", "red")], - } - if f == "highlight_min": - df = -self.df - else: - df = self.df - result = getattr(df.style, f)(axis=1, color="red")._compute().ctx - assert result == expected +def test_highlight_null(df): + result = df.style.highlight_null()._compute().ctx + expected = { + (1, 0): [("background-color", "red")], + (1, 1): [("background-color", "red")], + } + assert result == expected - @pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) - @pytest.mark.parametrize( - "kwargs", - [ - {"axis": None, "color": "red"}, # test axis - {"axis": 0, "subset": ["A"], "color": "red"}, # test subset - {"axis": None, "props": "background-color: red"}, # test props - ], + +def test_highlight_null_subset(s): + # GH 31345 + result = ( + s.highlight_null(null_color="red", subset=["A"]) + .highlight_null(null_color="green", subset=["B"]) + ._compute() + .ctx ) - def test_highlight_minmax_ext(self, f, kwargs): - expected = {(1, 0): [("background-color", "red")]} - if f == "highlight_min": - df = -self.df - else: - df = self.df - result = getattr(df.style, f)(**kwargs)._compute().ctx - assert result == expected + expected = { + (1, 0): [("background-color", "red")], + (1, 1): [("background-color", "green")], + } + assert result == expected + + +@pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) +def test_highlight_minmax_basic(df, f): + expected = { + (0, 1): [("background-color", "red")], + # ignores NaN row, + (2, 0): [("background-color", "red")], + } + if f == "highlight_min": + df = -df + result = getattr(df.style, f)(axis=1, color="red")._compute().ctx + assert result == expected + + +@pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) +@pytest.mark.parametrize( + "kwargs", + [ + {"axis": None, "color": "red"}, # test axis + {"axis": 0, "subset": ["A"], "color": "red"}, # test subset and ignores NaN + {"axis": None, "props": "background-color: red"}, # test props + ], +) +def test_highlight_minmax_ext(df, f, kwargs): + expected = {(2, 0): [("background-color", "red")]} + if f == "highlight_min": + df = -df + result = getattr(df.style, f)(**kwargs)._compute().ctx + assert result == expected From 54b98a5333e23c134f61700f9062a7a45f6ae4c6 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 21 Mar 2021 13:21:33 +0100 Subject: [PATCH 2/4] test_highlight convert to functional tests not class --- pandas/tests/io/formats/style/test_highlight.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/style/test_highlight.py b/pandas/tests/io/formats/style/test_highlight.py index 228f266634d6e..caf948288778c 100644 --- a/pandas/tests/io/formats/style/test_highlight.py +++ b/pandas/tests/io/formats/style/test_highlight.py @@ -18,8 +18,8 @@ def s(df): return Styler(df, uuid_len=0) -def test_highlight_null(df): - result = df.style.highlight_null()._compute().ctx +def test_highlight_null(s): + result = s.highlight_null()._compute().ctx expected = { (1, 0): [("background-color", "red")], (1, 1): [("background-color", "red")], From e7390bc9eca3b084cffee95ac0b608912a96166b Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 22 Mar 2021 08:07:52 +0100 Subject: [PATCH 3/4] import order fix --- pandas/tests/io/formats/style/test_highlight.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/style/test_highlight.py b/pandas/tests/io/formats/style/test_highlight.py index caf948288778c..f3c4e283c4396 100644 --- a/pandas/tests/io/formats/style/test_highlight.py +++ b/pandas/tests/io/formats/style/test_highlight.py @@ -3,10 +3,10 @@ from pandas import DataFrame -from pandas.io.formats.style import Styler - pytest.importorskip("jinja2") +from pandas.io.formats.style import Styler + @pytest.fixture def df(): From 788290b9decdd10f4f73fd1954f1172384039ba6 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 22 Mar 2021 15:10:07 +0100 Subject: [PATCH 4/4] name change --- pandas/tests/io/formats/style/test_highlight.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/formats/style/test_highlight.py b/pandas/tests/io/formats/style/test_highlight.py index f3c4e283c4396..8aca3cadff0b4 100644 --- a/pandas/tests/io/formats/style/test_highlight.py +++ b/pandas/tests/io/formats/style/test_highlight.py @@ -14,12 +14,12 @@ def df(): @pytest.fixture -def s(df): +def styler(df): return Styler(df, uuid_len=0) -def test_highlight_null(s): - result = s.highlight_null()._compute().ctx +def test_highlight_null(styler): + result = styler.highlight_null()._compute().ctx expected = { (1, 0): [("background-color", "red")], (1, 1): [("background-color", "red")], @@ -27,10 +27,10 @@ def test_highlight_null(s): assert result == expected -def test_highlight_null_subset(s): +def test_highlight_null_subset(styler): # GH 31345 result = ( - s.highlight_null(null_color="red", subset=["A"]) + styler.highlight_null(null_color="red", subset=["A"]) .highlight_null(null_color="green", subset=["B"]) ._compute() .ctx