Skip to content

Commit a021ec3

Browse files
committed
chore: add more tests
1 parent c8a2b03 commit a021ec3

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

pyretailscience/plots/line.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ def plot(
8181
UserWarning,
8282
stacklevel=2,
8383
)
84+
85+
elif x_col is None and pd.api.types.is_datetime64_any_dtype(df.index):
86+
warnings.warn(
87+
"The DataFrame index is datetime-like. Consider using the 'plots.timeline' module for time-based plots.",
88+
UserWarning,
89+
stacklevel=2,
90+
)
8491
colors = get_base_cmap()
8592

8693
if group_col is None:

tests/plots/test_line.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,94 @@ def test_plot_adds_source_text(sample_dataframe):
178178
)
179179

180180
gu.add_source_text.assert_called_once_with(ax=result_ax, source_text=source_text)
181+
182+
183+
@pytest.mark.usefixtures("_mock_get_base_cmap", "_mock_gu_functions")
184+
def test_plot_with_legend_title(sample_dataframe):
185+
"""Test the plot function with a legend title."""
186+
_, ax = plt.subplots()
187+
188+
# Create the plot with a legend title
189+
legend_title = "Test Legend"
190+
result_ax = line.plot(
191+
df=sample_dataframe,
192+
value_col="y",
193+
x_label="X Axis",
194+
y_label="Y Axis",
195+
title="Test Plot with Legend Title",
196+
x_col="x",
197+
group_col="group",
198+
ax=ax,
199+
legend_title=legend_title,
200+
)
201+
202+
# Assert that standard_graph_styles was called with the provided legend title
203+
gu.standard_graph_styles.assert_called_once_with(
204+
ax=result_ax,
205+
title="Test Plot with Legend Title",
206+
x_label="X Axis",
207+
y_label="Y Axis",
208+
legend_title=legend_title,
209+
move_legend_outside=False,
210+
)
211+
212+
213+
@pytest.mark.usefixtures("_mock_get_base_cmap", "_mock_gu_functions")
214+
def test_plot_with_legend_title_and_move_outside(sample_dataframe):
215+
"""Test the plot function with both move_legend_outside=True and legend_title."""
216+
_, ax = plt.subplots()
217+
218+
# Create the plot with both options
219+
legend_title = "Test Legend"
220+
result_ax = line.plot(
221+
df=sample_dataframe,
222+
value_col="y",
223+
x_label="X Axis",
224+
y_label="Y Axis",
225+
title="Test Plot Legend Outside with Title",
226+
x_col="x",
227+
group_col="group",
228+
ax=ax,
229+
move_legend_outside=True,
230+
legend_title=legend_title,
231+
)
232+
233+
# Assert that standard_graph_styles was called with both options
234+
gu.standard_graph_styles.assert_called_once_with(
235+
ax=result_ax,
236+
title="Test Plot Legend Outside with Title",
237+
x_label="X Axis",
238+
y_label="Y Axis",
239+
legend_title=legend_title,
240+
move_legend_outside=True,
241+
)
242+
243+
244+
@pytest.mark.usefixtures("_mock_get_base_cmap", "_mock_gu_functions")
245+
def test_plot_with_datetime_index_warns(sample_dataframe, mocker):
246+
"""Test the plot function with a datetime index and no x_col, expecting a warning."""
247+
df_with_datetime_index = sample_dataframe.set_index("x")
248+
_, ax = plt.subplots()
249+
250+
# Mock the warnings.warn method to check if it's called
251+
mocker.patch("warnings.warn")
252+
253+
# Create the plot with a datetime index and no x_col
254+
result_ax = line.plot(
255+
df=df_with_datetime_index,
256+
value_col="y",
257+
x_label="X Axis",
258+
y_label="Y Axis",
259+
title="Test Plot Datetime Index",
260+
ax=ax,
261+
)
262+
263+
# Assert that the plot was created
264+
assert isinstance(result_ax, Axes)
265+
266+
# Assert that the warning about datetime-like index was raised
267+
warnings.warn.assert_called_once_with(
268+
"The DataFrame index is datetime-like. Consider using the 'plots.timeline' module for time-based plots.",
269+
UserWarning,
270+
stacklevel=2,
271+
)

0 commit comments

Comments
 (0)