forked from RocketPy-Team/RocketPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plots.py
More file actions
173 lines (130 loc) · 4.64 KB
/
test_plots.py
File metadata and controls
173 lines (130 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
from unittest.mock import MagicMock, patch
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pytest
from rocketpy.plots.compare import Compare
from rocketpy.plots.plot_helpers import (
show_or_save_fig,
show_or_save_plot,
show_or_save_animation,
)
@patch("matplotlib.pyplot.show")
def test_compare(mock_show, flight_calisto): # pylint: disable=unused-argument
"""Here we want to test the 'x_attributes' argument, which is the only one
that is not tested in the other tests.
Parameters
----------
mock_show :
Mocks the matplotlib.pyplot.show() function to avoid showing the plots.
flight_calisto : rocketpy.Flight
Flight object to be used in the tests. See conftest.py for more details.
"""
flight = flight_calisto
objects = [flight, flight, flight]
comparison = Compare(object_list=objects)
fig, _ = comparison.create_comparison_figure(
y_attributes=["z"],
n_rows=1,
n_cols=1,
figsize=(10, 10),
legend=False,
title="Test",
x_labels=["Time (s)"],
y_labels=["Altitude (m)"],
x_lim=(0, 3),
y_lim=(0, 1000),
x_attributes=["time"],
)
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)
@pytest.mark.parametrize("filename", [None, "test.gif"])
@patch("matplotlib.pyplot.show")
def test_show_or_save_animation(mock_show, filename):
"""This test is to check if the show_or_save_animation function is
working properly.
Parameters
----------
mock_show :
Mocks the matplotlib.pyplot.show() function to avoid showing the animation.
filename : str
Name of the file to save the animation. If None, the animation will be
shown instead.
"""
# Create a simple animation object
fig, ax = plt.subplots()
def update(frame):
ax.plot([0, frame], [0, frame])
return ax
animation = FuncAnimation(fig, update, frames=5)
show_or_save_animation(animation, filename)
if filename is None:
mock_show.assert_called_once()
else:
assert os.path.exists(filename)
os.remove(filename)
def test_show_or_save_animation_unsupported_format():
# Test that show_or_save_animation raises ValueError for unsupported formats.
fig, ax = plt.subplots()
def update(frame):
ax.plot([0, frame], [0, frame])
return ax
animation = FuncAnimation(fig, update, frames=5)
with pytest.raises(ValueError, match="Unsupported file ending"):
show_or_save_animation(animation, "test.mp4")
def test_animate_propellant_mass(cesaroni_m1670):
"""Test that animate_propellant_mass saves a .gif file correctly."""
motor = cesaroni_m1670
animation = motor.plots.animate_propellant_mass(filename="cesaroni_m1670.gif")
# Check animation type
assert isinstance(animation, FuncAnimation)
# check if file exists
assert os.path.exists("cesaroni_m1670.gif")
os.remove("cesaroni_m1670.gif")
def test_animate_fluid_volume(example_mass_flow_rate_based_tank_seblm):
"""Test that animate_fluid_volume saves a .gif file correctly."""
tank = example_mass_flow_rate_based_tank_seblm
animation = tank.plots.animate_fluid_volume(filename="test_fluid_volume.gif")
# Check animation type
assert isinstance(animation, FuncAnimation)
# Check if file exists
assert os.path.exists("test_fluid_volume.gif")
os.remove("test_fluid_volume.gif")