From 4151939ee29f471965e4276f9e806ad3271d8d2d Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 10 Aug 2023 03:55:31 -0500 Subject: [PATCH] test: Add test for iterator exhaustion with `next` * Add test to tests/contrib/test_viz.py to cover the case where the wrong labels are somehow returned in brazil.plot_results, causing the legend label ordering logic to fail. * Amends PR # 2264 --- tests/contrib/test_viz.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/contrib/test_viz.py b/tests/contrib/test_viz.py index 7a49eddb49..f8f760931f 100644 --- a/tests/contrib/test_viz.py +++ b/tests/contrib/test_viz.py @@ -181,3 +181,25 @@ def test_plot_results_components_data_structure(datadir): brazil.plot_results( data["testmus"], data["results"], test_size=0.05, ax=ax, components=True ) + + +def test_plot_results_wrong_axis_labels(datadir, mocker): + """ + If the returned labels are different from the expected, then the hardcoded + values in label_part will be wrong, causing `next` to fail on the iterator + for label_idx. + """ + data = json.load(datadir.joinpath("hypotest_results.json").open(encoding="utf-8")) + + fig = Figure() + ax = fig.subplots() + + get_legend_handles_labels = mocker.patch( + "matplotlib.axes._axes.Axes.get_legend_handles_labels", + return_value=(None, ["fail"]), + ) + + with pytest.raises(StopIteration): + brazil.plot_results(data["testmus"], data["results"], test_size=0.05, ax=ax) + + assert get_legend_handles_labels.called