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:
1 parent
4222f7562b
commit
08fe7c3ece
223 files changed
+6747
-7237
No files matched your search
+163
-163
@@ -12,203 +12,203 @@ class TestColorUtil(unittest.TestCase):
|
||||
# pylint: disable=invalid-name
|
||||
def test_color_RGB_to_xy_brightness(self):
|
||||
"""Test color_RGB_to_xy_brightness."""
|
||||
self.assertEqual((0, 0, 0),
|
||||
color_util.color_RGB_to_xy_brightness(0, 0, 0))
|
||||
self.assertEqual((0.323, 0.329, 255),
|
||||
color_util.color_RGB_to_xy_brightness(255, 255, 255))
|
||||
assert (0, 0, 0) == \
|
||||
color_util.color_RGB_to_xy_brightness(0, 0, 0)
|
||||
assert (0.323, 0.329, 255) == \
|
||||
color_util.color_RGB_to_xy_brightness(255, 255, 255)
|
||||
|
||||
self.assertEqual((0.136, 0.04, 12),
|
||||
color_util.color_RGB_to_xy_brightness(0, 0, 255))
|
||||
assert (0.136, 0.04, 12) == \
|
||||
color_util.color_RGB_to_xy_brightness(0, 0, 255)
|
||||
|
||||
self.assertEqual((0.172, 0.747, 170),
|
||||
color_util.color_RGB_to_xy_brightness(0, 255, 0))
|
||||
assert (0.172, 0.747, 170) == \
|
||||
color_util.color_RGB_to_xy_brightness(0, 255, 0)
|
||||
|
||||
self.assertEqual((0.701, 0.299, 72),
|
||||
color_util.color_RGB_to_xy_brightness(255, 0, 0))
|
||||
assert (0.701, 0.299, 72) == \
|
||||
color_util.color_RGB_to_xy_brightness(255, 0, 0)
|
||||
|
||||
self.assertEqual((0.701, 0.299, 16),
|
||||
color_util.color_RGB_to_xy_brightness(128, 0, 0))
|
||||
assert (0.701, 0.299, 16) == \
|
||||
color_util.color_RGB_to_xy_brightness(128, 0, 0)
|
||||
|
||||
def test_color_RGB_to_xy(self):
|
||||
"""Test color_RGB_to_xy."""
|
||||
self.assertEqual((0, 0),
|
||||
color_util.color_RGB_to_xy(0, 0, 0))
|
||||
self.assertEqual((0.323, 0.329),
|
||||
color_util.color_RGB_to_xy(255, 255, 255))
|
||||
assert (0, 0) == \
|
||||
color_util.color_RGB_to_xy(0, 0, 0)
|
||||
assert (0.323, 0.329) == \
|
||||
color_util.color_RGB_to_xy(255, 255, 255)
|
||||
|
||||
self.assertEqual((0.136, 0.04),
|
||||
color_util.color_RGB_to_xy(0, 0, 255))
|
||||
assert (0.136, 0.04) == \
|
||||
color_util.color_RGB_to_xy(0, 0, 255)
|
||||
|
||||
self.assertEqual((0.172, 0.747),
|
||||
color_util.color_RGB_to_xy(0, 255, 0))
|
||||
assert (0.172, 0.747) == \
|
||||
color_util.color_RGB_to_xy(0, 255, 0)
|
||||
|
||||
self.assertEqual((0.701, 0.299),
|
||||
color_util.color_RGB_to_xy(255, 0, 0))
|
||||
assert (0.701, 0.299) == \
|
||||
color_util.color_RGB_to_xy(255, 0, 0)
|
||||
|
||||
self.assertEqual((0.701, 0.299),
|
||||
color_util.color_RGB_to_xy(128, 0, 0))
|
||||
assert (0.701, 0.299) == \
|
||||
color_util.color_RGB_to_xy(128, 0, 0)
|
||||
|
||||
def test_color_xy_brightness_to_RGB(self):
|
||||
"""Test color_xy_brightness_to_RGB."""
|
||||
self.assertEqual((0, 0, 0),
|
||||
color_util.color_xy_brightness_to_RGB(1, 1, 0))
|
||||
assert (0, 0, 0) == \
|
||||
color_util.color_xy_brightness_to_RGB(1, 1, 0)
|
||||
|
||||
self.assertEqual((194, 186, 169),
|
||||
color_util.color_xy_brightness_to_RGB(.35, .35, 128))
|
||||
assert (194, 186, 169) == \
|
||||
color_util.color_xy_brightness_to_RGB(.35, .35, 128)
|
||||
|
||||
self.assertEqual((255, 243, 222),
|
||||
color_util.color_xy_brightness_to_RGB(.35, .35, 255))
|
||||
assert (255, 243, 222) == \
|
||||
color_util.color_xy_brightness_to_RGB(.35, .35, 255)
|
||||
|
||||
self.assertEqual((255, 0, 60),
|
||||
color_util.color_xy_brightness_to_RGB(1, 0, 255))
|
||||
assert (255, 0, 60) == \
|
||||
color_util.color_xy_brightness_to_RGB(1, 0, 255)
|
||||
|
||||
self.assertEqual((0, 255, 0),
|
||||
color_util.color_xy_brightness_to_RGB(0, 1, 255))
|
||||
assert (0, 255, 0) == \
|
||||
color_util.color_xy_brightness_to_RGB(0, 1, 255)
|
||||
|
||||
self.assertEqual((0, 63, 255),
|
||||
color_util.color_xy_brightness_to_RGB(0, 0, 255))
|
||||
assert (0, 63, 255) == \
|
||||
color_util.color_xy_brightness_to_RGB(0, 0, 255)
|
||||
|
||||
def test_color_xy_to_RGB(self):
|
||||
"""Test color_xy_to_RGB."""
|
||||
self.assertEqual((255, 243, 222),
|
||||
color_util.color_xy_to_RGB(.35, .35))
|
||||
assert (255, 243, 222) == \
|
||||
color_util.color_xy_to_RGB(.35, .35)
|
||||
|
||||
self.assertEqual((255, 0, 60),
|
||||
color_util.color_xy_to_RGB(1, 0))
|
||||
assert (255, 0, 60) == \
|
||||
color_util.color_xy_to_RGB(1, 0)
|
||||
|
||||
self.assertEqual((0, 255, 0),
|
||||
color_util.color_xy_to_RGB(0, 1))
|
||||
assert (0, 255, 0) == \
|
||||
color_util.color_xy_to_RGB(0, 1)
|
||||
|
||||
self.assertEqual((0, 63, 255),
|
||||
color_util.color_xy_to_RGB(0, 0))
|
||||
assert (0, 63, 255) == \
|
||||
color_util.color_xy_to_RGB(0, 0)
|
||||
|
||||
def test_color_RGB_to_hsv(self):
|
||||
"""Test color_RGB_to_hsv."""
|
||||
self.assertEqual((0, 0, 0),
|
||||
color_util.color_RGB_to_hsv(0, 0, 0))
|
||||
assert (0, 0, 0) == \
|
||||
color_util.color_RGB_to_hsv(0, 0, 0)
|
||||
|
||||
self.assertEqual((0, 0, 100),
|
||||
color_util.color_RGB_to_hsv(255, 255, 255))
|
||||
assert (0, 0, 100) == \
|
||||
color_util.color_RGB_to_hsv(255, 255, 255)
|
||||
|
||||
self.assertEqual((240, 100, 100),
|
||||
color_util.color_RGB_to_hsv(0, 0, 255))
|
||||
assert (240, 100, 100) == \
|
||||
color_util.color_RGB_to_hsv(0, 0, 255)
|
||||
|
||||
self.assertEqual((120, 100, 100),
|
||||
color_util.color_RGB_to_hsv(0, 255, 0))
|
||||
assert (120, 100, 100) == \
|
||||
color_util.color_RGB_to_hsv(0, 255, 0)
|
||||
|
||||
self.assertEqual((0, 100, 100),
|
||||
color_util.color_RGB_to_hsv(255, 0, 0))
|
||||
assert (0, 100, 100) == \
|
||||
color_util.color_RGB_to_hsv(255, 0, 0)
|
||||
|
||||
def test_color_hsv_to_RGB(self):
|
||||
"""Test color_hsv_to_RGB."""
|
||||
self.assertEqual((0, 0, 0),
|
||||
color_util.color_hsv_to_RGB(0, 0, 0))
|
||||
assert (0, 0, 0) == \
|
||||
color_util.color_hsv_to_RGB(0, 0, 0)
|
||||
|
||||
self.assertEqual((255, 255, 255),
|
||||
color_util.color_hsv_to_RGB(0, 0, 100))
|
||||
assert (255, 255, 255) == \
|
||||
color_util.color_hsv_to_RGB(0, 0, 100)
|
||||
|
||||
self.assertEqual((0, 0, 255),
|
||||
color_util.color_hsv_to_RGB(240, 100, 100))
|
||||
assert (0, 0, 255) == \
|
||||
color_util.color_hsv_to_RGB(240, 100, 100)
|
||||
|
||||
self.assertEqual((0, 255, 0),
|
||||
color_util.color_hsv_to_RGB(120, 100, 100))
|
||||
assert (0, 255, 0) == \
|
||||
color_util.color_hsv_to_RGB(120, 100, 100)
|
||||
|
||||
self.assertEqual((255, 0, 0),
|
||||
color_util.color_hsv_to_RGB(0, 100, 100))
|
||||
assert (255, 0, 0) == \
|
||||
color_util.color_hsv_to_RGB(0, 100, 100)
|
||||
|
||||
def test_color_hsb_to_RGB(self):
|
||||
"""Test color_hsb_to_RGB."""
|
||||
self.assertEqual((0, 0, 0),
|
||||
color_util.color_hsb_to_RGB(0, 0, 0))
|
||||
assert (0, 0, 0) == \
|
||||
color_util.color_hsb_to_RGB(0, 0, 0)
|
||||
|
||||
self.assertEqual((255, 255, 255),
|
||||
color_util.color_hsb_to_RGB(0, 0, 1.0))
|
||||
assert (255, 255, 255) == \
|
||||
color_util.color_hsb_to_RGB(0, 0, 1.0)
|
||||
|
||||
self.assertEqual((0, 0, 255),
|
||||
color_util.color_hsb_to_RGB(240, 1.0, 1.0))
|
||||
assert (0, 0, 255) == \
|
||||
color_util.color_hsb_to_RGB(240, 1.0, 1.0)
|
||||
|
||||
self.assertEqual((0, 255, 0),
|
||||
color_util.color_hsb_to_RGB(120, 1.0, 1.0))
|
||||
assert (0, 255, 0) == \
|
||||
color_util.color_hsb_to_RGB(120, 1.0, 1.0)
|
||||
|
||||
self.assertEqual((255, 0, 0),
|
||||
color_util.color_hsb_to_RGB(0, 1.0, 1.0))
|
||||
assert (255, 0, 0) == \
|
||||
color_util.color_hsb_to_RGB(0, 1.0, 1.0)
|
||||
|
||||
def test_color_xy_to_hs(self):
|
||||
"""Test color_xy_to_hs."""
|
||||
self.assertEqual((47.294, 100),
|
||||
color_util.color_xy_to_hs(1, 1))
|
||||
assert (47.294, 100) == \
|
||||
color_util.color_xy_to_hs(1, 1)
|
||||
|
||||
self.assertEqual((38.182, 12.941),
|
||||
color_util.color_xy_to_hs(.35, .35))
|
||||
assert (38.182, 12.941) == \
|
||||
color_util.color_xy_to_hs(.35, .35)
|
||||
|
||||
self.assertEqual((345.882, 100),
|
||||
color_util.color_xy_to_hs(1, 0))
|
||||
assert (345.882, 100) == \
|
||||
color_util.color_xy_to_hs(1, 0)
|
||||
|
||||
self.assertEqual((120, 100),
|
||||
color_util.color_xy_to_hs(0, 1))
|
||||
assert (120, 100) == \
|
||||
color_util.color_xy_to_hs(0, 1)
|
||||
|
||||
self.assertEqual((225.176, 100),
|
||||
color_util.color_xy_to_hs(0, 0))
|
||||
assert (225.176, 100) == \
|
||||
color_util.color_xy_to_hs(0, 0)
|
||||
|
||||
def test_color_hs_to_xy(self):
|
||||
"""Test color_hs_to_xy."""
|
||||
self.assertEqual((0.151, 0.343),
|
||||
color_util.color_hs_to_xy(180, 100))
|
||||
assert (0.151, 0.343) == \
|
||||
color_util.color_hs_to_xy(180, 100)
|
||||
|
||||
self.assertEqual((0.356, 0.321),
|
||||
color_util.color_hs_to_xy(350, 12.5))
|
||||
assert (0.356, 0.321) == \
|
||||
color_util.color_hs_to_xy(350, 12.5)
|
||||
|
||||
self.assertEqual((0.229, 0.474),
|
||||
color_util.color_hs_to_xy(140, 50))
|
||||
assert (0.229, 0.474) == \
|
||||
color_util.color_hs_to_xy(140, 50)
|
||||
|
||||
self.assertEqual((0.474, 0.317),
|
||||
color_util.color_hs_to_xy(0, 40))
|
||||
assert (0.474, 0.317) == \
|
||||
color_util.color_hs_to_xy(0, 40)
|
||||
|
||||
self.assertEqual((0.323, 0.329),
|
||||
color_util.color_hs_to_xy(360, 0))
|
||||
assert (0.323, 0.329) == \
|
||||
color_util.color_hs_to_xy(360, 0)
|
||||
|
||||
def test_rgb_hex_to_rgb_list(self):
|
||||
"""Test rgb_hex_to_rgb_list."""
|
||||
self.assertEqual([255, 255, 255],
|
||||
color_util.rgb_hex_to_rgb_list('ffffff'))
|
||||
assert [255, 255, 255] == \
|
||||
color_util.rgb_hex_to_rgb_list('ffffff')
|
||||
|
||||
self.assertEqual([0, 0, 0],
|
||||
color_util.rgb_hex_to_rgb_list('000000'))
|
||||
assert [0, 0, 0] == \
|
||||
color_util.rgb_hex_to_rgb_list('000000')
|
||||
|
||||
self.assertEqual([255, 255, 255, 255],
|
||||
color_util.rgb_hex_to_rgb_list('ffffffff'))
|
||||
assert [255, 255, 255, 255] == \
|
||||
color_util.rgb_hex_to_rgb_list('ffffffff')
|
||||
|
||||
self.assertEqual([0, 0, 0, 0],
|
||||
color_util.rgb_hex_to_rgb_list('00000000'))
|
||||
assert [0, 0, 0, 0] == \
|
||||
color_util.rgb_hex_to_rgb_list('00000000')
|
||||
|
||||
self.assertEqual([51, 153, 255],
|
||||
color_util.rgb_hex_to_rgb_list('3399ff'))
|
||||
assert [51, 153, 255] == \
|
||||
color_util.rgb_hex_to_rgb_list('3399ff')
|
||||
|
||||
self.assertEqual([51, 153, 255, 0],
|
||||
color_util.rgb_hex_to_rgb_list('3399ff00'))
|
||||
assert [51, 153, 255, 0] == \
|
||||
color_util.rgb_hex_to_rgb_list('3399ff00')
|
||||
|
||||
def test_color_name_to_rgb_valid_name(self):
|
||||
"""Test color_name_to_rgb."""
|
||||
self.assertEqual((255, 0, 0),
|
||||
color_util.color_name_to_rgb('red'))
|
||||
assert (255, 0, 0) == \
|
||||
color_util.color_name_to_rgb('red')
|
||||
|
||||
self.assertEqual((0, 0, 255),
|
||||
color_util.color_name_to_rgb('blue'))
|
||||
assert (0, 0, 255) == \
|
||||
color_util.color_name_to_rgb('blue')
|
||||
|
||||
self.assertEqual((0, 128, 0),
|
||||
color_util.color_name_to_rgb('green'))
|
||||
assert (0, 128, 0) == \
|
||||
color_util.color_name_to_rgb('green')
|
||||
|
||||
# spaces in the name
|
||||
self.assertEqual((72, 61, 139),
|
||||
color_util.color_name_to_rgb('dark slate blue'))
|
||||
assert (72, 61, 139) == \
|
||||
color_util.color_name_to_rgb('dark slate blue')
|
||||
|
||||
# spaces removed from name
|
||||
self.assertEqual((72, 61, 139),
|
||||
color_util.color_name_to_rgb('darkslateblue'))
|
||||
self.assertEqual((72, 61, 139),
|
||||
color_util.color_name_to_rgb('dark slateblue'))
|
||||
self.assertEqual((72, 61, 139),
|
||||
color_util.color_name_to_rgb('darkslate blue'))
|
||||
assert (72, 61, 139) == \
|
||||
color_util.color_name_to_rgb('darkslateblue')
|
||||
assert (72, 61, 139) == \
|
||||
color_util.color_name_to_rgb('dark slateblue')
|
||||
assert (72, 61, 139) == \
|
||||
color_util.color_name_to_rgb('darkslate blue')
|
||||
|
||||
def test_color_name_to_rgb_unknown_name_raises_value_error(self):
|
||||
"""Test color_name_to_rgb."""
|
||||
@@ -217,55 +217,55 @@ class TestColorUtil(unittest.TestCase):
|
||||
|
||||
def test_color_rgb_to_rgbw(self):
|
||||
"""Test color_rgb_to_rgbw."""
|
||||
self.assertEqual((0, 0, 0, 0),
|
||||
color_util.color_rgb_to_rgbw(0, 0, 0))
|
||||
assert (0, 0, 0, 0) == \
|
||||
color_util.color_rgb_to_rgbw(0, 0, 0)
|
||||
|
||||
self.assertEqual((0, 0, 0, 255),
|
||||
color_util.color_rgb_to_rgbw(255, 255, 255))
|
||||
assert (0, 0, 0, 255) == \
|
||||
color_util.color_rgb_to_rgbw(255, 255, 255)
|
||||
|
||||
self.assertEqual((255, 0, 0, 0),
|
||||
color_util.color_rgb_to_rgbw(255, 0, 0))
|
||||
assert (255, 0, 0, 0) == \
|
||||
color_util.color_rgb_to_rgbw(255, 0, 0)
|
||||
|
||||
self.assertEqual((0, 255, 0, 0),
|
||||
color_util.color_rgb_to_rgbw(0, 255, 0))
|
||||
assert (0, 255, 0, 0) == \
|
||||
color_util.color_rgb_to_rgbw(0, 255, 0)
|
||||
|
||||
self.assertEqual((0, 0, 255, 0),
|
||||
color_util.color_rgb_to_rgbw(0, 0, 255))
|
||||
assert (0, 0, 255, 0) == \
|
||||
color_util.color_rgb_to_rgbw(0, 0, 255)
|
||||
|
||||
self.assertEqual((255, 127, 0, 0),
|
||||
color_util.color_rgb_to_rgbw(255, 127, 0))
|
||||
assert (255, 127, 0, 0) == \
|
||||
color_util.color_rgb_to_rgbw(255, 127, 0)
|
||||
|
||||
self.assertEqual((255, 0, 0, 253),
|
||||
color_util.color_rgb_to_rgbw(255, 127, 127))
|
||||
assert (255, 0, 0, 253) == \
|
||||
color_util.color_rgb_to_rgbw(255, 127, 127)
|
||||
|
||||
self.assertEqual((0, 0, 0, 127),
|
||||
color_util.color_rgb_to_rgbw(127, 127, 127))
|
||||
assert (0, 0, 0, 127) == \
|
||||
color_util.color_rgb_to_rgbw(127, 127, 127)
|
||||
|
||||
def test_color_rgbw_to_rgb(self):
|
||||
"""Test color_rgbw_to_rgb."""
|
||||
self.assertEqual((0, 0, 0),
|
||||
color_util.color_rgbw_to_rgb(0, 0, 0, 0))
|
||||
assert (0, 0, 0) == \
|
||||
color_util.color_rgbw_to_rgb(0, 0, 0, 0)
|
||||
|
||||
self.assertEqual((255, 255, 255),
|
||||
color_util.color_rgbw_to_rgb(0, 0, 0, 255))
|
||||
assert (255, 255, 255) == \
|
||||
color_util.color_rgbw_to_rgb(0, 0, 0, 255)
|
||||
|
||||
self.assertEqual((255, 0, 0),
|
||||
color_util.color_rgbw_to_rgb(255, 0, 0, 0))
|
||||
assert (255, 0, 0) == \
|
||||
color_util.color_rgbw_to_rgb(255, 0, 0, 0)
|
||||
|
||||
self.assertEqual((0, 255, 0),
|
||||
color_util.color_rgbw_to_rgb(0, 255, 0, 0))
|
||||
assert (0, 255, 0) == \
|
||||
color_util.color_rgbw_to_rgb(0, 255, 0, 0)
|
||||
|
||||
self.assertEqual((0, 0, 255),
|
||||
color_util.color_rgbw_to_rgb(0, 0, 255, 0))
|
||||
assert (0, 0, 255) == \
|
||||
color_util.color_rgbw_to_rgb(0, 0, 255, 0)
|
||||
|
||||
self.assertEqual((255, 127, 0),
|
||||
color_util.color_rgbw_to_rgb(255, 127, 0, 0))
|
||||
assert (255, 127, 0) == \
|
||||
color_util.color_rgbw_to_rgb(255, 127, 0, 0)
|
||||
|
||||
self.assertEqual((255, 127, 127),
|
||||
color_util.color_rgbw_to_rgb(255, 0, 0, 253))
|
||||
assert (255, 127, 127) == \
|
||||
color_util.color_rgbw_to_rgb(255, 0, 0, 253)
|
||||
|
||||
self.assertEqual((127, 127, 127),
|
||||
color_util.color_rgbw_to_rgb(0, 0, 0, 127))
|
||||
assert (127, 127, 127) == \
|
||||
color_util.color_rgbw_to_rgb(0, 0, 0, 127)
|
||||
|
||||
def test_color_rgb_to_hex(self):
|
||||
"""Test color_rgb_to_hex."""
|
||||
@@ -281,12 +281,12 @@ class ColorTemperatureMiredToKelvinTests(unittest.TestCase):
|
||||
def test_should_return_25000_kelvin_when_input_is_40_mired(self):
|
||||
"""Function should return 25000K if given 40 mired."""
|
||||
kelvin = color_util.color_temperature_mired_to_kelvin(40)
|
||||
self.assertEqual(25000, kelvin)
|
||||
assert 25000 == kelvin
|
||||
|
||||
def test_should_return_5000_kelvin_when_input_is_200_mired(self):
|
||||
"""Function should return 5000K if given 200 mired."""
|
||||
kelvin = color_util.color_temperature_mired_to_kelvin(200)
|
||||
self.assertEqual(5000, kelvin)
|
||||
assert 5000 == kelvin
|
||||
|
||||
|
||||
class ColorTemperatureKelvinToMiredTests(unittest.TestCase):
|
||||
@@ -295,12 +295,12 @@ class ColorTemperatureKelvinToMiredTests(unittest.TestCase):
|
||||
def test_should_return_40_mired_when_input_is_25000_kelvin(self):
|
||||
"""Function should return 40 mired when given 25000 Kelvin."""
|
||||
mired = color_util.color_temperature_kelvin_to_mired(25000)
|
||||
self.assertEqual(40, mired)
|
||||
assert 40 == mired
|
||||
|
||||
def test_should_return_200_mired_when_input_is_5000_kelvin(self):
|
||||
"""Function should return 200 mired when given 5000 Kelvin."""
|
||||
mired = color_util.color_temperature_kelvin_to_mired(5000)
|
||||
self.assertEqual(200, mired)
|
||||
assert 200 == mired
|
||||
|
||||
|
||||
class ColorTemperatureToRGB(unittest.TestCase):
|
||||
@@ -310,13 +310,13 @@ class ColorTemperatureToRGB(unittest.TestCase):
|
||||
"""Function should return same value for 999 Kelvin and 0 Kelvin."""
|
||||
rgb_1 = color_util.color_temperature_to_rgb(999)
|
||||
rgb_2 = color_util.color_temperature_to_rgb(0)
|
||||
self.assertEqual(rgb_1, rgb_2)
|
||||
assert rgb_1 == rgb_2
|
||||
|
||||
def test_returns_same_value_for_any_two_temperatures_above_40000(self):
|
||||
"""Function should return same value for 40001K and 999999K."""
|
||||
rgb_1 = color_util.color_temperature_to_rgb(40001)
|
||||
rgb_2 = color_util.color_temperature_to_rgb(999999)
|
||||
self.assertEqual(rgb_1, rgb_2)
|
||||
assert rgb_1 == rgb_2
|
||||
|
||||
def test_should_return_pure_white_at_6600(self):
|
||||
"""
|
||||
@@ -327,19 +327,19 @@ class ColorTemperatureToRGB(unittest.TestCase):
|
||||
guess" approach.
|
||||
"""
|
||||
rgb = color_util.color_temperature_to_rgb(6600)
|
||||
self.assertEqual((255, 255, 255), rgb)
|
||||
assert (255, 255, 255) == rgb
|
||||
|
||||
def test_color_above_6600_should_have_more_blue_than_red_or_green(self):
|
||||
"""Function should return a higher blue value for blue-ish light."""
|
||||
rgb = color_util.color_temperature_to_rgb(6700)
|
||||
self.assertGreater(rgb[2], rgb[1])
|
||||
self.assertGreater(rgb[2], rgb[0])
|
||||
assert rgb[2] > rgb[1]
|
||||
assert rgb[2] > rgb[0]
|
||||
|
||||
def test_color_below_6600_should_have_more_red_than_blue_or_green(self):
|
||||
"""Function should return a higher red value for red-ish light."""
|
||||
rgb = color_util.color_temperature_to_rgb(6500)
|
||||
self.assertGreater(rgb[0], rgb[1])
|
||||
self.assertGreater(rgb[0], rgb[2])
|
||||
assert rgb[0] > rgb[1]
|
||||
assert rgb[0] > rgb[2]
|
||||
|
||||
|
||||
def test_get_color_in_voluptuous():
|
||||
|
||||
+35
-47
@@ -4,6 +4,7 @@ import unittest
|
||||
import homeassistant.util.distance as distance_util
|
||||
from homeassistant.const import (LENGTH_KILOMETERS, LENGTH_METERS, LENGTH_FEET,
|
||||
LENGTH_MILES)
|
||||
import pytest
|
||||
|
||||
INVALID_SYMBOL = 'bob'
|
||||
VALID_SYMBOL = LENGTH_KILOMETERS
|
||||
@@ -14,78 +15,65 @@ class TestDistanceUtil(unittest.TestCase):
|
||||
|
||||
def test_convert_same_unit(self):
|
||||
"""Test conversion from any unit to same unit."""
|
||||
self.assertEqual(5,
|
||||
distance_util.convert(5, LENGTH_KILOMETERS,
|
||||
LENGTH_KILOMETERS))
|
||||
self.assertEqual(2,
|
||||
distance_util.convert(2, LENGTH_METERS,
|
||||
LENGTH_METERS))
|
||||
self.assertEqual(10,
|
||||
distance_util.convert(10, LENGTH_MILES, LENGTH_MILES))
|
||||
self.assertEqual(9,
|
||||
distance_util.convert(9, LENGTH_FEET, LENGTH_FEET))
|
||||
assert 5 == distance_util.convert(5, LENGTH_KILOMETERS,
|
||||
LENGTH_KILOMETERS)
|
||||
assert 2 == distance_util.convert(2, LENGTH_METERS,
|
||||
LENGTH_METERS)
|
||||
assert 10 == distance_util.convert(10, LENGTH_MILES, LENGTH_MILES)
|
||||
assert 9 == distance_util.convert(9, LENGTH_FEET, LENGTH_FEET)
|
||||
|
||||
def test_convert_invalid_unit(self):
|
||||
"""Test exception is thrown for invalid units."""
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
distance_util.convert(5, INVALID_SYMBOL,
|
||||
VALID_SYMBOL)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
distance_util.convert(5, VALID_SYMBOL,
|
||||
INVALID_SYMBOL)
|
||||
|
||||
def test_convert_nonnumeric_value(self):
|
||||
"""Test exception is thrown for nonnumeric type."""
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
distance_util.convert('a', LENGTH_KILOMETERS, LENGTH_METERS)
|
||||
|
||||
def test_convert_from_miles(self):
|
||||
"""Test conversion from miles to other units."""
|
||||
miles = 5
|
||||
self.assertEqual(
|
||||
distance_util.convert(miles, LENGTH_MILES, LENGTH_KILOMETERS),
|
||||
8.04672)
|
||||
self.assertEqual(
|
||||
distance_util.convert(miles, LENGTH_MILES, LENGTH_METERS),
|
||||
8046.72)
|
||||
self.assertEqual(
|
||||
distance_util.convert(miles, LENGTH_MILES, LENGTH_FEET),
|
||||
26400.0008448)
|
||||
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_FEET) == \
|
||||
26400.0008448
|
||||
|
||||
def test_convert_from_feet(self):
|
||||
"""Test conversion from feet to other units."""
|
||||
feet = 5000
|
||||
self.assertEqual(
|
||||
distance_util.convert(feet, LENGTH_FEET, LENGTH_KILOMETERS),
|
||||
1.524)
|
||||
self.assertEqual(
|
||||
distance_util.convert(feet, LENGTH_FEET, LENGTH_METERS),
|
||||
1524)
|
||||
self.assertEqual(
|
||||
distance_util.convert(feet, LENGTH_FEET, LENGTH_MILES),
|
||||
0.9469694040000001)
|
||||
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_MILES) == \
|
||||
0.9469694040000001
|
||||
|
||||
def test_convert_from_kilometers(self):
|
||||
"""Test conversion from kilometers to other units."""
|
||||
km = 5
|
||||
self.assertEqual(
|
||||
distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_FEET),
|
||||
16404.2)
|
||||
self.assertEqual(
|
||||
distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_METERS),
|
||||
5000)
|
||||
self.assertEqual(
|
||||
distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_MILES),
|
||||
3.106855)
|
||||
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_FEET) == \
|
||||
16404.2
|
||||
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_METERS) == \
|
||||
5000
|
||||
assert distance_util.convert(km, LENGTH_KILOMETERS, LENGTH_MILES) == \
|
||||
3.106855
|
||||
|
||||
def test_convert_from_meters(self):
|
||||
"""Test conversion from meters to other units."""
|
||||
m = 5000
|
||||
self.assertEqual(distance_util.convert(m, LENGTH_METERS, LENGTH_FEET),
|
||||
16404.2)
|
||||
self.assertEqual(
|
||||
distance_util.convert(m, LENGTH_METERS, LENGTH_KILOMETERS),
|
||||
5)
|
||||
self.assertEqual(distance_util.convert(m, LENGTH_METERS, LENGTH_MILES),
|
||||
3.106855)
|
||||
assert distance_util.convert(m, LENGTH_METERS, LENGTH_FEET) == \
|
||||
16404.2
|
||||
assert distance_util.convert(m, LENGTH_METERS, LENGTH_KILOMETERS) == \
|
||||
5
|
||||
assert distance_util.convert(m, LENGTH_METERS, LENGTH_MILES) == \
|
||||
3.106855
|
||||
+57
-95
@@ -3,6 +3,7 @@ import unittest
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
import pytest
|
||||
|
||||
TEST_TIME_ZONE = 'America/Los_Angeles'
|
||||
|
||||
@@ -22,14 +23,14 @@ class TestDateUtil(unittest.TestCase):
|
||||
"""Test getting a time zone."""
|
||||
time_zone = dt_util.get_time_zone(TEST_TIME_ZONE)
|
||||
|
||||
self.assertIsNotNone(time_zone)
|
||||
self.assertEqual(TEST_TIME_ZONE, time_zone.zone)
|
||||
assert time_zone is not None
|
||||
assert TEST_TIME_ZONE == time_zone.zone
|
||||
|
||||
def test_get_time_zone_returns_none_for_garbage_time_zone(self):
|
||||
"""Test getting a non existing time zone."""
|
||||
time_zone = dt_util.get_time_zone("Non existing time zone")
|
||||
|
||||
self.assertIsNone(time_zone)
|
||||
assert time_zone is None
|
||||
|
||||
def test_set_default_time_zone(self):
|
||||
"""Test setting default time zone."""
|
||||
@@ -38,36 +39,34 @@ class TestDateUtil(unittest.TestCase):
|
||||
dt_util.set_default_time_zone(time_zone)
|
||||
|
||||
# We cannot compare the timezones directly because of DST
|
||||
self.assertEqual(time_zone.zone, dt_util.now().tzinfo.zone)
|
||||
assert time_zone.zone == dt_util.now().tzinfo.zone
|
||||
|
||||
def test_utcnow(self):
|
||||
"""Test the UTC now method."""
|
||||
self.assertAlmostEqual(
|
||||
dt_util.utcnow().replace(tzinfo=None),
|
||||
datetime.utcnow(),
|
||||
delta=timedelta(seconds=1))
|
||||
assert abs(dt_util.utcnow().replace(tzinfo=None)-datetime.utcnow()) < \
|
||||
timedelta(seconds=1)
|
||||
|
||||
def test_now(self):
|
||||
"""Test the now method."""
|
||||
dt_util.set_default_time_zone(dt_util.get_time_zone(TEST_TIME_ZONE))
|
||||
|
||||
self.assertAlmostEqual(
|
||||
dt_util.as_utc(dt_util.now()).replace(tzinfo=None),
|
||||
datetime.utcnow(),
|
||||
delta=timedelta(seconds=1))
|
||||
assert abs(
|
||||
dt_util.as_utc(dt_util.now()).replace(
|
||||
tzinfo=None
|
||||
) - datetime.utcnow()
|
||||
) < timedelta(seconds=1)
|
||||
|
||||
def test_as_utc_with_naive_object(self):
|
||||
"""Test the now method."""
|
||||
utcnow = datetime.utcnow()
|
||||
|
||||
self.assertEqual(utcnow,
|
||||
dt_util.as_utc(utcnow).replace(tzinfo=None))
|
||||
assert utcnow == dt_util.as_utc(utcnow).replace(tzinfo=None)
|
||||
|
||||
def test_as_utc_with_utc_object(self):
|
||||
"""Test UTC time with UTC object."""
|
||||
utcnow = dt_util.utcnow()
|
||||
|
||||
self.assertEqual(utcnow, dt_util.as_utc(utcnow))
|
||||
assert utcnow == dt_util.as_utc(utcnow)
|
||||
|
||||
def test_as_utc_with_local_object(self):
|
||||
"""Test the UTC time with local object."""
|
||||
@@ -75,20 +74,19 @@ class TestDateUtil(unittest.TestCase):
|
||||
localnow = dt_util.now()
|
||||
utcnow = dt_util.as_utc(localnow)
|
||||
|
||||
self.assertEqual(localnow, utcnow)
|
||||
self.assertNotEqual(localnow.tzinfo, utcnow.tzinfo)
|
||||
assert localnow == utcnow
|
||||
assert localnow.tzinfo != utcnow.tzinfo
|
||||
|
||||
def test_as_local_with_naive_object(self):
|
||||
"""Test local time with native object."""
|
||||
now = dt_util.now()
|
||||
self.assertAlmostEqual(
|
||||
now, dt_util.as_local(datetime.utcnow()),
|
||||
delta=timedelta(seconds=1))
|
||||
assert abs(now-dt_util.as_local(datetime.utcnow())) < \
|
||||
timedelta(seconds=1)
|
||||
|
||||
def test_as_local_with_local_object(self):
|
||||
"""Test local with local object."""
|
||||
now = dt_util.now()
|
||||
self.assertEqual(now, now)
|
||||
assert now == now
|
||||
|
||||
def test_as_local_with_utc_object(self):
|
||||
"""Test local time with UTC object."""
|
||||
@@ -97,27 +95,26 @@ class TestDateUtil(unittest.TestCase):
|
||||
utcnow = dt_util.utcnow()
|
||||
localnow = dt_util.as_local(utcnow)
|
||||
|
||||
self.assertEqual(localnow, utcnow)
|
||||
self.assertNotEqual(localnow.tzinfo, utcnow.tzinfo)
|
||||
assert localnow == utcnow
|
||||
assert localnow.tzinfo != utcnow.tzinfo
|
||||
|
||||
def test_utc_from_timestamp(self):
|
||||
"""Test utc_from_timestamp method."""
|
||||
self.assertEqual(
|
||||
datetime(1986, 7, 9, tzinfo=dt_util.UTC),
|
||||
dt_util.utc_from_timestamp(521251200))
|
||||
assert datetime(1986, 7, 9, tzinfo=dt_util.UTC) == \
|
||||
dt_util.utc_from_timestamp(521251200)
|
||||
|
||||
def test_as_timestamp(self):
|
||||
"""Test as_timestamp method."""
|
||||
ts = 1462401234
|
||||
utc_dt = dt_util.utc_from_timestamp(ts)
|
||||
self.assertEqual(ts, dt_util.as_timestamp(utc_dt))
|
||||
assert ts == dt_util.as_timestamp(utc_dt)
|
||||
utc_iso = utc_dt.isoformat()
|
||||
self.assertEqual(ts, dt_util.as_timestamp(utc_iso))
|
||||
assert ts == dt_util.as_timestamp(utc_iso)
|
||||
|
||||
# confirm the ability to handle a string passed in
|
||||
delta = dt_util.as_timestamp("2016-01-01 12:12:12")
|
||||
delta -= dt_util.as_timestamp("2016-01-01 12:12:11")
|
||||
self.assertEqual(1, delta)
|
||||
assert 1 == delta
|
||||
|
||||
def test_parse_datetime_converts_correctly(self):
|
||||
"""Test parse_datetime converts strings."""
|
||||
@@ -131,72 +128,61 @@ class TestDateUtil(unittest.TestCase):
|
||||
|
||||
def test_parse_datetime_returns_none_for_incorrect_format(self):
|
||||
"""Test parse_datetime returns None if incorrect format."""
|
||||
self.assertIsNone(dt_util.parse_datetime("not a datetime string"))
|
||||
assert dt_util.parse_datetime("not a datetime string") is None
|
||||
|
||||
def test_get_age(self):
|
||||
"""Test get_age."""
|
||||
diff = dt_util.now() - timedelta(seconds=0)
|
||||
self.assertEqual(dt_util.get_age(diff), "0 seconds")
|
||||
assert dt_util.get_age(diff) == "0 seconds"
|
||||
|
||||
diff = dt_util.now() - timedelta(seconds=1)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 second")
|
||||
assert dt_util.get_age(diff) == "1 second"
|
||||
|
||||
diff = dt_util.now() - timedelta(seconds=30)
|
||||
self.assertEqual(dt_util.get_age(diff), "30 seconds")
|
||||
assert dt_util.get_age(diff) == "30 seconds"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=5)
|
||||
self.assertEqual(dt_util.get_age(diff), "5 minutes")
|
||||
assert dt_util.get_age(diff) == "5 minutes"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=1)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 minute")
|
||||
assert dt_util.get_age(diff) == "1 minute"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=300)
|
||||
self.assertEqual(dt_util.get_age(diff), "5 hours")
|
||||
assert dt_util.get_age(diff) == "5 hours"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=320)
|
||||
self.assertEqual(dt_util.get_age(diff), "5 hours")
|
||||
assert dt_util.get_age(diff) == "5 hours"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=2*60*24)
|
||||
self.assertEqual(dt_util.get_age(diff), "2 days")
|
||||
assert dt_util.get_age(diff) == "2 days"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=32*60*24)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 month")
|
||||
assert dt_util.get_age(diff) == "1 month"
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=365*60*24)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 year")
|
||||
assert dt_util.get_age(diff) == "1 year"
|
||||
|
||||
def test_parse_time_expression(self):
|
||||
"""Test parse_time_expression."""
|
||||
self.assertEqual(
|
||||
[x for x in range(60)],
|
||||
assert [x for x in range(60)] == \
|
||||
dt_util.parse_time_expression('*', 0, 59)
|
||||
)
|
||||
self.assertEqual(
|
||||
[x for x in range(60)],
|
||||
assert [x for x in range(60)] == \
|
||||
dt_util.parse_time_expression(None, 0, 59)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[x for x in range(0, 60, 5)],
|
||||
assert [x for x in range(0, 60, 5)] == \
|
||||
dt_util.parse_time_expression('/5', 0, 59)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[1, 2, 3],
|
||||
assert [1, 2, 3] == \
|
||||
dt_util.parse_time_expression([2, 1, 3], 0, 59)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[x for x in range(24)],
|
||||
assert [x for x in range(24)] == \
|
||||
dt_util.parse_time_expression('*', 0, 23)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[42],
|
||||
assert [42] == \
|
||||
dt_util.parse_time_expression(42, 0, 59)
|
||||
)
|
||||
|
||||
self.assertRaises(ValueError, dt_util.parse_time_expression, 61, 0, 60)
|
||||
with pytest.raises(ValueError):
|
||||
dt_util.parse_time_expression(61, 0, 60)
|
||||
|
||||
def test_find_next_time_expression_time_basic(self):
|
||||
"""Test basic stuff for find_next_time_expression_time."""
|
||||
@@ -209,25 +195,17 @@ class TestDateUtil(unittest.TestCase):
|
||||
return dt_util.find_next_time_expression_time(
|
||||
dt, seconds, minutes, hours)
|
||||
|
||||
self.assertEqual(
|
||||
datetime(2018, 10, 7, 10, 30, 0),
|
||||
assert datetime(2018, 10, 7, 10, 30, 0) == \
|
||||
find(datetime(2018, 10, 7, 10, 20, 0), '*', '/30', 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
datetime(2018, 10, 7, 10, 30, 0),
|
||||
assert datetime(2018, 10, 7, 10, 30, 0) == \
|
||||
find(datetime(2018, 10, 7, 10, 30, 0), '*', '/30', 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
datetime(2018, 10, 7, 12, 30, 30),
|
||||
assert datetime(2018, 10, 7, 12, 30, 30) == \
|
||||
find(datetime(2018, 10, 7, 10, 30, 0), '/3', '/30', [30, 45])
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
datetime(2018, 10, 8, 5, 0, 0),
|
||||
assert datetime(2018, 10, 8, 5, 0, 0) == \
|
||||
find(datetime(2018, 10, 7, 10, 30, 0), 5, 0, 0)
|
||||
)
|
||||
|
||||
def test_find_next_time_expression_time_dst(self):
|
||||
"""Test daylight saving time for find_next_time_expression_time."""
|
||||
@@ -244,48 +222,32 @@ class TestDateUtil(unittest.TestCase):
|
||||
dt, seconds, minutes, hours)
|
||||
|
||||
# Entering DST, clocks are rolled forward
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 3, 26, 2, 30, 0)),
|
||||
assert tz.localize(datetime(2018, 3, 26, 2, 30, 0)) == \
|
||||
find(tz.localize(datetime(2018, 3, 25, 1, 50, 0)), 2, 30, 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 3, 26, 2, 30, 0)),
|
||||
assert tz.localize(datetime(2018, 3, 26, 2, 30, 0)) == \
|
||||
find(tz.localize(datetime(2018, 3, 25, 3, 50, 0)), 2, 30, 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 3, 26, 2, 30, 0)),
|
||||
assert tz.localize(datetime(2018, 3, 26, 2, 30, 0)) == \
|
||||
find(tz.localize(datetime(2018, 3, 26, 1, 50, 0)), 2, 30, 0)
|
||||
)
|
||||
|
||||
# Leaving DST, clocks are rolled back
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 10, 28, 2, 30, 0), is_dst=False),
|
||||
assert tz.localize(datetime(2018, 10, 28, 2, 30, 0), is_dst=False) == \
|
||||
find(tz.localize(datetime(2018, 10, 28, 2, 5, 0), is_dst=False),
|
||||
2, 30, 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 10, 28, 2, 30, 0), is_dst=False),
|
||||
assert tz.localize(datetime(2018, 10, 28, 2, 30, 0), is_dst=False) == \
|
||||
find(tz.localize(datetime(2018, 10, 28, 2, 55, 0), is_dst=True),
|
||||
2, 30, 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 10, 28, 4, 30, 0), is_dst=False),
|
||||
assert tz.localize(datetime(2018, 10, 28, 4, 30, 0), is_dst=False) == \
|
||||
find(tz.localize(datetime(2018, 10, 28, 2, 55, 0), is_dst=True),
|
||||
4, 30, 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 10, 28, 2, 30, 0), is_dst=True),
|
||||
assert tz.localize(datetime(2018, 10, 28, 2, 30, 0), is_dst=True) == \
|
||||
find(tz.localize(datetime(2018, 10, 28, 2, 5, 0), is_dst=True),
|
||||
2, 30, 0)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
tz.localize(datetime(2018, 10, 29, 2, 30, 0)),
|
||||
assert tz.localize(datetime(2018, 10, 29, 2, 30, 0)) == \
|
||||
find(tz.localize(datetime(2018, 10, 28, 2, 55, 0), is_dst=False),
|
||||
2, 30, 0)
|
||||
)
|
||||
+107
-110
@@ -5,6 +5,7 @@ from datetime import datetime, timedelta
|
||||
|
||||
from homeassistant import util
|
||||
import homeassistant.util.dt as dt_util
|
||||
import pytest
|
||||
|
||||
|
||||
class TestUtil(unittest.TestCase):
|
||||
@@ -12,60 +13,56 @@ class TestUtil(unittest.TestCase):
|
||||
|
||||
def test_sanitize_filename(self):
|
||||
"""Test sanitize_filename."""
|
||||
self.assertEqual("test", util.sanitize_filename("test"))
|
||||
self.assertEqual("test", util.sanitize_filename("/test"))
|
||||
self.assertEqual("test", util.sanitize_filename("..test"))
|
||||
self.assertEqual("test", util.sanitize_filename("\\test"))
|
||||
self.assertEqual("test", util.sanitize_filename("\\../test"))
|
||||
assert "test" == util.sanitize_filename("test")
|
||||
assert "test" == util.sanitize_filename("/test")
|
||||
assert "test" == util.sanitize_filename("..test")
|
||||
assert "test" == util.sanitize_filename("\\test")
|
||||
assert "test" == util.sanitize_filename("\\../test")
|
||||
|
||||
def test_sanitize_path(self):
|
||||
"""Test sanitize_path."""
|
||||
self.assertEqual("test/path", util.sanitize_path("test/path"))
|
||||
self.assertEqual("test/path", util.sanitize_path("~test/path"))
|
||||
self.assertEqual("//test/path",
|
||||
util.sanitize_path("~/../test/path"))
|
||||
assert "test/path" == util.sanitize_path("test/path")
|
||||
assert "test/path" == util.sanitize_path("~test/path")
|
||||
assert "//test/path" == util.sanitize_path("~/../test/path")
|
||||
|
||||
def test_slugify(self):
|
||||
"""Test slugify."""
|
||||
self.assertEqual("test", util.slugify("T-!@#$!#@$!$est"))
|
||||
self.assertEqual("test_more", util.slugify("Test More"))
|
||||
self.assertEqual("test_more", util.slugify("Test_(More)"))
|
||||
self.assertEqual("test_more", util.slugify("Tèst_Mörê"))
|
||||
self.assertEqual("b827eb000000", util.slugify("B8:27:EB:00:00:00"))
|
||||
self.assertEqual("testcom", util.slugify("test.com"))
|
||||
self.assertEqual("greg_phone__exp_wayp1",
|
||||
util.slugify("greg_phone - exp_wayp1"))
|
||||
self.assertEqual("we_are_we_are_a_test_calendar",
|
||||
util.slugify("We are, we are, a... Test Calendar"))
|
||||
self.assertEqual("test_aouss_aou", util.slugify("Tèst_äöüß_ÄÖÜ"))
|
||||
assert "test" == util.slugify("T-!@#$!#@$!$est")
|
||||
assert "test_more" == util.slugify("Test More")
|
||||
assert "test_more" == util.slugify("Test_(More)")
|
||||
assert "test_more" == util.slugify("Tèst_Mörê")
|
||||
assert "b827eb000000" == util.slugify("B8:27:EB:00:00:00")
|
||||
assert "testcom" == util.slugify("test.com")
|
||||
assert "greg_phone__exp_wayp1" == \
|
||||
util.slugify("greg_phone - exp_wayp1")
|
||||
assert "we_are_we_are_a_test_calendar" == \
|
||||
util.slugify("We are, we are, a... Test Calendar")
|
||||
assert "test_aouss_aou" == util.slugify("Tèst_äöüß_ÄÖÜ")
|
||||
|
||||
def test_repr_helper(self):
|
||||
"""Test repr_helper."""
|
||||
self.assertEqual("A", util.repr_helper("A"))
|
||||
self.assertEqual("5", util.repr_helper(5))
|
||||
self.assertEqual("True", util.repr_helper(True))
|
||||
self.assertEqual("test=1",
|
||||
util.repr_helper({"test": 1}))
|
||||
self.assertEqual("1986-07-09T12:00:00+00:00",
|
||||
util.repr_helper(datetime(1986, 7, 9, 12, 0, 0)))
|
||||
assert "A" == util.repr_helper("A")
|
||||
assert "5" == util.repr_helper(5)
|
||||
assert "True" == util.repr_helper(True)
|
||||
assert "test=1" == util.repr_helper({"test": 1})
|
||||
assert "1986-07-09T12:00:00+00:00" == \
|
||||
util.repr_helper(datetime(1986, 7, 9, 12, 0, 0))
|
||||
|
||||
def test_convert(self):
|
||||
"""Test convert."""
|
||||
self.assertEqual(5, util.convert("5", int))
|
||||
self.assertEqual(5.0, util.convert("5", float))
|
||||
self.assertEqual(True, util.convert("True", bool))
|
||||
self.assertEqual(1, util.convert("NOT A NUMBER", int, 1))
|
||||
self.assertEqual(1, util.convert(None, int, 1))
|
||||
self.assertEqual(1, util.convert(object, int, 1))
|
||||
assert 5 == util.convert("5", int)
|
||||
assert 5.0 == util.convert("5", float)
|
||||
assert util.convert("True", bool) is True
|
||||
assert 1 == util.convert("NOT A NUMBER", int, 1)
|
||||
assert 1 == util.convert(None, int, 1)
|
||||
assert 1 == util.convert(object, int, 1)
|
||||
|
||||
def test_ensure_unique_string(self):
|
||||
"""Test ensure_unique_string."""
|
||||
self.assertEqual(
|
||||
"Beer_3",
|
||||
util.ensure_unique_string("Beer", ["Beer", "Beer_2"]))
|
||||
self.assertEqual(
|
||||
"Beer",
|
||||
util.ensure_unique_string("Beer", ["Wine", "Soda"]))
|
||||
assert "Beer_3" == \
|
||||
util.ensure_unique_string("Beer", ["Beer", "Beer_2"])
|
||||
assert "Beer" == \
|
||||
util.ensure_unique_string("Beer", ["Wine", "Soda"])
|
||||
|
||||
def test_ordered_enum(self):
|
||||
"""Test the ordered enum class."""
|
||||
@@ -76,96 +73,96 @@ class TestUtil(unittest.TestCase):
|
||||
SECOND = 2
|
||||
THIRD = 3
|
||||
|
||||
self.assertTrue(TestEnum.SECOND >= TestEnum.FIRST)
|
||||
self.assertTrue(TestEnum.SECOND >= TestEnum.SECOND)
|
||||
self.assertFalse(TestEnum.SECOND >= TestEnum.THIRD)
|
||||
assert TestEnum.SECOND >= TestEnum.FIRST
|
||||
assert TestEnum.SECOND >= TestEnum.SECOND
|
||||
assert not (TestEnum.SECOND >= TestEnum.THIRD)
|
||||
|
||||
self.assertTrue(TestEnum.SECOND > TestEnum.FIRST)
|
||||
self.assertFalse(TestEnum.SECOND > TestEnum.SECOND)
|
||||
self.assertFalse(TestEnum.SECOND > TestEnum.THIRD)
|
||||
assert TestEnum.SECOND > TestEnum.FIRST
|
||||
assert not (TestEnum.SECOND > TestEnum.SECOND)
|
||||
assert not (TestEnum.SECOND > TestEnum.THIRD)
|
||||
|
||||
self.assertFalse(TestEnum.SECOND <= TestEnum.FIRST)
|
||||
self.assertTrue(TestEnum.SECOND <= TestEnum.SECOND)
|
||||
self.assertTrue(TestEnum.SECOND <= TestEnum.THIRD)
|
||||
assert not (TestEnum.SECOND <= TestEnum.FIRST)
|
||||
assert TestEnum.SECOND <= TestEnum.SECOND
|
||||
assert TestEnum.SECOND <= TestEnum.THIRD
|
||||
|
||||
self.assertFalse(TestEnum.SECOND < TestEnum.FIRST)
|
||||
self.assertFalse(TestEnum.SECOND < TestEnum.SECOND)
|
||||
self.assertTrue(TestEnum.SECOND < TestEnum.THIRD)
|
||||
assert not (TestEnum.SECOND < TestEnum.FIRST)
|
||||
assert not (TestEnum.SECOND < TestEnum.SECOND)
|
||||
assert TestEnum.SECOND < TestEnum.THIRD
|
||||
|
||||
# Python will raise a TypeError if the <, <=, >, >= methods
|
||||
# raise a NotImplemented error.
|
||||
self.assertRaises(TypeError,
|
||||
lambda x, y: x < y, TestEnum.FIRST, 1)
|
||||
with pytest.raises(TypeError):
|
||||
TestEnum.FIRST < 1
|
||||
|
||||
self.assertRaises(TypeError,
|
||||
lambda x, y: x <= y, TestEnum.FIRST, 1)
|
||||
with pytest.raises(TypeError):
|
||||
TestEnum.FIRST <= 1
|
||||
|
||||
self.assertRaises(TypeError,
|
||||
lambda x, y: x > y, TestEnum.FIRST, 1)
|
||||
with pytest.raises(TypeError):
|
||||
TestEnum.FIRST > 1
|
||||
|
||||
self.assertRaises(TypeError,
|
||||
lambda x, y: x >= y, TestEnum.FIRST, 1)
|
||||
with pytest.raises(TypeError):
|
||||
TestEnum.FIRST >= 1
|
||||
|
||||
def test_ordered_set(self):
|
||||
"""Test ordering of set."""
|
||||
set1 = util.OrderedSet([1, 2, 3, 4])
|
||||
set2 = util.OrderedSet([3, 4, 5])
|
||||
|
||||
self.assertEqual(4, len(set1))
|
||||
self.assertEqual(3, len(set2))
|
||||
assert 4 == len(set1)
|
||||
assert 3 == len(set2)
|
||||
|
||||
self.assertIn(1, set1)
|
||||
self.assertIn(2, set1)
|
||||
self.assertIn(3, set1)
|
||||
self.assertIn(4, set1)
|
||||
self.assertNotIn(5, set1)
|
||||
assert 1 in set1
|
||||
assert 2 in set1
|
||||
assert 3 in set1
|
||||
assert 4 in set1
|
||||
assert 5 not in set1
|
||||
|
||||
self.assertNotIn(1, set2)
|
||||
self.assertNotIn(2, set2)
|
||||
self.assertIn(3, set2)
|
||||
self.assertIn(4, set2)
|
||||
self.assertIn(5, set2)
|
||||
assert 1 not in set2
|
||||
assert 2 not in set2
|
||||
assert 3 in set2
|
||||
assert 4 in set2
|
||||
assert 5 in set2
|
||||
|
||||
set1.add(5)
|
||||
self.assertIn(5, set1)
|
||||
assert 5 in set1
|
||||
|
||||
set1.discard(5)
|
||||
self.assertNotIn(5, set1)
|
||||
assert 5 not in set1
|
||||
|
||||
# Try again while key is not in
|
||||
set1.discard(5)
|
||||
self.assertNotIn(5, set1)
|
||||
assert 5 not in set1
|
||||
|
||||
self.assertEqual([1, 2, 3, 4], list(set1))
|
||||
self.assertEqual([4, 3, 2, 1], list(reversed(set1)))
|
||||
assert [1, 2, 3, 4] == list(set1)
|
||||
assert [4, 3, 2, 1] == list(reversed(set1))
|
||||
|
||||
self.assertEqual(1, set1.pop(False))
|
||||
self.assertEqual([2, 3, 4], list(set1))
|
||||
assert 1 == set1.pop(False)
|
||||
assert [2, 3, 4] == list(set1)
|
||||
|
||||
self.assertEqual(4, set1.pop())
|
||||
self.assertEqual([2, 3], list(set1))
|
||||
assert 4 == set1.pop()
|
||||
assert [2, 3] == list(set1)
|
||||
|
||||
self.assertEqual('OrderedSet()', str(util.OrderedSet()))
|
||||
self.assertEqual('OrderedSet([2, 3])', str(set1))
|
||||
assert 'OrderedSet()' == str(util.OrderedSet())
|
||||
assert 'OrderedSet([2, 3])' == str(set1)
|
||||
|
||||
self.assertEqual(set1, util.OrderedSet([2, 3]))
|
||||
self.assertNotEqual(set1, util.OrderedSet([3, 2]))
|
||||
self.assertEqual(set1, set([2, 3]))
|
||||
self.assertEqual(set1, {3, 2})
|
||||
self.assertEqual(set1, [2, 3])
|
||||
self.assertEqual(set1, [3, 2])
|
||||
self.assertNotEqual(set1, {2})
|
||||
assert set1 == util.OrderedSet([2, 3])
|
||||
assert set1 != util.OrderedSet([3, 2])
|
||||
assert set1 == set([2, 3])
|
||||
assert set1 == {3, 2}
|
||||
assert set1 == [2, 3]
|
||||
assert set1 == [3, 2]
|
||||
assert set1 != {2}
|
||||
|
||||
set3 = util.OrderedSet(set1)
|
||||
set3.update(set2)
|
||||
|
||||
self.assertEqual([3, 4, 5, 2], set3)
|
||||
self.assertEqual([3, 4, 5, 2], set1 | set2)
|
||||
self.assertEqual([3], set1 & set2)
|
||||
self.assertEqual([2], set1 - set2)
|
||||
assert [3, 4, 5, 2] == set3
|
||||
assert [3, 4, 5, 2] == set1 | set2
|
||||
assert [3] == set1 & set2
|
||||
assert [2] == set1 - set2
|
||||
|
||||
set1.update([1, 2], [5, 6])
|
||||
self.assertEqual([2, 3, 1, 5, 6], set1)
|
||||
assert [2, 3, 1, 5, 6] == set1
|
||||
|
||||
def test_throttle(self):
|
||||
"""Test the add cooldown decorator."""
|
||||
@@ -188,36 +185,36 @@ class TestUtil(unittest.TestCase):
|
||||
test_throttle1()
|
||||
test_throttle2()
|
||||
|
||||
self.assertEqual(1, len(calls1))
|
||||
self.assertEqual(1, len(calls2))
|
||||
assert 1 == len(calls1)
|
||||
assert 1 == len(calls2)
|
||||
|
||||
# Call second time. Methods should not get called
|
||||
test_throttle1()
|
||||
test_throttle2()
|
||||
|
||||
self.assertEqual(1, len(calls1))
|
||||
self.assertEqual(1, len(calls2))
|
||||
assert 1 == len(calls1)
|
||||
assert 1 == len(calls2)
|
||||
|
||||
# Call again, overriding throttle, only first one should fire
|
||||
test_throttle1(no_throttle=True)
|
||||
test_throttle2(no_throttle=True)
|
||||
|
||||
self.assertEqual(2, len(calls1))
|
||||
self.assertEqual(1, len(calls2))
|
||||
assert 2 == len(calls1)
|
||||
assert 1 == len(calls2)
|
||||
|
||||
with patch('homeassistant.util.utcnow', return_value=plus3):
|
||||
test_throttle1()
|
||||
test_throttle2()
|
||||
|
||||
self.assertEqual(2, len(calls1))
|
||||
self.assertEqual(1, len(calls2))
|
||||
assert 2 == len(calls1)
|
||||
assert 1 == len(calls2)
|
||||
|
||||
with patch('homeassistant.util.utcnow', return_value=plus5):
|
||||
test_throttle1()
|
||||
test_throttle2()
|
||||
|
||||
self.assertEqual(3, len(calls1))
|
||||
self.assertEqual(2, len(calls2))
|
||||
assert 3 == len(calls1)
|
||||
assert 2 == len(calls2)
|
||||
|
||||
def test_throttle_per_instance(self):
|
||||
"""Test that the throttle method is done per instance of a class."""
|
||||
@@ -229,8 +226,8 @@ class TestUtil(unittest.TestCase):
|
||||
"""Test the throttle."""
|
||||
return True
|
||||
|
||||
self.assertTrue(Tester().hello())
|
||||
self.assertTrue(Tester().hello())
|
||||
assert Tester().hello()
|
||||
assert Tester().hello()
|
||||
|
||||
def test_throttle_on_method(self):
|
||||
"""Test that throttle works when wrapping a method."""
|
||||
@@ -244,8 +241,8 @@ class TestUtil(unittest.TestCase):
|
||||
tester = Tester()
|
||||
throttled = util.Throttle(timedelta(seconds=1))(tester.hello)
|
||||
|
||||
self.assertTrue(throttled())
|
||||
self.assertIsNone(throttled())
|
||||
assert throttled()
|
||||
assert throttled() is None
|
||||
|
||||
def test_throttle_on_two_method(self):
|
||||
"""Test that throttle works when wrapping two methods."""
|
||||
@@ -264,8 +261,8 @@ class TestUtil(unittest.TestCase):
|
||||
|
||||
tester = Tester()
|
||||
|
||||
self.assertTrue(tester.hello())
|
||||
self.assertTrue(tester.goodbye())
|
||||
assert tester.hello()
|
||||
assert tester.goodbye()
|
||||
|
||||
@patch.object(util, 'random')
|
||||
def test_get_random_string(self, mock_random):
|
||||
|
||||
@@ -7,6 +7,7 @@ from tempfile import mkdtemp
|
||||
from homeassistant.util.json import (SerializationError,
|
||||
load_json, save_json)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import pytest
|
||||
|
||||
# Test data that can be saved as JSON
|
||||
TEST_JSON_A = {"a": 1, "B": "two"}
|
||||
@@ -38,7 +39,7 @@ class TestJSON(unittest.TestCase):
|
||||
fname = self._path_for("test1")
|
||||
save_json(fname, TEST_JSON_A)
|
||||
data = load_json(fname)
|
||||
self.assertEqual(data, TEST_JSON_A)
|
||||
assert data == TEST_JSON_A
|
||||
|
||||
# Skipped on Windows
|
||||
@unittest.skipIf(sys.platform.startswith('win'),
|
||||
@@ -48,9 +49,9 @@ class TestJSON(unittest.TestCase):
|
||||
fname = self._path_for("test2")
|
||||
save_json(fname, TEST_JSON_A, private=True)
|
||||
data = load_json(fname)
|
||||
self.assertEqual(data, TEST_JSON_A)
|
||||
assert data == TEST_JSON_A
|
||||
stats = os.stat(fname)
|
||||
self.assertEqual(stats.st_mode & 0o77, 0)
|
||||
assert stats.st_mode & 0o77 == 0
|
||||
|
||||
def test_overwrite_and_reload(self):
|
||||
"""Test that we can overwrite an existing file and read back."""
|
||||
@@ -58,12 +59,12 @@ class TestJSON(unittest.TestCase):
|
||||
save_json(fname, TEST_JSON_A)
|
||||
save_json(fname, TEST_JSON_B)
|
||||
data = load_json(fname)
|
||||
self.assertEqual(data, TEST_JSON_B)
|
||||
assert data == TEST_JSON_B
|
||||
|
||||
def test_save_bad_data(self):
|
||||
"""Test error from trying to save unserialisable data."""
|
||||
fname = self._path_for("test4")
|
||||
with self.assertRaises(SerializationError):
|
||||
with pytest.raises(SerializationError):
|
||||
save_json(fname, TEST_BAD_OBJECT)
|
||||
|
||||
def test_load_bad_data(self):
|
||||
@@ -71,5 +72,5 @@ class TestJSON(unittest.TestCase):
|
||||
fname = self._path_for("test5")
|
||||
with open(fname, "w") as fh:
|
||||
fh.write(TEST_BAD_SERIALIED)
|
||||
with self.assertRaises(HomeAssistantError):
|
||||
with pytest.raises(HomeAssistantError):
|
||||
load_json(fname)
|
||||
@@ -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
|
||||
+15
-15
@@ -4,6 +4,7 @@ import unittest
|
||||
import homeassistant.util.volume as volume_util
|
||||
from homeassistant.const import (VOLUME_LITERS, VOLUME_MILLILITERS,
|
||||
VOLUME_GALLONS, VOLUME_FLUID_OUNCE)
|
||||
import pytest
|
||||
|
||||
INVALID_SYMBOL = 'bob'
|
||||
VALID_SYMBOL = VOLUME_LITERS
|
||||
@@ -14,36 +15,35 @@ class TestVolumeUtil(unittest.TestCase):
|
||||
|
||||
def test_convert_same_unit(self):
|
||||
"""Test conversion from any unit to same unit."""
|
||||
self.assertEqual(2, volume_util.convert(2, VOLUME_LITERS,
|
||||
VOLUME_LITERS))
|
||||
self.assertEqual(3, volume_util.convert(3, VOLUME_MILLILITERS,
|
||||
VOLUME_MILLILITERS))
|
||||
self.assertEqual(4, volume_util.convert(4, VOLUME_GALLONS,
|
||||
VOLUME_GALLONS))
|
||||
self.assertEqual(5, volume_util.convert(5, VOLUME_FLUID_OUNCE,
|
||||
VOLUME_FLUID_OUNCE))
|
||||
assert 2 == volume_util.convert(2, VOLUME_LITERS, VOLUME_LITERS)
|
||||
assert 3 == volume_util.convert(3, VOLUME_MILLILITERS,
|
||||
VOLUME_MILLILITERS)
|
||||
assert 4 == volume_util.convert(4, VOLUME_GALLONS,
|
||||
VOLUME_GALLONS)
|
||||
assert 5 == volume_util.convert(5, VOLUME_FLUID_OUNCE,
|
||||
VOLUME_FLUID_OUNCE)
|
||||
|
||||
def test_convert_invalid_unit(self):
|
||||
"""Test exception is thrown for invalid units."""
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
volume_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
with pytest.raises(ValueError):
|
||||
volume_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
|
||||
|
||||
def test_convert_nonnumeric_value(self):
|
||||
"""Test exception is thrown for nonnumeric type."""
|
||||
with self.assertRaises(TypeError):
|
||||
with pytest.raises(TypeError):
|
||||
volume_util.convert('a', VOLUME_GALLONS, VOLUME_LITERS)
|
||||
|
||||
def test_convert_from_liters(self):
|
||||
"""Test conversion from liters to other units."""
|
||||
liters = 5
|
||||
self.assertEqual(volume_util.convert(liters, VOLUME_LITERS,
|
||||
VOLUME_GALLONS), 1.321)
|
||||
assert volume_util.convert(liters, VOLUME_LITERS,
|
||||
VOLUME_GALLONS) == 1.321
|
||||
|
||||
def test_convert_from_gallons(self):
|
||||
"""Test conversion from gallons to other units."""
|
||||
gallons = 5
|
||||
self.assertEqual(volume_util.convert(gallons, VOLUME_GALLONS,
|
||||
VOLUME_LITERS), 18.925)
|
||||
assert volume_util.convert(gallons, VOLUME_GALLONS,
|
||||
VOLUME_LITERS) == 18.925
|
||||
+15
-14
@@ -43,14 +43,14 @@ class TestYaml(unittest.TestCase):
|
||||
def test_unhashable_key(self):
|
||||
"""Test an unhasable key."""
|
||||
files = {YAML_CONFIG_FILE: 'message:\n {{ states.state }}'}
|
||||
with self.assertRaises(HomeAssistantError), \
|
||||
with pytest.raises(HomeAssistantError), \
|
||||
patch_yaml_files(files):
|
||||
load_yaml_config_file(YAML_CONFIG_FILE)
|
||||
|
||||
def test_no_key(self):
|
||||
"""Test item without a key."""
|
||||
files = {YAML_CONFIG_FILE: 'a: a\nnokeyhere'}
|
||||
with self.assertRaises(HomeAssistantError), \
|
||||
with pytest.raises(HomeAssistantError), \
|
||||
patch_yaml_files(files):
|
||||
yaml.load_yaml(YAML_CONFIG_FILE)
|
||||
|
||||
@@ -73,7 +73,7 @@ class TestYaml(unittest.TestCase):
|
||||
def test_invalid_environment_variable(self):
|
||||
"""Test config file with no environment variable sat."""
|
||||
conf = "password: !env_var PASSWORD"
|
||||
with self.assertRaises(HomeAssistantError):
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with io.StringIO(conf) as file:
|
||||
yaml.yaml.safe_load(file)
|
||||
|
||||
@@ -261,7 +261,8 @@ class TestYaml(unittest.TestCase):
|
||||
def test_load_yaml_encoding_error(self, mock_open):
|
||||
"""Test raising a UnicodeDecodeError."""
|
||||
mock_open.side_effect = UnicodeDecodeError('', b'', 1, 0, '')
|
||||
self.assertRaises(HomeAssistantError, yaml.load_yaml, 'test')
|
||||
with pytest.raises(HomeAssistantError):
|
||||
yaml.load_yaml('test')
|
||||
|
||||
def test_dump(self):
|
||||
"""The that the dump method returns empty None values."""
|
||||
@@ -332,12 +333,12 @@ class TestSecrets(unittest.TestCase):
|
||||
def test_secrets_from_yaml(self):
|
||||
"""Did secrets load ok."""
|
||||
expected = {'api_password': 'pwhttp'}
|
||||
self.assertEqual(expected, self._yaml['http'])
|
||||
assert expected == self._yaml['http']
|
||||
|
||||
expected = {
|
||||
'username': 'un1',
|
||||
'password': 'pw1'}
|
||||
self.assertEqual(expected, self._yaml['component'])
|
||||
assert expected == self._yaml['component']
|
||||
|
||||
def test_secrets_from_parent_folder(self):
|
||||
"""Test loading secrets from parent foler."""
|
||||
@@ -350,7 +351,7 @@ class TestSecrets(unittest.TestCase):
|
||||
' password: !secret comp1_pw\n'
|
||||
'')
|
||||
|
||||
self.assertEqual(expected, self._yaml['http'])
|
||||
assert expected == self._yaml['http']
|
||||
|
||||
def test_secret_overrides_parent(self):
|
||||
"""Test loading current directory secret overrides the parent."""
|
||||
@@ -365,13 +366,13 @@ class TestSecrets(unittest.TestCase):
|
||||
' password: !secret comp1_pw\n'
|
||||
'')
|
||||
|
||||
self.assertEqual(expected, self._yaml['http'])
|
||||
assert expected == self._yaml['http']
|
||||
|
||||
def test_secrets_from_unrelated_fails(self):
|
||||
"""Test loading secrets from unrelated folder fails."""
|
||||
load_yaml(os.path.join(self._unrelated_path, yaml.SECRET_YAML),
|
||||
'test: failure')
|
||||
with self.assertRaises(HomeAssistantError):
|
||||
with pytest.raises(HomeAssistantError):
|
||||
load_yaml(os.path.join(self._sub_folder_path, 'sub.yaml'),
|
||||
'http:\n'
|
||||
' api_password: !secret test')
|
||||
@@ -380,12 +381,12 @@ class TestSecrets(unittest.TestCase):
|
||||
"""Test keyring fallback & get_password."""
|
||||
yaml.keyring = None # Ensure its not there
|
||||
yaml_str = 'http:\n api_password: !secret http_pw_keyring'
|
||||
with self.assertRaises(yaml.HomeAssistantError):
|
||||
with pytest.raises(yaml.HomeAssistantError):
|
||||
load_yaml(self._yaml_path, yaml_str)
|
||||
|
||||
yaml.keyring = FakeKeyring({'http_pw_keyring': 'yeah'})
|
||||
_yaml = load_yaml(self._yaml_path, yaml_str)
|
||||
self.assertEqual({'http': {'api_password': 'yeah'}}, _yaml)
|
||||
assert {'http': {'api_password': 'yeah'}} == _yaml
|
||||
|
||||
@patch.object(yaml, 'credstash')
|
||||
def test_secrets_credstash(self, mock_credstash):
|
||||
@@ -395,11 +396,11 @@ class TestSecrets(unittest.TestCase):
|
||||
_yaml = load_yaml(self._yaml_path, yaml_str)
|
||||
log = logging.getLogger()
|
||||
log.error(_yaml['http'])
|
||||
self.assertEqual({'api_password': 'yeah'}, _yaml['http'])
|
||||
assert {'api_password': 'yeah'} == _yaml['http']
|
||||
|
||||
def test_secrets_logger_removed(self):
|
||||
"""Ensure logger: debug was removed."""
|
||||
with self.assertRaises(yaml.HomeAssistantError):
|
||||
with pytest.raises(yaml.HomeAssistantError):
|
||||
load_yaml(self._yaml_path, 'api_password: !secret logger')
|
||||
|
||||
@patch('homeassistant.util.yaml._LOGGER.error')
|
||||
@@ -418,7 +419,7 @@ class TestSecrets(unittest.TestCase):
|
||||
' comp1_un: un1\n'
|
||||
' comp1_pw: pw1\n')
|
||||
yaml.clear_secret_cache()
|
||||
with self.assertRaises(HomeAssistantError):
|
||||
with pytest.raises(HomeAssistantError):
|
||||
load_yaml(self._yaml_path,
|
||||
'http:\n'
|
||||
' api_password: !secret http_pw\n'
|
||||
|
||||
Reference in new issue
Block a user