2020-01-05 12:09:17 +00:00
|
|
|
"""Test Home Assistant volume utility functions."""
|
2018-10-11 08:55:22 +00:00
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
import pytest
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import (
|
2021-08-11 16:58:19 +00:00
|
|
|
VOLUME_CUBIC_FEET,
|
|
|
|
VOLUME_CUBIC_METERS,
|
2019-12-09 15:52:24 +00:00
|
|
|
VOLUME_FLUID_OUNCE,
|
|
|
|
VOLUME_GALLONS,
|
2019-07-31 19:25:30 +00:00
|
|
|
VOLUME_LITERS,
|
|
|
|
VOLUME_MILLILITERS,
|
|
|
|
)
|
2019-12-09 15:52:24 +00:00
|
|
|
import homeassistant.util.volume as volume_util
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
INVALID_SYMBOL = "bob"
|
2018-10-11 08:55:22 +00:00
|
|
|
VALID_SYMBOL = VOLUME_LITERS
|
|
|
|
|
|
|
|
|
2019-04-30 16:20:38 +00:00
|
|
|
def test_convert_same_unit():
|
|
|
|
"""Test conversion from any unit to same unit."""
|
|
|
|
assert volume_util.convert(2, VOLUME_LITERS, VOLUME_LITERS) == 2
|
|
|
|
assert volume_util.convert(3, VOLUME_MILLILITERS, VOLUME_MILLILITERS) == 3
|
|
|
|
assert volume_util.convert(4, VOLUME_GALLONS, VOLUME_GALLONS) == 4
|
|
|
|
assert volume_util.convert(5, VOLUME_FLUID_OUNCE, VOLUME_FLUID_OUNCE) == 5
|
|
|
|
|
|
|
|
|
|
|
|
def test_convert_invalid_unit():
|
|
|
|
"""Test exception is thrown for invalid units."""
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
volume_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
volume_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
|
|
|
|
|
|
|
|
|
|
|
|
def test_convert_nonnumeric_value():
|
|
|
|
"""Test exception is thrown for nonnumeric type."""
|
|
|
|
with pytest.raises(TypeError):
|
2019-07-31 19:25:30 +00:00
|
|
|
volume_util.convert("a", VOLUME_GALLONS, VOLUME_LITERS)
|
2019-04-30 16:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_convert_from_liters():
|
|
|
|
"""Test conversion from liters to other units."""
|
|
|
|
liters = 5
|
|
|
|
assert volume_util.convert(liters, VOLUME_LITERS, VOLUME_GALLONS) == 1.321
|
|
|
|
|
|
|
|
|
|
|
|
def test_convert_from_gallons():
|
|
|
|
"""Test conversion from gallons to other units."""
|
|
|
|
gallons = 5
|
2019-07-31 19:25:30 +00:00
|
|
|
assert volume_util.convert(gallons, VOLUME_GALLONS, VOLUME_LITERS) == 18.925
|
2021-08-11 16:58:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_convert_from_cubic_meters():
|
|
|
|
"""Test conversion from cubic meter to other units."""
|
|
|
|
cubic_meters = 5
|
|
|
|
assert (
|
|
|
|
volume_util.convert(cubic_meters, VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET)
|
|
|
|
== 176.5733335
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_convert_from_cubic_feet():
|
|
|
|
"""Test conversion from cubic feet to cubic meters to other units."""
|
|
|
|
cubic_feets = 500
|
|
|
|
assert (
|
|
|
|
volume_util.convert(cubic_feets, VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS)
|
|
|
|
== 14.1584233
|
|
|
|
)
|