diff --git a/tests/fixtures/monte_carlo/monte_carlo_fixtures.py b/tests/fixtures/monte_carlo/monte_carlo_fixtures.py index 08287e752..59f8bd2c2 100644 --- a/tests/fixtures/monte_carlo/monte_carlo_fixtures.py +++ b/tests/fixtures/monte_carlo/monte_carlo_fixtures.py @@ -4,7 +4,7 @@ @pytest.fixture -def calisto_dispersion(stochastic_environment, stochastic_calisto, stochastic_flight): +def monte_carlo_calisto(stochastic_environment, stochastic_calisto, stochastic_flight): return MonteCarlo( filename="monte_carlo_test", environment=stochastic_environment, diff --git a/tests/test_monte_carlo.py b/tests/test_monte_carlo.py index d829aec93..debf7d6d3 100644 --- a/tests/test_monte_carlo.py +++ b/tests/test_monte_carlo.py @@ -1,7 +1,9 @@ -from unittest.mock import patch import os +from unittest.mock import patch + import matplotlib as plt import numpy as np +import pytest plt.rcParams.update({"figure.max_open_warning": 0}) @@ -27,16 +29,34 @@ def test_stochastic_environment_create_object_with_wind_x(stochastic_environment def test_stochastic_solid_motor_create_object_with_impulse(stochastic_solid_motor): + """Tests the stochastic solid motor object by checking if the total impulse + can be generated properly. The goal is to check if the create_object() + method is being called without any problems. + + Parameters + ---------- + stochastic_solid_motor : StochasticSolidMotor + The stochastic solid motor object, this is a pytest fixture. + """ total_impulse = [] for _ in range(20): random_motor = stochastic_solid_motor.create_object() total_impulse.append(random_motor.total_impulse) - assert np.isclose(np.mean(total_impulse), 6500, rtol=0.15) - assert np.isclose(np.std(total_impulse), 1000, rtol=0.15) + assert np.isclose(np.mean(total_impulse), 6500, rtol=0.3) + assert np.isclose(np.std(total_impulse), 1000, rtol=0.3) def test_stochastic_calisto_create_object_with_static_margin(stochastic_calisto): + """Tests the stochastic calisto object by checking if the static margin + can be generated properly. The goal is to check if the create_object() + method is being called without any problems. + + Parameters + ---------- + stochastic_calisto : StochasticCalisto + The stochastic calisto object, this is a pytest fixture. + """ all_margins = [] for _ in range(10): @@ -44,35 +64,60 @@ def test_stochastic_calisto_create_object_with_static_margin(stochastic_calisto) all_margins.append(random_rocket.static_margin(0)) assert np.isclose(np.mean(all_margins), 2.2625350013000434, rtol=0.15) - assert np.isclose(np.std(all_margins), 0.08222901168867777, rtol=0.15) + assert np.isclose(np.std(all_margins), 0.1, atol=0.2) -def test_monte_carlo_simulate(calisto_dispersion): - # NOTE: this is really slow, it runs 10 flight simulations - calisto_dispersion.simulate(number_of_simulations=10, append=False) +@pytest.mark.slow +def test_monte_carlo_simulate(monte_carlo_calisto): + """Tests the simulate method of the MonteCarlo class. - assert calisto_dispersion.num_of_loaded_sims == 10 + Parameters + ---------- + monte_carlo_calisto : MonteCarlo + The MonteCarlo object, this is a pytest fixture. + """ + # NOTE: this is really slow, it runs 10 flight simulations + monte_carlo_calisto.simulate(number_of_simulations=10, append=False) + + assert monte_carlo_calisto.num_of_loaded_sims == 10 + assert monte_carlo_calisto.number_of_simulations == 10 + assert monte_carlo_calisto.filename == "monte_carlo_test" + assert monte_carlo_calisto.error_file == "monte_carlo_test.errors.txt" + assert monte_carlo_calisto.output_file == "monte_carlo_test.outputs.txt" + assert np.isclose( + monte_carlo_calisto.processed_results["apogee"][0], 4711, rtol=0.15 + ) + assert np.isclose( + monte_carlo_calisto.processed_results["impact_velocity"][0], + -5.234, + rtol=0.15, + ) + os.remove("monte_carlo_test.errors.txt") + os.remove("monte_carlo_test.outputs.txt") + os.remove("monte_carlo_test.inputs.txt") -def test_monte_carlo_prints(calisto_dispersion): - calisto_dispersion.info() +def test_monte_carlo_prints(monte_carlo_calisto): + """Tests the prints methods of the MonteCarlo class.""" + monte_carlo_calisto.info() @patch("matplotlib.pyplot.show") -def test_monte_carlo_plots(mock_show, calisto_dispersion): - assert calisto_dispersion.all_info() is None +def test_monte_carlo_plots(mock_show, monte_carlo_calisto): + """Tests the plots methods of the MonteCarlo class.""" + assert monte_carlo_calisto.all_info() is None -def test_monte_carlo_export_ellipses_to_kml(calisto_dispersion): +def test_monte_carlo_export_ellipses_to_kml(monte_carlo_calisto): """Tests the export_ellipses_to_kml method of the MonteCarlo class. Parameters ---------- - calisto_dispersion : MonteCarlo + monte_carlo_calisto : MonteCarlo The MonteCarlo object, this is a pytest fixture. """ assert ( - calisto_dispersion.export_ellipses_to_kml( + monte_carlo_calisto.export_ellipses_to_kml( filename="monte_carlo_class_example.kml", origin_lat=32, origin_lon=-104, diff --git a/tests/unit/test_utilities.py b/tests/unit/test_utilities.py index 68dbc3d7f..40ba8d7bf 100644 --- a/tests/unit/test_utilities.py +++ b/tests/unit/test_utilities.py @@ -48,7 +48,7 @@ def test_create_dispersion_dictionary(): """Test if the function returns a dictionary with the correct keys. It reads the keys from the dictionary generated by the utilities function and compares them to the expected. - Be careful if you change the "fixtures/dispersion/Valetudo_inputs.csv" file. + Be careful if you change the "fixtures/monte_carlo/Valetudo_inputs.csv" file. Parameters ---------- @@ -60,11 +60,11 @@ def test_create_dispersion_dictionary(): """ returned_dict = utilities.create_dispersion_dictionary( - "tests/fixtures/dispersion/Valetudo_inputs.csv" + "tests/fixtures/monte_carlo/Valetudo_inputs.csv" ) test_array = np.genfromtxt( - "tests/fixtures/dispersion/Valetudo_inputs.csv", + "tests/fixtures/monte_carlo/Valetudo_inputs.csv", usecols=(1, 2, 3), skip_header=1, delimiter=";",