mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Fix PEP257 issues
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
"""
|
||||
tests.test_core
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Provides tests to verify that Home Assistant core works.
|
||||
"""
|
||||
"""Test to verify that Home Assistant core works."""
|
||||
# pylint: disable=protected-access,too-many-public-methods
|
||||
# pylint: disable=too-few-public-methods
|
||||
import os
|
||||
@@ -31,18 +26,16 @@ PST = pytz.timezone('America/Los_Angeles')
|
||||
|
||||
|
||||
class TestHomeAssistant(unittest.TestCase):
|
||||
"""
|
||||
Tests the Home Assistant core classes.
|
||||
"""
|
||||
"""Test the Home Assistant core classes."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" things to be run when tests are started. """
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.states.set("light.Bowl", "on")
|
||||
self.hass.states.set("switch.AC", "off")
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
"""Stop down stuff we started."""
|
||||
try:
|
||||
self.hass.stop()
|
||||
except HomeAssistantError:
|
||||
@@ -50,6 +43,7 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
pass
|
||||
|
||||
def test_start(self):
|
||||
"""Start the test."""
|
||||
calls = []
|
||||
self.hass.bus.listen_once(EVENT_HOMEASSISTANT_START,
|
||||
lambda event: calls.append(1))
|
||||
@@ -59,7 +53,7 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
|
||||
# @patch('homeassistant.core.time.sleep')
|
||||
def test_block_till_stoped(self):
|
||||
""" Test if we can block till stop service is called. """
|
||||
"""Test if we can block till stop service is called."""
|
||||
with patch('time.sleep'):
|
||||
blocking_thread = threading.Thread(
|
||||
target=self.hass.block_till_stopped)
|
||||
@@ -82,11 +76,13 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
self.assertFalse(blocking_thread.is_alive())
|
||||
|
||||
def test_stopping_with_sigterm(self):
|
||||
"""Test for stopping with sigterm."""
|
||||
calls = []
|
||||
self.hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||
lambda event: calls.append(1))
|
||||
|
||||
def send_sigterm(length):
|
||||
"""Send sigterm."""
|
||||
os.kill(os.getpid(), signal.SIGTERM)
|
||||
|
||||
with patch('homeassistant.core.time.sleep', send_sigterm):
|
||||
@@ -96,8 +92,10 @@ class TestHomeAssistant(unittest.TestCase):
|
||||
|
||||
|
||||
class TestEvent(unittest.TestCase):
|
||||
""" Test Event class. """
|
||||
"""A Test Event class."""
|
||||
|
||||
def test_eq(self):
|
||||
"""Test events."""
|
||||
now = dt_util.utcnow()
|
||||
data = {'some': 'attr'}
|
||||
event1, event2 = [
|
||||
@@ -108,7 +106,7 @@ class TestEvent(unittest.TestCase):
|
||||
self.assertEqual(event1, event2)
|
||||
|
||||
def test_repr(self):
|
||||
""" Test that repr method works. #MoreCoverage """
|
||||
"""Test that repr method works. #MoreCoverage"""
|
||||
self.assertEqual(
|
||||
"<Event TestEvent[L]>",
|
||||
str(ha.Event("TestEvent")))
|
||||
@@ -120,6 +118,7 @@ class TestEvent(unittest.TestCase):
|
||||
ha.EventOrigin.remote)))
|
||||
|
||||
def test_as_dict(self):
|
||||
"""Test as dictionary."""
|
||||
event_type = 'some_type'
|
||||
now = dt_util.utcnow()
|
||||
data = {'some': 'attr'}
|
||||
@@ -135,19 +134,19 @@ class TestEvent(unittest.TestCase):
|
||||
|
||||
|
||||
class TestEventBus(unittest.TestCase):
|
||||
""" Test EventBus methods. """
|
||||
"""Test EventBus methods."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" things to be run when tests are started. """
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.bus = ha.EventBus(ha.create_worker_pool(0))
|
||||
self.bus.listen('test_event', lambda x: len)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
"""Stop down stuff we started."""
|
||||
self.bus._pool.stop()
|
||||
|
||||
def test_add_remove_listener(self):
|
||||
""" Test remove_listener method. """
|
||||
"""Test remove_listener method."""
|
||||
self.bus._pool.add_worker()
|
||||
old_count = len(self.bus.listeners)
|
||||
|
||||
@@ -168,7 +167,7 @@ class TestEventBus(unittest.TestCase):
|
||||
self.bus.remove_listener('test', listener)
|
||||
|
||||
def test_listen_once_event(self):
|
||||
""" Test listen_once_event method. """
|
||||
"""Test listen_once_event method."""
|
||||
runs = []
|
||||
|
||||
self.bus.listen_once('test_event', lambda x: runs.append(1))
|
||||
@@ -183,43 +182,49 @@ class TestEventBus(unittest.TestCase):
|
||||
|
||||
|
||||
class TestState(unittest.TestCase):
|
||||
""" Test EventBus methods. """
|
||||
"""Test State methods."""
|
||||
|
||||
def test_init(self):
|
||||
""" Test state.init """
|
||||
"""Test state.init."""
|
||||
self.assertRaises(
|
||||
InvalidEntityFormatError, ha.State,
|
||||
'invalid_entity_format', 'test_state')
|
||||
|
||||
def test_domain(self):
|
||||
"""Test domain."""
|
||||
state = ha.State('some_domain.hello', 'world')
|
||||
self.assertEqual('some_domain', state.domain)
|
||||
|
||||
def test_object_id(self):
|
||||
"""Test object ID."""
|
||||
state = ha.State('domain.hello', 'world')
|
||||
self.assertEqual('hello', state.object_id)
|
||||
|
||||
def test_name_if_no_friendly_name_attr(self):
|
||||
"""Test if there is no friendly name."""
|
||||
state = ha.State('domain.hello_world', 'world')
|
||||
self.assertEqual('hello world', state.name)
|
||||
|
||||
def test_name_if_friendly_name_attr(self):
|
||||
"""Test if there is a friendly name."""
|
||||
name = 'Some Unique Name'
|
||||
state = ha.State('domain.hello_world', 'world',
|
||||
{ATTR_FRIENDLY_NAME: name})
|
||||
self.assertEqual(name, state.name)
|
||||
|
||||
def test_dict_conversion(self):
|
||||
"""Test conversion of dict."""
|
||||
state = ha.State('domain.hello', 'world', {'some': 'attr'})
|
||||
self.assertEqual(state, ha.State.from_dict(state.as_dict()))
|
||||
|
||||
def test_dict_conversion_with_wrong_data(self):
|
||||
"""Test conversion with wrong data."""
|
||||
self.assertIsNone(ha.State.from_dict(None))
|
||||
self.assertIsNone(ha.State.from_dict({'state': 'yes'}))
|
||||
self.assertIsNone(ha.State.from_dict({'entity_id': 'yes'}))
|
||||
|
||||
def test_repr(self):
|
||||
""" Test state.repr """
|
||||
"""Test state.repr."""
|
||||
self.assertEqual("<state happy.happy=on @ 12:00:00 08-12-1984>",
|
||||
str(ha.State(
|
||||
"happy.happy", "on",
|
||||
@@ -232,10 +237,10 @@ class TestState(unittest.TestCase):
|
||||
|
||||
|
||||
class TestStateMachine(unittest.TestCase):
|
||||
""" Test EventBus methods. """
|
||||
"""Test State machine methods. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" things to be run when tests are started. """
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.pool = ha.create_worker_pool(0)
|
||||
self.bus = ha.EventBus(self.pool)
|
||||
self.states = ha.StateMachine(self.bus)
|
||||
@@ -243,17 +248,17 @@ class TestStateMachine(unittest.TestCase):
|
||||
self.states.set("switch.AC", "off")
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
"""Stop down stuff we started."""
|
||||
self.pool.stop()
|
||||
|
||||
def test_is_state(self):
|
||||
""" Test is_state method. """
|
||||
"""Test is_state method."""
|
||||
self.assertTrue(self.states.is_state('light.Bowl', 'on'))
|
||||
self.assertFalse(self.states.is_state('light.Bowl', 'off'))
|
||||
self.assertFalse(self.states.is_state('light.Non_existing', 'on'))
|
||||
|
||||
def test_is_state_attr(self):
|
||||
""" Test is_state_attr method. """
|
||||
"""Test is_state_attr method."""
|
||||
self.states.set("light.Bowl", "on", {"brightness": 100})
|
||||
self.assertTrue(
|
||||
self.states.is_state_attr('light.Bowl', 'brightness', 100))
|
||||
@@ -265,7 +270,7 @@ class TestStateMachine(unittest.TestCase):
|
||||
self.states.is_state_attr('light.Non_existing', 'brightness', 100))
|
||||
|
||||
def test_entity_ids(self):
|
||||
""" Test get_entity_ids method. """
|
||||
"""Test get_entity_ids method."""
|
||||
ent_ids = self.states.entity_ids()
|
||||
self.assertEqual(2, len(ent_ids))
|
||||
self.assertTrue('light.bowl' in ent_ids)
|
||||
@@ -276,11 +281,12 @@ class TestStateMachine(unittest.TestCase):
|
||||
self.assertTrue('light.bowl' in ent_ids)
|
||||
|
||||
def test_all(self):
|
||||
"""Test everything."""
|
||||
states = sorted(state.entity_id for state in self.states.all())
|
||||
self.assertEqual(['light.bowl', 'switch.ac'], states)
|
||||
|
||||
def test_remove(self):
|
||||
""" Test remove method. """
|
||||
"""Test remove method."""
|
||||
self.pool.add_worker()
|
||||
events = []
|
||||
self.bus.listen(EVENT_STATE_CHANGED,
|
||||
@@ -303,6 +309,7 @@ class TestStateMachine(unittest.TestCase):
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
def test_case_insensitivty(self):
|
||||
"""Test insensitivty."""
|
||||
self.pool.add_worker()
|
||||
runs = []
|
||||
|
||||
@@ -315,6 +322,7 @@ class TestStateMachine(unittest.TestCase):
|
||||
self.assertEqual(1, len(runs))
|
||||
|
||||
def test_last_changed_not_updated_on_same_state(self):
|
||||
"""Test to not update the existing, same state."""
|
||||
state = self.states.get('light.Bowl')
|
||||
|
||||
future = dt_util.utcnow() + timedelta(hours=10)
|
||||
@@ -327,9 +335,10 @@ class TestStateMachine(unittest.TestCase):
|
||||
|
||||
|
||||
class TestServiceCall(unittest.TestCase):
|
||||
""" Test ServiceCall class. """
|
||||
"""Test ServiceCall class."""
|
||||
|
||||
def test_repr(self):
|
||||
""" Test repr method. """
|
||||
"""Test repr method."""
|
||||
self.assertEqual(
|
||||
"<ServiceCall homeassistant.start>",
|
||||
str(ha.ServiceCall('homeassistant', 'start')))
|
||||
@@ -340,22 +349,22 @@ class TestServiceCall(unittest.TestCase):
|
||||
|
||||
|
||||
class TestServiceRegistry(unittest.TestCase):
|
||||
""" Test EventBus methods. """
|
||||
""" Test ServicerRegistry methods. """
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" things to be run when tests are started. """
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.pool = ha.create_worker_pool(0)
|
||||
self.bus = ha.EventBus(self.pool)
|
||||
self.services = ha.ServiceRegistry(self.bus, self.pool)
|
||||
self.services.register("test_domain", "test_service", lambda x: None)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
""" Stop down stuff we started. """
|
||||
"""Stop down stuff we started."""
|
||||
if self.pool.worker_count:
|
||||
self.pool.stop()
|
||||
|
||||
def test_has_service(self):
|
||||
""" Test has_service method. """
|
||||
"""Test has_service method."""
|
||||
self.assertTrue(
|
||||
self.services.has_service("test_domain", "test_service"))
|
||||
self.assertFalse(
|
||||
@@ -364,12 +373,14 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
self.services.has_service("non_existing", "test_service"))
|
||||
|
||||
def test_services(self):
|
||||
"""Test services."""
|
||||
expected = {
|
||||
'test_domain': {'test_service': {'description': '', 'fields': {}}}
|
||||
}
|
||||
self.assertEqual(expected, self.services.services)
|
||||
|
||||
def test_call_with_blocking_done_in_time(self):
|
||||
"""Test call with blocking."""
|
||||
self.pool.add_worker()
|
||||
self.pool.add_worker()
|
||||
calls = []
|
||||
@@ -381,6 +392,7 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
self.assertEqual(1, len(calls))
|
||||
|
||||
def test_call_with_blocking_not_done_in_time(self):
|
||||
"""Test with blocking."""
|
||||
calls = []
|
||||
self.services.register("test_domain", "register_calls",
|
||||
lambda x: calls.append(1))
|
||||
@@ -393,6 +405,7 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
ha.SERVICE_CALL_LIMIT = orig_limit
|
||||
|
||||
def test_call_non_existing_with_blocking(self):
|
||||
"""Test non-existing with blocking."""
|
||||
self.pool.add_worker()
|
||||
self.pool.add_worker()
|
||||
orig_limit = ha.SERVICE_CALL_LIMIT
|
||||
@@ -403,26 +416,28 @@ class TestServiceRegistry(unittest.TestCase):
|
||||
|
||||
|
||||
class TestConfig(unittest.TestCase):
|
||||
"""Test configuration methods."""
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" things to be run when tests are started. """
|
||||
"""Setup things to be run when tests are started. """
|
||||
self.config = ha.Config()
|
||||
|
||||
def test_config_dir_set_correct(self):
|
||||
""" Test config dir set correct. """
|
||||
"""Test config dir set correct."""
|
||||
data_dir = os.getenv('APPDATA') if os.name == "nt" \
|
||||
else os.path.expanduser('~')
|
||||
self.assertEqual(os.path.join(data_dir, ".homeassistant"),
|
||||
self.config.config_dir)
|
||||
|
||||
def test_path_with_file(self):
|
||||
""" Test get_config_path method. """
|
||||
"""Test get_config_path method."""
|
||||
data_dir = os.getenv('APPDATA') if os.name == "nt" \
|
||||
else os.path.expanduser('~')
|
||||
self.assertEqual(os.path.join(data_dir, ".homeassistant", "test.conf"),
|
||||
self.config.path("test.conf"))
|
||||
|
||||
def test_path_with_dir_and_file(self):
|
||||
""" Test get_config_path method. """
|
||||
"""Test get_config_path method."""
|
||||
data_dir = os.getenv('APPDATA') if os.name == "nt" \
|
||||
else os.path.expanduser('~')
|
||||
self.assertEqual(
|
||||
@@ -430,7 +445,7 @@ class TestConfig(unittest.TestCase):
|
||||
self.config.path("dir", "test.conf"))
|
||||
|
||||
def test_temperature_not_convert_if_no_preference(self):
|
||||
""" No unit conversion to happen if no preference. """
|
||||
"""No unit conversion to happen if no preference."""
|
||||
self.assertEqual(
|
||||
(25, TEMP_CELCIUS),
|
||||
self.config.temperature(25, TEMP_CELCIUS))
|
||||
@@ -439,19 +454,20 @@ class TestConfig(unittest.TestCase):
|
||||
self.config.temperature(80, TEMP_FAHRENHEIT))
|
||||
|
||||
def test_temperature_not_convert_if_invalid_value(self):
|
||||
""" No unit conversion to happen if no preference. """
|
||||
"""No unit conversion to happen if no preference."""
|
||||
self.config.temperature_unit = TEMP_FAHRENHEIT
|
||||
self.assertEqual(
|
||||
('25a', TEMP_CELCIUS),
|
||||
self.config.temperature('25a', TEMP_CELCIUS))
|
||||
|
||||
def test_temperature_not_convert_if_invalid_unit(self):
|
||||
""" No unit conversion to happen if no preference. """
|
||||
"""No unit conversion to happen if no preference."""
|
||||
self.assertEqual(
|
||||
(25, 'Invalid unit'),
|
||||
self.config.temperature(25, 'Invalid unit'))
|
||||
|
||||
def test_temperature_to_convert_to_celcius(self):
|
||||
"""Test temperature conversion to celsius."""
|
||||
self.config.temperature_unit = TEMP_CELCIUS
|
||||
|
||||
self.assertEqual(
|
||||
@@ -462,6 +478,7 @@ class TestConfig(unittest.TestCase):
|
||||
self.config.temperature(80, TEMP_FAHRENHEIT))
|
||||
|
||||
def test_temperature_to_convert_to_fahrenheit(self):
|
||||
"""Test temperature conversion to fahrenheit."""
|
||||
self.config.temperature_unit = TEMP_FAHRENHEIT
|
||||
|
||||
self.assertEqual(
|
||||
@@ -472,6 +489,7 @@ class TestConfig(unittest.TestCase):
|
||||
self.config.temperature(80, TEMP_FAHRENHEIT))
|
||||
|
||||
def test_as_dict(self):
|
||||
"""Test as dict."""
|
||||
expected = {
|
||||
'latitude': None,
|
||||
'longitude': None,
|
||||
@@ -486,7 +504,10 @@ class TestConfig(unittest.TestCase):
|
||||
|
||||
|
||||
class TestWorkerPool(unittest.TestCase):
|
||||
"""Test WorkerPool methods."""
|
||||
|
||||
def test_exception_during_job(self):
|
||||
"""Test exception during a job."""
|
||||
pool = ha.create_worker_pool(1)
|
||||
|
||||
def malicious_job(_):
|
||||
|
||||
Reference in New Issue
Block a user