Removing calls to mock.assert_called_once_with (#3896)

If a mock's assert_called_once_with method is misspelled (e.g. asert_called_once_with) then the test will appear as passing.  Therefore, this commit removes all instances of assert_called_once_with calls and replaces them with two assertions:

        self.assertEqual(mock.call_count, 1)
        self.assertEqual(mock.call_args, mock.call(call_args))
This commit is contained in:
Rob Capellini 2016-10-16 19:13:27 -04:00 committed by Paulus Schoutsen
parent 10c9132046
commit 4891ca1610
18 changed files with 308 additions and 109 deletions

View File

@ -53,7 +53,10 @@ class TestNX584SensorSetup(unittest.TestCase):
mock_nx.assert_has_calls( mock_nx.assert_has_calls(
[mock.call(zone, 'opening') for zone in self.fake_zones]) [mock.call(zone, 'opening') for zone in self.fake_zones])
self.assertTrue(add_devices.called) self.assertTrue(add_devices.called)
nx584_client.Client.assert_called_once_with('http://localhost:5007') self.assertEqual(nx584_client.Client.call_count, 1)
self.assertEqual(
nx584_client.Client.call_args, mock.call('http://localhost:5007')
)
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher') @mock.patch('homeassistant.components.binary_sensor.nx584.NX584Watcher')
@mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor') @mock.patch('homeassistant.components.binary_sensor.nx584.NX584ZoneSensor')
@ -73,7 +76,10 @@ class TestNX584SensorSetup(unittest.TestCase):
mock.call(self.fake_zones[2], 'motion'), mock.call(self.fake_zones[2], 'motion'),
]) ])
self.assertTrue(add_devices.called) self.assertTrue(add_devices.called)
nx584_client.Client.assert_called_once_with('http://foo:123') self.assertEqual(nx584_client.Client.call_count, 1)
self.assertEqual(
nx584_client.Client.call_args, mock.call('http://foo:123')
)
self.assertTrue(mock_watcher.called) self.assertTrue(mock_watcher.called)
def _test_assert_graceful_fail(self, config): def _test_assert_graceful_fail(self, config):
@ -174,7 +180,8 @@ class TestNX584Watcher(unittest.TestCase):
def run(fake_process): def run(fake_process):
fake_process.side_effect = StopMe fake_process.side_effect = StopMe
self.assertRaises(StopMe, watcher._run) self.assertRaises(StopMe, watcher._run)
fake_process.assert_called_once_with(fake_events[0]) self.assertEqual(fake_process.call_count, 1)
self.assertEqual(fake_process.call_args, mock.call(fake_events[0]))
run() run()
self.assertEqual(3, client.get_events.call_count) self.assertEqual(3, client.get_events.call_count)

View File

@ -44,9 +44,17 @@ class TestBinarySensorTemplate(unittest.TestCase):
add_devices = mock.MagicMock() add_devices = mock.MagicMock()
result = template.setup_platform(self.hass, config, add_devices) result = template.setup_platform(self.hass, config, add_devices)
self.assertTrue(result) self.assertTrue(result)
mock_template.assert_called_once_with( self.assertEqual(mock_template.call_count, 1)
self.hass, 'test', 'virtual thingy', 'motion', tpl, 'test') self.assertEqual(
add_devices.assert_called_once_with([mock_template.return_value]) mock_template.call_args,
mock.call(
self.hass, 'test', 'virtual thingy', 'motion', tpl, 'test'
)
)
self.assertEqual(add_devices.call_count, 1)
self.assertEqual(
add_devices.call_args, mock.call([mock_template.return_value])
)
def test_setup_no_sensors(self): def test_setup_no_sensors(self):
""""Test setup with no sensors.""" """"Test setup with no sensors."""

View File

