-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: adding unit tests for generic surfaces
- Loading branch information
1 parent
6f36a10
commit 984ff18
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
42 changes: 42 additions & 0 deletions
42
tests/fixtures/generic_surfaces/generic_surfaces_fixtures.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |