core/tests/util/test_distance.py

147 lines
6.9 KiB
Python
Raw Normal View History

"""Test Home Assistant distance utility functions."""
import pytest
2019-07-31 19:25:30 +00:00
from homeassistant.const import (
LENGTH_CENTIMETERS,
LENGTH_FEET,
LENGTH_INCHES,
2019-07-31 19:25:30 +00:00
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
LENGTH_MILLIMETERS,
LENGTH_YARD,
2019-07-31 19:25:30 +00:00
)
import homeassistant.util.distance as distance_util
2019-07-31 19:25:30 +00:00
INVALID_SYMBOL = "bob"
Add unit system support Add unit symbol constants Initial unit system object Import more constants Pydoc for unit system file Import constants for configuration validation Unit system validation method Typing for constants Inches are valid lengths too Typings Change base class to dict - needed for remote api call serialization Validation Use dictionary keys Defined unit systems Update location util to use metric instead of us fahrenheit Update constant imports Import defined unit systems Update configuration to use unit system Update schema to use unit system Update constants Add imports to core for unit system and distance Type for config Default unit system Convert distance from HASS instance Update temperature conversion to use unit system Update temperature conversion Set unit system based on configuration Set info unit system Return unit system dictionary with config dictionary Auto discover unit system Update location test for use metric Update forecast unit system Update mold indicator unit system Update thermostat unit system Update thermostat demo test Unit tests around unit system Update test common hass configuration Update configuration unit tests There should always be a unit system! Update core unit tests Constants typing Linting issues Remove unused import Update fitbit sensor to use application unit system Update google travel time to use application unit system Update configuration example Update dht sensor Update DHT temperature conversion to use the utility function Update swagger config Update my sensors metric flag Update hvac component temperature conversion HVAC conversion for temperature Pull unit from sensor type map Pull unit from sensor type map Update the temper sensor unit Update yWeather sensor unit Update hvac demo unit test Set unit test config unit system to metric Use hass unit system length for default in proximity Use the name of the system instead of temperature Use constants from const Unused import Forecasted temperature Fix calculation in case furthest distance is greater than 1000000 units Remove unneeded constants Set default length to km or miles Use constants Linting doesn't like importing just for typing Fix reference Test is expecting meters - set config to meters Use constant Use constant PyDoc for unit test Should be not in Rename to units Change unit system to be an object - not a dictionary Return tuple in conversion Move convert to temperature util Temperature conversion is now in unit system Update imports Rename to units Units is now an object Use temperature util conversion Unit system is now an object Validate and convert unit system config Return the scalar value in template distance Test is expecting meters Update unit tests around unit system Distance util returns tuple Fix location info test Set units Update unit tests Convert distance DOH Pull out the scalar from the vector Linting I really hate python linting Linting again BLARG Unit test documentation Unit test around is metric flag Break ternary statement into if/else blocks Don't use dictionary - use members is metric flag Rename constants Use is metric flag Move constants to CONST file Move to const file Raise error if unit is not expected Typing No need to return unit since only performing conversion if it can work Use constants Line wrapping Raise error if invalid value Remove subscripts from conversion as they are no longer returned as tuples No longer tuples No longer tuples Check for numeric type Fix string format to use correct variable Typing Assert errors raised Remove subscript Only convert temperature if we know the unit If no unit of measurement set - default to HASS config Convert only if we know the unit Remove subscription Fix not in clause Linting fixes Wants a boolean Clearer if-block Check if the key is in the config first Missed a couple expecting tuples Backwards compatibility No like-y ternary! Error handling around state setting Pretty unit system configuration validation More tuple crap Use is metric flag Error handling around min/max temp Explode if no unit Pull unit from config Celsius has a decimal Unused import Check if it's a temperature before we try to convert it to a temperature Linting says too many statements - combine lat/long in a fairly reasonable manner Backwards compatibility unit test Better doc
2016-07-31 20:24:49 +00:00
VALID_SYMBOL = LENGTH_KILOMETERS
def test_convert_same_unit():
"""Test conversion from any unit to same unit."""
assert distance_util.convert(5, LENGTH_KILOMETERS, LENGTH_KILOMETERS) == 5
assert distance_util.convert(2, LENGTH_METERS, LENGTH_METERS) == 2
assert distance_util.convert(6, LENGTH_CENTIMETERS, LENGTH_CENTIMETERS) == 6
assert distance_util.convert(3, LENGTH_MILLIMETERS, LENGTH_MILLIMETERS) == 3
assert distance_util.convert(10, LENGTH_MILES, LENGTH_MILES) == 10
assert distance_util.convert(9, LENGTH_YARD, LENGTH_YARD) == 9
assert distance_util.convert(8, LENGTH_FEET, LENGTH_FEET) == 8
assert distance_util.convert(7, LENGTH_INCHES, LENGTH_INCHES) == 7
def test_convert_invalid_unit():
"""Test exception is thrown for invalid units."""
with pytest.raises(ValueError):
distance_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)
with pytest.raises(ValueError):
distance_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
distance_util.convert("a", LENGTH_KILOMETERS, LENGTH_METERS)
def test_convert_from_miles():
"""Test conversion from miles to other units."""
miles = 5
2019-07-31 19:25:30 +00:00
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_KILOMETERS) == 8.04672
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_METERS) == 8046.72
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_CENTIMETERS) == 804672.0
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_MILLIMETERS) == 8046720.0
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_YARD) == 8799.9734592
2019-07-31 19:25:30 +00:00
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_FEET) == 26400.0008448
assert distance_util.convert(miles, LENGTH_MILES, LENGTH_INCHES) == 316800.171072
def test_convert_from_yards():
"""Test conversion from yards to other units."""
yards = 5
assert (
distance_util.convert(yards, LENGTH_YARD, LENGTH_KILOMETERS)
== 0.0045720000000000005
)
assert distance_util.convert(yards, LENGTH_YARD, LENGTH_METERS) == 4.572
assert distance_util.convert(yards, LENGTH_YARD, LENGTH_CENTIMETERS) == 457.2
assert distance_util.convert(yards, LENGTH_YARD, LENGTH_MILLIMETERS) == 4572.0
assert distance_util.convert(yards, LENGTH_YARD, LENGTH_MILES) == 0.002840908212
assert distance_util.convert(yards, LENGTH_YARD, LENGTH_FEET) == 15.00000048
assert distance_util.convert(yards, LENGTH_YARD, LENGTH_INCHES) == 180.0000972
def test_convert_from_feet():
"""Test conversion from feet to other units."""
feet = 5000
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_KILOMETERS) == 1.524
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_METERS) == 1524
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_CENTIMETERS) == 152400.0
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_MILLIMETERS) == 1524000.0
2019-07-31 19:25:30 +00:00
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_MILES) == 0.9469694040000001
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_YARD) == 1666.66164
assert distance_util.convert(feet, LENGTH_FEET, LENGTH_INCHES) == 60000.032400000004
def test_convert_from_inches():
"""Test conversion from inches to other units."""
inches = 5000
assert distance_util.convert(inches, LENGTH_INCHES, LENGTH_KILOMETERS) == 0.127
assert distance_util.convert(inches, LENGTH_INCHES, LENGTH_METERS) == 127.0
assert distance_util.convert(inches, LENGTH_INCHES, LENGTH_CENTIMETERS) == 12700.0
assert distance_util.convert(inches, LENGTH_INCHES, LENGTH_MILLIMETERS) == 127000.0
assert distance_util.convert(inches, LENGTH_INCHES, LENGTH_MILES) == 0.078914117
assert (
distance_util.convert(inches, LENGTH_INCHES, LENGTH_YARD) == 138.88846999999998
)
assert distance_util.convert(inches, LENGTH_INCHES, LENGTH_FEET) == 416.66668
def test_convert_from_kilometers():
"""Test conversion from kilometers to other units."""
km = 5
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_METERS) == 5000
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_CENTIMETERS) == 500000
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_MILLIMETERS) == 5000000
2019-07-31 19:25:30 +00:00
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_MILES) == 3.106855
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_YARD) == 5468.05
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_FEET) == 16404.2
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_INCHES) == 196850.5
def test_convert_from_meters():
"""Test conversion from meters to other units."""
m = 5000
assert distance_util.convert(m, LENGTH_METERS, LENGTH_KILOMETERS) == 5
assert distance_util.convert(m, LENGTH_METERS, LENGTH_CENTIMETERS) == 500000
assert distance_util.convert(m, LENGTH_METERS, LENGTH_MILLIMETERS) == 5000000
assert distance_util.convert(m, LENGTH_METERS, LENGTH_MILES) == 3.106855
assert distance_util.convert(m, LENGTH_METERS, LENGTH_YARD) == 5468.05
assert distance_util.convert(m, LENGTH_METERS, LENGTH_FEET) == 16404.2
assert distance_util.convert(m, LENGTH_METERS, LENGTH_INCHES) == 196850.5
def test_convert_from_centimeters():
"""Test conversion from centimeters to other units."""
cm = 500000
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_KILOMETERS) == 5
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_METERS) == 5000
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_MILLIMETERS) == 5000000
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_MILES) == 3.106855
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_YARD) == 5468.05
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_FEET) == 16404.2
assert distance_util.convert(cm, LENGTH_CENTIMETERS, LENGTH_INCHES) == 196850.5
def test_convert_from_millimeters():
"""Test conversion from millimeters to other units."""
mm = 5000000
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_KILOMETERS) == 5
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_METERS) == 5000
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_CENTIMETERS) == 500000
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_MILES) == 3.106855
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_YARD) == 5468.05
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_FEET) == 16404.2
assert distance_util.convert(mm, LENGTH_MILLIMETERS, LENGTH_INCHES) == 196850.5