Skip to content

Commit

Permalink
test: update and add test for refactored dormancy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
weibullguy committed Oct 11, 2024
1 parent 65e2e0a commit dec70e7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/ramstk/analyses/dormancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def get_environment_type(env_id: int, is_active: bool) -> Optional[str]:
:return: the name of the environment associated with the end_id.
:rtype: str
"""
if env_id <= 0:
return None # Invalid env_id, must be positive

_index = env_id - 1
if is_active:
if _index < len(ENVIRONMENTS_ACTIVE):
Expand Down
65 changes: 61 additions & 4 deletions tests/analyses/test_dormancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def test_get_environment_type_dormant(dormant_env):
)


@pytest.mark.unit
@pytest.mark.parametrize("env_id", [-1, 0, 14])
def test_get_environment_type_invalid(env_id):
"""get_environment_type() should return None for invalid environment IDs."""
assert get_environment_type(env_id, True) is None
assert get_environment_type(env_id, False) is None


@pytest.mark.unit
@pytest.mark.parametrize("category_id", [1, 2, 3, 4, 5, 6, 7, 8])
def test_get_dormant_hr_multiplier_ground(category_id):
Expand Down Expand Up @@ -208,15 +216,21 @@ def test_get_dormant_hr_multiplier_space(category_id):
def test_get_dormant_hr_multiplier_no_category():
"""get_dormant_hr_multiplier() should return 0.0 if the category ID is not in
1-8."""
assert get_dormant_hr_multiplier([10, 1, 0.0], "ground", "ground") == 0.0
assert get_dormant_hr_multiplier([10, 1, 0.0], "ground", "ground") == pytest.approx(
0.0
)


@pytest.mark.unit
def test_get_dormant_hr_multiplier_no_environment():
"""get_dormant_hr_multiplier() should return 0.0 if the active/dormant environments
combination is not valid."""
assert get_dormant_hr_multiplier([1, 1, 0.0], "missile", "ground") == 0.0
assert get_dormant_hr_multiplier([1, 1, 0.0], "ground", "airborne") == 0.0
assert get_dormant_hr_multiplier([1, 1, 0.0], "missile", "ground") == pytest.approx(
0.0
)
assert get_dormant_hr_multiplier(
[1, 1, 0.0], "ground", "airborne"
) == pytest.approx(0.0)


@pytest.mark.unit
Expand All @@ -241,12 +255,55 @@ def test_dormant_hazard_rate(category_id, subcategory_id, expected):
assert _hr_dormant == pytest.approx(expected)


@pytest.mark.unit
def test_get_dormant_hr_multiplier_invalid_subcategory():
"""get_dormant_hr_multiplier() should return 0.0 if the subcategory is out of range
for semiconductors."""
# Test with invalid subcategory for semiconductors.
assert get_dormant_hr_multiplier([2, 5, 0.0], "ground", "ground") == pytest.approx(
0.0
)


@pytest.mark.unit
@pytest.mark.calculation
def test_dormant_hazard_rate_bad_index():
"""do_calculate_dormant_hazard_rate() should raise an IndexError when a bad index
value is passed."""
# with pytest.raises(ValueError):
_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[4, 5, 0.008642374], env_info=[3, 12]
)


@pytest.mark.unit
def test_dormant_hazard_rate_invalid_environment():
"""do_calculate_dormant_hazard_rate() should return 0.0 when an invalid environment
is passed."""
_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[1, 1, 0.008642374], env_info=[14, 1]
)
assert _hr_dormant == pytest.approx(0.0)

_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[1, 1, 0.008642374], env_info=[3, 5]
)
assert _hr_dormant == pytest.approx(0.0)


@pytest.mark.unit
def test_dormant_hazard_rate_zero_hazard_rate():
"""do_calculate_dormant_hazard_rate() should return 0.0 when the hazard rate is
0."""
_hr_dormant = do_calculate_dormant_hazard_rate(hw_info=[1, 1, 0.0], env_info=[3, 1])
assert _hr_dormant == pytest.approx(0.0)


@pytest.mark.unit
@pytest.mark.parametrize("subcategory_id", [1, 2, 3, 4])
def test_dormant_hazard_rate_category_1(subcategory_id):
"""do_calculate_dormant_hazard_rate() should handle different subcategories of
category 1."""
_hr_dormant = do_calculate_dormant_hazard_rate(
hw_info=[1, subcategory_id, 0.008642374], env_info=[3, 1]
)
assert isinstance(_hr_dormant, float)

0 comments on commit dec70e7

Please sign in to comment.