Skip to content

Commit 0c86edd

Browse files
authored
Merge pull request #467 from RocketPy-Team/tst/multiple-asserts
TST: new set of tests
2 parents 14937a7 + 361e592 commit 0c86edd

File tree

4 files changed

+134
-5
lines changed

4 files changed

+134
-5
lines changed

rocketpy/mathutils/function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
carefully as it may impact all the rest of the project.
55
"""
66
import warnings
7-
from inspect import signature
87
from collections.abc import Iterable
8+
from inspect import signature
99
from pathlib import Path
1010

1111
import matplotlib.pyplot as plt

tests/test_flight.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,19 @@ def test_initial_solution(mock_show, example_env, calisto_robust):
147147
def test_get_solution_at_time(flight_calisto):
148148
"""Test the get_solution_at_time method of the Flight class. This test
149149
simply calls the method at the initial and final time and checks if the
150-
returned values are correct.
150+
returned values are correct. Also, checking for valid return instance.
151151
152152
Parameters
153153
----------
154154
flight_calisto : rocketpy.Flight
155155
Flight object to be tested. See the conftest.py file for more info
156156
regarding this pytest fixture.
157157
"""
158+
assert isinstance(flight_calisto.get_solution_at_time(0), np.ndarray)
159+
assert isinstance(
160+
flight_calisto.get_solution_at_time(flight_calisto.t_final), np.ndarray
161+
)
162+
158163
assert np.allclose(
159164
flight_calisto.get_solution_at_time(0),
160165
np.array([0, 0, 0, 0, 0, 0, 0, 0.99904822, -0.04361939, 0, 0, 0, 0, 0]),
@@ -576,7 +581,7 @@ def test_lat_lon_conversion_from_origin(mock_show, example_env, calisto_robust):
576581
],
577582
)
578583
def test_rail_length(calisto_robust, example_env, rail_length, out_of_rail_time):
579-
"""Test the rail length parameter of the Flight class. This test simply
584+
"""Tests the rail length parameter of the Flight class. This test simply
580585
simulate the flight using different rail lengths and checks if the expected
581586
out of rail altitude is achieved. Four different rail lengths are
582587
tested: 0.001, 1, 10, and 100000 meters. This provides a good test range.

tests/test_function.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_plots(mock_show, func_from_csv):
8888

8989

9090
def test_interpolation_methods(linear_func):
91-
"""Test some of the interpolation methods of the Function class. Methods
91+
"""Tests some of the interpolation methods of the Function class. Methods
9292
not tested here are already being called in other tests.
9393
9494
Parameters
@@ -97,12 +97,17 @@ def test_interpolation_methods(linear_func):
9797
A Function object created from a list of values.
9898
"""
9999
# Test Akima
100+
assert isinstance(linear_func.set_interpolation("akima"), Function)
100101
linear_func.set_interpolation("akima")
102+
assert isinstance(linear_func.get_interpolation_method(), str)
101103
assert linear_func.get_interpolation_method() == "akima"
102104
assert np.isclose(linear_func.get_value(0), 0.0, atol=1e-6)
103105

104106
# Test polynomial
107+
108+
assert isinstance(linear_func.set_interpolation("polynomial"), Function)
105109
linear_func.set_interpolation("polynomial")
110+
assert isinstance(linear_func.get_interpolation_method(), str)
106111
assert linear_func.get_interpolation_method() == "polynomial"
107112
assert np.isclose(linear_func.get_value(0), 0.0, atol=1e-6)
108113

@@ -122,12 +127,16 @@ def test_extrapolation_methods(linear_func):
122127
assert np.isclose(linear_func.get_value(-1), 0, atol=1e-6)
123128

124129
# Test constant
130+
assert isinstance(linear_func.set_extrapolation("constant"), Function)
125131
linear_func.set_extrapolation("constant")
132+
assert isinstance(linear_func.get_extrapolation_method(), str)
126133
assert linear_func.get_extrapolation_method() == "constant"
127134
assert np.isclose(linear_func.get_value(-1), 0, atol=1e-6)
128135

129136
# Test natural
137+
assert isinstance(linear_func.set_extrapolation("natural"), Function)
130138
linear_func.set_extrapolation("natural")
139+
assert isinstance(linear_func.get_extrapolation_method(), str)
131140
assert linear_func.get_extrapolation_method() == "natural"
132141
assert np.isclose(linear_func.get_value(-1), -1, atol=1e-6)
133142

