Skip to content

Commit

Permalink
Further testing
Browse files Browse the repository at this point in the history
TST: added unit tests to rocketpy.Function, rocketpy.Rocket, rocketpy.Flight submodules.
  • Loading branch information
lucasfourier committed Nov 18, 2023
1 parent f2be825 commit 907e922
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 4 deletions.
7 changes: 5 additions & 2 deletions tests/test_flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,17 @@ def test_initial_solution(mock_show, example_env, calisto_robust):
def test_get_solution_at_time(flight_calisto):
"""Test the get_solution_at_time method of the Flight class. This test
simply calls the method at the initial and final time and checks if the
returned values are correct.
returned values are correct. Also, checking for valid return instance.
Parameters
----------
flight_calisto : rocketpy.Flight
Flight object to be tested. See the conftest.py file for more info
regarding this pytest fixture.
"""
assert isinstance(flight_calisto.get_solution_at_time(0), np.ndarray)
assert isinstance(flight_calisto.get_solution_at_time(flight_calisto.t_final), np.ndarray)

assert np.allclose(
flight_calisto.get_solution_at_time(0),
np.array([0, 0, 0, 0, 0, 0, 0, 0.99904822, -0.04361939, 0, 0, 0, 0, 0]),
Expand Down Expand Up @@ -576,7 +579,7 @@ def test_lat_lon_conversion_from_origin(mock_show, example_env, calisto_robust):
],
)
def test_rail_length(calisto_robust, example_env, rail_length, out_of_rail_time):
"""Test the rail length parameter of the Flight class. This test simply
"""Tests the rail length parameter of the Flight class. This test simply
simulate the flight using different rail lengths and checks if the expected
out of rail altitude is achieved. Four different rail lengths are
tested: 0.001, 1, 10, and 100000 meters. This provides a good test range.
Expand Down
76 changes: 75 additions & 1 deletion tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_plots(mock_show, func_from_csv):


def test_interpolation_methods(linear_func):
"""Test some of the interpolation methods of the Function class. Methods
"""Tests some of the interpolation methods of the Function class. Methods
not tested here are already being called in other tests.
Parameters
Expand All @@ -97,12 +97,17 @@ def test_interpolation_methods(linear_func):
A Function object created from a list of values.
"""
# Test Akima
assert isinstance(linear_func.set_interpolation("akima"), Function)
linear_func.set_interpolation("akima")
assert isinstance(linear_func.get_interpolation_method(), str)
assert linear_func.get_interpolation_method() == "akima"
assert np.isclose(linear_func.get_value(0), 0.0, atol=1e-6)

# Test polynomial

assert isinstance(linear_func.set_interpolation("polynomial"), Function)
linear_func.set_interpolation("polynomial")
assert isinstance(linear_func.get_interpolation_method(), str)
assert linear_func.get_interpolation_method() == "polynomial"
assert np.isclose(linear_func.get_value(0), 0.0, atol=1e-6)

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

# Test constant
assert isinstance(linear_func.set_extrapolation("constant"), Function)
linear_func.set_extrapolation("constant")
assert isinstance(linear_func.get_extrapolation_method(), str)
assert linear_func.get_extrapolation_method() == "constant"
assert np.isclose(linear_func.get_value(-1), 0, atol=1e-6)

# Test natural
assert isinstance(linear_func.set_extrapolation("natural"), Function)
linear_func.set_extrapolation("natural")
assert isinstance(linear_func.get_extrapolation_method(), str)
assert linear_func.get_extrapolation_method() == "natural"
assert np.isclose(linear_func.get_value(-1), -1, atol=1e-6)

Expand All @@ -143,6 +152,7 @@ def test_integral_linear_interpolation(linearly_interpolated_func, a, b):
A Function object created from a list of values.
"""
# Test integral
assert isinstance(linearly_interpolated_func.integral(a, b, numerical=True), float)
assert np.isclose(
linearly_interpolated_func.integral(a, b, numerical=False),
linearly_interpolated_func.integral(a, b, numerical=True),
Expand Down Expand Up @@ -173,3 +183,67 @@ def test_integral_spline_interpolation(request, func, a, b):
func.integral(a, b, numerical=True),
atol=1e-3,
)

