From 984ff182108bfe8f037083837774ea1cbeba40ad Mon Sep 17 00:00:00 2001 From: Lucas de Oliveira Prates Date: Mon, 16 Sep 2024 21:57:16 -0300 Subject: [PATCH 1/4] TST: adding unit tests for generic surfaces --- tests/conftest.py | 1 + tests/fixtures/generic_surfaces/__init__.py | 0 .../generic_surfaces_fixtures.py | 42 +++++++++ tests/unit/test_generic_surfaces.py | 92 +++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 tests/fixtures/generic_surfaces/__init__.py create mode 100644 tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py create mode 100644 tests/unit/test_generic_surfaces.py diff --git a/tests/conftest.py b/tests/conftest.py index 6c4171b66..48315240a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,6 +18,7 @@ "tests.fixtures.monte_carlo.stochastic_fixtures", "tests.fixtures.monte_carlo.stochastic_motors_fixtures", "tests.fixtures.sensors.sensors_fixtures", + "tests.fixtures.generic_surfaces.generic_surfaces_fixtures" ] diff --git a/tests/fixtures/generic_surfaces/__init__.py b/tests/fixtures/generic_surfaces/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py b/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py new file mode 100644 index 000000000..7049c5ba9 --- /dev/null +++ b/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py @@ -0,0 +1,42 @@ +import pandas as pd +import pytest + + +@pytest.fixture(scope="session") +def filename_valid_coeff(tmpdir_factory): + filename = tmpdir_factory.mktemp("aero_surface_data").join("valid_coefficients.csv") + pd.DataFrame( + { + "alpha": [0, 1, 2, 3, 0.1], + "mach": [3, 2, 1, 0, 0.2], + "cL": [4, 2, 2, 4, 5], + } + ).to_csv(filename, index=False) + + return filename + + +@pytest.fixture( + params=( + { + "alpha": [0, 1, 2, 3, 0.1], + "cL": [4, 2, 2, 4, 5], + "mach": [3, 2, 1, 0, 0.2], + }, + { + "a": [0, 1, 2, 3, 0.1], + "b": [4, 2, 2, 4, 5], + }, + [0, 1, 2, 3], + ) +) +def filename_invalid_coeff(tmpdir_factory, request): + filename = tmpdir_factory.mktemp("aero_surface_data").join( + "tmp_invalid_coefficients.csv" + ) + if isinstance(request.param, dict): + pd.DataFrame(request.param).to_csv(filename, index=False) + else: + pd.DataFrame(request.param).to_csv(filename, index=False, header=False) + + return filename diff --git a/tests/unit/test_generic_surfaces.py b/tests/unit/test_generic_surfaces.py new file mode 100644 index 000000000..f349e598d --- /dev/null +++ b/tests/unit/test_generic_surfaces.py @@ -0,0 +1,92 @@ +import pytest + +from rocketpy import Function, GenericSurface +from rocketpy.mathutils import Vector + +REFERENCE_AREA = 1 +REFERENCE_LENGTH = 1 + + +@pytest.mark.parametrize( + "coefficients", + [ + "cL", + {"invalid_name": 0}, + {"cL": "inexistent_file.csv"}, + {"cL": Function(lambda x1, x2, x3, x4, x5, x6: 0)}, + {"cL": lambda x1: 0}, + {"cL": {}}, + {"cL": "tmp_invalid_coefficients.csv"}, + ], +) +def test_invalid_initialization(coefficients): + """Checks if generic surface raises errors in initialization + when coefficient argument is invalid""" + + with pytest.raises((ValueError, TypeError)): + GenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients=coefficients, + ) + + +def test_invalid_initialization_from_csv(filename_invalid_coeff): + """Checks if generic surfaces initializes correctly when + coefficients is set from a csv file""" + with pytest.raises(ValueError): + GenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients={"cL": str(filename_invalid_coeff)}, + ) + + +@pytest.mark.parametrize( + "coefficients", + [ + {}, + {"cL": 0}, + { + "cL": 0, + "cQ": Function(lambda x1, x2, x3, x4, x5, x6, x7: 0), + "cD": lambda x1, x2, x3, x4, x5, x6, x7: 0, + }, + ], +) +def test_valid_initialization(coefficients): + """Checks if generic surface raises errors in initialization + when coefficient argument is valid""" + + GenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients=coefficients, + ) + + +def test_valid_initialization_from_csv(filename_valid_coeff): + """Checks if generic surfaces initializes correctly when + coefficients is set from a csv file""" + GenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients={"cL": str(filename_valid_coeff)}, + ) + + +def test_compute_forces_and_moments(): + """Checks if there are not logical errors in + compute forces and moments""" + + gs_object = GenericSurface(REFERENCE_AREA, REFERENCE_LENGTH, {}) + forces_and_moments = gs_object.compute_forces_and_moments( + stream_velocity=Vector((0, 0, 0)), + stream_speed=0, + stream_mach=0, + rho=0, + cp=Vector((0, 0, 0)), + omega=(0, 0, 0), + reynolds=0, + ) + assert forces_and_moments == (0, 0, 0, 0, 0, 0) From 2170759db1a6a500d7b70e55bbd9d91a164fe4e4 Mon Sep 17 00:00:00 2001 From: Lucas de Oliveira Prates Date: Mon, 16 Sep 2024 22:08:24 -0300 Subject: [PATCH 2/4] MNT: fix black --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 48315240a..9600bebe5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,7 +18,7 @@ "tests.fixtures.monte_carlo.stochastic_fixtures", "tests.fixtures.monte_carlo.stochastic_motors_fixtures", "tests.fixtures.sensors.sensors_fixtures", - "tests.fixtures.generic_surfaces.generic_surfaces_fixtures" + "tests.fixtures.generic_surfaces.generic_surfaces_fixtures", ] From 693efc4f868f815fa8fa0f23bf296850e7aa7fcf Mon Sep 17 00:00:00 2001 From: Lucas de Oliveira Prates Date: Tue, 17 Sep 2024 19:39:11 -0300 Subject: [PATCH 3/4] MNT: removing unnecessary parameters and adding missing docstrings --- .../generic_surfaces/generic_surfaces_fixtures.py | 10 +++++----- tests/unit/test_generic_surfaces.py | 9 ++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py b/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py index 7049c5ba9..e3aa869ea 100644 --- a/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py +++ b/tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py @@ -4,6 +4,8 @@ @pytest.fixture(scope="session") def filename_valid_coeff(tmpdir_factory): + """Creates temporary files used to test if generic surfaces + initializes correctly from CSV files""" filename = tmpdir_factory.mktemp("aero_surface_data").join("valid_coefficients.csv") pd.DataFrame( { @@ -27,16 +29,14 @@ def filename_valid_coeff(tmpdir_factory): "a": [0, 1, 2, 3, 0.1], "b": [4, 2, 2, 4, 5], }, - [0, 1, 2, 3], ) ) def filename_invalid_coeff(tmpdir_factory, request): + """Creates temporary CSV files used to test if generic surfaces + raises errors when initialized incorrectly from CSV files""" filename = tmpdir_factory.mktemp("aero_surface_data").join( "tmp_invalid_coefficients.csv" ) - if isinstance(request.param, dict): - pd.DataFrame(request.param).to_csv(filename, index=False) - else: - pd.DataFrame(request.param).to_csv(filename, index=False, header=False) + pd.DataFrame(request.param).to_csv(filename, index=False) return filename diff --git a/tests/unit/test_generic_surfaces.py b/tests/unit/test_generic_surfaces.py index f349e598d..a04ed429d 100644 --- a/tests/unit/test_generic_surfaces.py +++ b/tests/unit/test_generic_surfaces.py @@ -16,7 +16,6 @@ {"cL": Function(lambda x1, x2, x3, x4, x5, x6: 0)}, {"cL": lambda x1: 0}, {"cL": {}}, - {"cL": "tmp_invalid_coefficients.csv"}, ], ) def test_invalid_initialization(coefficients): @@ -32,8 +31,8 @@ def test_invalid_initialization(coefficients): def test_invalid_initialization_from_csv(filename_invalid_coeff): - """Checks if generic surfaces initializes correctly when - coefficients is set from a csv file""" + """Checks if generic surfaces raises errors when initialized incorrectly + from a csv file""" with pytest.raises(ValueError): GenericSurface( reference_area=REFERENCE_AREA, @@ -55,8 +54,8 @@ def test_invalid_initialization_from_csv(filename_invalid_coeff): ], ) def test_valid_initialization(coefficients): - """Checks if generic surface raises errors in initialization - when coefficient argument is valid""" + """Checks if generic surface initializes correctly when coefficient + argument is valid""" GenericSurface( reference_area=REFERENCE_AREA, From 93c368ebefd6a3781ed1f042e93af08efc3dfa2a Mon Sep 17 00:00:00 2001 From: Lucas de Oliveira Prates Date: Thu, 19 Sep 2024 22:49:16 -0300 Subject: [PATCH 4/4] TST: adding tests to linear generic surfaces --- tests/conftest.py | 1 + .../linear_generic_surfaces_fixtures.py | 44 +++++++++ tests/unit/test_linear_generic_surfaces.py | 91 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tests/fixtures/generic_surfaces/linear_generic_surfaces_fixtures.py create mode 100644 tests/unit/test_linear_generic_surfaces.py diff --git a/tests/conftest.py b/tests/conftest.py index 9600bebe5..a23f5fd89 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,6 +19,7 @@ "tests.fixtures.monte_carlo.stochastic_motors_fixtures", "tests.fixtures.sensors.sensors_fixtures", "tests.fixtures.generic_surfaces.generic_surfaces_fixtures", + "tests.fixtures.generic_surfaces.linear_generic_surfaces_fixtures", ] diff --git a/tests/fixtures/generic_surfaces/linear_generic_surfaces_fixtures.py b/tests/fixtures/generic_surfaces/linear_generic_surfaces_fixtures.py new file mode 100644 index 000000000..35ab9c1a2 --- /dev/null +++ b/tests/fixtures/generic_surfaces/linear_generic_surfaces_fixtures.py @@ -0,0 +1,44 @@ +import pandas as pd +import pytest + + +@pytest.fixture(scope="session") +def filename_valid_coeff_linear_generic_surface(tmpdir_factory): + """Creates temporary files used to test if a linear generic surface + initializes correctly from CSV files""" + filename = tmpdir_factory.mktemp("aero_surface_data").join( + "valid_coefficients_lgs.csv" + ) + pd.DataFrame( + { + "alpha": [0, 1, 2, 3, 0.1], + "mach": [3, 2, 1, 0, 0.2], + "cL_0": [4, 2, 2, 4, 5], + } + ).to_csv(filename, index=False) + + return filename + + +@pytest.fixture( + params=( + { + "alpha": [0, 1, 2, 3, 0.1], + "cL_0": [4, 2, 2, 4, 5], + "mach": [3, 2, 1, 0, 0.2], + }, + { + "a": [0, 1, 2, 3, 0.1], + "b": [4, 2, 2, 4, 5], + }, + ) +) +def filename_invalid_coeff_linear_generic_surface(tmpdir_factory, request): + """Creates temporary CSV files used to test if a linear generic surface + raises errors when initialized incorrectly from CSV files""" + filename = tmpdir_factory.mktemp("aero_surface_data").join( + "tmp_invalid_coefficients_lgs.csv" + ) + pd.DataFrame(request.param).to_csv(filename, index=False) + + return filename diff --git a/tests/unit/test_linear_generic_surfaces.py b/tests/unit/test_linear_generic_surfaces.py new file mode 100644 index 000000000..858226444 --- /dev/null +++ b/tests/unit/test_linear_generic_surfaces.py @@ -0,0 +1,91 @@ +import pytest + +from rocketpy import Function, LinearGenericSurface +from rocketpy.mathutils import Vector + +REFERENCE_AREA = 1 +REFERENCE_LENGTH = 1 + + +@pytest.mark.parametrize( + "coefficients", + [ + "cL_0", + {"invalid_name": 0}, + {"cL_0": "inexistent_file.csv"}, + {"cL_0": Function(lambda x1, x2, x3, x4, x5, x6: 0)}, + {"cL_0": lambda x1: 0}, + {"cL_0": {}}, + ], +) +def test_invalid_initialization(coefficients): + """Checks if linear generic surface raises errors in initialization + when coefficient argument is invalid""" + + with pytest.raises((ValueError, TypeError)): + LinearGenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients=coefficients, + ) + + +def test_invalid_initialization_from_csv(filename_invalid_coeff_linear_generic_surface): + """Checks if linear generic surfaces raises errors when initialized incorrectly + from a csv file""" + with pytest.raises(ValueError): + LinearGenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients={"cL_0": str(filename_invalid_coeff_linear_generic_surface)}, + ) + + +@pytest.mark.parametrize( + "coefficients", + [ + {}, + {"cL_0": 0}, + { + "cL_0": 0, + "cQ_0": Function(lambda x1, x2, x3, x4, x5, x6, x7: 0), + "cD_0": lambda x1, x2, x3, x4, x5, x6, x7: 0, + }, + ], +) +def test_valid_initialization(coefficients): + """Checks if linear generic surface initializes correctly when coefficient + argument is valid""" + + LinearGenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients=coefficients, + ) + + +def test_valid_initialization_from_csv(filename_valid_coeff_linear_generic_surface): + """Checks if linear generic surfaces initializes correctly when + coefficients is set from a csv file""" + LinearGenericSurface( + reference_area=REFERENCE_AREA, + reference_length=REFERENCE_LENGTH, + coefficients={"cL_0": str(filename_valid_coeff_linear_generic_surface)}, + ) + + +def test_compute_forces_and_moments(): + """Checks if there are not logical errors in + compute forces and moments""" + + lgs_object = LinearGenericSurface(REFERENCE_AREA, REFERENCE_LENGTH, {}) + forces_and_moments = lgs_object.compute_forces_and_moments( + stream_velocity=Vector((0, 0, 0)), + stream_speed=1, + stream_mach=0, + rho=0, + cp=Vector((0, 0, 0)), + omega=(0, 0, 0), + reynolds=0, + ) + assert forces_and_moments == (0, 0, 0, 0, 0, 0)