Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Reorganize only the test_environment.py tests into Integration/Unit Tests #577

Merged
merged 11 commits into from
Jun 29, 2024
Merged
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ instance/
# Sphinx documentation
docs/_build/

# WebServer extension
.iis

# PyBuilder
.pybuilder/
target/
Expand Down
Empty file added tests/integration/__init__.py
Empty file.
201 changes: 80 additions & 121 deletions tests/test_environment.py → tests/integration/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,11 @@
import datetime
import time
from datetime import datetime
from unittest.mock import patch

import numpy.ma as ma
import pytest


@patch("matplotlib.pyplot.show")
def test_standard_atmosphere(mock_show, example_plain_env):
"""Tests the standard atmosphere model in the environment object.

Parameters
----------
mock_show : mock
Mock object to replace matplotlib.pyplot.show() method.
example_plain_env : rocketpy.Environment
Example environment object to be tested.
"""
example_plain_env.set_atmospheric_model(type="standard_atmosphere")
assert example_plain_env.info() == None
assert example_plain_env.all_info() == None
assert abs(example_plain_env.pressure(0) - 101325.0) < 1e-8
assert abs(example_plain_env.barometric_height(101325.0)) < 1e-2
assert example_plain_env.prints.print_earth_details() == None


@patch("matplotlib.pyplot.show")
def test_custom_atmosphere(mock_show, example_plain_env):
"""Tests the custom atmosphere model in the environment object.

Parameters
----------
mock_show : mock
Mock object to replace matplotlib.pyplot.show() method.
example_plain_env : rocketpy.Environment
Example environment object to be tested.
"""
example_plain_env.set_atmospheric_model(
type="custom_atmosphere",
pressure=None,
temperature=300,
wind_u=[(0, 5), (1000, 10)],
wind_v=[(0, -2), (500, 3), (1600, 2)],
)
assert example_plain_env.all_info() == None
assert abs(example_plain_env.pressure(0) - 101325.0) < 1e-8
assert abs(example_plain_env.barometric_height(101325.0)) < 1e-2
assert abs(example_plain_env.wind_velocity_x(0) - 5) < 1e-8
assert abs(example_plain_env.temperature(100) - 300) < 1e-8


@patch("matplotlib.pyplot.show")
def test_wyoming_sounding_atmosphere(mock_show, example_plain_env):
"""Tests the Wyoming sounding model in the environment object.

Parameters
----------
mock_show : mock
Mock object to replace matplotlib.pyplot.show() method.
example_plain_env : rocketpy.Environment
Example environment object to be tested.
"""
# TODO:: this should be added to the set_atmospheric_model() method as a
# "file" option, instead of receiving the URL as a string.
URL = "http://weather.uwyo.edu/cgi-bin/sounding?region=samer&TYPE=TEXT%3ALIST&YEAR=2019&MONTH=02&FROM=0500&TO=0512&STNM=83779"
# give it at least 5 times to try to download the file
example_plain_env.set_atmospheric_model(type="wyoming_sounding", file=URL)

assert example_plain_env.all_info() == None
assert abs(example_plain_env.pressure(0) - 93600.0) < 1e-8
assert (
abs(example_plain_env.barometric_height(example_plain_env.pressure(0)) - 722.0)
< 1e-8
)
assert abs(example_plain_env.wind_velocity_x(0) - -2.9005178894925043) < 1e-8
assert abs(example_plain_env.temperature(100) - 291.75) < 1e-8


@pytest.mark.skip(reason="legacy tests")
@pytest.mark.slow
@patch("matplotlib.pyplot.show")
def test_noaa_ruc_sounding_atmosphere(mock_show, example_plain_env):
URL = r"https://rucsoundings.noaa.gov/get_raobs.cgi?data_source=RAOB&latest=latest&start_year=2019&start_month_name=Feb&start_mday=5&start_hour=12&start_min=0&n_hrs=1.0&fcst_len=shortest&airport=83779&text=Ascii%20text%20%28GSD%20format%29&hydrometeors=false&start=latest"
example_plain_env.set_atmospheric_model(type="NOAARucSounding", file=URL)
assert example_plain_env.all_info() == None
assert example_plain_env.pressure(0) == 100000.0
from rocketpy import Environment


@pytest.mark.slow
Expand Down Expand Up @@ -168,9 +91,8 @@ def test_gefs_atmosphere(mock_show, example_spaceport_env):

@pytest.mark.skip(reason="legacy tests") # deprecated method
@patch("matplotlib.pyplot.show")
def test_info_returns(mock_show, example_plain_env):
"""Tests the all_info_returned() all_plot_info_returned() and methods of the
Environment class.
def test_custom_atmosphere(mock_show, example_plain_env):
"""Tests the custom atmosphere model in the environment object.

