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:
Paulus Schoutsen 2018-10-24 12:10:05 +02:00 committed by GitHub
parent 4222f7562b
commit 08fe7c3ece
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
223 changed files with 6747 additions and 7237 deletions

View file

@ -85,11 +85,11 @@ class TestPilight(unittest.TestCase):
with assert_setup_component(4):
with patch('pilight.pilight.Client',
side_effect=socket.error) as mock_client:
self.assertFalse(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert not setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
mock_client.assert_called_once_with(host=pilight.DEFAULT_HOST,
port=pilight.DEFAULT_PORT)
self.assertEqual(1, mock_error.call_count)
assert 1 == mock_error.call_count
@patch('homeassistant.components.pilight._LOGGER.error')
def test_connection_timeout_error(self, mock_error):
@ -97,11 +97,11 @@ class TestPilight(unittest.TestCase):
with assert_setup_component(4):
with patch('pilight.pilight.Client',
side_effect=socket.timeout) as mock_client:
self.assertFalse(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert not setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
mock_client.assert_called_once_with(host=pilight.DEFAULT_HOST,
port=pilight.DEFAULT_PORT)
self.assertEqual(1, mock_error.call_count)
assert 1 == mock_error.call_count
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.error')
@ -109,8 +109,8 @@ class TestPilight(unittest.TestCase):
def test_send_code_no_protocol(self, mock_pilight_error, mock_error):
"""Try to send data without protocol information, should give error."""
with assert_setup_component(4):
self.assertTrue(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
# Call without protocol info, should be ignored with error
self.hass.services.call(pilight.DOMAIN, pilight.SERVICE_NAME,
@ -119,17 +119,16 @@ class TestPilight(unittest.TestCase):
blocking=True)
self.hass.block_till_done()
error_log_call = mock_error.call_args_list[-1]
self.assertTrue(
'required key not provided @ data[\'protocol\']' in
str(error_log_call))
assert 'required key not provided @ data[\'protocol\']' in \
str(error_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('tests.components.test_pilight._LOGGER.error')
def test_send_code(self, mock_pilight_error):
"""Try to send proper data."""
with assert_setup_component(4):
self.assertTrue(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
# Call with protocol info, should not give error
service_data = {'protocol': 'test',
@ -140,7 +139,7 @@ class TestPilight(unittest.TestCase):
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[-1]
service_data['protocol'] = [service_data['protocol']]
self.assertTrue(str(service_data) in str(error_log_call))
assert str(service_data) in str(error_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.components.pilight._LOGGER.error')
@ -149,8 +148,8 @@ class TestPilight(unittest.TestCase):
with assert_setup_component(4):
with patch('pilight.pilight.Client.send_code',
side_effect=IOError):
self.assertTrue(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
# Call with protocol info, should not give error
service_data = {'protocol': 'test',
@ -160,16 +159,16 @@ class TestPilight(unittest.TestCase):
blocking=True)
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[-1]
self.assertTrue('Pilight send failed' in str(error_log_call))
assert 'Pilight send failed' in str(error_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('tests.components.test_pilight._LOGGER.error')
def test_send_code_delay(self, mock_pilight_error):
"""Try to send proper data with delay afterwards."""
with assert_setup_component(4):
self.assertTrue(setup_component(
assert setup_component(
self.hass, pilight.DOMAIN,
{pilight.DOMAIN: {pilight.CONF_SEND_DELAY: 5.0}}))
{pilight.DOMAIN: {pilight.CONF_SEND_DELAY: 5.0}})
# Call with protocol info, should not give error
service_data1 = {'protocol': 'test11',
@ -189,47 +188,44 @@ class TestPilight(unittest.TestCase):
{ha.ATTR_NOW: dt_util.utcnow()})
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[-1]
self.assertTrue(str(service_data1) in str(error_log_call))
assert str(service_data1) in str(error_log_call)
new_time = dt_util.utcnow() + timedelta(seconds=5)
self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
{ha.ATTR_NOW: new_time})
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[-1]
self.assertTrue(str(service_data2) in str(error_log_call))
assert str(service_data2) in str(error_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('tests.components.test_pilight._LOGGER.error')
def test_start_stop(self, mock_pilight_error):
"""Check correct startup and stop of pilight daemon."""
with assert_setup_component(4):
self.assertTrue(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
# Test startup
self.hass.start()
self.hass.block_till_done()
error_log_call = mock_pilight_error.call_args_list[-2]
self.assertTrue(
'PilightDaemonSim callback' in str(error_log_call))
assert 'PilightDaemonSim callback' in str(error_log_call)
error_log_call = mock_pilight_error.call_args_list[-1]
self.assertTrue(
'PilightDaemonSim start' in str(error_log_call))
assert 'PilightDaemonSim start' in str(error_log_call)
# Test stop
self.skip_teardown_stop = True
self.hass.stop()
error_log_call = mock_pilight_error.call_args_list[-1]
self.assertTrue(
'PilightDaemonSim stop' in str(error_log_call))
assert 'PilightDaemonSim stop' in str(error_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
def test_receive_code(self, mock_info):
"""Check if code receiving via pilight daemon works."""
with assert_setup_component(4):
self.assertTrue(setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}}))
assert setup_component(
self.hass, pilight.DOMAIN, {pilight.DOMAIN: {}})
# Test startup
self.hass.start()
@ -243,8 +239,8 @@ class TestPilight(unittest.TestCase):
# Check if all message parts are put on event bus
for key, value in expected_message.items():
self.assertTrue(str(key) in str(error_log_call))
self.assertTrue(str(value) in str(error_log_call))
assert str(key) in str(error_log_call)
assert str(value) in str(error_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
@ -256,9 +252,9 @@ class TestPilight(unittest.TestCase):
'uuid': [PilightDaemonSim.test_message['uuid']],
'id': [PilightDaemonSim.test_message['message']['id']],
'unit': [PilightDaemonSim.test_message['message']['unit']]}
self.assertTrue(setup_component(
assert setup_component(
self.hass, pilight.DOMAIN,
{pilight.DOMAIN: {"whitelist": whitelist}}))
{pilight.DOMAIN: {"whitelist": whitelist}})
self.hass.start()
self.hass.block_till_done()
@ -271,8 +267,8 @@ class TestPilight(unittest.TestCase):
# Check if all message parts are put on event bus
for key, value in expected_message.items():
self.assertTrue(str(key) in str(info_log_call))
self.assertTrue(str(value) in str(info_log_call))
assert str(key) in str(info_log_call)
assert str(value) in str(info_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
@ -282,9 +278,9 @@ class TestPilight(unittest.TestCase):
whitelist = {
'protocol': [PilightDaemonSim.test_message['protocol']],
'id': [PilightDaemonSim.test_message['message']['id']]}
self.assertTrue(setup_component(
assert setup_component(
self.hass, pilight.DOMAIN,
{pilight.DOMAIN: {"whitelist": whitelist}}))
{pilight.DOMAIN: {"whitelist": whitelist}})
self.hass.start()
self.hass.block_till_done()
@ -297,8 +293,8 @@ class TestPilight(unittest.TestCase):
# Check if all message parts are put on event bus
for key, value in expected_message.items():
self.assertTrue(str(key) in str(info_log_call))
self.assertTrue(str(value) in str(info_log_call))
assert str(key) in str(info_log_call)
assert str(value) in str(info_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
@ -309,9 +305,9 @@ class TestPilight(unittest.TestCase):
'protocol': [PilightDaemonSim.test_message['protocol'],
'other_protocol'],
'id': [PilightDaemonSim.test_message['message']['id']]}
self.assertTrue(setup_component(
assert setup_component(
self.hass, pilight.DOMAIN,
{pilight.DOMAIN: {"whitelist": whitelist}}))
{pilight.DOMAIN: {"whitelist": whitelist}})
self.hass.start()
self.hass.block_till_done()
@ -324,8 +320,8 @@ class TestPilight(unittest.TestCase):
# Check if all message parts are put on event bus
for key, value in expected_message.items():
self.assertTrue(str(key) in str(info_log_call))
self.assertTrue(str(value) in str(info_log_call))
assert str(key) in str(info_log_call)
assert str(value) in str(info_log_call)
@patch('pilight.pilight.Client', PilightDaemonSim)
@patch('homeassistant.core._LOGGER.info')
@ -335,16 +331,16 @@ class TestPilight(unittest.TestCase):
whitelist = {
'protocol': ['wrong_protocol'],
'id': [PilightDaemonSim.test_message['message']['id']]}
self.assertTrue(setup_component(
assert setup_component(
self.hass, pilight.DOMAIN,
{pilight.DOMAIN: {"whitelist": whitelist}}))
{pilight.DOMAIN: {"whitelist": whitelist}})
self.hass.start()
self.hass.block_till_done()
info_log_call = mock_info.call_args_list[-1]
self.assertFalse('Event pilight_received' in info_log_call)
assert not ('Event pilight_received' in info_log_call)
class TestPilightCallrateThrottler(unittest.TestCase):
@ -368,7 +364,7 @@ class TestPilightCallrateThrottler(unittest.TestCase):
for i in range(3):
action(i)
self.assertEqual(runs, [0, 1, 2])
assert runs == [0, 1, 2]
def test_call_rate_delay_throttle_enabled(self):
"""Test that throttling actually work."""
@ -381,7 +377,7 @@ class TestPilightCallrateThrottler(unittest.TestCase):
for i in range(3):
action(i)
self.assertEqual(runs, [])
assert runs == []
exp = []
now = dt_util.utcnow()
@ -391,4 +387,4 @@ class TestPilightCallrateThrottler(unittest.TestCase):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
{ha.ATTR_NOW: shifted_time})
self.hass.block_till_done()
self.assertEqual(runs, exp)
assert runs == exp