@ -54,7 +54,10 @@ class TestUVCSetup(unittest.TestCase):
assert setup_component(self.hass, 'camera', {'camera': config}) assert setup_component(self.hass, 'camera', {'camera': config})
mock_remote.assert_called_once_with('foo', 123, 'secret') self.assertEqual(mock_remote.call_count, 1)
self.assertEqual(
mock_remote.call_args, mock.call('foo', 123, 'secret')
)
mock_uvc.assert_has_calls([ mock_uvc.assert_has_calls([
mock.call(mock_remote.return_value, 'id1', 'Front'), mock.call(mock_remote.return_value, 'id1', 'Front'),
mock.call(mock_remote.return_value, 'id2', 'Back'), mock.call(mock_remote.return_value, 'id2', 'Back'),
@ -79,7 +82,10 @@ class TestUVCSetup(unittest.TestCase):
assert setup_component(self.hass, 'camera', {'camera': config}) assert setup_component(self.hass, 'camera', {'camera': config})
mock_remote.assert_called_once_with('foo', 7080, 'secret') self.assertEqual(mock_remote.call_count, 1)
self.assertEqual(
mock_remote.call_args, mock.call('foo', 7080, 'secret')
)
mock_uvc.assert_has_calls([ mock_uvc.assert_has_calls([
mock.call(mock_remote.return_value, 'id1', 'Front'), mock.call(mock_remote.return_value, 'id1', 'Front'),
mock.call(mock_remote.return_value, 'id2', 'Back'), mock.call(mock_remote.return_value, 'id2', 'Back'),
@ -104,7 +110,10 @@ class TestUVCSetup(unittest.TestCase):
assert setup_component(self.hass, 'camera', {'camera': config}) assert setup_component(self.hass, 'camera', {'camera': config})
mock_remote.assert_called_once_with('foo', 7080, 'secret') self.assertEqual(mock_remote.call_count, 1)
self.assertEqual(
mock_remote.call_args, mock.call('foo', 7080, 'secret')
)
mock_uvc.assert_has_calls([ mock_uvc.assert_has_calls([
mock.call(mock_remote.return_value, 'one', 'Front'), mock.call(mock_remote.return_value, 'one', 'Front'),
mock.call(mock_remote.return_value, 'two', 'Back'), mock.call(mock_remote.return_value, 'two', 'Back'),
@ -173,8 +182,12 @@ class TestUVC(unittest.TestCase):
""""Test the login.""" """"Test the login."""
mock_store.return_value.get_camera_password.return_value = 'seekret' mock_store.return_value.get_camera_password.return_value = 'seekret'
self.uvc._login() self.uvc._login()
mock_camera.assert_called_once_with('host-a', 'admin', 'seekret') self.assertEqual(mock_camera.call_count, 1)
mock_camera.return_value.login.assert_called_once_with() self.assertEqual(
mock_camera.call_args, mock.call('host-a', 'admin', 'seekret')
)
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())
@mock.patch('uvcclient.store.get_info_store') @mock.patch('uvcclient.store.get_info_store')
@mock.patch('uvcclient.camera.UVCCameraClient') @mock.patch('uvcclient.camera.UVCCameraClient')
@ -183,8 +196,12 @@ class TestUVC(unittest.TestCase):
mock_store.return_value.get_camera_password.return_value = 'seekret' mock_store.return_value.get_camera_password.return_value = 'seekret'
self.nvr.server_version = (3, 1, 3) self.nvr.server_version = (3, 1, 3)
self.uvc._login() self.uvc._login()
mock_camera.assert_called_once_with('host-a', 'admin', 'seekret') self.assertEqual(mock_camera.call_count, 1)
mock_camera.return_value.login.assert_called_once_with() self.assertEqual(
mock_camera.call_args, mock.call('host-a', 'admin', 'seekret')
)
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())
@mock.patch('uvcclient.store.get_info_store') @mock.patch('uvcclient.store.get_info_store')
@mock.patch('uvcclient.camera.UVCCameraClientV320') @mock.patch('uvcclient.camera.UVCCameraClientV320')
@ -192,8 +209,12 @@ class TestUVC(unittest.TestCase):
""""Test the login with no password.""" """"Test the login with no password."""
mock_store.return_value.get_camera_password.return_value = None mock_store.return_value.get_camera_password.return_value = None
self.uvc._login() self.uvc._login()
mock_camera.assert_called_once_with('host-a', 'admin', 'ubnt') self.assertEqual(mock_camera.call_count, 1)
mock_camera.return_value.login.assert_called_once_with() self.assertEqual(
mock_camera.call_args, mock.call('host-a', 'admin', 'ubnt')
)
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())
@mock.patch('uvcclient.store.get_info_store') @mock.patch('uvcclient.store.get_info_store')
@mock.patch('uvcclient.camera.UVCCameraClientV320') @mock.patch('uvcclient.camera.UVCCameraClientV320')
@ -216,8 +237,12 @@ class TestUVC(unittest.TestCase):
mock_camera.reset_mock() mock_camera.reset_mock()
self.uvc._login() self.uvc._login()
mock_camera.assert_called_once_with('host-b', 'admin', 'ubnt') self.assertEqual(mock_camera.call_count, 1)
mock_camera.return_value.login.assert_called_once_with() self.assertEqual(
mock_camera.call_args, mock.call('host-b', 'admin', 'ubnt')
)
self.assertEqual(mock_camera.return_value.login.call_count, 1)
self.assertEqual(mock_camera.return_value.login.call_args, mock.call())
@mock.patch('uvcclient.store.get_info_store') @mock.patch('uvcclient.store.get_info_store')
@mock.patch('uvcclient.camera.UVCCameraClientV320') @mock.patch('uvcclient.camera.UVCCameraClientV320')
@ -232,7 +257,8 @@ class TestUVC(unittest.TestCase):
with mock.patch.object(self.uvc, '_login') as mock_login: with mock.patch.object(self.uvc, '_login') as mock_login:
mock_login.return_value = False mock_login.return_value = False
self.assertEqual(None, self.uvc.camera_image()) self.assertEqual(None, self.uvc.camera_image())
mock_login.assert_called_once_with() self.assertEqual(mock_login.call_count, 1)
self.assertEqual(mock_login.call_args, mock.call())
def test_camera_image_logged_in(self): def test_camera_image_logged_in(self):
""""Test the login state.""" """"Test the login state."""
@ -262,7 +288,8 @@ class TestUVC(unittest.TestCase):
self.uvc._camera.get_snapshot.side_effect = fake_snapshot self.uvc._camera.get_snapshot.side_effect = fake_snapshot
with mock.patch.object(self.uvc, '_login') as mock_login: with mock.patch.object(self.uvc, '_login') as mock_login:
self.assertEqual('image', self.uvc.camera_image()) self.assertEqual('image', self.uvc.camera_image())
mock_login.assert_called_once_with() self.assertEqual(mock_login.call_count, 1)
self.assertEqual(mock_login.call_args, mock.call())
self.assertEqual([], responses) self.assertEqual([], responses)
def test_camera_image_reauths_only_once(self): def test_camera_image_reauths_only_once(self):
@ -271,4 +298,5 @@ class TestUVC(unittest.TestCase):
self.uvc._camera.get_snapshot.side_effect = camera.CameraAuthError self.uvc._camera.get_snapshot.side_effect = camera.CameraAuthError
with mock.patch.object(self.uvc, '_login') as mock_login: with mock.patch.object(self.uvc, '_login') as mock_login:
self.assertRaises(camera.CameraAuthError, self.uvc.camera_image) self.assertRaises(camera.CameraAuthError, self.uvc.camera_image)
mock_login.assert_called_once_with() self.assertEqual(mock_login.call_count, 1)
self.assertEqual(mock_login.call_args, mock.call())

View File

@ -62,7 +62,8 @@ class TestHoneywell(unittest.TestCase):
result = honeywell.setup_platform(hass, config, add_devices) result = honeywell.setup_platform(hass, config, add_devices)
self.assertTrue(result) self.assertTrue(result)
mock_sc.assert_called_once_with('user', 'pass') self.assertEqual(mock_sc.call_count, 1)
self.assertEqual(mock_sc.call_args, mock.call('user', 'pass'))
mock_ht.assert_has_calls([ mock_ht.assert_has_calls([
mock.call(mock_sc.return_value, devices_1[0]), mock.call(mock_sc.return_value, devices_1[0]),
mock.call(mock_sc.return_value, devices_2[0]), mock.call(mock_sc.return_value, devices_2[0]),
@ -174,9 +175,13 @@ class TestHoneywell(unittest.TestCase):
hass = mock.MagicMock() hass = mock.MagicMock()
add_devices = mock.MagicMock() add_devices = mock.MagicMock()
self.assertTrue(honeywell.setup_platform(hass, config, add_devices)) self.assertTrue(honeywell.setup_platform(hass, config, add_devices))
mock_evo.assert_called_once_with('user', 'pass') self.assertEqual(mock_evo.call_count, 1)
mock_evo.return_value.temperatures.assert_called_once_with( self.assertEqual(mock_evo.call_args, mock.call('user', 'pass'))
force_refresh=True) self.assertEqual(mock_evo.return_value.temperatures.call_count, 1)
self.assertEqual(
mock_evo.return_value.temperatures.call_args,
mock.call(force_refresh=True)
)
mock_round.assert_has_calls([ mock_round.assert_has_calls([
mock.call(mock_evo.return_value, 'foo', True, 20.0), mock.call(mock_evo.return_value, 'foo', True, 20.0),
mock.call(mock_evo.return_value, 'bar', False, 20.0), mock.call(mock_evo.return_value, 'bar', False, 20.0),
@ -280,17 +285,26 @@ class TestHoneywellRound(unittest.TestCase):
self.assertFalse(self.round1.is_away_mode_on) self.assertFalse(self.round1.is_away_mode_on)
self.round1.turn_away_mode_on() self.round1.turn_away_mode_on()
self.assertTrue(self.round1.is_away_mode_on) self.assertTrue(self.round1.is_away_mode_on)
self.device.set_temperature.assert_called_once_with('House', 16) self.assertEqual(self.device.set_temperature.call_count, 1)
self.assertEqual(
self.device.set_temperature.call_args, mock.call('House', 16)
)
self.device.set_temperature.reset_mock() self.device.set_temperature.reset_mock()
self.round1.turn_away_mode_off() self.round1.turn_away_mode_off()
self.assertFalse(self.round1.is_away_mode_on) self.assertFalse(self.round1.is_away_mode_on)
self.device.cancel_temp_override.assert_called_once_with('House') self.assertEqual(self.device.cancel_temp_override.call_count, 1)
self.assertEqual(
self.device.cancel_temp_override.call_args, mock.call('House')
)
def test_set_temperature(self): def test_set_temperature(self):
"""Test setting the temperature.""" """Test setting the temperature."""
self.round1.set_temperature(temperature=25) self.round1.set_temperature(temperature=25)
self.device.set_temperature.assert_called_once_with('House', 25) self.assertEqual(self.device.set_temperature.call_count, 1)
self.assertEqual(
self.device.set_temperature.call_args, mock.call('House', 25)
)
def test_set_operation_mode(self: unittest.TestCase) -> None: def test_set_operation_mode(self: unittest.TestCase) -> None:
"""Test setting the system operation.""" """Test setting the system operation."""

View File

@ -40,7 +40,10 @@ class TestCommandCover(unittest.TestCase):
mock_run.return_value = b' foo bar ' mock_run.return_value = b' foo bar '
result = self.rs._query_state_value('runme') result = self.rs._query_state_value('runme')
self.assertEqual('foo bar', result) self.assertEqual('foo bar', result)
mock_run.assert_called_once_with('runme', shell=True) self.assertEqual(mock_run.call_count, 1)
self.assertEqual(
mock_run.call_args, mock.call('runme', shell=True)
)
def test_state_value(self): def test_state_value(self):
"""Test with state value.""" """Test with state value."""

View File

@ -79,7 +79,8 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
conf_dict[DOMAIN][CONF_MODE] = 'router' conf_dict[DOMAIN][CONF_MODE] = 'router'
conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh' conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh'
asuswrt_mock.assert_called_once_with(conf_dict[DOMAIN]) self.assertEqual(asuswrt_mock.call_count, 1)
self.assertEqual(asuswrt_mock.call_args, mock.call(conf_dict[DOMAIN]))
@mock.patch( @mock.patch(
'homeassistant.components.device_tracker.asuswrt.AsusWrtDeviceScanner', 'homeassistant.components.device_tracker.asuswrt.AsusWrtDeviceScanner',
@ -101,7 +102,8 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
conf_dict[DOMAIN][CONF_MODE] = 'router' conf_dict[DOMAIN][CONF_MODE] = 'router'
conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh' conf_dict[DOMAIN][CONF_PROTOCOL] = 'ssh'
asuswrt_mock.assert_called_once_with(conf_dict[DOMAIN]) self.assertEqual(asuswrt_mock.call_count, 1)
self.assertEqual(asuswrt_mock.call_args, mock.call(conf_dict[DOMAIN]))
def test_ssh_login_with_pub_key(self): def test_ssh_login_with_pub_key(self):
"""Test that login is done with pub_key when configured to.""" """Test that login is done with pub_key when configured to."""
@ -122,8 +124,11 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
self.addCleanup(update_mock.stop) self.addCleanup(update_mock.stop)
asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict) asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict)
asuswrt.ssh_connection() asuswrt.ssh_connection()
ssh.login.assert_called_once_with('fake_host', 'fake_user', self.assertEqual(ssh.login.call_count, 1)
ssh_key=FAKEFILE) self.assertEqual(
ssh.login.call_args,
mock.call('fake_host', 'fake_user', ssh_key=FAKEFILE)
)
def test_ssh_login_with_password(self): def test_ssh_login_with_password(self):
"""Test that login is done with password when configured to.""" """Test that login is done with password when configured to."""
@ -144,8 +149,11 @@ class TestComponentsDeviceTrackerASUSWRT(unittest.TestCase):
self.addCleanup(update_mock.stop) self.addCleanup(update_mock.stop)
asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict) asuswrt = device_tracker.asuswrt.AsusWrtDeviceScanner(conf_dict)
asuswrt.ssh_connection() asuswrt.ssh_connection()
ssh.login.assert_called_once_with('fake_host', 'fake_user', self.assertEqual(ssh.login.call_count, 1)
password='fake_pass') self.assertEqual(
ssh.login.call_args,
mock.call('fake_host', 'fake_user', password='fake_pass')
)
def test_ssh_login_without_password_or_pubkey(self): \ def test_ssh_login_without_password_or_pubkey(self): \
# pylint: disable=invalid-name # pylint: disable=invalid-name

View File

@ -2,7 +2,7 @@
# pylint: disable=protected-access,too-many-public-methods # pylint: disable=protected-access,too-many-public-methods
import logging import logging
import unittest import unittest
from unittest.mock import patch from unittest.mock import call, patch
from datetime import datetime, timedelta from datetime import datetime, timedelta
import os import os
@ -288,7 +288,8 @@ class TestComponentsDeviceTracker(unittest.TestCase):
device_tracker.see(self.hass, **params) device_tracker.see(self.hass, **params)
self.hass.block_till_done() self.hass.block_till_done()
assert mock_see.call_count == 1 assert mock_see.call_count == 1
mock_see.assert_called_once_with(**params) self.assertEqual(mock_see.call_count, 1)
self.assertEqual(mock_see.call_args, call(**params))
mock_see.reset_mock() mock_see.reset_mock()
params['dev_id'] += chr(233) # e' acute accent from icloud params['dev_id'] += chr(233) # e' acute accent from icloud
@ -296,7 +297,8 @@ class TestComponentsDeviceTracker(unittest.TestCase):
device_tracker.see(self.hass, **params) device_tracker.see(self.hass, **params)
self.hass.block_till_done() self.hass.block_till_done()
assert mock_see.call_count == 1 assert mock_see.call_count == 1
mock_see.assert_called_once_with(**params) self.assertEqual(mock_see.call_count, 1)
self.assertEqual(mock_see.call_args, call(**params))
def test_not_write_duplicate_yaml_keys(self): \ def test_not_write_duplicate_yaml_keys(self): \
# pylint: disable=invalid-name # pylint: disable=invalid-name

View File

@ -27,9 +27,16 @@ class TestUnifiScanner(unittest.TestCase):
} }
result = unifi.get_scanner(None, config) result = unifi.get_scanner(None, config)
self.assertEqual(mock_scanner.return_value, result) self.assertEqual(mock_scanner.return_value, result)
mock_ctrl.assert_called_once_with('localhost', 'foo', 'password', self.assertEqual(mock_ctrl.call_count, 1)
8443, 'v4', 'default') self.assertEqual(
mock_scanner.assert_called_once_with(mock_ctrl.return_value) mock_ctrl.call_args,
mock.call('localhost', 'foo', 'password', 8443, 'v4', 'default')
)
self.assertEqual(mock_scanner.call_count, 1)
self.assertEqual(
mock_scanner.call_args,
mock.call(mock_ctrl.return_value)
)
@mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner') @mock.patch('homeassistant.components.device_tracker.unifi.UnifiScanner')
@mock.patch.object(controller, 'Controller') @mock.patch.object(controller, 'Controller')
@ -47,9 +54,16 @@ class TestUnifiScanner(unittest.TestCase):
} }
result = unifi.get_scanner(None, config) result = unifi.get_scanner(None, config)
self.assertEqual(mock_scanner.return_value, result) self.assertEqual(mock_scanner.return_value, result)
mock_ctrl.assert_called_once_with('myhost', 'foo', 'password', self.assertEqual(mock_ctrl.call_count, 1)
123, 'v4', 'abcdef01') self.assertEqual(
mock_scanner.assert_called_once_with(mock_ctrl.return_value) mock_ctrl.call_args,
mock.call('myhost', 'foo', 'password', 123, 'v4', 'abcdef01')
)
self.assertEqual(mock_scanner.call_count, 1)
self.assertEqual(
mock_scanner.call_args,
mock.call(mock_ctrl.return_value)
)
def test_config_error(self): def test_config_error(self):
"""Test for configuration errors.""" """Test for configuration errors."""
@ -94,7 +108,8 @@ class TestUnifiScanner(unittest.TestCase):
] ]
ctrl.get_clients.return_value = fake_clients ctrl.get_clients.return_value = fake_clients
unifi.UnifiScanner(ctrl) unifi.UnifiScanner(ctrl)
ctrl.get_clients.assert_called_once_with() self.assertEqual(ctrl.get_clients.call_count, 1)
self.assertEqual(ctrl.get_clients.call_args, mock.call())
def test_scanner_update_error(self): # pylint: disable=no-self-use def test_scanner_update_error(self): # pylint: disable=no-self-use
"""Test the scanner update for error.""" """Test the scanner update for error."""

View File

@ -125,7 +125,8 @@ class TestSonosMediaPlayer(unittest.TestCase):
device = sonos.DEVICES[-1] device = sonos.DEVICES[-1]
partymodeMock.return_value = True partymodeMock.return_value = True
device.group_players() device.group_players()
partymodeMock.assert_called_once_with() self.assertEqual(partymodeMock.call_count, 1)
self.assertEqual(partymodeMock.call_args, mock.call())
@mock.patch('soco.SoCo', new=SoCoMock) @mock.patch('soco.SoCo', new=SoCoMock)
@mock.patch.object(SoCoMock, 'unjoin') @mock.patch.object(SoCoMock, 'unjoin')
@ -135,7 +136,8 @@ class TestSonosMediaPlayer(unittest.TestCase):
device = sonos.DEVICES[-1] device = sonos.DEVICES[-1]
unjoinMock.return_value = True unjoinMock.return_value = True
device.unjoin() device.unjoin()
unjoinMock.assert_called_once_with() self.assertEqual(unjoinMock.call_count, 1)
self.assertEqual(unjoinMock.call_args, mock.call())
@mock.patch('soco.SoCo', new=SoCoMock) @mock.patch('soco.SoCo', new=SoCoMock)
@mock.patch.object(soco.snapshot.Snapshot, 'snapshot') @mock.patch.object(soco.snapshot.Snapshot, 'snapshot')
@ -145,7 +147,8 @@ class TestSonosMediaPlayer(unittest.TestCase):
device = sonos.DEVICES[-1] device = sonos.DEVICES[-1]
snapshotMock.return_value = True snapshotMock.return_value = True
device.snapshot() device.snapshot()
snapshotMock.assert_called_once_with() self.assertEqual(snapshotMock.call_count, 1)
self.assertEqual(snapshotMock.call_args, mock.call())
@mock.patch('soco.SoCo', new=SoCoMock) @mock.patch('soco.SoCo', new=SoCoMock)
@mock.patch.object(soco.snapshot.Snapshot, 'restore') @mock.patch.object(soco.snapshot.Snapshot, 'restore')
@ -155,4 +158,5 @@ class TestSonosMediaPlayer(unittest.TestCase):
device = sonos.DEVICES[-1] device = sonos.DEVICES[-1]
restoreMock.return_value = True restoreMock.return_value = True
device.restore() device.restore()
restoreMock.assert_called_once_with(True) self.assertEqual(restoreMock.call_count, 1)
self.assertEqual(restoreMock.call_args, mock.call(True))

View File

@ -41,7 +41,10 @@ class TestCommandRollerShutter(unittest.TestCase):
mock_run.return_value = b' foo bar ' mock_run.return_value = b' foo bar '
result = self.rs._query_state_value('runme') result = self.rs._query_state_value('runme')
self.assertEqual('foo bar', result) self.assertEqual('foo bar', result)
mock_run.assert_called_once_with('runme', shell=True) self.assertEqual(mock_run.call_count, 1)
self.assertEqual(
mock_run.call_args, mock.call('runme', shell=True)
)
def test_state_value(self): def test_state_value(self):
"""Test with state value.""" """Test with state value."""

View File

@ -71,8 +71,13 @@ class TestMfiSensorSetup(unittest.TestCase):
config = dict(self.GOOD_CONFIG) config = dict(self.GOOD_CONFIG)
del config[self.THING]['port'] del config[self.THING]['port']
assert setup_component(self.hass, self.COMPONENT.DOMAIN, config) assert setup_component(self.hass, self.COMPONENT.DOMAIN, config)
mock_client.assert_called_once_with( self.assertEqual(mock_client.call_count, 1)
'foo', 'user', 'pass', port=6443, use_tls=True, verify=True) self.assertEqual(
mock_client.call_args,
mock.call(
'foo', 'user', 'pass', port=6443, use_tls=True, verify=True
)
)
@mock.patch('mficlient.client.MFiClient') @mock.patch('mficlient.client.MFiClient')
def test_setup_with_port(self, mock_client): def test_setup_with_port(self, mock_client):
@ -80,8 +85,13 @@ class TestMfiSensorSetup(unittest.TestCase):
config = dict(self.GOOD_CONFIG) config = dict(self.GOOD_CONFIG)
config[self.THING]['port'] = 6123 config[self.THING]['port'] = 6123
assert setup_component(self.hass, self.COMPONENT.DOMAIN, config) assert setup_component(self.hass, self.COMPONENT.DOMAIN, config)
mock_client.assert_called_once_with( self.assertEqual(mock_client.call_count, 1)
'foo', 'user', 'pass', port=6123, use_tls=True, verify=True) self.assertEqual(
mock_client.call_args,
mock.call(
'foo', 'user', 'pass', port=6123, use_tls=True, verify=True
)
)
@mock.patch('mficlient.client.MFiClient') @mock.patch('mficlient.client.MFiClient')
def test_setup_with_tls_disabled(self, mock_client): def test_setup_with_tls_disabled(self, mock_client):
@ -91,8 +101,13 @@ class TestMfiSensorSetup(unittest.TestCase):
config[self.THING]['ssl'] = False config[self.THING]['ssl'] = False
config[self.THING]['verify_ssl'] = False config[self.THING]['verify_ssl'] = False
assert setup_component(self.hass, self.COMPONENT.DOMAIN, config) assert setup_component(self.hass, self.COMPONENT.DOMAIN, config)
mock_client.assert_called_once_with( self.assertEqual(mock_client.call_count, 1)
'foo', 'user', 'pass', port=6080, use_tls=False, verify=False) self.assertEqual(
mock_client.call_args,
mock.call(
'foo', 'user', 'pass', port=6080, use_tls=False, verify=False
)
)
@mock.patch('mficlient.client.MFiClient') @mock.patch('mficlient.client.MFiClient')
@mock.patch('homeassistant.components.sensor.mfi.MfiSensor') @mock.patch('homeassistant.components.sensor.mfi.MfiSensor')
@ -180,4 +195,5 @@ class TestMfiSensor(unittest.TestCase):
def test_update(self): def test_update(self):
"""Test the update.""" """Test the update."""
self.sensor.update() self.sensor.update()
self.port.refresh.assert_called_once_with() self.assertEqual(self.port.refresh.call_count, 1)
self.assertEqual(self.port.refresh.call_args, mock.call())

View File

@ -65,7 +65,8 @@ class TestMfiSwitch(unittest.TestCase):
def test_update(self): def test_update(self):
"""Test update.""" """Test update."""
self.switch.update() self.switch.update()
self.port.refresh.assert_called_once_with() self.assertEqual(self.port.refresh.call_count, 1)
self.assertEqual(self.port.refresh.call_args, mock.call())
def test_update_with_target_state(self): def test_update_with_target_state(self):
"""Test update with target state.""" """Test update with target state."""
@ -82,13 +83,15 @@ class TestMfiSwitch(unittest.TestCase):
def test_turn_on(self): def test_turn_on(self):
"""Test turn_on.""" """Test turn_on."""
self.switch.turn_on() self.switch.turn_on()
self.port.control.assert_called_once_with(True) self.assertEqual(self.port.control.call_count, 1)
self.assertEqual(self.port.control.call_args, mock.call(True))
self.assertTrue(self.switch._target_state) self.assertTrue(self.switch._target_state)
def test_turn_off(self): def test_turn_off(self):
"""Test turn_off.""" """Test turn_off."""
self.switch.turn_off() self.switch.turn_off()
self.port.control.assert_called_once_with(False) self.assertEqual(self.port.control.call_count, 1)
self.assertEqual(self.port.control.call_args, mock.call(False))
self.assertFalse(self.switch._target_state) self.assertFalse(self.switch._target_state)
def test_current_power_mwh(self): def test_current_power_mwh(self):

View File

@ -29,7 +29,11 @@ class TestGraphite(unittest.TestCase):
def test_setup(self, mock_socket): def test_setup(self, mock_socket):
"""Test setup.""" """Test setup."""
assert setup_component(self.hass, graphite.DOMAIN, {'graphite': {}}) assert setup_component(self.hass, graphite.DOMAIN, {'graphite': {}})
mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(mock_socket.call_count, 1)
self.assertEqual(
mock_socket.call_args,
mock.call(socket.AF_INET, socket.SOCK_STREAM)
)
@patch('socket.socket') @patch('socket.socket')
@patch('homeassistant.components.graphite.GraphiteFeeder') @patch('homeassistant.components.graphite.GraphiteFeeder')
@ -44,8 +48,15 @@ class TestGraphite(unittest.TestCase):
} }
self.assertTrue(setup_component(self.hass, graphite.DOMAIN, config)) self.assertTrue(setup_component(self.hass, graphite.DOMAIN, config))
mock_gf.assert_called_once_with(self.hass, 'foo', 123, 'me') self.assertEqual(mock_gf.call_count, 1)
mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(
mock_gf.call_args, mock.call(self.hass, 'foo', 123, 'me')
)
self.assertEqual(mock_socket.call_count, 1)
self.assertEqual(
mock_socket.call_args,
mock.call(socket.AF_INET, socket.SOCK_STREAM)
)
@patch('socket.socket') @patch('socket.socket')
@patch('homeassistant.components.graphite.GraphiteFeeder') @patch('homeassistant.components.graphite.GraphiteFeeder')
@ -60,7 +71,11 @@ class TestGraphite(unittest.TestCase):
self.assertTrue(setup_component(self.hass, graphite.DOMAIN, config)) self.assertTrue(setup_component(self.hass, graphite.DOMAIN, config))
self.assertTrue(mock_gf.called) self.assertTrue(mock_gf.called)
mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM) self.assertEqual(mock_socket.call_count, 1)
self.assertEqual(
mock_socket.call_args,
mock.call(socket.AF_INET, socket.SOCK_STREAM)
)
def test_subscribe(self): def test_subscribe(self):
"""Test the subscription.""" """Test the subscription."""
@ -70,26 +85,34 @@ class TestGraphite(unittest.TestCase):
mock.call(EVENT_HOMEASSISTANT_START, gf.start_listen), mock.call(EVENT_HOMEASSISTANT_START, gf.start_listen),
mock.call(EVENT_HOMEASSISTANT_STOP, gf.shutdown), mock.call(EVENT_HOMEASSISTANT_STOP, gf.shutdown),
]) ])
fake_hass.bus.listen.assert_called_once_with( self.assertEqual(fake_hass.bus.listen.call_count, 1)
EVENT_STATE_CHANGED, gf.event_listener) self.assertEqual(
fake_hass.bus.listen.call_args,
mock.call(EVENT_STATE_CHANGED, gf.event_listener)
)
def test_start(self): def test_start(self):
"""Test the start.""" """Test the start."""
with mock.patch.object(self.gf, 'start') as mock_start: with mock.patch.object(self.gf, 'start') as mock_start:
self.gf.start_listen('event') self.gf.start_listen('event')
mock_start.assert_called_once_with() self.assertEqual(mock_start.call_count, 1)
self.assertEqual(mock_start.call_args, mock.call())
def test_shutdown(self): def test_shutdown(self):
"""Test the shutdown.""" """Test the shutdown."""
with mock.patch.object(self.gf, '_queue') as mock_queue: with mock.patch.object(self.gf, '_queue') as mock_queue:
self.gf.shutdown('event') self.gf.shutdown('event')
mock_queue.put.assert_called_once_with(self.gf._quit_object) self.assertEqual(mock_queue.put.call_count, 1)
self.assertEqual(
mock_queue.put.call_args, mock.call(self.gf._quit_object)
)
def test_event_listener(self): def test_event_listener(self):
"""Test the event listener.""" """Test the event listener."""
with mock.patch.object(self.gf, '_queue') as mock_queue: with mock.patch.object(self.gf, '_queue') as mock_queue:
self.gf.event_listener('foo') self.gf.event_listener('foo')
mock_queue.put.assert_called_once_with('foo') self.assertEqual(mock_queue.put.call_count, 1)
self.assertEqual(mock_queue.put.call_args, mock.call('foo'))
@patch('time.time') @patch('time.time')
def test_report_attributes(self, mock_time): def test_report_attributes(self, mock_time):
@ -164,21 +187,32 @@ class TestGraphite(unittest.TestCase):
def test_send_to_graphite(self, mock_socket): def test_send_to_graphite(self, mock_socket):
"""Test the sending of data.""" """Test the sending of data."""
self.gf._send_to_graphite('foo') self.gf._send_to_graphite('foo')
mock_socket.assert_called_once_with(socket.AF_INET, self.assertEqual(mock_socket.call_count, 1)
socket.SOCK_STREAM) self.assertEqual(
mock_socket.call_args,
mock.call(socket.AF_INET, socket.SOCK_STREAM)
)
sock = mock_socket.return_value sock = mock_socket.return_value
sock.connect.assert_called_once_with(('foo', 123)) self.assertEqual(sock.connect.call_count, 1)
sock.sendall.assert_called_once_with('foo'.encode('ascii')) self.assertEqual(sock.connect.call_args, mock.call(('foo', 123)))
sock.send.assert_called_once_with('\n'.encode('ascii')) self.assertEqual(sock.sendall.call_count, 1)
sock.close.assert_called_once_with() self.assertEqual(
sock.sendall.call_args, mock.call('foo'.encode('ascii'))
)
self.assertEqual(sock.send.call_count, 1)
self.assertEqual(sock.send.call_args, mock.call('\n'.encode('ascii')))
self.assertEqual(sock.close.call_count, 1)
self.assertEqual(sock.close.call_args, mock.call())
def test_run_stops(self): def test_run_stops(self):
"""Test the stops.""" """Test the stops."""
with mock.patch.object(self.gf, '_queue') as mock_queue: with mock.patch.object(self.gf, '_queue') as mock_queue:
mock_queue.get.return_value = self.gf._quit_object mock_queue.get.return_value = self.gf._quit_object
self.assertEqual(None, self.gf.run()) self.assertEqual(None, self.gf.run())
mock_queue.get.assert_called_once_with() self.assertEqual(mock_queue.get.call_count, 1)
mock_queue.task_done.assert_called_once_with() self.assertEqual(mock_queue.get.call_args, mock.call())
self.assertEqual(mock_queue.task_done.call_count, 1)
self.assertEqual(mock_queue.task_done.call_args, mock.call())
def test_run(self): def test_run(self):
"""Test the running.""" """Test the running."""
@ -204,6 +238,8 @@ class TestGraphite(unittest.TestCase):
self.gf.run() self.gf.run()
# Twice for two events, once for the stop # Twice for two events, once for the stop
self.assertEqual(3, mock_queue.task_done.call_count) self.assertEqual(3, mock_queue.task_done.call_count)
mock_r.assert_called_once_with( self.assertEqual(mock_r.call_count, 1)
'entity', self.assertEqual(
event.data['new_state']) mock_r.call_args,
mock.call('entity', event.data['new_state'])
)

View File

@ -131,7 +131,13 @@ class TestInfluxDB(unittest.TestCase):
}, },
}] }]
self.handler_method(event) self.handler_method(event)
mock_client.return_value.write_points.assert_called_once_with(body) self.assertEqual(
mock_client.return_value.write_points.call_count, 1
)
self.assertEqual(
mock_client.return_value.write_points.call_args,
mock.call(body)
)
mock_client.return_value.write_points.reset_mock() mock_client.return_value.write_points.reset_mock()
def test_event_listener_no_units(self, mock_client): def test_event_listener_no_units(self, mock_client):
@ -162,7 +168,13 @@ class TestInfluxDB(unittest.TestCase):
}, },
}] }]
self.handler_method(event) self.handler_method(event)
mock_client.return_value.write_points.assert_called_once_with(body) self.assertEqual(
mock_client.return_value.write_points.call_count, 1
)
self.assertEqual(
mock_client.return_value.write_points.call_args,
mock.call(body)
)
mock_client.return_value.write_points.reset_mock() mock_client.return_value.write_points.reset_mock()
def test_event_listener_fail_write(self, mock_client): def test_event_listener_fail_write(self, mock_client):
@ -205,8 +217,13 @@ class TestInfluxDB(unittest.TestCase):
}] }]
self.handler_method(event) self.handler_method(event)
if state_state == 1: if state_state == 1:
mock_client.return_value.write_points.assert_called_once_with( self.assertEqual(
body) mock_client.return_value.write_points.call_count, 1
)
self.assertEqual(
mock_client.return_value.write_points.call_args,
mock.call(body)
)
else: else:
self.assertFalse(mock_client.return_value.write_points.called) self.assertFalse(mock_client.return_value.write_points.called)
mock_client.return_value.write_points.reset_mock() mock_client.return_value.write_points.reset_mock()
@ -236,8 +253,13 @@ class TestInfluxDB(unittest.TestCase):
}] }]
self.handler_method(event) self.handler_method(event)
if entity_id == 'ok': if entity_id == 'ok':
mock_client.return_value.write_points.assert_called_once_with( self.assertEqual(
body) mock_client.return_value.write_points.call_count, 1
)
self.assertEqual(
mock_client.return_value.write_points.call_args,
mock.call(body)
)
else: else:
self.assertFalse(mock_client.return_value.write_points.called) self.assertFalse(mock_client.return_value.write_points.called)
mock_client.return_value.write_points.reset_mock() mock_client.return_value.write_points.reset_mock()

