|
| 1 | +import os |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +from rocketpy import Flight |
| 7 | +from rocketpy.plots.compare import Compare, CompareFlights |
| 8 | + |
| 9 | + |
| 10 | +@patch("matplotlib.pyplot.show") |
| 11 | +def test_compare(mock_show, rocket, example_env): |
| 12 | + """Here we want to test the 'x_attributes' argument, which is the only one |
| 13 | + that is not tested in the other tests. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + mock_show : |
| 18 | + Mocks the matplotlib.pyplot.show() function to avoid showing the plots. |
| 19 | + rocket : rocketpy.Rocket |
| 20 | + Rocket object to be used in the tests. See conftest.py for more details. |
| 21 | + example_env : rocketpy.Environment |
| 22 | + Environment object to be used in the tests. See conftest.py for more details. |
| 23 | + """ |
| 24 | + rocket.setRailButtons([-0.5, 0.2]) |
| 25 | + flight = Flight(environment=example_env, rocket=rocket, inclination=85, heading=0) |
| 26 | + |
| 27 | + objects = [flight, flight, flight] |
| 28 | + |
| 29 | + comparison = Compare(object_list=objects) |
| 30 | + |
| 31 | + fig, _ = comparison.create_comparison_figure( |
| 32 | + y_attributes=["z"], |
| 33 | + n_rows=1, |
| 34 | + n_cols=1, |
| 35 | + figsize=(10, 10), |
| 36 | + legend=False, |
| 37 | + title="Test", |
| 38 | + x_labels=["Time (s)"], |
| 39 | + y_labels=["Altitude (m)"], |
| 40 | + x_lim=(0, 3), |
| 41 | + y_lim=(0, 1000), |
| 42 | + x_attributes=["time"], |
| 43 | + ) |
| 44 | + |
| 45 | + assert isinstance(fig, plt.Figure) == True |
| 46 | + |
| 47 | + |
| 48 | +@patch("matplotlib.pyplot.show") |
| 49 | +def test_compare_flights(mock_show, rocket, example_env): |
| 50 | + """Tests the CompareFlights class. It simply ensures that all the methods |
| 51 | + are being called without errors. It does not test the actual plots, which |
| 52 | + would be very difficult to do. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + mock_show : |
| 57 | + Mocks the matplotlib.pyplot.show() function to avoid showing the plots. |
| 58 | + rocket : rocketpy.Rocket |
| 59 | + Rocket object to be used in the tests. See conftest.py for more details. |
| 60 | + example_env : rocketpy.Environment |
| 61 | + Environment object to be used in the tests. See conftest.py for more details. |
| 62 | +
|
| 63 | + Returns |
| 64 | + ------- |
| 65 | + None |
| 66 | + """ |
| 67 | + example_env.setAtmosphericModel( |
| 68 | + type="CustomAtmosphere", |
| 69 | + pressure=None, |
| 70 | + temperature=300, |
| 71 | + wind_u=[(0, 5), (1000, 10)], |
| 72 | + wind_v=[(0, -2), (500, 3), (1600, 2)], |
| 73 | + ) |
| 74 | + |
| 75 | + rocket.setRailButtons([-0.5, 0.2]) |
| 76 | + |
| 77 | + inclinations = [60, 70, 80, 90] |
| 78 | + headings = [0, 45, 90, 180] |
| 79 | + flights = [] |
| 80 | + |
| 81 | + # Create (4 * 4) = 16 different flights to be compared |
| 82 | + for heading in headings: |
| 83 | + for inclination in inclinations: |
| 84 | + flight = Flight( |
| 85 | + environment=example_env, |
| 86 | + rocket=rocket, |
| 87 | + inclination=inclination, |
| 88 | + heading=heading, |
| 89 | + name=f"Incl {inclination} Head {heading}", |
| 90 | + ) |
| 91 | + flights.append(flight) |
| 92 | + |
| 93 | + comparison = CompareFlights(flights) |
| 94 | + |
| 95 | + assert comparison.all() == None |
| 96 | + assert comparison.trajectories_2d(plane="xz", legend=False) == None |
| 97 | + assert comparison.trajectories_2d(plane="yz", legend=True) == None |
| 98 | + |
| 99 | + # Test save fig and then remove file |
| 100 | + assert comparison.positions(filename="test.png") == None |
| 101 | + os.remove("test.png") |
| 102 | + |
| 103 | + # Test xlim and ylim arguments |
| 104 | + assert comparison.positions(x_lim=[0, 100], y_lim=[0, 1000]) == None |
| 105 | + assert comparison.positions(x_lim=[0, "apogee"]) == None |
0 commit comments