Skip to content

Commit

Permalink
Correctly handle a possible divide by zero in convert_to_mpg (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
joro75 authored Oct 11, 2021
1 parent c85b3bb commit 75d5447
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion mytoyota/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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),
],
)
Expand Down

0 comments on commit 75d5447

Please sign in to comment.