Parameters
----------
Expand All @@ -179,53 +101,72 @@ def test_info_returns(mock_show, example_plain_env):
example_plain_env : rocketpy.Environment
Example environment object to be tested.
"""
returned_plots = example_plain_env.all_plot_info_returned()
returned_infos = example_plain_env.all_info_returned()
expected_info = {
"grav": example_plain_env.gravity,
"elevation": 0,
"model_type": "standard_atmosphere",
"model_type_max_expected_height": 80000,
"wind_speed": 0,
"wind_direction": 0,
"wind_heading": 0,
"surface_pressure": 1013.25,
"surface_temperature": 288.15,
"surface_air_density": 1.225000018124288,
"surface_speed_of_sound": 340.293988026089,
"lat": 0,
"lon": 0,
}
expected_plots_keys = [
"grid",
"wind_speed",
"wind_direction",
"speed_of_sound",
"density",
"wind_vel_x",
"wind_vel_y",
"pressure",
"temperature",
]
assert list(returned_infos.keys()) == list(expected_info.keys())
assert list(returned_infos.values()) == list(expected_info.values())
assert list(returned_plots.keys()) == expected_plots_keys
example_plain_env.set_atmospheric_model(
type="custom_atmosphere",
pressure=None,
temperature=300,
wind_u=[(0, 5), (1000, 10)],
wind_v=[(0, -2), (500, 3), (1600, 2)],
)
assert example_plain_env.all_info() == None
assert abs(example_plain_env.pressure(0) - 101325.0) < 1e-8
assert abs(example_plain_env.barometric_height(101325.0)) < 1e-2
assert abs(example_plain_env.wind_velocity_x(0) - 5) < 1e-8
assert abs(example_plain_env.temperature(100) - 300) < 1e-8


@pytest.mark.slow
@patch("matplotlib.pyplot.show")
def test_cmc_atmosphere(mock_show, example_spaceport_env):
"""Tests the Ensemble model with the CMC file.
def test_standard_atmosphere(mock_show, example_plain_env):
"""Tests the standard atmosphere model in the environment object.

Parameters
----------
mock_show : mock
Mock object to replace matplotlib.pyplot.show() method.
example_spaceport_env : rocketpy.Environment
example_plain_env : rocketpy.Environment
Example environment object to be tested.
"""
example_spaceport_env.set_atmospheric_model(type="Ensemble", file="CMC")
assert example_spaceport_env.all_info() == None
example_plain_env.set_atmospheric_model(type="standard_atmosphere")
assert example_plain_env.info() == None
assert example_plain_env.all_info() == None
assert abs(example_plain_env.pressure(0) - 101325.0) < 1e-8
assert abs(example_plain_env.barometric_height(101325.0)) < 1e-2
assert example_plain_env.prints.print_earth_details() == None


@patch("matplotlib.pyplot.show")
def test_wyoming_sounding_atmosphere(mock_show, example_plain_env):
"""Asserts whether the Wyoming sounding model in the environment
object behaves as expected with respect to some attributes such
as pressure, barometric_height, wind_velocity and temperature.

Parameters
----------
mock_show : mock
Mock object to replace matplotlib.pyplot.show() method.
example_plain_env : rocketpy.Environment
Example environment object to be tested.
"""

# TODO:: this should be added to the set_atmospheric_model() method as a
# "file" option, instead of receiving the URL as a string.
URL = "http://weather.uwyo.edu/cgi-bin/sounding?region=samer&TYPE=TEXT%3ALIST&YEAR=2019&MONTH=02&FROM=0500&TO=0512&STNM=83779"
# give it at least 5 times to try to download the file
for i in range(5):
try:
example_plain_env.set_atmospheric_model(type="wyoming_sounding", file=URL)
break
except:
time.sleep(1) # wait 1 second before trying again
pass
assert example_plain_env.all_info() == None
assert abs(example_plain_env.pressure(0) - 93600.0) < 1e-8
assert (
abs(example_plain_env.barometric_height(example_plain_env.pressure(0)) - 722.0)
< 1e-8
)
assert abs(example_plain_env.wind_velocity_x(0) - -2.9005178894925043) < 1e-8
assert abs(example_plain_env.temperature(100) - 291.75) < 1e-8


@pytest.mark.slow
Expand Down Expand Up @@ -257,9 +198,27 @@ def test_hiresw_ensemble_atmosphere(mock_show, example_spaceport_env):
date_string = f"{date_info[0]}{date_info[1]:02}{date_info[2]:02}"

example_spaceport_env.set_date(date_info)

example_spaceport_env.set_atmospheric_model(
type="Forecast",
file=f"https://nomads.ncep.noaa.gov/dods/hiresw/hiresw{date_string}/hiresw_conusarw_12z",
dictionary=HIRESW_dictionary,
)

assert example_spaceport_env.all_info() == None


@pytest.mark.slow
@patch("matplotlib.pyplot.show")
def test_cmc_atmosphere(mock_show, example_spaceport_env):
"""Tests the Ensemble model with the CMC file.

Parameters
----------
mock_show : mock
Mock object to replace matplotlib.pyplot.show() method.
example_spaceport_env : rocketpy.Environment
Example environment object to be tested.
"""
example_spaceport_env.set_atmospheric_model(type="Ensemble", file="CMC")
assert example_spaceport_env.all_info() == None
Loading
Loading