@@ -143,6 +152,7 @@ def test_integral_linear_interpolation(linearly_interpolated_func, a, b):
143152
A Function object created from a list of values.
144153
"""
145154
# Test integral
155+
assert isinstance(linearly_interpolated_func.integral(a, b, numerical=True), float)
146156
assert np.isclose(
147157
linearly_interpolated_func.integral(a, b, numerical=False),
148158
linearly_interpolated_func.integral(a, b, numerical=True),
@@ -175,6 +185,75 @@ def test_integral_spline_interpolation(request, func, a, b):
175185
)
176186

177187

188+
def test_differentiate():
189+
"""Tests the differentiation method of the Function class.
190+
Both with respect to return instances and expected behaviour.
191+
"""
192+
func = Function(1)
193+
assert isinstance(func.differentiate(0), float)
194+
assert np.isclose(func.differentiate(5), 0)
195+
196+
func_x = Function(lambda x: x)
197+
assert isinstance(func_x.differentiate(0), float)
198+
assert np.isclose(func_x.differentiate(0), 1)
199+
200+
f_square = Function(lambda x: x**2)
201+
assert isinstance(f_square.differentiate(1), float)
202+
assert np.isclose(f_square.differentiate(1), 2)
203+
204+
205+
def test_get_value():
206+
"""Tests the get_value method of the Function class.
207+
Both with respect to return instances and expected behaviour.
208+
"""
209+
func = Function(lambda x: 2 * x)
210+
assert isinstance(func.get_value(1), int or float)
211+
212+
213+
def test_identity_function():
214+
"""Tests the identity_function method of the Function class.
215+
Both with respect to return instances and expected behaviour.
216+
"""
217+
218+
func = Function(lambda x: x**2)
219+
assert isinstance(func.identity_function(), Function)
220+
221+
222+
def test_derivative_function():
223+
"""Tests the derivative_function method of the Function class.
224+
Both with respect to return instances and expected behaviour.
225+
"""
226+
square = Function(lambda x: x**2)
227+
assert isinstance(square.derivative_function(), Function)
228+
229+
230+
def test_integral():
231+
"""Tests the integral method of the Function class.
232+
Both with respect to return instances and expected behaviour.
233+
"""
234+
235+
zero_func = Function(0)
236+
assert isinstance(zero_func.integral(2, 4, numerical=True), float)
237+
assert zero_func.integral(2, 4, numerical=True) == 0
238+
239+
square = Function(lambda x: x**2)
240+
assert isinstance
241+
assert square.integral(2, 4, numerical=True) == -square.integral(
242+
4, 2, numerical=True
243+
)
244+
assert square.integral(2, 4, numerical=False) == -square.integral(
245+
4, 2, numerical=False
246+
)
247+
248+
249+
def test_integral_function():
250+
"""Tests the integral_function method of the Function class.
251+
Both with respect to return instances and expected behaviour.
252+
"""
253+
zero_func = Function(0)
254+
assert isinstance(zero_func, Function)
255+
256+
178257
@pytest.mark.parametrize("a", [-1, 0, 1])
179258
@pytest.mark.parametrize("b", [-1, 0, 1])
180259
def test_multivariable_dataset(a, b):

tests/test_rocket.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import numpy as np
44
import pytest
55

6-
from rocketpy import NoseCone, Rocket, SolidMotor
6+
from rocketpy import NoseCone, Rocket, SolidMotor, Function
7+
from rocketpy.motors.motor import Motor, EmptyMotor
78

89

910
@patch("matplotlib.pyplot.show")
@@ -489,6 +490,27 @@ def test_add_cp_eccentricity_assert_properties_set(calisto):
489490
assert calisto.cp_eccentricity_y == 5
490491

491492

493+
def test_add_motor(calisto_motorless, cesaroni_m1670):
494+
"""Tests the add_motor method of the Rocket class.
495+
Both with respect to return instances and expected behaviour.
496+
Parameters
497+
----------
498+
calisto_motorless : Rocket instance
499+
A predefined instance of a Rocket without a motor, used as a base for testing.
500+
cesaroni_m1670 : rocketpy.SolidMotor
501+
Cesaroni M1670 motor
502+
"""
503+
504+
assert isinstance(calisto_motorless.motor, EmptyMotor)
505+
center_of_mass_motorless = calisto_motorless.center_of_mass
506+
calisto_motorless.add_motor(cesaroni_m1670, 0)
507+
508+
assert isinstance(calisto_motorless.motor, Motor)
509+
center_of_mass_with_motor = calisto_motorless.center_of_mass
510+
511+
assert center_of_mass_motorless is not center_of_mass_with_motor
512+
513+
492514
def test_set_rail_button(calisto):
493515
rail_buttons = calisto.set_rail_buttons(0.2, -0.5, 30)
494516
# assert buttons_distance
@@ -509,3 +531,26 @@ def test_set_rail_button(calisto):
509531
assert calisto.rail_buttons[0].component.buttons_distance + calisto.rail_buttons[
510532
0
511533
].position == pytest.approx(0.2, 1e-12)
534+
535+
536+
def test_evaluate_total_mass(calisto_motorless):
537+
"""Tests the evaluate_total_mass method of the Rocket class.
538+
Both with respect to return instances and expected behaviour.
539+
540+
Parameters
541+
----------
542+
calisto_motorless : Rocket instance
543+
A predefined instance of a Rocket without a motor, used as a base for testing.
544+
"""
545+
assert isinstance(calisto_motorless.evaluate_total_mass(), Function)
546+
547+
548+
def test_evaluate_center_of_mass(calisto):
549+
"""Tests the evaluate_center_of_mass method of the Rocket class.
550+
Both with respect to return instances and expected behaviour.
551+
Parameters
552+
----------
553+
calisto : Rocket instance
554+
A predefined instance of the calisto Rocket with a motor, used as a base for testing.
555+
"""
556+
assert isinstance(calisto.evaluate_center_of_mass(), Function)

0 commit comments

Comments
 (0)