Skip to content

Commit

Permalink
TST: implement testing of show or save plots.
Browse files Browse the repository at this point in the history
  • Loading branch information
phmbressan committed Nov 8, 2024
1 parent dcde26a commit f68a4af
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tests/unit/test_plots.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest.mock import patch
import os
from unittest.mock import MagicMock, patch

import matplotlib.pyplot as plt
import pytest

from rocketpy.plots.compare import Compare
from rocketpy.plots.plot_helpers import show_or_save_fig, show_or_save_plot


@patch("matplotlib.pyplot.show")
Expand Down Expand Up @@ -38,3 +41,51 @@ def test_compare(mock_show, flight_calisto): # pylint: disable=unused-argument
)

assert isinstance(fig, plt.Figure)


@patch("matplotlib.pyplot.show")
@pytest.mark.parametrize("filename", [None, "test.png"])
def test_show_or_save_plot(mock_show, filename):
"""This test is to check if the show_or_save_plot function is
working properly.
Parameters
----------
mock_show :
Mocks the matplotlib.pyplot.show() function to avoid showing
the plots.
filename : str
Name of the file to save the plot. If None, the plot will be
shown instead.
"""
plt.subplots()
show_or_save_plot(filename)

if filename is None:
mock_show.assert_called_once()
else:
assert os.path.exists(filename)
os.remove(filename)


@pytest.mark.parametrize("filename", [None, "test.png"])
def test_show_or_save_fig(filename):
"""This test is to check if the show_or_save_fig function is
working properly.
Parameters
----------
filename : str
Name of the file to save the plot. If None, the plot will be
shown instead.
"""
fig, _ = plt.subplots()

fig.show = MagicMock()
show_or_save_fig(fig, filename)

if filename is None:
fig.show.assert_called_once()
else:
assert os.path.exists(filename)
os.remove(filename)

0 comments on commit f68a4af

Please sign in to comment.