Skip to content

Commit 54d5ce9

Browse files
committed
Updated formatting of swarmplot related unittests
1 parent 625812f commit 54d5ce9

1 file changed

Lines changed: 45 additions & 88 deletions

File tree

nbs/tests/test_plot_tools.py

Lines changed: 45 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -29,63 +29,35 @@ def test_width_determine():
2929

3030

3131
# swarmplot() UNIT TESTS
32-
@pytest.mark.parametrize(
33-
"param_name, param_value, error_msg, error_type",
34-
[
35-
# Basic input validation checks
36-
("data", None, "`data` must be a Pandas Dataframe.", ValueError),
37-
("x", None, "`x` must be a string.", ValueError),
38-
("y", None, "`y` must be a string.", ValueError),
39-
(
40-
"ax",
41-
None,
42-
"`ax` must be a Matplotlib AxesSubplot. The current `ax` is a <class 'NoneType'>",
43-
ValueError,
44-
),
45-
("order", 5, "`order` must be either an Iterable or None.", ValueError),
46-
("hue", 5, "`hue` must be either a string or None.", ValueError),
47-
(
48-
"palette",
49-
None,
50-
"`palette` must be either a string or an Iterable.",
51-
ValueError,
52-
),
53-
("zorder", None, "`zorder` must be a scalar or float.", ValueError),
54-
("size", None, "`size` must be a scalar or float.", ValueError),
55-
(
56-
"side",
57-
None,
58-
"Invalid `side`. Must be one of 'center', 'right', or 'left'.",
59-
ValueError,
60-
),
61-
("jitter", None, "`jitter` must be a scalar or float.", ValueError),
62-
("is_drop_gutter", None, "`is_drop_gutter` must be a boolean.", ValueError),
63-
("gutter_limit", None, "`gutter_limit` must be a scalar or float.", ValueError),
64-
# More thorough input validation checks
65-
("x", "a", "a is not a column in `data`.", IndexError),
66-
("y", "b", "b is not a column in `data`.", IndexError),
67-
("hue", "c", "c is not a column in `data`.", IndexError),
68-
(
69-
"order",
70-
["Control 1", "Test 2"],
71-
"Test 2 in `order` is not in the 'group' column of `data`.",
72-
IndexError,
73-
),
74-
(
75-
"palette",
76-
{"Control 3": "black"},
77-
"Control 3 in `palette` is not in the 'group' column of `data`.",
78-
IndexError,
79-
),
80-
# TODO: to add palette validation testing for when color_col is hue
81-
(
82-
"side",
83-
"top",
84-
"Invalid `side`. Must be one of 'center', 'right', or 'left'.",
85-
ValueError,
86-
),
87-
],
88-
)
32+
# fmt: off
33+
@pytest.mark.parametrize("param_name, param_value, error_msg, error_type", [
34+
# Basic input validation checks
35+
("data", None, "`data` must be a Pandas Dataframe.", ValueError),
36+
("x", None, "`x` must be a string.", ValueError),
37+
("y", None, "`y` must be a string.", ValueError),
38+
("ax", None, "`ax` must be a Matplotlib AxesSubplot. The current `ax` is a <class 'NoneType'>", ValueError),
39+
("order", 5, "`order` must be either an Iterable or None.", ValueError),
40+
("hue", 5, "`hue` must be either a string or None.", ValueError),
41+
("palette", None, "`palette` must be either a string or an Iterable.", ValueError),
42+
("zorder", None, "`zorder` must be a scalar or float.", ValueError),
43+
("size", None, "`size` must be a scalar or float.", ValueError),
44+
("side", None, "Invalid `side`. Must be one of 'center', 'right', or 'left'.", ValueError),
45+
("jitter", None, "`jitter` must be a scalar or float.", ValueError),
46+
("is_drop_gutter", None, "`is_drop_gutter` must be a boolean.", ValueError),
47+
("gutter_limit", None, "`gutter_limit` must be a scalar or float.", ValueError),
48+
49+
# More thorough input validation checks
50+
("x", "a", "a is not a column in `data`.", IndexError),
51+
("y", "b", "b is not a column in `data`.", IndexError),
52+
("hue", "c", "c is not a column in `data`.", IndexError),
53+
("order", ["Control 1", "Test 2"], "Test 2 in `order` is not in the 'group' column of `data`.", IndexError),
54+
("palette", " ", "`palette` cannot be an empty string. It must be either a string indicating a color name or an Iterable.", ValueError),
55+
("palette", {"Control 1": " "}, "The color mapping for Control 3 in `palette` is an empty string. It must contain a color name.", ValueError),
56+
("palette", {"Control 1": "black"}, "Control 3 in `palette` is not in the 'group' column of `data`.", IndexError),
57+
# TODO: to add palette validation testing for when color_col is hue
58+
("side", "top", "Invalid `side`. Must be one of 'center', 'right', or 'left'.", ValueError)
59+
])
60+
# fmt: on
8961
def test_swarmplot_input_error_handling(param_name, param_value, error_msg, error_type):
9062
with pytest.raises(error_type) as excinfo:
9163
my_data = swarmplot(
@@ -107,6 +79,7 @@ def test_swarmplot_input_error_handling(param_name, param_value, error_msg, erro
10779
assert error_msg in str(excinfo.value)
10880

10981

82+
# fmt: on
11083
def test_swarmplot_warnings():
11184
warning_msg = (
11285
"{0:.1f}% of the points cannot be placed. "
@@ -132,48 +105,32 @@ def test_swarmplot_warnings():
132105

133106
def test_swarmplot_order_params():
134107
# `order` should be able to handle customised order -> swapping of params in `order` list
135-
check_exceptions(
136-
swarmplot, order=["Control 1", "Test 1"], **default_swarmplot_kwargs
137-
)
138-
check_exceptions(
139-
swarmplot, order=["Test 1", "Control 1"], **default_swarmplot_kwargs
140-
)
108+
swarmplot(order=["Control 1", "Test 1"], **default_swarmplot_kwargs)
109+
swarmplot(order=["Test 1", "Control 1"], **default_swarmplot_kwargs)
141110
# `order` should be able to handle None, where it will then be autogenerated
142-
check_exceptions(swarmplot, order=None, **default_swarmplot_kwargs)
111+
swarmplot(order=None, **default_swarmplot_kwargs)
143112

144113

145114
def test_swarmplot_hue_params():
146-
check_exceptions(swarmplot, hue="gender", **default_swarmplot_kwargs)
115+
swarmplot(hue="gender", **default_swarmplot_kwargs)
147116

148117

149118
def test_swarmplot_palette_params():
150119
# `palette` can be a string, list, tuple or a dict
151120
# Testing `palette` when color of swarms is based on `x` value
152-
check_exceptions(swarmplot, hue=None, palette="black", **default_swarmplot_kwargs)
153-
check_exceptions(
154-
swarmplot, hue=None, palette=["black", "red"], **default_swarmplot_kwargs
155-
)
156-
check_exceptions(
157-
swarmplot, hue=None, palette=("black", "red"), **default_swarmplot_kwargs
158-
)
159-
check_exceptions(
160-
swarmplot,
121+
swarmplot(hue=None, palette="black", **default_swarmplot_kwargs)
122+
swarmplot(hue=None, palette=["", "red"], **default_swarmplot_kwargs)
123+
swarmplot(hue=None, palette=("black", "red"), **default_swarmplot_kwargs)
124+
swarmplot(
161125
hue=None,
162126
palette={"Control 1": "black", "Test 1": "red"},
163127
**default_swarmplot_kwargs
164128
)
165129
# Testing `palette` when color of swarms is based on `hue` value
166-
check_exceptions(
167-
swarmplot, hue="gender", palette="black", **default_swarmplot_kwargs
168-
)
169-
check_exceptions(
170-
swarmplot, hue="gender", palette=["black", "red"], **default_swarmplot_kwargs
171-
)
172-
check_exceptions(
173-
swarmplot, hue="gender", palette=("black", "red"), **default_swarmplot_kwargs
174-
)
175-
check_exceptions(
176-
swarmplot,
130+
swarmplot(hue="gender", palette="black", **default_swarmplot_kwargs)
131+
swarmplot(hue="gender", palette=["black", "red"], **default_swarmplot_kwargs)
132+
swarmplot(hue="gender", palette=("black", "red"), **default_swarmplot_kwargs)
133+
swarmplot(
177134
hue="gender",
178135
palette={"Female": "black", "Male": "red"},
179136
**default_swarmplot_kwargs
@@ -184,6 +141,6 @@ def test_swarmplot_palette_params():
184141

185142

186143
def test_swarmplot_side_params():
187-
check_exceptions(swarmplot, side="center", **default_swarmplot_kwargs)
188-
check_exceptions(swarmplot, side="right", **default_swarmplot_kwargs)
189-
check_exceptions(swarmplot, side="left", **default_swarmplot_kwargs)
144+
swarmplot(side="center", **default_swarmplot_kwargs)
145+
swarmplot(side="right", **default_swarmplot_kwargs)
146+
swarmplot(side="left", **default_swarmplot_kwargs)

0 commit comments

Comments
 (0)