Migrates tests to use UnitOfSpeed enum (#86777)

This commit is contained in:
Franck Nijhof 2023-01-27 10:58:55 +01:00 committed by GitHub
parent fca3382d37
commit 49148421cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 18 deletions

View File

@ -21,10 +21,10 @@ from homeassistant.const import (
LENGTH_MILLIMETERS, LENGTH_MILLIMETERS,
MASS_GRAMS, MASS_GRAMS,
PRESSURE_PA, PRESSURE_PA,
SPEED_KILOMETERS_PER_HOUR,
STATE_ON, STATE_ON,
TEMP_CELSIUS, TEMP_CELSIUS,
VOLUME_LITERS, VOLUME_LITERS,
UnitOfSpeed,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
@ -55,7 +55,7 @@ def _set_up_units(hass: HomeAssistant) -> None:
pressure=PRESSURE_PA, pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS, temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS, volume=VOLUME_LITERS,
wind_speed=SPEED_KILOMETERS_PER_HOUR, wind_speed=UnitOfSpeed.KILOMETERS_PER_HOUR,
) )

View File

@ -2,20 +2,16 @@
import pytest import pytest
from homeassistant.const import ( from homeassistant.const import (
SPEED_FEET_PER_SECOND,
SPEED_INCHES_PER_DAY, SPEED_INCHES_PER_DAY,
SPEED_INCHES_PER_HOUR, SPEED_INCHES_PER_HOUR,
SPEED_KILOMETERS_PER_HOUR,
SPEED_KNOTS,
SPEED_METERS_PER_SECOND,
SPEED_MILES_PER_HOUR,
SPEED_MILLIMETERS_PER_DAY, SPEED_MILLIMETERS_PER_DAY,
UnitOfSpeed,
) )
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
import homeassistant.util.speed as speed_util import homeassistant.util.speed as speed_util
INVALID_SYMBOL = "bob" INVALID_SYMBOL = "bob"
VALID_SYMBOL = SPEED_KILOMETERS_PER_HOUR VALID_SYMBOL = UnitOfSpeed.KILOMETERS_PER_HOUR
def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None: def test_raise_deprecation_warning(caplog: pytest.LogCaptureFixture) -> None:
@ -29,10 +25,21 @@ def test_convert_same_unit():
assert speed_util.convert(2, SPEED_INCHES_PER_DAY, SPEED_INCHES_PER_DAY) == 2 assert speed_util.convert(2, SPEED_INCHES_PER_DAY, SPEED_INCHES_PER_DAY) == 2
assert speed_util.convert(3, SPEED_INCHES_PER_HOUR, SPEED_INCHES_PER_HOUR) == 3 assert speed_util.convert(3, SPEED_INCHES_PER_HOUR, SPEED_INCHES_PER_HOUR) == 3
assert ( assert (
speed_util.convert(4, SPEED_KILOMETERS_PER_HOUR, SPEED_KILOMETERS_PER_HOUR) == 4 speed_util.convert(
4, UnitOfSpeed.KILOMETERS_PER_HOUR, UnitOfSpeed.KILOMETERS_PER_HOUR
)
== 4
)
assert (
speed_util.convert(
5, UnitOfSpeed.METERS_PER_SECOND, UnitOfSpeed.METERS_PER_SECOND
)
== 5
)
assert (
speed_util.convert(6, UnitOfSpeed.MILES_PER_HOUR, UnitOfSpeed.MILES_PER_HOUR)
== 6
) )
assert speed_util.convert(5, SPEED_METERS_PER_SECOND, SPEED_METERS_PER_SECOND) == 5
assert speed_util.convert(6, SPEED_MILES_PER_HOUR, SPEED_MILES_PER_HOUR) == 6
assert ( assert (
speed_util.convert(7, SPEED_MILLIMETERS_PER_DAY, SPEED_MILLIMETERS_PER_DAY) == 7 speed_util.convert(7, SPEED_MILLIMETERS_PER_DAY, SPEED_MILLIMETERS_PER_DAY) == 7
) )
@ -50,16 +57,18 @@ def test_convert_invalid_unit():
def test_convert_nonnumeric_value(): def test_convert_nonnumeric_value():
"""Test exception is thrown for nonnumeric type.""" """Test exception is thrown for nonnumeric type."""
with pytest.raises(TypeError): with pytest.raises(TypeError):
speed_util.convert("a", SPEED_KILOMETERS_PER_HOUR, SPEED_MILES_PER_HOUR) speed_util.convert(
"a", UnitOfSpeed.KILOMETERS_PER_HOUR, UnitOfSpeed.MILES_PER_HOUR
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"from_value, from_unit, expected, to_unit", "from_value, from_unit, expected, to_unit",
[ [
# 5 km/h / 1.609 km/mi = 3.10686 mi/h # 5 km/h / 1.609 km/mi = 3.10686 mi/h
(5, SPEED_KILOMETERS_PER_HOUR, 3.10686, SPEED_MILES_PER_HOUR), (5, UnitOfSpeed.KILOMETERS_PER_HOUR, 3.10686, UnitOfSpeed.MILES_PER_HOUR),
# 5 mi/h * 1.609 km/mi = 8.04672 km/h # 5 mi/h * 1.609 km/mi = 8.04672 km/h
(5, SPEED_MILES_PER_HOUR, 8.04672, SPEED_KILOMETERS_PER_HOUR), (5, UnitOfSpeed.MILES_PER_HOUR, 8.04672, UnitOfSpeed.KILOMETERS_PER_HOUR),
# 5 in/day * 25.4 mm/in = 127 mm/day # 5 in/day * 25.4 mm/in = 127 mm/day
(5, SPEED_INCHES_PER_DAY, 127, SPEED_MILLIMETERS_PER_DAY), (5, SPEED_INCHES_PER_DAY, 127, SPEED_MILLIMETERS_PER_DAY),
# 5 mm/day / 25.4 mm/in = 0.19685 in/day # 5 mm/day / 25.4 mm/in = 0.19685 in/day
@ -67,13 +76,13 @@ def test_convert_nonnumeric_value():
# 5 in/hr * 24 hr/day = 3048 mm/day # 5 in/hr * 24 hr/day = 3048 mm/day
(5, SPEED_INCHES_PER_HOUR, 3048, SPEED_MILLIMETERS_PER_DAY), (5, SPEED_INCHES_PER_HOUR, 3048, SPEED_MILLIMETERS_PER_DAY),
# 5 m/s * 39.3701 in/m * 3600 s/hr = 708661 # 5 m/s * 39.3701 in/m * 3600 s/hr = 708661
(5, SPEED_METERS_PER_SECOND, 708661, SPEED_INCHES_PER_HOUR), (5, UnitOfSpeed.METERS_PER_SECOND, 708661, SPEED_INCHES_PER_HOUR),
# 5000 in/h / 39.3701 in/m / 3600 s/h = 0.03528 m/s # 5000 in/h / 39.3701 in/m / 3600 s/h = 0.03528 m/s
(5000, SPEED_INCHES_PER_HOUR, 0.03528, SPEED_METERS_PER_SECOND), (5000, SPEED_INCHES_PER_HOUR, 0.03528, UnitOfSpeed.METERS_PER_SECOND),
# 5 kt * 1852 m/nmi / 3600 s/h = 2.5722 m/s # 5 kt * 1852 m/nmi / 3600 s/h = 2.5722 m/s
(5, SPEED_KNOTS, 2.5722, SPEED_METERS_PER_SECOND), (5, UnitOfSpeed.KNOTS, 2.5722, UnitOfSpeed.METERS_PER_SECOND),
# 5 ft/s * 0.3048 m/ft = 1.524 m/s # 5 ft/s * 0.3048 m/ft = 1.524 m/s
(5, SPEED_FEET_PER_SECOND, 1.524, SPEED_METERS_PER_SECOND), (5, UnitOfSpeed.FEET_PER_SECOND, 1.524, UnitOfSpeed.METERS_PER_SECOND),
], ],
) )
def test_convert_different_units(from_value, from_unit, expected, to_unit): def test_convert_different_units(from_value, from_unit, expected, to_unit):