2016-07-31 20:24:49 +00:00
|
|
|
"""Test the unit system helper."""
|
2019-04-30 16:20:38 +00:00
|
|
|
import pytest
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-12-09 15:52:24 +00:00
|
|
|
LENGTH,
|
2016-07-31 20:24:49 +00:00
|
|
|
LENGTH_KILOMETERS,
|
2019-12-09 15:52:24 +00:00
|
|
|
LENGTH_METERS,
|
|
|
|
MASS,
|
2016-07-31 20:24:49 +00:00
|
|
|
MASS_GRAMS,
|
2019-12-09 15:52:24 +00:00
|
|
|
PRESSURE,
|
2019-03-24 17:37:31 +00:00
|
|
|
PRESSURE_PA,
|
2016-07-31 20:24:49 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMPERATURE,
|
2019-07-31 19:25:30 +00:00
|
|
|
VOLUME,
|
2019-12-09 15:52:24 +00:00
|
|
|
VOLUME_LITERS,
|
2016-07-31 20:24:49 +00:00
|
|
|
)
|
2019-12-09 15:52:24 +00:00
|
|
|
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
SYSTEM_NAME = "TEST"
|
|
|
|
INVALID_UNIT = "INVALID"
|
2016-07-31 20:24:49 +00:00
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
def test_invalid_units():
|
|
|
|
"""Test errors are raised when invalid units are passed in."""
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
|
|
|
INVALID_UNIT,
|
|
|
|
LENGTH_METERS,
|
|
|
|
VOLUME_LITERS,
|
|
|
|
MASS_GRAMS,
|
|
|
|
PRESSURE_PA,
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
INVALID_UNIT,
|
|
|
|
VOLUME_LITERS,
|
|
|
|
MASS_GRAMS,
|
|
|
|
PRESSURE_PA,
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
LENGTH_METERS,
|
|
|
|
INVALID_UNIT,
|
|
|
|
MASS_GRAMS,
|
|
|
|
PRESSURE_PA,
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
LENGTH_METERS,
|
|
|
|
VOLUME_LITERS,
|
|
|
|
INVALID_UNIT,
|
|
|
|
PRESSURE_PA,
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
UnitSystem(
|
|
|
|
SYSTEM_NAME,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
LENGTH_METERS,
|
|
|
|
VOLUME_LITERS,
|
|
|
|
MASS_GRAMS,
|
|
|
|
INVALID_UNIT,
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_value():
|
|
|
|
"""Test no conversion happens if value is non-numeric."""
|
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.length("25a", LENGTH_KILOMETERS)
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.temperature("50K", TEMP_CELSIUS)
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.volume("50L", VOLUME_LITERS)
|
2019-04-30 16:20:38 +00:00
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.pressure("50Pa", PRESSURE_PA)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_as_dict():
|
|
|
|
"""Test that the as_dict() method returns the expected dictionary."""
|
|
|
|
expected = {
|
|
|
|
LENGTH: LENGTH_KILOMETERS,
|
|
|
|
TEMPERATURE: TEMP_CELSIUS,
|
|
|
|
VOLUME: VOLUME_LITERS,
|
|
|
|
MASS: MASS_GRAMS,
|
2019-07-31 19:25:30 +00:00
|
|
|
PRESSURE: PRESSURE_PA,
|
2019-04-30 16:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert expected == METRIC_SYSTEM.as_dict()
|
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_same_unit():
|
|
|
|
"""Test no conversion happens if to unit is same as from unit."""
|
|
|
|
assert METRIC_SYSTEM.temperature(5, METRIC_SYSTEM.temperature_unit) == 5
|
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_unknown_unit():
|
|
|
|
"""Test no conversion happens if unknown unit."""
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.temperature(5, "K")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_to_metric():
|
|
|
|
"""Test temperature conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit) == 25
|
2019-07-31 19:25:30 +00:00
|
|
|
assert (
|
|
|
|
round(METRIC_SYSTEM.temperature(80, IMPERIAL_SYSTEM.temperature_unit), 1)
|
|
|
|
== 26.7
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_temperature_to_imperial():
|
|
|
|
"""Test temperature conversion to imperial system."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert IMPERIAL_SYSTEM.temperature(77, IMPERIAL_SYSTEM.temperature_unit) == 77
|
|
|
|
assert IMPERIAL_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit) == 77
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_length_unknown_unit():
|
|
|
|
"""Test length conversion with unknown from unit."""
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.length(5, "fr")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_length_to_metric():
|
|
|
|
"""Test length conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit) == 100
|
|
|
|
assert METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit) == 8.04672
|
|
|
|
|
|
|
|
|
|
|
|
def test_length_to_imperial():
|
|
|
|
"""Test length conversion to imperial system."""
|
|
|
|
assert IMPERIAL_SYSTEM.length(100, IMPERIAL_SYSTEM.length_unit) == 100
|
|
|
|
assert IMPERIAL_SYSTEM.length(5, METRIC_SYSTEM.length_unit) == 3.106855
|
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_same_unit():
|
|
|
|
"""Test no conversion happens if to unit is same as from unit."""
|
|
|
|
assert METRIC_SYSTEM.pressure(5, METRIC_SYSTEM.pressure_unit) == 5
|
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_unknown_unit():
|
|
|
|
"""Test no conversion happens if unknown unit."""
|
|
|
|
with pytest.raises(ValueError):
|
2019-07-31 19:25:30 +00:00
|
|
|
METRIC_SYSTEM.pressure(5, "K")
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_to_metric():
|
|
|
|
"""Test pressure conversion to metric system."""
|
|
|
|
assert METRIC_SYSTEM.pressure(25, METRIC_SYSTEM.pressure_unit) == 25
|
2019-07-31 19:25:30 +00:00
|
|
|
assert METRIC_SYSTEM.pressure(14.7, IMPERIAL_SYSTEM.pressure_unit) == pytest.approx(
|
|
|
|
101352.932, abs=1e-1
|
|
|
|
)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_pressure_to_imperial():
|
|
|
|
"""Test pressure conversion to imperial system."""
|
|
|
|
assert IMPERIAL_SYSTEM.pressure(77, IMPERIAL_SYSTEM.pressure_unit) == 77
|
|
|
|
assert IMPERIAL_SYSTEM.pressure(
|
2019-07-31 19:25:30 +00:00
|
|
|
101352.932, METRIC_SYSTEM.pressure_unit
|
|
|
|
) == pytest.approx(14.7, abs=1e-4)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_properties():
|
|
|
|
"""Test the unit properties are returned as expected."""
|
|
|
|
assert LENGTH_KILOMETERS == METRIC_SYSTEM.length_unit
|
|
|
|
assert TEMP_CELSIUS == METRIC_SYSTEM.temperature_unit
|
|
|
|
assert MASS_GRAMS == METRIC_SYSTEM.mass_unit
|
|
|
|
assert VOLUME_LITERS == METRIC_SYSTEM.volume_unit
|
|
|
|
assert PRESSURE_PA == METRIC_SYSTEM.pressure_unit
|
|
|
|
|
|
|
|
|
|
|
|
def test_is_metric():
|
|
|
|
"""Test the is metric flag."""
|
|
|
|
assert METRIC_SYSTEM.is_metric
|
|
|
|
assert not IMPERIAL_SYSTEM.is_metric
|