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

Correctly handle a possible divide by zero in convert_to_mpg #78

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Changes from all 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
4 changes: 3 additions & 1 deletion mytoyota/utils.py
Original file line number Diff line number Diff line change
@@ -55,4 +55,6 @@ def convert_to_liter_per_100_miles(liters: float) -> float:
def convert_to_mpg(liters_per_100_km: float) -> float:
"""Convert to miles per UK gallon (MPG)"""
_LOGGER.debug("Converting to MPG...")
return round(282.5 / liters_per_100_km, 4)
if liters_per_100_km > 0.0:
return round(282.5 / liters_per_100_km, 4)
return 0.0
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -102,9 +102,9 @@ def test_convert_to_liter_per_100_miles(self, liters_km, per_mile):
@pytest.mark.parametrize(
"liters_km,mpg",
[
(0, 0),
(0.01, 28250.0),
(1, 282.5),
# Joro75: A divide by zero is triggered!!
# (0, 0),
(12, 23.5417),
],
)