From 018075545d76b5134bb6480aaf49058dabdfe6e3 Mon Sep 17 00:00:00 2001 From: Lucas <69172945+lucasfourier@users.noreply.github.com> Date: Wed, 29 Nov 2023 00:16:45 -0300 Subject: [PATCH 1/2] TST: Simple refactoring While studying some of the tests that were written, I made some simple changes to conftest.py, test_function.py and test_solidmotor.py. --- tests/conftest.py | 33 +++++++++++++++++++ tests/test_function.py | 36 ++++++++++++--------- tests/test_solidmotor.py | 68 +++++++++++++++++++++++----------------- 3 files changed, 93 insertions(+), 44 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2e4b040f0..9b23e27c1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -109,6 +109,39 @@ def cesaroni_m1670(): # old name: solid_motor ) return example_motor +@pytest.fixture +def cesaroni_m1670_shifted(): # old name: solid_motor + """Create a simple object of the SolidMotor class to be used in the tests. + This is the same motor that has been used in the getting started guide for + years. The difference relies in the thrust_source, which was shifted for + testing purposes. + + Returns + ------- + rocketpy.SolidMotor + A simple object of the SolidMotor class + """ + example_motor = SolidMotor( + thrust_source="tests/fixtures/motor/Cesaroni_M1670_shifted.eng", + burn_time=3.9, + dry_mass=1.815, + dry_inertia=(0.125, 0.125, 0.002), + center_of_dry_mass_position=0.317, + nozzle_position=0, + grain_number=5, + grain_density=1815, + nozzle_radius=33 / 1000, + throat_radius=11 / 1000, + grain_separation=5 / 1000, + grain_outer_radius=33 / 1000, + grain_initial_height=120 / 1000, + grains_center_of_mass_position=0.397, + grain_initial_inner_radius=15 / 1000, + interpolation_method="linear", + coordinate_system_orientation="nozzle_to_combustion_chamber", + reshape_thrust_curve=(5, 3000), + ) + return example_motor @pytest.fixture def calisto_motorless(): diff --git a/tests/test_function.py b/tests/test_function.py index c67a21b30..40cb80bf6 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -232,23 +232,29 @@ def test_integral_spline_interpolation(request, func, a, b): atol=1e-3, ) +@pytest.mark.parametrize( + "func_input, derivative_input, expected_derivative", + [ + (1, 0, 0), # Test case 1: Function(1) + (lambda x: x, 0, 1), # Test case 2: Function(lambda x: x) + (lambda x: x**2, 1, 2), # Test case 3: Function(lambda x: x**2) + ], +) +def test_differentiate(func_input, derivative_input, expected_derivative): + """Test the differentiate method of the Function class. -def test_differentiate(): - """Tests the differentiation method of the Function class. - Both with respect to return instances and expected behaviour. + Parameters + ---------- + func_input : function + A function object created from a list of values. + derivative_input : int + Point at which to differentiate. + expected_derivative : float + Expected value of the derivative. """ - 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) - + func = Function(func_input) + assert isinstance(func.differentiate(derivative_input), float) + assert np.isclose(func.differentiate(derivative_input), expected_derivative) def test_get_value(): """Tests the get_value method of the Function class. diff --git a/tests/test_solidmotor.py b/tests/test_solidmotor.py index 6802399e1..853f163d0 100644 --- a/tests/test_solidmotor.py +++ b/tests/test_solidmotor.py @@ -4,7 +4,7 @@ import numpy as np import pytest -from rocketpy import SolidMotor +from rocketpy import Function, SolidMotor burn_time = 3.9 grain_number = 5 @@ -15,6 +15,8 @@ grain_initial_height = 120 / 1000 nozzle_radius = 33 / 1000 throat_radius = 11 / 1000 +grain_vol = 0.12 * (np.pi * (0.033**2 - 0.015**2)) +grain_mass = grain_vol * 1815 * 5 @patch("matplotlib.pyplot.show") @@ -170,6 +172,14 @@ def test_evaluate_inertia_33_asserts_extreme_values(cesaroni_m1670): def tests_import_eng_asserts_read_values_correctly(cesaroni_m1670): + """Tests the import_eng method. It checks whether the import operation + extracts the values correctly. + + Parameters + ---------- + cesaroni_m1670_shifted : rocketpy.SolidMotor + The SolidMotor object to be used in the tests. + """ comments, description, data_points = cesaroni_m1670.import_eng( "tests/fixtures/motor/Cesaroni_M1670.eng" ) @@ -197,8 +207,14 @@ def tests_import_eng_asserts_read_values_correctly(cesaroni_m1670): def tests_export_eng_asserts_exported_values_correct(cesaroni_m1670): - grain_vol = 0.12 * (np.pi * (0.033**2 - 0.015**2)) - grain_mass = grain_vol * 1815 * 5 + """Tests the export_eng method. It checks whether the exported values + of the thrust curve still match data_points. + + Parameters + ---------- + cesaroni_m1670_shifted : rocketpy.SolidMotor + The SolidMotor object to be used in the tests. + """ cesaroni_m1670.export_eng( file_name="tests/cesaroni_m1670.eng", motor_name="test_motor" @@ -238,32 +254,26 @@ def tests_export_eng_asserts_exported_values_correct(cesaroni_m1670): [3.9, 0.0], ] +@pytest.mark.parametrize("tuple_parametric", [(5, 3000)]) +def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct(cesaroni_m1670_shifted, tuple_parametric, linear_func): + """Tests the reshape_thrust_curve. It checks whether the resultant + thrust curve is correct when the user passes a certain tuple to the + reshape_thrust_curve attribute. Also checking for the correct return + data type. -def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct(): - example_motor = SolidMotor( - thrust_source="tests/fixtures/motor/Cesaroni_M1670_shifted.eng", - burn_time=burn_time, - dry_mass=1.815, - dry_inertia=(0.125, 0.125, 0.002), - center_of_dry_mass_position=0.317, - nozzle_position=0, - grain_number=grain_number, - grain_density=grain_density, - nozzle_radius=nozzle_radius, - throat_radius=throat_radius, - grain_separation=grain_separation, - grain_outer_radius=grain_outer_radius, - grain_initial_height=grain_initial_height, - grains_center_of_mass_position=0.397, - grain_initial_inner_radius=grain_initial_inner_radius, - interpolation_method="linear", - coordinate_system_orientation="nozzle_to_combustion_chamber", - reshape_thrust_curve=(5, 3000), - ) + Parameters + ---------- + cesaroni_m1670_shifted : rocketpy.SolidMotor + The SolidMotor object to be used in the tests. + tuple_parametric : tuple + Tuple passed to the reshape_thrust_curve method. + """ + + assert isinstance(cesaroni_m1670_shifted.reshape_thrust_curve(linear_func, 1, 3000), Function) + thrust_reshaped = cesaroni_m1670_shifted.thrust.get_source() - thrust_reshaped = example_motor.thrust.get_source() - assert thrust_reshaped[1][0] == 0.155 * (5 / 4) - assert thrust_reshaped[-1][0] == 5 + assert thrust_reshaped[1][0] == 0.155 * (tuple_parametric[0] / 4) + assert thrust_reshaped[-1][0] == tuple_parametric[0] - assert thrust_reshaped[1][1] == 100 * (3000 / 7539.1875) - assert thrust_reshaped[7][1] == 2034 * (3000 / 7539.1875) + assert thrust_reshaped[1][1] == 100 * (tuple_parametric[1] / 7539.1875) + assert thrust_reshaped[7][1] == 2034 * (tuple_parametric[1] / 7539.1875) \ No newline at end of file From 62c8bb6226a63a2ee5ca4d1867dcd85ba263d073 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Wed, 29 Nov 2023 03:39:29 +0000 Subject: [PATCH 2/2] Fix code style issues with Black --- tests/conftest.py | 4 +++- tests/test_function.py | 6 ++++-- tests/test_solidmotor.py | 17 +++++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9b23e27c1..22b869bf0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -109,6 +109,7 @@ def cesaroni_m1670(): # old name: solid_motor ) return example_motor + @pytest.fixture def cesaroni_m1670_shifted(): # old name: solid_motor """Create a simple object of the SolidMotor class to be used in the tests. @@ -141,7 +142,8 @@ def cesaroni_m1670_shifted(): # old name: solid_motor coordinate_system_orientation="nozzle_to_combustion_chamber", reshape_thrust_curve=(5, 3000), ) - return example_motor + return example_motor + @pytest.fixture def calisto_motorless(): diff --git a/tests/test_function.py b/tests/test_function.py index 40cb80bf6..0c288a9f3 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -232,11 +232,12 @@ def test_integral_spline_interpolation(request, func, a, b): atol=1e-3, ) + @pytest.mark.parametrize( "func_input, derivative_input, expected_derivative", [ - (1, 0, 0), # Test case 1: Function(1) - (lambda x: x, 0, 1), # Test case 2: Function(lambda x: x) + (1, 0, 0), # Test case 1: Function(1) + (lambda x: x, 0, 1), # Test case 2: Function(lambda x: x) (lambda x: x**2, 1, 2), # Test case 3: Function(lambda x: x**2) ], ) @@ -256,6 +257,7 @@ def test_differentiate(func_input, derivative_input, expected_derivative): assert isinstance(func.differentiate(derivative_input), float) assert np.isclose(func.differentiate(derivative_input), expected_derivative) + def test_get_value(): """Tests the get_value method of the Function class. Both with respect to return instances and expected behaviour. diff --git a/tests/test_solidmotor.py b/tests/test_solidmotor.py index 853f163d0..b260c9c39 100644 --- a/tests/test_solidmotor.py +++ b/tests/test_solidmotor.py @@ -254,11 +254,14 @@ def tests_export_eng_asserts_exported_values_correct(cesaroni_m1670): [3.9, 0.0], ] + @pytest.mark.parametrize("tuple_parametric", [(5, 3000)]) -def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct(cesaroni_m1670_shifted, tuple_parametric, linear_func): - """Tests the reshape_thrust_curve. It checks whether the resultant - thrust curve is correct when the user passes a certain tuple to the - reshape_thrust_curve attribute. Also checking for the correct return +def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct( + cesaroni_m1670_shifted, tuple_parametric, linear_func +): + """Tests the reshape_thrust_curve. It checks whether the resultant + thrust curve is correct when the user passes a certain tuple to the + reshape_thrust_curve attribute. Also checking for the correct return data type. Parameters @@ -269,11 +272,13 @@ def test_reshape_thrust_curve_asserts_resultant_thrust_curve_correct(cesaroni_m1 Tuple passed to the reshape_thrust_curve method. """ - assert isinstance(cesaroni_m1670_shifted.reshape_thrust_curve(linear_func, 1, 3000), Function) + assert isinstance( + cesaroni_m1670_shifted.reshape_thrust_curve(linear_func, 1, 3000), Function + ) thrust_reshaped = cesaroni_m1670_shifted.thrust.get_source() assert thrust_reshaped[1][0] == 0.155 * (tuple_parametric[0] / 4) assert thrust_reshaped[-1][0] == tuple_parametric[0] assert thrust_reshaped[1][1] == 100 * (tuple_parametric[1] / 7539.1875) - assert thrust_reshaped[7][1] == 2034 * (tuple_parametric[1] / 7539.1875) \ No newline at end of file + assert thrust_reshaped[7][1] == 2034 * (tuple_parametric[1] / 7539.1875)