def test_differentiate():
"""Tests the differentiation method of the Function class.
Both with respect to return instances and expected behaviour.
"""
func = Function(1)
assert isinstance(func.differentiate(0), float)
assert np.isclose(func.differentiate(5), 0)

func_x = Function(lambda x: x)
assert isinstance(func_x.differentiate(0), float)
assert np.isclose(func_x.differentiate(0), 1)

f_square = Function(lambda x: x**2)
assert isinstance(f_square.differentiate(1), float)
assert np.isclose(f_square.differentiate(1), 2)

def test_get_value():
"""Tests the get_value method of the Function class.
Both with respect to return instances and expected behaviour.
"""
func = Function(lambda x: 2*x)
assert isinstance(func.get_value(1), int or float)

def test_identity_function():
"""Tests the identity_function method of the Function class.
Both with respect to return instances and expected behaviour.
"""

func = Function(lambda x: x**2)
assert isinstance(func.identity_function(), Function)


def test_derivative_function():
"""Tests the derivative_function method of the Function class.
Both with respect to return instances and expected behaviour.
"""
square = Function(lambda x: x**2)
assert isinstance(square.derivative_function(), Function)


def test_integral():
"""Tests the integral method of the Function class.
Both with respect to return instances and expected behaviour.
"""

zero_func = Function(0)
assert isinstance(zero_func.integral(2, 4, numerical=True), float)
assert zero_func.integral(2, 4, numerical=True) == 0

square = Function(lambda x: x**2)
assert isinstance
assert square.integral(2, 4, numerical=True) == -square.integral(4, 2, numerical=True)
assert square.integral(2, 4, numerical=False) == -square.integral(4, 2, numerical=False)


def test_integral_function():
"""Tests the integral_function method of the Function class.
Both with respect to return instances and expected behaviour.
"""
zero_func = Function(0)
assert isinstance(zero_func, Function)


47 changes: 46 additions & 1 deletion tests/test_rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import numpy as np
import pytest

from rocketpy import NoseCone, Rocket, SolidMotor
from rocketpy import NoseCone, Rocket, SolidMotor, Function
from rocketpy.motors.motor import EmptyMotor


@patch("matplotlib.pyplot.show")
Expand Down Expand Up @@ -488,6 +489,27 @@ def test_add_cp_eccentricity_assert_properties_set(calisto):
assert calisto.cp_eccentricity_x == 4
assert calisto.cp_eccentricity_y == 5

def test_add_motor(calisto_motorless, cesaroni_m1670):
"""Tests the add_motor method of the Rocket class.
Both with respect to return instances and expected behaviour.
Parameters
----------
calisto_motorless : Rocket instance
A predefined instance of a Rocket without a motor, used as a base for testing.
cesaroni_m1670 : rocketpy.SolidMotor
Cesaroni M1670 motor
"""
assert isinstance(calisto_motorless.motor, EmptyMotor)
center_of_mass_motorless = calisto_motorless.center_of_mass
calisto_motorless.add_motor(cesaroni_m1670, 0)

assert calisto_motorless.motor is not None
center_of_mass_with_motor = calisto_motorless.center_of_mass

print(center_of_mass_motorless)
print(center_of_mass_with_motor)
assert center_of_mass_motorless is not center_of_mass_with_motor


def test_set_rail_button(calisto):
rail_buttons = calisto.set_rail_buttons(0.2, -0.5, 30)
Expand All @@ -509,3 +531,26 @@ def test_set_rail_button(calisto):
assert calisto.rail_buttons[0].component.buttons_distance + calisto.rail_buttons[
0
].position == pytest.approx(0.2, 1e-12)

def test_evaluate_total_mass(calisto_motorless):
"""Tests the evaluate_total_mass method of the Rocket class.
Both with respect to return instances and expected behaviour.
Parameters
----------
calisto_motorless : Rocket instance
A predefined instance of a Rocket without a motor, used as a base for testing.
"""
assert isinstance(calisto_motorless.evaluate_total_mass(), Function)

def test_evaluate_center_of_mass(calisto):
"""Tests the evaluate_center_of_mass method of the Rocket class.
Both with respect to return instances and expected behaviour.
Parameters
----------
calisto : Rocket instance
A predefined instance of the calisto Rocket with a motor, used as a base for testing.
"""
assert isinstance(calisto.evaluate_center_of_mass(), Function)



0 comments on commit 907e922

Please sign in to comment.