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

refactor: stress ratio calculations #1418

Merged
merged 3 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## Does this PR introduce a breaking change?
## Does this pull request introduce a breaking change?
- [ ] Yes
- [ ] No

<!-- If yes, describe the impact and migration path below. -->

## Describe the purpose of this pull request.
## Purpose of this pull request


## Describe how this was implemented.
## Benefits of the pull request


## Describe any particular area(s) reviewers should focus on.
## Any particular area(s) reviewers should focus on


## Provide any other pertinent information.
## Any other pertinent information
<!-- Provide any other information that is import to this PR such as
screenshots if this impacts the GUI. -->
weibullguy marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
29 changes: 14 additions & 15 deletions src/ramstk/analyses/stress.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
#
# ramstk.analyses.Stress.py is part of the RAMSTK Project
# ramstk.analyses.stress.py is part of the RAMSTK Project
#
# All rights reserved.
# Copyright 2019 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
# Copyright since 2007 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
"""Component Stress Calculations Module."""


Expand All @@ -21,23 +21,22 @@ def calculate_stress_ratio(stress_operating, stress_rated):
>>> calculate_stress_ratio(1, 1.29)
0.7751937984496123

Rated stress must not be zero:
>>> calculate_stress_ratio(0.382, 0.0)
Traceback (most recent call last):
...
ZeroDivisionError: float division by zero

Stress inputs must not be strings:
>>> calculate_stress_ratio(0.382, '3.2')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for /: 'float' and 'str'

:param stress_operating: the device's operating level of the stress.
:param stress_rated: the devices's rated stress.
:return: _stress_ratio; the ratio of operating stress to rated stress.
:return: the ratio of operating stress to rated stress.
:rtype: float
:raise: TypeError if an input value is non-numerical.
:raise: ZeroDivisionError if the rated stress is zero.
"""
if not isinstance(stress_operating, (int, float)) or not isinstance(
stress_rated, (int, float)
):
raise TypeError("Inputs must be numerical (int or float).")

if stress_operating < 0 or stress_rated < 0:
raise ValueError("Stress values must be non-negative.")

if stress_rated == 0:
raise ZeroDivisionError("Rated stress must not be zero.")

return stress_operating / stress_rated
36 changes: 34 additions & 2 deletions tests/analyses/test_stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# tests.analyses.test_stress.py is part of The RAMSTK Project
#
# All rights reserved.
# Copyright 2019 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
# Copyright since 2007 Doyle Rowland doyle.rowland <AT> reliaqual <DOT> com
"""Test class for the electrical stress module."""

# Third Party Imports
Expand All @@ -21,7 +21,7 @@ def test_calculate_stress_ratio():
"""Return a float stress ratio on success."""
_stress_ratio = stress.calculate_stress_ratio(0.625, 1.25)

assert pytest.approx(_stress_ratio) == 0.5
assert _stress_ratio == pytest.approx(0.5)


@pytest.mark.unit
Expand All @@ -38,3 +38,35 @@ def test_calculate_stress_ratio_zero_rated():
"""Raise a ZeroDivisionError if passed a rated stress of zero."""
with pytest.raises(ZeroDivisionError):
stress.calculate_stress_ratio(0.625, 0.0)


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_negative_operating():
"""Raise a ValueError if passed a negative operating stress."""
with pytest.raises(ValueError):
stress.calculate_stress_ratio(-0.625, 1.25)


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_negative_rated():
"""Raise a ValueError if passed a negative rated stress."""
with pytest.raises(ValueError):
stress.calculate_stress_ratio(0.625, -1.25)
weibullguy marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_zero_operating():
"""Return 0.0 when operating stress is zero."""
_stress_ratio = stress.calculate_stress_ratio(0.0, 1.25)
assert _stress_ratio == pytest.approx(0.0)
weibullguy marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.unit
@pytest.mark.calculation
def test_calculate_stress_ratio_both_zero():
"""Raise a ZeroDivisionError if both operating and rated stresses are zero."""
with pytest.raises(ZeroDivisionError):
stress.calculate_stress_ratio(0.0, 0.0)
weibullguy marked this conversation as resolved.
Show resolved Hide resolved
Loading