Skip to content

Commit

Permalink
TST: better documentation for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-FernandesBR committed Mar 5, 2024
1 parent 4e00af7 commit 4ac7187
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
2 changes: 1 addition & 1 deletion tests/fixtures/monte_carlo/monte_carlo_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
75 changes: 60 additions & 15 deletions tests/test_monte_carlo.py
Original file line number Diff line number Diff line change
@@ -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})

Expand All @@ -27,52 +29,95 @@ 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):
random_rocket = stochastic_calisto.create_object()
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,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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=";",
Expand Down

0 comments on commit 4ac7187

Please sign in to comment.