View File

@ -84,6 +84,9 @@ class TestLogentries(unittest.TestCase):
'logs/token', 'logs/token',
'event': body} 'event': body}
self.handler_method(event) self.handler_method(event)
self.mock_post.assert_called_once_with( self.assertEqual(self.mock_post.call_count, 1)
payload['host'], data=payload, timeout=10) self.assertEqual(
self.mock_post.call_args,
mock.call(payload['host'], data=payload, timeout=10)
)
self.mock_post.reset_mock() self.mock_post.reset_mock()

View File

@ -94,7 +94,12 @@ class TestSplunk(unittest.TestCase):
payload = {'host': 'http://host:8088/services/collector/event', payload = {'host': 'http://host:8088/services/collector/event',
'event': body} 'event': body}
self.handler_method(event) self.handler_method(event)
self.mock_post.assert_called_once_with( self.assertEqual(self.mock_post.call_count, 1)
payload['host'], data=payload, self.assertEqual(
headers={'Authorization': 'Splunk secret'}) self.mock_post.call_args,
mock.call(
payload['host'], data=payload,
headers={'Authorization': 'Splunk secret'}
)
)
self.mock_post.reset_mock() self.mock_post.reset_mock()

View File

@ -40,10 +40,11 @@ class TestStatsd(unittest.TestCase):
hass = mock.MagicMock() hass = mock.MagicMock()
hass.pool.worker_count = 2 hass.pool.worker_count = 2
self.assertTrue(setup_component(hass, statsd.DOMAIN, config)) self.assertTrue(setup_component(hass, statsd.DOMAIN, config))
mock_connection.assert_called_once_with( self.assertEqual(mock_connection.call_count, 1)
host='host', self.assertEqual(
port=123, mock_connection.call_args,
prefix='foo') mock.call(host='host', port=123, prefix='foo')
)
self.assertTrue(hass.bus.listen.called) self.assertTrue(hass.bus.listen.called)
self.assertEqual(EVENT_STATE_CHANGED, self.assertEqual(EVENT_STATE_CHANGED,
@ -64,10 +65,11 @@ class TestStatsd(unittest.TestCase):
hass = mock.MagicMock() hass = mock.MagicMock()
hass.pool.worker_count = 2 hass.pool.worker_count = 2
self.assertTrue(setup_component(hass, statsd.DOMAIN, config)) self.assertTrue(setup_component(hass, statsd.DOMAIN, config))
mock_connection.assert_called_once_with( self.assertEqual(mock_connection.call_count, 1)
host='host', self.assertEqual(
port=8125, mock_connection.call_args,
prefix='hass') mock.call(host='host', port=8125, prefix='hass')
)
self.assertTrue(hass.bus.listen.called) self.assertTrue(hass.bus.listen.called)
@mock.patch('statsd.StatsClient') @mock.patch('statsd.StatsClient')
@ -101,8 +103,11 @@ class TestStatsd(unittest.TestCase):
mock_client.return_value.gauge.reset_mock() mock_client.return_value.gauge.reset_mock()
mock_client.return_value.incr.assert_called_once_with( self.assertEqual(mock_client.return_value.incr.call_count, 1)
state.entity_id, rate=statsd.DEFAULT_RATE) self.assertEqual(
mock_client.return_value.incr.call_args,
mock.call(state.entity_id, rate=statsd.DEFAULT_RATE)
)
mock_client.return_value.incr.reset_mock() mock_client.return_value.incr.reset_mock()
for invalid in ('foo', '', object): for invalid in ('foo', '', object):
@ -146,8 +151,11 @@ class TestStatsd(unittest.TestCase):
mock_client.return_value.gauge.reset_mock() mock_client.return_value.gauge.reset_mock()
mock_client.return_value.incr.assert_called_once_with( self.assertEqual(mock_client.return_value.incr.call_count, 1)
state.entity_id, rate=statsd.DEFAULT_RATE) self.assertEqual(
mock_client.return_value.incr.call_args,
mock.call(state.entity_id, rate=statsd.DEFAULT_RATE)
)
mock_client.return_value.incr.reset_mock() mock_client.return_value.incr.reset_mock()
for invalid in ('foo', '', object): for invalid in ('foo', '', object):

View File

@ -52,7 +52,8 @@ class TestHoneywell(unittest.TestCase):
self.assertFalse(result) self.assertFalse(result)
result = honeywell.setup_platform(hass, config, add_devices) result = honeywell.setup_platform(hass, config, add_devices)
self.assertTrue(result) self.assertTrue(result)
mock_sc.assert_called_once_with('user', 'pass') self.assertEqual(mock_sc.call_count, 1)
self.assertEqual(mock_sc.call_args, mock.call('user', 'pass'))
mock_ht.assert_has_calls([ mock_ht.assert_has_calls([
mock.call(mock_sc.return_value, devices_1[0]), mock.call(mock_sc.return_value, devices_1[0]),
mock.call(mock_sc.return_value, devices_2[0]), mock.call(mock_sc.return_value, devices_2[0]),
@ -164,9 +165,13 @@ class TestHoneywell(unittest.TestCase):
hass = mock.MagicMock() hass = mock.MagicMock()
add_devices = mock.MagicMock() add_devices = mock.MagicMock()
self.assertTrue(honeywell.setup_platform(hass, config, add_devices)) self.assertTrue(honeywell.setup_platform(hass, config, add_devices))
mock_evo.assert_called_once_with('user', 'pass') self.assertEqual(mock_evo.call_count, 1)
mock_evo.return_value.temperatures.assert_called_once_with( self.assertEqual(mock_evo.call_args, mock.call('user', 'pass'))
force_refresh=True) self.assertEqual(mock_evo.return_value.temperatures.call_count, 1)
self.assertEqual(
mock_evo.return_value.temperatures.call_args,
mock.call(force_refresh=True)
)
mock_round.assert_has_calls([ mock_round.assert_has_calls([
mock.call(mock_evo.return_value, 'foo', True, 20), mock.call(mock_evo.return_value, 'foo', True, 20),
mock.call(mock_evo.return_value, 'bar', False, 20), mock.call(mock_evo.return_value, 'bar', False, 20),
@ -265,17 +270,26 @@ class TestHoneywellRound(unittest.TestCase):
self.assertFalse(self.round1.is_away_mode_on) self.assertFalse(self.round1.is_away_mode_on)
self.round1.turn_away_mode_on() self.round1.turn_away_mode_on()
self.assertTrue(self.round1.is_away_mode_on) self.assertTrue(self.round1.is_away_mode_on)
self.device.set_temperature.assert_called_once_with('House', 16) self.assertEqual(self.device.set_temperature.call_count, 1)
self.assertEqual(
self.device.set_temperature.call_args, mock.call('House', 16)
)
self.device.set_temperature.reset_mock() self.device.set_temperature.reset_mock()
self.round1.turn_away_mode_off() self.round1.turn_away_mode_off()
self.assertFalse(self.round1.is_away_mode_on) self.assertFalse(self.round1.is_away_mode_on)
self.device.cancel_temp_override.assert_called_once_with('House') self.assertEqual(self.device.cancel_temp_override.call_count, 1)
self.assertEqual(
self.device.cancel_temp_override.call_args, mock.call('House')
)
def test_set_temperature(self): def test_set_temperature(self):
"""Test setting the temperature.""" """Test setting the temperature."""
self.round1.set_temperature(25) self.round1.set_temperature(25)
self.device.set_temperature.assert_called_once_with('House', 25) self.assertEqual(self.device.set_temperature.call_count, 1)
self.assertEqual(
self.device.set_temperature.call_args, mock.call('House', 25)
)
def test_set_hvac_mode(self: unittest.TestCase) -> None: def test_set_hvac_mode(self: unittest.TestCase) -> None:
"""Test setting the system operation.""" """Test setting the system operation."""