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 authored and GitHub committed 2018-10-24 12:10:05 +02:00
1 parent 4222f7562b
commit 08fe7c3ece
223 files changed
+6747 -7237

No files matched your search

+47 -50
View File
@@ -120,11 +120,10 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN,
'apns_test_app',
{'push_id': '1234',
'name': 'test device'},
blocking=True))
assert self.hass.services.call(notify.DOMAIN, 'apns_test_app', {
'push_id': '1234',
'name': 'test device'
}, blocking=True)
assert len(written_devices) == 1
assert written_devices[0].name == 'test device'
@@ -156,16 +155,16 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN, 'apns_test_app',
{'push_id': '1234'},
blocking=True))
assert self.hass.services.call(notify.DOMAIN, 'apns_test_app', {
'push_id': '1234'
}, blocking=True)
devices = {dev.push_id: dev for dev in written_devices}
test_device = devices.get('1234')
self.assertIsNotNone(test_device)
self.assertIsNone(test_device.name)
assert test_device is not None
assert test_device.name is None
@patch('homeassistant.components.notify.apns._write_device')
def test_update_existing_device(self, mock_write):
@@ -192,21 +191,20 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN,
'apns_test_app',
{'push_id': '1234',
'name': 'updated device 1'},
blocking=True))
assert self.hass.services.call(notify.DOMAIN, 'apns_test_app', {
'push_id': '1234',
'name': 'updated device 1'
}, blocking=True)
devices = {dev.push_id: dev for dev in written_devices}
test_device_1 = devices.get('1234')
test_device_2 = devices.get('5678')
self.assertIsNotNone(test_device_1)
self.assertIsNotNone(test_device_2)
assert test_device_1 is not None
assert test_device_2 is not None
self.assertEqual('updated device 1', test_device_1.name)
assert 'updated device 1' == test_device_1.name
@patch('homeassistant.components.notify.apns._write_device')
def test_update_existing_device_with_tracking_id(self, mock_write):
@@ -235,24 +233,23 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call(notify.DOMAIN,
'apns_test_app',
{'push_id': '1234',
'name': 'updated device 1'},
blocking=True))
assert self.hass.services.call(notify.DOMAIN, 'apns_test_app', {
'push_id': '1234',
'name': 'updated device 1'
}, blocking=True)
devices = {dev.push_id: dev for dev in written_devices}
test_device_1 = devices.get('1234')
test_device_2 = devices.get('5678')
self.assertIsNotNone(test_device_1)
self.assertIsNotNone(test_device_2)
assert test_device_1 is not None
assert test_device_2 is not None
self.assertEqual('tracking123',
test_device_1.tracking_device_id)
self.assertEqual('tracking456',
test_device_2.tracking_device_id)
assert 'tracking123' == \
test_device_1.tracking_device_id
assert 'tracking456' == \
test_device_2.tracking_device_id
@patch('apns2.client.APNsClient')
def test_send(self, mock_client):
@@ -266,25 +263,25 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call(
assert self.hass.services.call(
'notify', 'test_app',
{'message': 'Hello', 'data': {
'badge': 1,
'sound': 'test.mp3',
'category': 'testing'}},
blocking=True))
blocking=True)
self.assertTrue(send.called)
self.assertEqual(1, len(send.mock_calls))
assert send.called
assert 1 == len(send.mock_calls)
target = send.mock_calls[0][1][0]
payload = send.mock_calls[0][1][1]
self.assertEqual('1234', target)
self.assertEqual('Hello', payload.alert)
self.assertEqual(1, payload.badge)
self.assertEqual('test.mp3', payload.sound)
self.assertEqual('testing', payload.category)
assert '1234' == target
assert 'Hello' == payload.alert
assert 1 == payload.badge
assert 'test.mp3' == payload.sound
assert 'testing' == payload.category
@patch('apns2.client.APNsClient')
def test_send_when_disabled(self, mock_client):
@@ -301,15 +298,15 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call(
assert self.hass.services.call(
'notify', 'test_app',
{'message': 'Hello', 'data': {
'badge': 1,
'sound': 'test.mp3',
'category': 'testing'}},
blocking=True))
blocking=True)
self.assertFalse(send.called)
assert not send.called
@patch('apns2.client.APNsClient')
def test_send_with_state(self, mock_client):
@@ -346,14 +343,14 @@ class TestApns(unittest.TestCase):
notify_service.send_message(message='Hello', target='home')
self.assertTrue(send.called)
self.assertEqual(1, len(send.mock_calls))
assert send.called
assert 1 == len(send.mock_calls)
target = send.mock_calls[0][1][0]
payload = send.mock_calls[0][1][1]
self.assertEqual('5678', target)
self.assertEqual('Hello', payload.alert)
assert '5678' == target
assert 'Hello' == payload.alert
@patch('apns2.client.APNsClient')
@patch('homeassistant.components.notify.apns._write_device')
@@ -386,15 +383,15 @@ class TestApns(unittest.TestCase):
Mock(return_value=yaml_file)):
self._setup_notify()
self.assertTrue(self.hass.services.call('notify', 'test_app',
{'message': 'Hello'},
blocking=True))
assert self.hass.services.call('notify', 'test_app',
{'message': 'Hello'},
blocking=True)
devices = {dev.push_id: dev for dev in written_devices}
test_device_1 = devices.get('1234')
self.assertIsNotNone(test_device_1)
self.assertEqual(True, test_device_1.disabled)
assert test_device_1 is not None
assert test_device_1.disabled is True
def test_write_device():
+10 -14
View File
@@ -49,39 +49,35 @@ class TestCommandLine(unittest.TestCase):
filename = os.path.join(tempdirname, 'message.txt')
message = 'one, two, testing, testing'
with assert_setup_component(1) as handle_config:
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
assert setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat) > {}'.format(filename)
}
}))
})
assert handle_config[notify.DOMAIN]
self.assertTrue(
self.hass.services.call('notify', 'test', {'message': message},
blocking=True)
)
assert self.hass.services.call(
'notify', 'test', {'message': message}, blocking=True)
with open(filename) as fil:
# the echo command adds a line break
self.assertEqual(fil.read(), "{}\n".format(message))
assert fil.read() == "{}\n".format(message)
@patch('homeassistant.components.notify.command_line._LOGGER.error')
def test_error_for_none_zero_exit_code(self, mock_error):
"""Test if an error is logged for non zero exit codes."""
with assert_setup_component(1) as handle_config:
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
assert setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'command_line',
'command': 'echo $(cat); exit 1'
}
}))
})
assert handle_config[notify.DOMAIN]
self.assertTrue(
self.hass.services.call('notify', 'test', {'message': 'error'},
blocking=True)
)
self.assertEqual(1, mock_error.call_count)
assert self.hass.services.call('notify', 'test', {'message': 'error'},
blocking=True)
assert 1 == mock_error.call_count
+10 -11
View File
@@ -56,10 +56,9 @@ class TestNotifyDemo(unittest.TestCase):
self._setup_notify()
self.hass.block_till_done()
assert mock_demo_get_service.called
self.assertEqual(
log_handle.output,
assert log_handle.output == \
['ERROR:homeassistant.components.notify:'
'Failed to initialize notification service demo'])
'Failed to initialize notification service demo']
@patch('homeassistant.components.notify.demo.get_service', autospec=True)
def test_discover_notify(self, mock_demo_get_service):
@@ -83,7 +82,7 @@ class TestNotifyDemo(unittest.TestCase):
self._setup_notify()
common.send_message(self.hass, None)
self.hass.block_till_done()
self.assertTrue(len(self.events) == 0)
assert len(self.events) == 0
def test_sending_templated_message(self):
"""Send a templated message."""
@@ -93,8 +92,8 @@ class TestNotifyDemo(unittest.TestCase):
'{{ states.sensor.temperature.name }}')
self.hass.block_till_done()
last_event = self.events[-1]
self.assertEqual(last_event.data[notify.ATTR_TITLE], 'temperature')
self.assertEqual(last_event.data[notify.ATTR_MESSAGE], '10')
assert last_event.data[notify.ATTR_TITLE] == 'temperature'
assert last_event.data[notify.ATTR_MESSAGE] == '10'
def test_method_forwards_correct_data(self):
"""Test that all data from the service gets forwarded to service."""
@@ -102,7 +101,7 @@ class TestNotifyDemo(unittest.TestCase):
common.send_message(self.hass, 'my message', 'my title',
{'hello': 'world'})
self.hass.block_till_done()
self.assertTrue(len(self.events) == 1)
assert len(self.events) == 1
data = self.events[0].data
assert {
'message': 'my message',
@@ -128,7 +127,7 @@ class TestNotifyDemo(unittest.TestCase):
script.call_from_config(self.hass, conf)
self.hass.block_till_done()
self.assertTrue(len(self.events) == 1)
assert len(self.events) == 1
assert {
'message': 'Test 123 4',
'data': {
@@ -158,7 +157,7 @@ class TestNotifyDemo(unittest.TestCase):
script.call_from_config(self.hass, conf)
self.hass.block_till_done()
self.assertTrue(len(self.events) == 1)
assert len(self.events) == 1
assert {
'message': 'Test 123 4',
'title': 'Test',
@@ -171,9 +170,9 @@ class TestNotifyDemo(unittest.TestCase):
def test_targets_are_services(self):
"""Test that all targets are exposed as individual services."""
self._setup_notify()
self.assertIsNotNone(self.hass.services.has_service("notify", "demo"))
assert self.hass.services.has_service("notify", "demo") is not None
service = "demo_test_target_name"
self.assertIsNotNone(self.hass.services.has_service("notify", service))
assert self.hass.services.has_service("notify", service) is not None
def test_messages_to_targets_route(self):
"""Test message routing to specific target services."""
+15 -15
View File
@@ -26,17 +26,17 @@ class TestFacebook(unittest.TestCase):
target = ["+15555551234"]
self.facebook.send_message(message=message, target=target)
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 1)
assert mock.called
assert mock.call_count == 1
expected_body = {
"recipient": {"phone_number": target[0]},
"message": {"text": message}
}
self.assertEqual(mock.last_request.json(), expected_body)
assert mock.last_request.json() == expected_body
expected_params = {"access_token": ["page-access-token"]}
self.assertEqual(mock.last_request.qs, expected_params)
assert mock.last_request.qs == expected_params
@requests_mock.Mocker()
def test_sending_multiple_messages(self, mock):
@@ -51,8 +51,8 @@ class TestFacebook(unittest.TestCase):
targets = ["+15555551234", "+15555551235"]
self.facebook.send_message(message=message, target=targets)
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 2)
assert mock.called
assert mock.call_count == 2
for idx, target in enumerate(targets):
request = mock.request_history[idx]
@@ -60,10 +60,10 @@ class TestFacebook(unittest.TestCase):
"recipient": {"phone_number": target},
"message": {"text": message}
}
self.assertEqual(request.json(), expected_body)
assert request.json() == expected_body
expected_params = {"access_token": ["page-access-token"]}
self.assertEqual(request.qs, expected_params)
assert request.qs == expected_params
@requests_mock.Mocker()
def test_send_message_attachment(self, mock):
@@ -84,17 +84,17 @@ class TestFacebook(unittest.TestCase):
target = ["+15555551234"]
self.facebook.send_message(message=message, data=data, target=target)
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 1)
assert mock.called
assert mock.call_count == 1
expected_body = {
"recipient": {"phone_number": target[0]},
"message": data
}
self.assertEqual(mock.last_request.json(), expected_body)
assert mock.last_request.json() == expected_body
expected_params = {"access_token": ["page-access-token"]}
self.assertEqual(mock.last_request.qs, expected_params)
assert mock.last_request.qs == expected_params
@requests_mock.Mocker()
def test_send_targetless_message(self, mock):
@@ -106,7 +106,7 @@ class TestFacebook(unittest.TestCase):
)
self.facebook.send_message(message="goin nowhere")
self.assertFalse(mock.called)
assert not mock.called
@requests_mock.Mocker()
def test_send_message_with_400(self, mock):
@@ -125,5 +125,5 @@ class TestFacebook(unittest.TestCase):
}
)
self.facebook.send_message(message="nope!", target=["+15555551234"])
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 1)
assert mock.called
assert mock.call_count == 1
+7 -11
View File
@@ -40,14 +40,14 @@ class TestNotifyFile(unittest.TestCase):
filename = 'mock_file'
message = 'one, two, testing, testing'
with assert_setup_component(1) as handle_config:
self.assertTrue(setup_component(self.hass, notify.DOMAIN, {
assert setup_component(self.hass, notify.DOMAIN, {
'notify': {
'name': 'test',
'platform': 'file',
'filename': filename,
'timestamp': timestamp,
}
}))
})
assert handle_config[notify.DOMAIN]
m_open = mock_open()
@@ -68,21 +68,17 @@ class TestNotifyFile(unittest.TestCase):
blocking=True)
full_filename = os.path.join(self.hass.config.path(), filename)
self.assertEqual(m_open.call_count, 1)
self.assertEqual(m_open.call_args, call(full_filename, 'a'))
assert m_open.call_count == 1
assert m_open.call_args == call(full_filename, 'a')
self.assertEqual(m_open.return_value.write.call_count, 2)
assert m_open.return_value.write.call_count == 2
if not timestamp:
self.assertEqual(
m_open.return_value.write.call_args_list,
assert m_open.return_value.write.call_args_list == \
[call(title), call('{}\n'.format(message))]
)
else:
self.assertEqual(
m_open.return_value.write.call_args_list,
assert m_open.return_value.write.call_args_list == \
[call(title), call('{} {}\n'.format(
dt_util.utcnow().isoformat(), message))]
)
def test_notify_file(self):
"""Test the notify file output without timestamp."""
+20 -20
View File
@@ -68,13 +68,13 @@ class TestPushBullet(unittest.TestCase):
'message': 'Test Message'}
self.hass.services.call(notify.DOMAIN, 'test', data)
self.hass.block_till_done()
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 1)
assert mock.called
assert mock.call_count == 1
expected_body = {'body': 'Test Message',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.last_request.json(), expected_body)
assert mock.last_request.json() == expected_body
@requests_mock.Mocker()
@patch.object(PushBullet, '_get_data',
@@ -99,14 +99,14 @@ class TestPushBullet(unittest.TestCase):
'target': ['device/DESKTOP']}
self.hass.services.call(notify.DOMAIN, 'test', data)
self.hass.block_till_done()
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 1)
assert mock.called
assert mock.call_count == 1
expected_body = {'body': 'Test Message',
'device_iden': 'identity1',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.last_request.json(), expected_body)
assert mock.last_request.json() == expected_body
@requests_mock.Mocker()
@patch.object(PushBullet, '_get_data',
@@ -131,20 +131,20 @@ class TestPushBullet(unittest.TestCase):
'target': ['device/DESKTOP', 'device/My iPhone']}
self.hass.services.call(notify.DOMAIN, 'test', data)
self.hass.block_till_done()
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 2)
self.assertEqual(len(mock.request_history), 2)
assert mock.called
assert mock.call_count == 2
assert len(mock.request_history) == 2
expected_body = {'body': 'Test Message',
'device_iden': 'identity1',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.request_history[0].json(), expected_body)
assert mock.request_history[0].json() == expected_body
expected_body = {'body': 'Test Message',
'device_iden': 'identity2',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.request_history[1].json(), expected_body)
assert mock.request_history[1].json() == expected_body
@requests_mock.Mocker()
@patch.object(PushBullet, '_get_data',
@@ -169,15 +169,15 @@ class TestPushBullet(unittest.TestCase):
'target': ['email/user@host.net']}
self.hass.services.call(notify.DOMAIN, 'test', data)
self.hass.block_till_done()
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 1)
self.assertEqual(len(mock.request_history), 1)
assert mock.called
assert mock.call_count == 1
assert len(mock.request_history) == 1
expected_body = {'body': 'Test Message',
'email': 'user@host.net',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.request_history[0].json(), expected_body)
assert mock.request_history[0].json() == expected_body
@requests_mock.Mocker()
@patch.object(PushBullet, '_get_data',
@@ -202,20 +202,20 @@ class TestPushBullet(unittest.TestCase):
'target': ['device/DESKTOP', 'email/user@host.net']}
self.hass.services.call(notify.DOMAIN, 'test', data)
self.hass.block_till_done()
self.assertTrue(mock.called)
self.assertEqual(mock.call_count, 2)
self.assertEqual(len(mock.request_history), 2)
assert mock.called
assert mock.call_count == 2
assert len(mock.request_history) == 2
expected_body = {'body': 'Test Message',
'device_iden': 'identity1',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.request_history[0].json(), expected_body)
assert mock.request_history[0].json() == expected_body
expected_body = {'body': 'Test Message',
'email': 'user@host.net',
'title': 'Test Title',
'type': 'note'}
self.assertEqual(mock.request_history[1].json(), expected_body)
assert mock.request_history[1].json() == expected_body
@requests_mock.Mocker()
@patch.object(PushBullet, '_get_data',
+4 -3
View File
@@ -5,6 +5,7 @@ from unittest.mock import patch
from homeassistant.components.notify import smtp
from tests.common import get_test_home_assistant
import re
class MockSMTP(smtp.MailNotificationService):
@@ -45,14 +46,14 @@ class TestNotifySmtp(unittest.TestCase):
'Message-Id: <[^@]+@[^>]+>\n'
'\n'
'Test msg$')
self.assertRegex(msg, expected)
assert re.search(expected, msg)
@patch('email.utils.make_msgid', return_value='<mock@mock>')
def test_mixed_email(self, mock_make_msgid):
"""Test build of mixed text email behavior."""
msg = self.mailer.send_message('Test msg',
data={'images': ['test.jpg']})
self.assertTrue('Content-Type: multipart/related' in msg)
assert 'Content-Type: multipart/related' in msg
@patch('email.utils.make_msgid', return_value='<mock@mock>')
def test_html_email(self, mock_make_msgid):
@@ -73,4 +74,4 @@ class TestNotifySmtp(unittest.TestCase):
msg = self.mailer.send_message('Test msg',
data={'html': html,
'images': ['test.jpg']})
self.assertTrue('Content-Type: multipart/related' in msg)
assert 'Content-Type: multipart/related' in msg