Enable unit conversion for DATA_SIZE (#84699)

This commit is contained in:
epenet
2022-12-29 09:58:15 +01:00
committed by Franck Nijhof
parent b24c40f2df
commit 5901964bf6
3 changed files with 82 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import pytest
from homeassistant.const import (
UnitOfDataRate,
UnitOfEnergy,
UnitOfInformation,
UnitOfLength,
UnitOfMass,
UnitOfPower,
@@ -19,6 +20,7 @@ from homeassistant.util.unit_conversion import (
DataRateConverter,
DistanceConverter,
EnergyConverter,
InformationConverter,
MassConverter,
PowerConverter,
PressureConverter,
@@ -46,6 +48,7 @@ INVALID_SYMBOL = "bob"
(EnergyConverter, UnitOfEnergy.KILO_WATT_HOUR),
(EnergyConverter, UnitOfEnergy.MEGA_WATT_HOUR),
(EnergyConverter, UnitOfEnergy.GIGA_JOULE),
(InformationConverter, UnitOfInformation.GIGABYTES),
(MassConverter, UnitOfMass.GRAMS),
(MassConverter, UnitOfMass.KILOGRAMS),
(MassConverter, UnitOfMass.MICROGRAMS),
@@ -91,6 +94,7 @@ def test_convert_same_unit(converter: type[BaseUnitConverter], valid_unit: str)
(DataRateConverter, UnitOfDataRate.GIBIBYTES_PER_SECOND),
(DistanceConverter, UnitOfLength.KILOMETERS),
(EnergyConverter, UnitOfEnergy.KILO_WATT_HOUR),
(InformationConverter, UnitOfInformation.GIBIBYTES),
(MassConverter, UnitOfMass.GRAMS),
(PowerConverter, UnitOfPower.WATT),
(PressureConverter, UnitOfPressure.PA),
@@ -122,6 +126,11 @@ def test_convert_invalid_unit(
),
(DistanceConverter, UnitOfLength.KILOMETERS, UnitOfLength.METERS),
(EnergyConverter, UnitOfEnergy.WATT_HOUR, UnitOfEnergy.KILO_WATT_HOUR),
(
InformationConverter,
UnitOfInformation.GIBIBYTES,
UnitOfInformation.GIGABYTES,
),
(MassConverter, UnitOfMass.GRAMS, UnitOfMass.KILOGRAMS),
(PowerConverter, UnitOfPower.WATT, UnitOfPower.KILO_WATT),
(PressureConverter, UnitOfPressure.HPA, UnitOfPressure.INHG),
@@ -149,6 +158,7 @@ def test_convert_nonnumeric_value(
),
(DistanceConverter, UnitOfLength.KILOMETERS, UnitOfLength.METERS, 1 / 1000),
(EnergyConverter, UnitOfEnergy.WATT_HOUR, UnitOfEnergy.KILO_WATT_HOUR, 1000),
(InformationConverter, UnitOfInformation.BITS, UnitOfInformation.BYTES, 8),
(PowerConverter, UnitOfPower.WATT, UnitOfPower.KILO_WATT, 1000),
(
PressureConverter,
@@ -364,6 +374,43 @@ def test_energy_convert(
assert EnergyConverter.convert(value, from_unit, to_unit) == expected
@pytest.mark.parametrize(
"value,from_unit,expected,to_unit",
[
(8e3, UnitOfInformation.BITS, 8, UnitOfInformation.KILOBITS),
(8e6, UnitOfInformation.BITS, 8, UnitOfInformation.MEGABITS),
(8e9, UnitOfInformation.BITS, 8, UnitOfInformation.GIGABITS),
(8, UnitOfInformation.BITS, 1, UnitOfInformation.BYTES),
(8e3, UnitOfInformation.BITS, 1, UnitOfInformation.KILOBYTES),
(8e6, UnitOfInformation.BITS, 1, UnitOfInformation.MEGABYTES),
(8e9, UnitOfInformation.BITS, 1, UnitOfInformation.GIGABYTES),
(8e12, UnitOfInformation.BITS, 1, UnitOfInformation.TERABYTES),
(8e15, UnitOfInformation.BITS, 1, UnitOfInformation.PETABYTES),
(8e18, UnitOfInformation.BITS, 1, UnitOfInformation.EXABYTES),
(8e21, UnitOfInformation.BITS, 1, UnitOfInformation.ZETTABYTES),
(8e24, UnitOfInformation.BITS, 1, UnitOfInformation.YOTTABYTES),
(8 * 2**10, UnitOfInformation.BITS, 1, UnitOfInformation.KIBIBYTES),
(8 * 2**20, UnitOfInformation.BITS, 1, UnitOfInformation.MEBIBYTES),
(8 * 2**30, UnitOfInformation.BITS, 1, UnitOfInformation.GIBIBYTES),
(8 * 2**40, UnitOfInformation.BITS, 1, UnitOfInformation.TEBIBYTES),
(8 * 2**50, UnitOfInformation.BITS, 1, UnitOfInformation.PEBIBYTES),
(8 * 2**60, UnitOfInformation.BITS, 1, UnitOfInformation.EXBIBYTES),
(8 * 2**70, UnitOfInformation.BITS, 1, UnitOfInformation.ZEBIBYTES),
(8 * 2**80, UnitOfInformation.BITS, 1, UnitOfInformation.YOBIBYTES),
],
)
def test_information_convert(
value: float,
from_unit: str,
expected: float,
to_unit: str,
) -> None:
"""Test conversion to other units."""
assert InformationConverter.convert(value, from_unit, to_unit) == pytest.approx(
expected
)
@pytest.mark.parametrize(
"value,from_unit,expected,to_unit",
[