Pytest tests (#17750)
* Convert core tests
* Convert component tests to use pytest assert
* Lint 🤷♂️
* Fix test
* Fix 3 typos in docs
This commit is contained in:
parent
4222f7562b
commit
08fe7c3ece
223 changed files with 6747 additions and 7237 deletions
|
@ -17,6 +17,7 @@ from homeassistant.const import (
|
|||
TEMPERATURE,
|
||||
VOLUME
|
||||
)
|
||||
import pytest
|
||||
|
||||
SYSTEM_NAME = 'TEST'
|
||||
INVALID_UNIT = 'INVALID'
|
||||
|
@ -27,27 +28,27 @@ class TestUnitSystem(unittest.TestCase):
|
|||
|
||||
def test_invalid_units(self):
|
||||
"""Test errors are raised when invalid units are passed in."""
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
UnitSystem(SYSTEM_NAME, INVALID_UNIT, LENGTH_METERS, VOLUME_LITERS,
|
||||
MASS_GRAMS)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
UnitSystem(SYSTEM_NAME, TEMP_CELSIUS, INVALID_UNIT, VOLUME_LITERS,
|
||||
MASS_GRAMS)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
UnitSystem(SYSTEM_NAME, TEMP_CELSIUS, LENGTH_METERS, INVALID_UNIT,
|
||||
MASS_GRAMS)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
UnitSystem(SYSTEM_NAME, TEMP_CELSIUS, LENGTH_METERS, VOLUME_LITERS,
|
||||
INVALID_UNIT)
|
||||
|
||||
def test_invalid_value(self):
|
||||
"""Test no conversion happens if value is non-numeric."""
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
METRIC_SYSTEM.length('25a', LENGTH_KILOMETERS)
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
METRIC_SYSTEM.temperature('50K', TEMP_CELSIUS)
|
||||
|
||||
def test_as_dict(self):
|
||||
|
@ -59,75 +60,62 @@ class TestUnitSystem(unittest.TestCase):
|
|||
MASS: MASS_GRAMS
|
||||
}
|
||||
|
||||
self.assertEqual(expected, METRIC_SYSTEM.as_dict())
|
||||
assert expected == METRIC_SYSTEM.as_dict()
|
||||
|
||||
def test_temperature_same_unit(self):
|
||||
"""Test no conversion happens if to unit is same as from unit."""
|
||||
self.assertEqual(
|
||||
5,
|
||||
assert 5 == \
|
||||
METRIC_SYSTEM.temperature(5,
|
||||
METRIC_SYSTEM.temperature_unit))
|
||||
METRIC_SYSTEM.temperature_unit)
|
||||
|
||||
def test_temperature_unknown_unit(self):
|
||||
"""Test no conversion happens if unknown unit."""
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
METRIC_SYSTEM.temperature(5, 'K')
|
||||
|
||||
def test_temperature_to_metric(self):
|
||||
"""Test temperature conversion to metric system."""
|
||||
self.assertEqual(
|
||||
25,
|
||||
METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit))
|
||||
self.assertEqual(
|
||||
26.7,
|
||||
assert 25 == \
|
||||
METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit)
|
||||
assert 26.7 == \
|
||||
round(METRIC_SYSTEM.temperature(
|
||||
80, IMPERIAL_SYSTEM.temperature_unit), 1))
|
||||
80, IMPERIAL_SYSTEM.temperature_unit), 1)
|
||||
|
||||
def test_temperature_to_imperial(self):
|
||||
"""Test temperature conversion to imperial system."""
|
||||
self.assertEqual(
|
||||
77,
|
||||
IMPERIAL_SYSTEM.temperature(77, IMPERIAL_SYSTEM.temperature_unit))
|
||||
self.assertEqual(
|
||||
77,
|
||||
IMPERIAL_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit))
|
||||
assert 77 == \
|
||||
IMPERIAL_SYSTEM.temperature(77, IMPERIAL_SYSTEM.temperature_unit)
|
||||
assert 77 == \
|
||||
IMPERIAL_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit)
|
||||
|
||||
def test_length_unknown_unit(self):
|
||||
"""Test length conversion with unknown from unit."""
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
METRIC_SYSTEM.length(5, 'fr')
|
||||
|
||||
def test_length_to_metric(self):
|
||||
"""Test length conversion to metric system."""
|
||||
self.assertEqual(
|
||||
100,
|
||||
assert 100 == \
|
||||
METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit)
|
||||
)
|
||||
self.assertEqual(
|
||||
8.04672,
|
||||
assert 8.04672 == \
|
||||
METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit)
|
||||
)
|
||||
|
||||
def test_length_to_imperial(self):
|
||||
"""Test length conversion to imperial system."""
|
||||
self.assertEqual(
|
||||
100,
|
||||
assert 100 == \
|
||||
IMPERIAL_SYSTEM.length(100,
|
||||
IMPERIAL_SYSTEM.length_unit)
|
||||
)
|
||||
self.assertEqual(
|
||||
3.106855,
|
||||
assert 3.106855 == \
|
||||
IMPERIAL_SYSTEM.length(5, METRIC_SYSTEM.length_unit)
|
||||
)
|
||||
|
||||
def test_properties(self):
|
||||
"""Test the unit properties are returned as expected."""
|
||||
self.assertEqual(LENGTH_KILOMETERS, METRIC_SYSTEM.length_unit)
|
||||
self.assertEqual(TEMP_CELSIUS, METRIC_SYSTEM.temperature_unit)
|
||||
self.assertEqual(MASS_GRAMS, METRIC_SYSTEM.mass_unit)
|
||||
self.assertEqual(VOLUME_LITERS, METRIC_SYSTEM.volume_unit)
|
||||
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
|
||||
|
||||
def test_is_metric(self):
|
||||
"""Test the is metric flag."""
|
||||
self.assertTrue(METRIC_SYSTEM.is_metric)
|
||||
self.assertFalse(IMPERIAL_SYSTEM.is_metric)
|
||||
assert METRIC_SYSTEM.is_metric
|
||||
assert not IMPERIAL_SYSTEM.is_metric
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue